AvaloniaActivationForViewFetcherTest.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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, IActivatable { }
  20. public class TestUserControlWithWhenActivated : UserControl, IActivatable
  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, IActivatable
  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 : ISupportsActivation
  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. var loader = new AvaloniaXamlLoader();
  75. loader.Load(@"
  76. <Window xmlns='https://github.com/avaloniaui'>
  77. <Border/>
  78. </Window>", null, this);
  79. }
  80. }
  81. public class ActivatableUserControl : ReactiveUserControl<ActivatableViewModel>
  82. {
  83. public ActivatableUserControl()
  84. {
  85. InitializeComponent();
  86. Assert.IsType<Border>(Content);
  87. this.WhenActivated(disposables => { });
  88. }
  89. private void InitializeComponent()
  90. {
  91. var loader = new AvaloniaXamlLoader();
  92. loader.Load(@"
  93. <UserControl xmlns='https://github.com/avaloniaui'>
  94. <Border/>
  95. </UserControl>", null, this);
  96. }
  97. }
  98. public AvaloniaActivationForViewFetcherTest()
  99. {
  100. Locator.CurrentMutable.RegisterConstant(
  101. new AvaloniaActivationForViewFetcher(),
  102. typeof(IActivationForViewFetcher));
  103. }
  104. [Fact]
  105. public void Visual_Element_Is_Activated_And_Deactivated()
  106. {
  107. var userControl = new TestUserControl();
  108. var activationForViewFetcher = new AvaloniaActivationForViewFetcher();
  109. activationForViewFetcher
  110. .GetActivationForView(userControl)
  111. .ToObservableChangeSet(scheduler: ImmediateScheduler.Instance)
  112. .Bind(out var activated)
  113. .Subscribe();
  114. var fakeRenderedDecorator = new TestRoot();
  115. fakeRenderedDecorator.Child = userControl;
  116. Assert.True(activated[0]);
  117. Assert.Equal(1, activated.Count);
  118. fakeRenderedDecorator.Child = null;
  119. Assert.True(activated[0]);
  120. Assert.False(activated[1]);
  121. Assert.Equal(2, activated.Count);
  122. }
  123. [Fact]
  124. public void Get_Affinity_For_View_Should_Return_Non_Zero_For_Visual_Elements()
  125. {
  126. var userControl = new TestUserControl();
  127. var activationForViewFetcher = new AvaloniaActivationForViewFetcher();
  128. var forUserControl = activationForViewFetcher.GetAffinityForView(userControl.GetType());
  129. var forNonUserControl = activationForViewFetcher.GetAffinityForView(typeof(object));
  130. Assert.NotEqual(0, forUserControl);
  131. Assert.Equal(0, forNonUserControl);
  132. }
  133. [Fact]
  134. public void Activation_For_View_Fetcher_Should_Support_When_Activated()
  135. {
  136. var userControl = new TestUserControlWithWhenActivated();
  137. Assert.False(userControl.Active);
  138. var fakeRenderedDecorator = new TestRoot();
  139. fakeRenderedDecorator.Child = userControl;
  140. Assert.True(userControl.Active);
  141. fakeRenderedDecorator.Child = null;
  142. Assert.False(userControl.Active);
  143. }
  144. [Fact]
  145. public void Activation_For_View_Fetcher_Should_Support_Windows()
  146. {
  147. using (var application = UnitTestApplication.Start(TestServices.MockWindowingPlatform))
  148. {
  149. var window = new TestWindowWithWhenActivated();
  150. Assert.False(window.Active);
  151. window.Show();
  152. Assert.True(window.Active);
  153. window.Close();
  154. Assert.False(window.Active);
  155. }
  156. }
  157. [Fact]
  158. public void Activatable_Window_View_Model_Is_Activated_And_Deactivated()
  159. {
  160. using (var application = UnitTestApplication.Start(TestServices.MockWindowingPlatform))
  161. {
  162. var viewModel = new ActivatableViewModel();
  163. var window = new ActivatableWindow { ViewModel = viewModel };
  164. Assert.False(viewModel.IsActivated);
  165. window.Show();
  166. Assert.True(viewModel.IsActivated);
  167. window.Close();
  168. Assert.False(viewModel.IsActivated);
  169. }
  170. }
  171. [Fact]
  172. public void Activatable_User_Control_View_Model_Is_Activated_And_Deactivated()
  173. {
  174. var root = new TestRoot();
  175. var viewModel = new ActivatableViewModel();
  176. var control = new ActivatableUserControl { ViewModel = viewModel };
  177. Assert.False(viewModel.IsActivated);
  178. root.Child = control;
  179. Assert.True(viewModel.IsActivated);
  180. root.Child = null;
  181. Assert.False(viewModel.IsActivated);
  182. }
  183. }
  184. }