AvaloniaActivationForViewFetcherTest.cs 6.9 KB

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