ViewModelViewHostTest.cs 5.5 KB

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