AvaloniaActivationForViewFetcherTest.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Reactive.Concurrency;
  3. using System.Reactive.Disposables;
  4. using Avalonia.Controls;
  5. using Avalonia.Rendering;
  6. using Avalonia.Platform;
  7. using Avalonia.UnitTests;
  8. using Avalonia;
  9. using ReactiveUI;
  10. using DynamicData;
  11. using Xunit;
  12. using Splat;
  13. using Avalonia.Markup.Xaml;
  14. using Avalonia.ReactiveUI;
  15. using Avalonia.Threading;
  16. namespace Avalonia.ReactiveUI.UnitTests
  17. {
  18. public class AvaloniaActivationForViewFetcherTest
  19. {
  20. public class TestUserControl : UserControl, IActivatableView { }
  21. public class TestUserControlWithWhenActivated : UserControl, IActivatableView
  22. {
  23. public bool Active { get; private set; }
  24. public TestUserControlWithWhenActivated()
  25. {
  26. this.WhenActivated(disposables =>
  27. {
  28. Active = true;
  29. Disposable
  30. .Create(() => Active = false)
  31. .DisposeWith(disposables);
  32. });
  33. }
  34. }
  35. public class TestWindowWithWhenActivated : Window, IActivatableView
  36. {
  37. public bool Active { get; private set; }
  38. public TestWindowWithWhenActivated()
  39. {
  40. this.WhenActivated(disposables =>
  41. {
  42. Active = true;
  43. Disposable
  44. .Create(() => Active = false)
  45. .DisposeWith(disposables);
  46. });
  47. }
  48. }
  49. public class ActivatableViewModel : IActivatableViewModel
  50. {
  51. public ViewModelActivator Activator { get; }
  52. public bool IsActivated { get; private set; }
  53. public ActivatableViewModel()
  54. {
  55. Activator = new ViewModelActivator();
  56. this.WhenActivated(disposables =>
  57. {
  58. IsActivated = true;
  59. Disposable
  60. .Create(() => IsActivated = false)
  61. .DisposeWith(disposables);
  62. });
  63. }
  64. }
  65. public class ActivatableWindow : ReactiveWindow<ActivatableViewModel>
  66. {
  67. public ActivatableWindow()
  68. {
  69. Content = new Border();
  70. this.WhenActivated(disposables => { });
  71. }
  72. }
  73. public class ActivatableUserControl : ReactiveUserControl<ActivatableViewModel>
  74. {
  75. public ActivatableUserControl()
  76. {
  77. Content = new Border();
  78. this.WhenActivated(disposables => { });
  79. }
  80. }
  81. public AvaloniaActivationForViewFetcherTest() =>
  82. Locator
  83. .CurrentMutable
  84. .RegisterConstant(
  85. new AvaloniaActivationForViewFetcher(),
  86. typeof(IActivationForViewFetcher));
  87. [Fact]
  88. public void Visual_Element_Is_Activated_And_Deactivated()
  89. {
  90. var userControl = new TestUserControl();
  91. var activationForViewFetcher = new AvaloniaActivationForViewFetcher();
  92. activationForViewFetcher
  93. .GetActivationForView(userControl)
  94. .ToObservableChangeSet(scheduler: ImmediateScheduler.Instance)
  95. .Bind(out var activated)
  96. .Subscribe();
  97. var fakeRenderedDecorator = new TestRoot();
  98. fakeRenderedDecorator.Child = userControl;
  99. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  100. Assert.True(activated[0]);
  101. Assert.Equal(1, activated.Count);
  102. fakeRenderedDecorator.Child = null;
  103. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  104. Assert.True(activated[0]);
  105. Assert.False(activated[1]);
  106. Assert.Equal(2, activated.Count);
  107. }
  108. [Fact]
  109. public void Get_Affinity_For_View_Should_Return_Non_Zero_For_Visual_Elements()
  110. {
  111. var userControl = new TestUserControl();
  112. var activationForViewFetcher = new AvaloniaActivationForViewFetcher();
  113. var forUserControl = activationForViewFetcher.GetAffinityForView(userControl.GetType());
  114. var forNonUserControl = activationForViewFetcher.GetAffinityForView(typeof(object));
  115. Assert.NotEqual(0, forUserControl);
  116. Assert.Equal(0, forNonUserControl);
  117. }
  118. [Fact]
  119. public void Activation_For_View_Fetcher_Should_Support_When_Activated()
  120. {
  121. var userControl = new TestUserControlWithWhenActivated();
  122. Assert.False(userControl.Active);
  123. var fakeRenderedDecorator = new TestRoot();
  124. fakeRenderedDecorator.Child = userControl;
  125. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  126. Assert.True(userControl.Active);
  127. fakeRenderedDecorator.Child = null;
  128. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  129. Assert.False(userControl.Active);
  130. }
  131. [Fact]
  132. public void Activation_For_View_Fetcher_Should_Support_Windows()
  133. {
  134. using (UnitTestApplication.Start(TestServices.StyledWindow))
  135. {
  136. var window = new TestWindowWithWhenActivated();
  137. Assert.False(window.Active);
  138. window.Show();
  139. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  140. Assert.True(window.Active);
  141. window.Close();
  142. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  143. Assert.False(window.Active);
  144. }
  145. }
  146. [Fact]
  147. public void Activatable_Window_View_Model_Is_Activated_And_Deactivated()
  148. {
  149. using (UnitTestApplication.Start(TestServices.StyledWindow))
  150. {
  151. var viewModel = new ActivatableViewModel();
  152. var window = new ActivatableWindow { ViewModel = viewModel };
  153. Assert.False(viewModel.IsActivated);
  154. window.Show();
  155. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  156. Assert.True(viewModel.IsActivated);
  157. window.Close();
  158. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  159. Assert.False(viewModel.IsActivated);
  160. }
  161. }
  162. [Fact]
  163. public void Activatable_User_Control_View_Model_Is_Activated_And_Deactivated()
  164. {
  165. var root = new TestRoot();
  166. var viewModel = new ActivatableViewModel();
  167. var control = new ActivatableUserControl { ViewModel = viewModel };
  168. Assert.False(viewModel.IsActivated);
  169. root.Child = control;
  170. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  171. Assert.True(viewModel.IsActivated);
  172. root.Child = null;
  173. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  174. Assert.False(viewModel.IsActivated);
  175. }
  176. }
  177. }