ViewModelViewHostTest.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Avalonia.Controls;
  2. using Avalonia.Threading;
  3. using Avalonia.UnitTests;
  4. using ReactiveUI;
  5. using Splat;
  6. using Xunit;
  7. namespace Avalonia.ReactiveUI.UnitTests
  8. {
  9. public class ViewModelViewHostTest
  10. {
  11. public class FirstViewModel { }
  12. public class FirstView : ReactiveUserControl<FirstViewModel> { }
  13. public class AlternativeFirstView : ReactiveUserControl<FirstViewModel> { }
  14. public class SecondViewModel : ReactiveObject { }
  15. public class SecondView : ReactiveUserControl<SecondViewModel> { }
  16. public class AlternativeSecondView : ReactiveUserControl<SecondViewModel> { }
  17. public static string AlternativeViewContract => "AlternativeView";
  18. public ViewModelViewHostTest()
  19. {
  20. Locator.CurrentMutable.RegisterConstant(new AvaloniaActivationForViewFetcher(), typeof(IActivationForViewFetcher));
  21. Locator.CurrentMutable.Register(() => new FirstView(), typeof(IViewFor<FirstViewModel>));
  22. Locator.CurrentMutable.Register(() => new SecondView(), typeof(IViewFor<SecondViewModel>));
  23. Locator.CurrentMutable.Register(() => new AlternativeFirstView(), typeof(IViewFor<FirstViewModel>), AlternativeViewContract);
  24. Locator.CurrentMutable.Register(() => new AlternativeSecondView(), typeof(IViewFor<SecondViewModel>), AlternativeViewContract);
  25. }
  26. [Fact]
  27. public void ViewModelViewHost_View_Should_Stay_In_Sync_With_ViewModel()
  28. {
  29. var defaultContent = new TextBlock();
  30. var host = new ViewModelViewHost
  31. {
  32. DefaultContent = defaultContent,
  33. PageTransition = null
  34. };
  35. var root = new TestRoot
  36. {
  37. Child = host
  38. };
  39. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  40. Assert.NotNull(host.Content);
  41. Assert.Equal(typeof(TextBlock), host.Content.GetType());
  42. Assert.Equal(defaultContent, host.Content);
  43. var first = new FirstViewModel();
  44. host.ViewModel = first;
  45. Assert.NotNull(host.Content);
  46. Assert.Equal(typeof(FirstView), host.Content.GetType());
  47. Assert.Equal(first, ((FirstView)host.Content).DataContext);
  48. Assert.Equal(first, ((FirstView)host.Content).ViewModel);
  49. var second = new SecondViewModel();
  50. host.ViewModel = second;
  51. Assert.NotNull(host.Content);
  52. Assert.Equal(typeof(SecondView), host.Content.GetType());
  53. Assert.Equal(second, ((SecondView)host.Content).DataContext);
  54. Assert.Equal(second, ((SecondView)host.Content).ViewModel);
  55. host.ViewModel = null;
  56. Assert.NotNull(host.Content);
  57. Assert.Equal(typeof(TextBlock), host.Content.GetType());
  58. Assert.Equal(defaultContent, host.Content);
  59. host.ViewModel = first;
  60. Assert.NotNull(host.Content);
  61. Assert.Equal(typeof(FirstView), host.Content.GetType());
  62. Assert.Equal(first, ((FirstView)host.Content).DataContext);
  63. Assert.Equal(first, ((FirstView)host.Content).ViewModel);
  64. }
  65. [Fact]
  66. public void ViewModelViewHost_View_Should_Stay_In_Sync_With_ViewModel_And_Contract()
  67. {
  68. var defaultContent = new TextBlock();
  69. var host = new ViewModelViewHost
  70. {
  71. DefaultContent = defaultContent,
  72. PageTransition = null
  73. };
  74. var root = new TestRoot
  75. {
  76. Child = host
  77. };
  78. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  79. Assert.NotNull(host.Content);
  80. Assert.Equal(typeof(TextBlock), host.Content.GetType());
  81. Assert.Equal(defaultContent, host.Content);
  82. var first = new FirstViewModel();
  83. host.ViewModel = first;
  84. host.ViewContract = null;
  85. Assert.NotNull(host.Content);
  86. Assert.Equal(typeof(FirstView), host.Content.GetType());
  87. Assert.Equal(first, ((FirstView)host.Content).DataContext);
  88. Assert.Equal(first, ((FirstView)host.Content).ViewModel);
  89. host.ViewContract = AlternativeViewContract;
  90. Assert.NotNull(host.Content);
  91. Assert.Equal(typeof(AlternativeFirstView), host.Content.GetType());
  92. Assert.Equal(first, ((AlternativeFirstView)host.Content).DataContext);
  93. Assert.Equal(first, ((AlternativeFirstView)host.Content).ViewModel);
  94. var second = new SecondViewModel();
  95. host.ViewModel = second;
  96. host.ViewContract = null;
  97. Assert.NotNull(host.Content);
  98. Assert.Equal(typeof(SecondView), host.Content.GetType());
  99. Assert.Equal(second, ((SecondView)host.Content).DataContext);
  100. Assert.Equal(second, ((SecondView)host.Content).ViewModel);
  101. host.ViewContract = AlternativeViewContract;
  102. Assert.NotNull(host.Content);
  103. Assert.Equal(typeof(AlternativeSecondView), host.Content.GetType());
  104. Assert.Equal(second, ((AlternativeSecondView)host.Content).DataContext);
  105. Assert.Equal(second, ((AlternativeSecondView)host.Content).ViewModel);
  106. host.ViewModel = null;
  107. host.ViewContract = null;
  108. Assert.NotNull(host.Content);
  109. Assert.Equal(typeof(TextBlock), host.Content.GetType());
  110. Assert.Equal(defaultContent, host.Content);
  111. host.ViewContract = AlternativeViewContract;
  112. Assert.NotNull(host.Content);
  113. Assert.Equal(typeof(TextBlock), host.Content.GetType());
  114. Assert.Equal(defaultContent, host.Content);
  115. }
  116. }
  117. }