AvaloniaObjectTests_Threading.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Reactive.Subjects;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Avalonia.Platform;
  6. using Avalonia.Threading;
  7. using Avalonia.UnitTests;
  8. using Xunit;
  9. namespace Avalonia.Base.UnitTests
  10. {
  11. public class AvaloniaObjectTests_Threading
  12. {
  13. private TestDipatcherImpl _threading = new(true);
  14. [Fact]
  15. public void AvaloniaObject_Constructor_Should_Throw()
  16. {
  17. using (UnitTestApplication.Start(new TestServices(dispatcherImpl: new TestDipatcherImpl())))
  18. {
  19. Assert.Throws<InvalidOperationException>(() => new Class1());
  20. }
  21. }
  22. [Fact]
  23. public void StyledProperty_GetValue_Should_Throw()
  24. {
  25. using (UnitTestApplication.Start(new TestServices(dispatcherImpl: _threading)))
  26. {
  27. var target = new Class1();
  28. _threading.CurrentThreadIsLoopThread = false;
  29. Assert.Throws<InvalidOperationException>(() => target.GetValue(Class1.StyledProperty));
  30. }
  31. }
  32. [Fact]
  33. public void StyledProperty_SetValue_Should_Throw()
  34. {
  35. using (UnitTestApplication.Start(new TestServices(dispatcherImpl: _threading)))
  36. {
  37. var target = new Class1();
  38. _threading.CurrentThreadIsLoopThread = false;
  39. Assert.Throws<InvalidOperationException>(() => target.SetValue(Class1.StyledProperty, "foo"));
  40. }
  41. }
  42. [Fact]
  43. public void Setting_StyledProperty_Binding_Should_Throw()
  44. {
  45. using (UnitTestApplication.Start(new TestServices(dispatcherImpl: _threading)))
  46. {
  47. var target = new Class1();
  48. _threading.CurrentThreadIsLoopThread = false;
  49. Assert.Throws<InvalidOperationException>(() =>
  50. target.Bind(
  51. Class1.StyledProperty,
  52. new BehaviorSubject<string>("foo")));
  53. }
  54. }
  55. [Fact]
  56. public void StyledProperty_ClearValue_Should_Throw()
  57. {
  58. using (UnitTestApplication.Start(new TestServices(dispatcherImpl: _threading)))
  59. {
  60. var target = new Class1();
  61. _threading.CurrentThreadIsLoopThread = false;
  62. Assert.Throws<InvalidOperationException>(() => target.ClearValue(Class1.StyledProperty));
  63. }
  64. }
  65. [Fact]
  66. public void StyledProperty_IsSet_Should_Throw()
  67. {
  68. using (UnitTestApplication.Start(new TestServices(dispatcherImpl: _threading)))
  69. {
  70. var target = new Class1();
  71. _threading.CurrentThreadIsLoopThread = false;
  72. Assert.Throws<InvalidOperationException>(() => target.IsSet(Class1.StyledProperty));
  73. }
  74. }
  75. [Fact]
  76. public void DirectProperty_GetValue_Should_Throw()
  77. {
  78. using (UnitTestApplication.Start(new TestServices(dispatcherImpl: _threading)))
  79. {
  80. var target = new Class1();
  81. _threading.CurrentThreadIsLoopThread = false;
  82. Assert.Throws<InvalidOperationException>(() => target.GetValue(Class1.DirectProperty));
  83. }
  84. }
  85. [Fact]
  86. public void DirectProperty_SetValue_Should_Throw()
  87. {
  88. using (UnitTestApplication.Start(new TestServices(dispatcherImpl: _threading)))
  89. {
  90. var target = new Class1();
  91. _threading.CurrentThreadIsLoopThread = false;
  92. Assert.Throws<InvalidOperationException>(() => target.SetValue(Class1.DirectProperty, "foo"));
  93. }
  94. }
  95. [Fact]
  96. public void Setting_DirectProperty_Binding_Should_Throw()
  97. {
  98. using (UnitTestApplication.Start(new TestServices(dispatcherImpl: _threading)))
  99. {
  100. var target = new Class1();
  101. _threading.CurrentThreadIsLoopThread = false;
  102. Assert.Throws<InvalidOperationException>(() =>
  103. target.Bind(
  104. Class1.DirectProperty,
  105. new BehaviorSubject<string>("foo")));
  106. }
  107. }
  108. [Fact]
  109. public void DirectProperty_ClearValue_Should_Throw()
  110. {
  111. using (UnitTestApplication.Start(new TestServices(dispatcherImpl: _threading)))
  112. {
  113. var target = new Class1();
  114. _threading.CurrentThreadIsLoopThread = false;
  115. Assert.Throws<InvalidOperationException>(() => target.ClearValue(Class1.DirectProperty));
  116. }
  117. }
  118. [Fact]
  119. public void DirectProperty_IsSet_Should_Throw()
  120. {
  121. using (UnitTestApplication.Start(new TestServices(dispatcherImpl: _threading)))
  122. {
  123. var target = new Class1();
  124. _threading.CurrentThreadIsLoopThread = false;
  125. Assert.Throws<InvalidOperationException>(() => target.IsSet(Class1.DirectProperty));
  126. }
  127. }
  128. private class Class1 : AvaloniaObject
  129. {
  130. public static readonly StyledProperty<string> StyledProperty =
  131. AvaloniaProperty.Register<Class1, string>("Foo", "foodefault");
  132. public static readonly DirectProperty<Class1, string> DirectProperty =
  133. AvaloniaProperty.RegisterDirect<Class1, string>("Qux", _ => null, (o, v) => { });
  134. }
  135. private class TestDipatcherImpl : IDispatcherImpl
  136. {
  137. public TestDipatcherImpl(bool isLoopThread = false)
  138. {
  139. CurrentThreadIsLoopThread = isLoopThread;
  140. }
  141. public bool CurrentThreadIsLoopThread { get; set; }
  142. #pragma warning disable 67
  143. public event Action Signaled;
  144. public event Action Timer;
  145. public long Now => 0;
  146. public void UpdateTimer(long? dueTimeInMs)
  147. {
  148. throw new NotImplementedException();
  149. }
  150. public void Signal() => throw new NotImplementedException();
  151. #pragma warning restore 67
  152. }
  153. }
  154. }