AvaloniaObjectTests_Threading.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.UnitTests;
  9. using Xunit;
  10. namespace Avalonia.Base.UnitTests
  11. {
  12. public class AvaloniaObjectTests_Threading
  13. {
  14. [Fact]
  15. public void StyledProperty_GetValue_Should_Throw()
  16. {
  17. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  18. {
  19. var target = new Class1();
  20. Assert.Throws<InvalidOperationException>(() => target.GetValue(Class1.StyledProperty));
  21. }
  22. }
  23. [Fact]
  24. public void StyledProperty_SetValue_Should_Throw()
  25. {
  26. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  27. {
  28. var target = new Class1();
  29. Assert.Throws<InvalidOperationException>(() => target.SetValue(Class1.StyledProperty, "foo"));
  30. }
  31. }
  32. [Fact]
  33. public void Setting_StyledProperty_Binding_Should_Throw()
  34. {
  35. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  36. {
  37. var target = new Class1();
  38. Assert.Throws<InvalidOperationException>(() =>
  39. target.Bind(
  40. Class1.StyledProperty,
  41. new BehaviorSubject<string>("foo")));
  42. }
  43. }
  44. [Fact]
  45. public void StyledProperty_Binding_Producing_Value_Should_Throw()
  46. {
  47. var ti = new ThreadingInterface(true);
  48. using (UnitTestApplication.Start(new TestServices(threadingInterface: ti)))
  49. {
  50. var target = new Class1();
  51. var source = new BehaviorSubject<string>("foo");
  52. target.Bind(Class1.StyledProperty, source);
  53. ti.CurrentThreadIsLoopThread = false;
  54. Assert.Throws<InvalidOperationException>(() => source.OnNext("bar"));
  55. }
  56. }
  57. [Fact]
  58. public void StyledProperty_ClearValue_Should_Throw()
  59. {
  60. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  61. {
  62. var target = new Class1();
  63. Assert.Throws<InvalidOperationException>(() => target.ClearValue(Class1.StyledProperty));
  64. }
  65. }
  66. [Fact]
  67. public void StyledProperty_IsSet_Should_Throw()
  68. {
  69. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  70. {
  71. var target = new Class1();
  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(threadingInterface: new ThreadingInterface())))
  79. {
  80. var target = new Class1();
  81. Assert.Throws<InvalidOperationException>(() => target.GetValue(Class1.DirectProperty));
  82. }
  83. }
  84. [Fact]
  85. public void DirectProperty_SetValue_Should_Throw()
  86. {
  87. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  88. {
  89. var target = new Class1();
  90. Assert.Throws<InvalidOperationException>(() => target.SetValue(Class1.DirectProperty, "foo"));
  91. }
  92. }
  93. [Fact]
  94. public void Setting_DirectProperty_Binding_Should_Throw()
  95. {
  96. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  97. {
  98. var target = new Class1();
  99. Assert.Throws<InvalidOperationException>(() =>
  100. target.Bind(
  101. Class1.DirectProperty,
  102. new BehaviorSubject<string>("foo")));
  103. }
  104. }
  105. [Fact]
  106. public void DirectProperty_Binding_Producing_Value_Should_Throw()
  107. {
  108. var ti = new ThreadingInterface(true);
  109. using (UnitTestApplication.Start(new TestServices(threadingInterface: ti)))
  110. {
  111. var target = new Class1();
  112. var source = new BehaviorSubject<string>("foo");
  113. target.Bind(Class1.DirectProperty, source);
  114. ti.CurrentThreadIsLoopThread = false;
  115. Assert.Throws<InvalidOperationException>(() => source.OnNext("bar"));
  116. }
  117. }
  118. [Fact]
  119. public void DirectProperty_ClearValue_Should_Throw()
  120. {
  121. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  122. {
  123. var target = new Class1();
  124. Assert.Throws<InvalidOperationException>(() => target.ClearValue(Class1.DirectProperty));
  125. }
  126. }
  127. [Fact]
  128. public void DirectProperty_IsSet_Should_Throw()
  129. {
  130. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  131. {
  132. var target = new Class1();
  133. Assert.Throws<InvalidOperationException>(() => target.IsSet(Class1.DirectProperty));
  134. }
  135. }
  136. private class Class1 : AvaloniaObject
  137. {
  138. public static readonly StyledProperty<string> StyledProperty =
  139. AvaloniaProperty.Register<Class1, string>("Foo", "foodefault");
  140. public static readonly DirectProperty<Class1, string> DirectProperty =
  141. AvaloniaProperty.RegisterDirect<Class1, string>("Qux", _ => null, (o, v) => { });
  142. }
  143. private class ThreadingInterface : IPlatformThreadingInterface
  144. {
  145. public ThreadingInterface(bool isLoopThread = false)
  146. {
  147. CurrentThreadIsLoopThread = isLoopThread;
  148. }
  149. public bool CurrentThreadIsLoopThread { get; set; }
  150. public event Action Signaled;
  151. public void RunLoop(CancellationToken cancellationToken)
  152. {
  153. throw new NotImplementedException();
  154. }
  155. public void Signal()
  156. {
  157. throw new NotImplementedException();
  158. }
  159. public IDisposable StartTimer(TimeSpan interval, Action tick)
  160. {
  161. throw new NotImplementedException();
  162. }
  163. }
  164. }
  165. }