AvaloniaObjectTests_Threading.cs 6.5 KB

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