TopLevelTests.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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;
  5. using System.Reactive.Subjects;
  6. using Moq;
  7. using Avalonia.Controls.Presenters;
  8. using Avalonia.Controls.Templates;
  9. using Avalonia.Input;
  10. using Avalonia.Input.Raw;
  11. using Avalonia.Layout;
  12. using Avalonia.Platform;
  13. using Avalonia.Rendering;
  14. using Avalonia.Styling;
  15. using Avalonia.UnitTests;
  16. using Ploeh.AutoFixture;
  17. using Ploeh.AutoFixture.AutoMoq;
  18. using Xunit;
  19. namespace Avalonia.Controls.UnitTests
  20. {
  21. public class TopLevelTests
  22. {
  23. [Fact]
  24. public void ClientSize_Should_Be_Set_On_Construction()
  25. {
  26. using (UnitTestApplication.Start(TestServices.StyledWindow))
  27. {
  28. var impl = new Mock<ITopLevelImpl>();
  29. impl.Setup(x => x.ClientSize).Returns(new Size(123, 456));
  30. var target = new TestTopLevel(impl.Object);
  31. Assert.Equal(new Size(123, 456), target.ClientSize);
  32. }
  33. }
  34. [Fact]
  35. public void Width_Should_Not_Be_Set_On_Construction()
  36. {
  37. using (UnitTestApplication.Start(TestServices.StyledWindow))
  38. {
  39. var impl = new Mock<ITopLevelImpl>();
  40. impl.Setup(x => x.ClientSize).Returns(new Size(123, 456));
  41. var target = new TestTopLevel(impl.Object);
  42. Assert.Equal(double.NaN, target.Width);
  43. }
  44. }
  45. [Fact]
  46. public void Height_Should_Not_Be_Set_On_Construction()
  47. {
  48. using (UnitTestApplication.Start(TestServices.StyledWindow))
  49. {
  50. var impl = new Mock<ITopLevelImpl>();
  51. impl.Setup(x => x.ClientSize).Returns(new Size(123, 456));
  52. var target = new TestTopLevel(impl.Object);
  53. Assert.Equal(double.NaN, target.Height);
  54. }
  55. }
  56. [Fact]
  57. public void Layout_Pass_Should_Not_Be_Automatically_Scheduled()
  58. {
  59. var services = TestServices.StyledWindow.With(layoutManager: Mock.Of<ILayoutManager>());
  60. using (UnitTestApplication.Start(services))
  61. {
  62. var impl = new Mock<ITopLevelImpl>();
  63. var target = new TestTopLevel(impl.Object);
  64. // The layout pass should be scheduled by the derived class.
  65. var layoutManagerMock = Mock.Get(LayoutManager.Instance);
  66. layoutManagerMock.Verify(x => x.ExecuteLayoutPass(), Times.Never);
  67. }
  68. }
  69. [Fact]
  70. public void Bounds_Should_Be_Set_After_Layout_Pass()
  71. {
  72. using (UnitTestApplication.Start(TestServices.StyledWindow))
  73. {
  74. var impl = new Mock<ITopLevelImpl>();
  75. impl.SetupProperty(x => x.Resized);
  76. impl.SetupGet(x => x.Scaling).Returns(1);
  77. var target = new TestTopLevel(impl.Object)
  78. {
  79. Template = CreateTemplate(),
  80. Content = new TextBlock
  81. {
  82. Width = 321,
  83. Height = 432,
  84. }
  85. };
  86. LayoutManager.Instance.ExecuteInitialLayoutPass(target);
  87. Assert.Equal(new Rect(0, 0, 321, 432), target.Bounds);
  88. }
  89. }
  90. [Fact]
  91. public void Width_And_Height_Should_Not_Be_Set_After_Layout_Pass()
  92. {
  93. using (UnitTestApplication.Start(TestServices.StyledWindow))
  94. {
  95. var impl = new Mock<ITopLevelImpl>();
  96. impl.Setup(x => x.ClientSize).Returns(new Size(123, 456));
  97. var target = new TestTopLevel(impl.Object);
  98. LayoutManager.Instance.ExecuteLayoutPass();
  99. Assert.Equal(double.NaN, target.Width);
  100. Assert.Equal(double.NaN, target.Height);
  101. }
  102. }
  103. [Fact]
  104. public void Width_And_Height_Should_Be_Set_After_Window_Resize_Notification()
  105. {
  106. using (UnitTestApplication.Start(TestServices.StyledWindow))
  107. {
  108. var impl = new Mock<ITopLevelImpl>();
  109. impl.SetupAllProperties();
  110. impl.Setup(x => x.ClientSize).Returns(new Size(123, 456));
  111. // The user has resized the window, so we can no longer auto-size.
  112. var target = new TestTopLevel(impl.Object);
  113. impl.Object.Resized(new Size(100, 200));
  114. Assert.Equal(100, target.Width);
  115. Assert.Equal(200, target.Height);
  116. }
  117. }
  118. [Fact]
  119. public void Impl_Close_Should_Call_Raise_Closed_Event()
  120. {
  121. using (UnitTestApplication.Start(TestServices.StyledWindow))
  122. {
  123. var impl = new Mock<ITopLevelImpl>();
  124. impl.SetupAllProperties();
  125. bool raised = false;
  126. var target = new TestTopLevel(impl.Object);
  127. target.Closed += (s, e) => raised = true;
  128. impl.Object.Closed();
  129. Assert.True(raised);
  130. }
  131. }
  132. [Fact]
  133. public void Impl_Input_Should_Pass_Input_To_InputManager()
  134. {
  135. var inputManagerMock = new Mock<IInputManager>();
  136. var services = TestServices.StyledWindow.With(inputManager: inputManagerMock.Object);
  137. using (UnitTestApplication.Start(services))
  138. {
  139. var impl = new Mock<ITopLevelImpl>();
  140. impl.SetupAllProperties();
  141. var target = new TestTopLevel(impl.Object);
  142. var input = new RawKeyEventArgs(
  143. new Mock<IKeyboardDevice>().Object,
  144. 0,
  145. RawKeyEventType.KeyDown,
  146. Key.A, InputModifiers.None);
  147. impl.Object.Input(input);
  148. inputManagerMock.Verify(x => x.ProcessInput(input));
  149. }
  150. }
  151. [Fact]
  152. public void Adding_Top_Level_As_Child_Should_Throw_Exception()
  153. {
  154. using (UnitTestApplication.Start(TestServices.StyledWindow))
  155. {
  156. var impl = new Mock<ITopLevelImpl>();
  157. impl.SetupAllProperties();
  158. var target = new TestTopLevel(impl.Object);
  159. var child = new TestTopLevel(impl.Object);
  160. target.Template = CreateTemplate();
  161. target.Content = child;
  162. Assert.Throws<InvalidOperationException>(() => target.ApplyTemplate());
  163. }
  164. }
  165. [Fact]
  166. public void Exiting_Application_Notifies_Top_Level()
  167. {
  168. using (UnitTestApplication.Start(TestServices.StyledWindow))
  169. {
  170. var impl = new Mock<ITopLevelImpl>();
  171. impl.SetupAllProperties();
  172. var target = new TestTopLevel(impl.Object);
  173. UnitTestApplication.Current.Exit();
  174. Assert.True(target.IsClosed);
  175. }
  176. }
  177. private FuncControlTemplate<TestTopLevel> CreateTemplate()
  178. {
  179. return new FuncControlTemplate<TestTopLevel>(x =>
  180. new ContentPresenter
  181. {
  182. Name = "PART_ContentPresenter",
  183. [!ContentPresenter.ContentProperty] = x[!ContentControl.ContentProperty],
  184. });
  185. }
  186. private class TestTopLevel : TopLevel
  187. {
  188. public bool IsClosed { get; private set; }
  189. public TestTopLevel(ITopLevelImpl impl)
  190. : base(impl)
  191. {
  192. }
  193. protected override void HandleApplicationExiting()
  194. {
  195. base.HandleApplicationExiting();
  196. IsClosed = true;
  197. }
  198. }
  199. }
  200. }