AvaloniaObjectTests_Threading.cs 5.5 KB

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