AvaloniaObjectTests_Threading.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. [Fact]
  16. public void StyledProperty_GetValue_Should_Throw()
  17. {
  18. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  19. {
  20. var target = new Class1();
  21. Assert.Throws<InvalidOperationException>(() => target.GetValue(Class1.StyledProperty));
  22. }
  23. }
  24. [Fact]
  25. public void StyledProperty_SetValue_Should_Throw()
  26. {
  27. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  28. {
  29. var target = new Class1();
  30. Assert.Throws<InvalidOperationException>(() => target.SetValue(Class1.StyledProperty, "foo"));
  31. }
  32. }
  33. [Fact]
  34. public void Setting_StyledProperty_Binding_Should_Throw()
  35. {
  36. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  37. {
  38. var target = new Class1();
  39. Assert.Throws<InvalidOperationException>(() =>
  40. target.Bind(
  41. Class1.StyledProperty,
  42. new BehaviorSubject<string>("foo")));
  43. }
  44. }
  45. [Fact]
  46. public void StyledProperty_ClearValue_Should_Throw()
  47. {
  48. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  49. {
  50. var target = new Class1();
  51. Assert.Throws<InvalidOperationException>(() => target.ClearValue(Class1.StyledProperty));
  52. }
  53. }
  54. [Fact]
  55. public void StyledProperty_IsSet_Should_Throw()
  56. {
  57. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  58. {
  59. var target = new Class1();
  60. Assert.Throws<InvalidOperationException>(() => target.IsSet(Class1.StyledProperty));
  61. }
  62. }
  63. [Fact]
  64. public void DirectProperty_GetValue_Should_Throw()
  65. {
  66. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  67. {
  68. var target = new Class1();
  69. Assert.Throws<InvalidOperationException>(() => target.GetValue(Class1.DirectProperty));
  70. }
  71. }
  72. [Fact]
  73. public void DirectProperty_SetValue_Should_Throw()
  74. {
  75. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  76. {
  77. var target = new Class1();
  78. Assert.Throws<InvalidOperationException>(() => target.SetValue(Class1.DirectProperty, "foo"));
  79. }
  80. }
  81. [Fact]
  82. public void Setting_DirectProperty_Binding_Should_Throw()
  83. {
  84. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  85. {
  86. var target = new Class1();
  87. Assert.Throws<InvalidOperationException>(() =>
  88. target.Bind(
  89. Class1.DirectProperty,
  90. new BehaviorSubject<string>("foo")));
  91. }
  92. }
  93. [Fact]
  94. public void DirectProperty_ClearValue_Should_Throw()
  95. {
  96. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  97. {
  98. var target = new Class1();
  99. Assert.Throws<InvalidOperationException>(() => target.ClearValue(Class1.DirectProperty));
  100. }
  101. }
  102. [Fact]
  103. public void DirectProperty_IsSet_Should_Throw()
  104. {
  105. using (UnitTestApplication.Start(new TestServices(threadingInterface: new ThreadingInterface())))
  106. {
  107. var target = new Class1();
  108. Assert.Throws<InvalidOperationException>(() => target.IsSet(Class1.DirectProperty));
  109. }
  110. }
  111. private class Class1 : AvaloniaObject
  112. {
  113. public static readonly StyledProperty<string> StyledProperty =
  114. AvaloniaProperty.Register<Class1, string>("Foo", "foodefault");
  115. public static readonly DirectProperty<Class1, string> DirectProperty =
  116. AvaloniaProperty.RegisterDirect<Class1, string>("Qux", _ => null, (o, v) => { });
  117. }
  118. private class ThreadingInterface : IPlatformThreadingInterface
  119. {
  120. public ThreadingInterface(bool isLoopThread = false)
  121. {
  122. CurrentThreadIsLoopThread = isLoopThread;
  123. }
  124. public bool CurrentThreadIsLoopThread { get; set; }
  125. public event Action<DispatcherPriority?> Signaled;
  126. public void RunLoop(CancellationToken cancellationToken)
  127. {
  128. throw new NotImplementedException();
  129. }
  130. public void Signal(DispatcherPriority prio)
  131. {
  132. throw new NotImplementedException();
  133. }
  134. public IDisposable StartTimer(DispatcherPriority priority, TimeSpan interval, Action tick)
  135. {
  136. throw new NotImplementedException();
  137. }
  138. }
  139. }
  140. }