AvaloniaActivationForViewFetcherTest.cs 6.7 KB

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