AvaloniaActivationForViewFetcherTest.cs 6.0 KB

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