AvaloniaActivationForViewFetcherTest.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. Content = new Border();
  69. this.WhenActivated(disposables => { });
  70. }
  71. }
  72. public class ActivatableUserControl : ReactiveUserControl<ActivatableViewModel>
  73. {
  74. public ActivatableUserControl()
  75. {
  76. Content = new Border();
  77. this.WhenActivated(disposables => { });
  78. }
  79. }
  80. public AvaloniaActivationForViewFetcherTest()
  81. {
  82. Locator.CurrentMutable.RegisterConstant(
  83. new AvaloniaActivationForViewFetcher(),
  84. typeof(IActivationForViewFetcher));
  85. }
  86. [Fact]
  87. public void Visual_Element_Is_Activated_And_Deactivated()
  88. {
  89. var userControl = new TestUserControl();
  90. var activationForViewFetcher = new AvaloniaActivationForViewFetcher();
  91. activationForViewFetcher
  92. .GetActivationForView(userControl)
  93. .ToObservableChangeSet(scheduler: ImmediateScheduler.Instance)
  94. .Bind(out var activated)
  95. .Subscribe();
  96. var fakeRenderedDecorator = new TestRoot();
  97. fakeRenderedDecorator.Child = userControl;
  98. Assert.True(activated[0]);
  99. Assert.Equal(1, activated.Count);
  100. fakeRenderedDecorator.Child = null;
  101. Assert.True(activated[0]);
  102. Assert.False(activated[1]);
  103. Assert.Equal(2, activated.Count);
  104. }
  105. [Fact]
  106. public void Get_Affinity_For_View_Should_Return_Non_Zero_For_Visual_Elements()
  107. {
  108. var userControl = new TestUserControl();
  109. var activationForViewFetcher = new AvaloniaActivationForViewFetcher();
  110. var forUserControl = activationForViewFetcher.GetAffinityForView(userControl.GetType());
  111. var forNonUserControl = activationForViewFetcher.GetAffinityForView(typeof(object));
  112. Assert.NotEqual(0, forUserControl);
  113. Assert.Equal(0, forNonUserControl);
  114. }
  115. [Fact]
  116. public void Activation_For_View_Fetcher_Should_Support_When_Activated()
  117. {
  118. var userControl = new TestUserControlWithWhenActivated();
  119. Assert.False(userControl.Active);
  120. var fakeRenderedDecorator = new TestRoot();
  121. fakeRenderedDecorator.Child = userControl;
  122. Assert.True(userControl.Active);
  123. fakeRenderedDecorator.Child = null;
  124. Assert.False(userControl.Active);
  125. }
  126. [Fact]
  127. public void Activation_For_View_Fetcher_Should_Support_Windows()
  128. {
  129. using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
  130. {
  131. var window = new TestWindowWithWhenActivated();
  132. Assert.False(window.Active);
  133. window.Show();
  134. Assert.True(window.Active);
  135. window.Close();
  136. Assert.False(window.Active);
  137. }
  138. }
  139. [Fact]
  140. public void Activatable_Window_View_Model_Is_Activated_And_Deactivated()
  141. {
  142. using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
  143. {
  144. var viewModel = new ActivatableViewModel();
  145. var window = new ActivatableWindow { ViewModel = viewModel };
  146. Assert.False(viewModel.IsActivated);
  147. window.Show();
  148. Assert.True(viewModel.IsActivated);
  149. window.Close();
  150. Assert.False(viewModel.IsActivated);
  151. }
  152. }
  153. [Fact]
  154. public void Activatable_User_Control_View_Model_Is_Activated_And_Deactivated()
  155. {
  156. var root = new TestRoot();
  157. var viewModel = new ActivatableViewModel();
  158. var control = new ActivatableUserControl { ViewModel = viewModel };
  159. Assert.False(viewModel.IsActivated);
  160. root.Child = control;
  161. Assert.True(viewModel.IsActivated);
  162. root.Child = null;
  163. Assert.False(viewModel.IsActivated);
  164. }
  165. }
  166. }