TopLevelTests.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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.ClientSize);
  76. impl.SetupProperty(x => x.Resized);
  77. impl.SetupGet(x => x.Scaling).Returns(1);
  78. var target = new TestTopLevel(impl.Object)
  79. {
  80. Template = CreateTemplate(),
  81. Content = new TextBlock
  82. {
  83. Width = 321,
  84. Height = 432,
  85. }
  86. };
  87. LayoutManager.Instance.ExecuteInitialLayoutPass(target);
  88. Assert.Equal(new Rect(0, 0, 321, 432), target.Bounds);
  89. }
  90. }
  91. [Fact]
  92. public void Impl_ClientSize_Should_Be_Set_After_Layout_Pass()
  93. {
  94. using (UnitTestApplication.Start(TestServices.StyledWindow))
  95. {
  96. var impl = Mock.Of<ITopLevelImpl>(x => x.Scaling == 1);
  97. var target = new TestTopLevel(impl)
  98. {
  99. Template = CreateTemplate(),
  100. Content = new TextBlock
  101. {
  102. Width = 321,
  103. Height = 432,
  104. }
  105. };
  106. LayoutManager.Instance.ExecuteInitialLayoutPass(target);
  107. Mock.Get(impl).VerifySet(x => x.ClientSize = new Size(321, 432));
  108. }
  109. }
  110. [Fact]
  111. public void Width_And_Height_Should_Not_Be_Set_After_Layout_Pass()
  112. {
  113. using (UnitTestApplication.Start(TestServices.StyledWindow))
  114. {
  115. var impl = new Mock<ITopLevelImpl>();
  116. impl.Setup(x => x.ClientSize).Returns(new Size(123, 456));
  117. var target = new TestTopLevel(impl.Object);
  118. LayoutManager.Instance.ExecuteLayoutPass();
  119. Assert.Equal(double.NaN, target.Width);
  120. Assert.Equal(double.NaN, target.Height);
  121. }
  122. }
  123. [Fact]
  124. public void Width_And_Height_Should_Be_Set_After_Window_Resize_Notification()
  125. {
  126. using (UnitTestApplication.Start(TestServices.StyledWindow))
  127. {
  128. var impl = new Mock<ITopLevelImpl>();
  129. impl.SetupAllProperties();
  130. impl.Setup(x => x.ClientSize).Returns(new Size(123, 456));
  131. // The user has resized the window, so we can no longer auto-size.
  132. var target = new TestTopLevel(impl.Object);
  133. impl.Object.Resized(new Size(100, 200));
  134. Assert.Equal(100, target.Width);
  135. Assert.Equal(200, target.Height);
  136. }
  137. }
  138. [Fact]
  139. public void Activate_Should_Call_Impl_Activate()
  140. {
  141. using (UnitTestApplication.Start(TestServices.StyledWindow))
  142. {
  143. var impl = new Mock<ITopLevelImpl>();
  144. var target = new TestTopLevel(impl.Object);
  145. target.Activate();
  146. impl.Verify(x => x.Activate());
  147. }
  148. }
  149. [Fact]
  150. public void Impl_Activate_Should_Call_Raise_Activated_Event()
  151. {
  152. using (UnitTestApplication.Start(TestServices.StyledWindow))
  153. {
  154. var impl = new Mock<ITopLevelImpl>();
  155. impl.SetupAllProperties();
  156. bool raised = false;
  157. var target = new TestTopLevel(impl.Object);
  158. target.Activated += (s, e) => raised = true;
  159. impl.Object.Activated();
  160. Assert.True(raised);
  161. }
  162. }
  163. [Fact]
  164. public void Impl_Close_Should_Call_Raise_Closed_Event()
  165. {
  166. using (UnitTestApplication.Start(TestServices.StyledWindow))
  167. {
  168. var impl = new Mock<ITopLevelImpl>();
  169. impl.SetupAllProperties();
  170. bool raised = false;
  171. var target = new TestTopLevel(impl.Object);
  172. target.Closed += (s, e) => raised = true;
  173. impl.Object.Closed();
  174. Assert.True(raised);
  175. }
  176. }
  177. [Fact]
  178. public void Impl_Deactivate_Should_Call_Raise_Activated_Event()
  179. {
  180. using (UnitTestApplication.Start(TestServices.StyledWindow))
  181. {
  182. var impl = new Mock<ITopLevelImpl>();
  183. impl.SetupAllProperties();
  184. bool raised = false;
  185. var target = new TestTopLevel(impl.Object);
  186. target.Deactivated += (s, e) => raised = true;
  187. impl.Object.Deactivated();
  188. Assert.True(raised);
  189. }
  190. }
  191. [Fact]
  192. public void Impl_Input_Should_Pass_Input_To_InputManager()
  193. {
  194. var inputManagerMock = new Mock<IInputManager>();
  195. var services = TestServices.StyledWindow.With(inputManager: inputManagerMock.Object);
  196. using (UnitTestApplication.Start(services))
  197. {
  198. var impl = new Mock<ITopLevelImpl>();
  199. impl.SetupAllProperties();
  200. var target = new TestTopLevel(impl.Object);
  201. var input = new RawKeyEventArgs(
  202. new Mock<IKeyboardDevice>().Object,
  203. 0,
  204. RawKeyEventType.KeyDown,
  205. Key.A, InputModifiers.None);
  206. impl.Object.Input(input);
  207. inputManagerMock.Verify(x => x.ProcessInput(input));
  208. }
  209. }
  210. [Fact]
  211. public void Adding_Top_Level_As_Child_Should_Throw_Exception()
  212. {
  213. using (UnitTestApplication.Start(TestServices.StyledWindow))
  214. {
  215. var impl = new Mock<ITopLevelImpl>();
  216. impl.SetupAllProperties();
  217. var target = new TestTopLevel(impl.Object);
  218. var child = new TestTopLevel(impl.Object);
  219. target.Template = CreateTemplate();
  220. target.Content = child;
  221. Assert.Throws<InvalidOperationException>(() => target.ApplyTemplate());
  222. }
  223. }
  224. [Fact]
  225. public void Exiting_Application_Notifies_Top_Level()
  226. {
  227. using (UnitTestApplication.Start(TestServices.StyledWindow))
  228. {
  229. var impl = new Mock<ITopLevelImpl>();
  230. impl.SetupAllProperties();
  231. var target = new TestTopLevel(impl.Object);
  232. UnitTestApplication.Current.Exit();
  233. Assert.True(target.IsClosed);
  234. }
  235. }
  236. private FuncControlTemplate<TestTopLevel> CreateTemplate()
  237. {
  238. return new FuncControlTemplate<TestTopLevel>(x =>
  239. new ContentPresenter
  240. {
  241. Name = "PART_ContentPresenter",
  242. [!ContentPresenter.ContentProperty] = x[!ContentControl.ContentProperty],
  243. });
  244. }
  245. private class TestTopLevel : TopLevel
  246. {
  247. public bool IsClosed { get; private set; }
  248. public TestTopLevel(ITopLevelImpl impl)
  249. : base(impl)
  250. {
  251. }
  252. protected override void HandleApplicationExiting()
  253. {
  254. base.HandleApplicationExiting();
  255. IsClosed = true;
  256. }
  257. }
  258. }
  259. }