TopLevelTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 Avalonia.Controls.Presenters;
  5. using Avalonia.Controls.Templates;
  6. using Avalonia.Input;
  7. using Avalonia.Input.Raw;
  8. using Avalonia.Layout;
  9. using Avalonia.LogicalTree;
  10. using Avalonia.Platform;
  11. using Avalonia.Styling;
  12. using Avalonia.UnitTests;
  13. using Moq;
  14. using Xunit;
  15. namespace Avalonia.Controls.UnitTests
  16. {
  17. public class TopLevelTests
  18. {
  19. [Fact]
  20. public void IsAttachedToLogicalTree_Is_True()
  21. {
  22. using (UnitTestApplication.Start(TestServices.StyledWindow))
  23. {
  24. var impl = new Mock<ITopLevelImpl>();
  25. var target = new TestTopLevel(impl.Object);
  26. Assert.True(((ILogical)target).IsAttachedToLogicalTree);
  27. }
  28. }
  29. [Fact]
  30. public void ClientSize_Should_Be_Set_On_Construction()
  31. {
  32. using (UnitTestApplication.Start(TestServices.StyledWindow))
  33. {
  34. var impl = new Mock<ITopLevelImpl>();
  35. impl.Setup(x => x.ClientSize).Returns(new Size(123, 456));
  36. var target = new TestTopLevel(impl.Object);
  37. Assert.Equal(new Size(123, 456), target.ClientSize);
  38. }
  39. }
  40. [Fact]
  41. public void Width_Should_Not_Be_Set_On_Construction()
  42. {
  43. using (UnitTestApplication.Start(TestServices.StyledWindow))
  44. {
  45. var impl = new Mock<ITopLevelImpl>();
  46. impl.Setup(x => x.ClientSize).Returns(new Size(123, 456));
  47. var target = new TestTopLevel(impl.Object);
  48. Assert.Equal(double.NaN, target.Width);
  49. }
  50. }
  51. [Fact]
  52. public void Height_Should_Not_Be_Set_On_Construction()
  53. {
  54. using (UnitTestApplication.Start(TestServices.StyledWindow))
  55. {
  56. var impl = new Mock<ITopLevelImpl>();
  57. impl.Setup(x => x.ClientSize).Returns(new Size(123, 456));
  58. var target = new TestTopLevel(impl.Object);
  59. Assert.Equal(double.NaN, target.Height);
  60. }
  61. }
  62. [Fact]
  63. public void Layout_Pass_Should_Not_Be_Automatically_Scheduled()
  64. {
  65. var services = TestServices.StyledWindow;
  66. using (UnitTestApplication.Start(services))
  67. {
  68. var impl = new Mock<ITopLevelImpl>();
  69. var target = new TestTopLevel(impl.Object, Mock.Of<ILayoutManager>());
  70. // The layout pass should be scheduled by the derived class.
  71. var layoutManagerMock = Mock.Get(target.LayoutManager);
  72. layoutManagerMock.Verify(x => x.ExecuteLayoutPass(), Times.Never);
  73. }
  74. }
  75. [Fact]
  76. public void Bounds_Should_Be_Set_After_Layout_Pass()
  77. {
  78. using (UnitTestApplication.Start(TestServices.StyledWindow))
  79. {
  80. var impl = new Mock<ITopLevelImpl>();
  81. impl.SetupProperty(x => x.Resized);
  82. impl.SetupGet(x => x.Scaling).Returns(1);
  83. var target = new TestTopLevel(impl.Object)
  84. {
  85. IsVisible = true,
  86. Template = CreateTemplate(),
  87. Content = new TextBlock
  88. {
  89. Width = 321,
  90. Height = 432,
  91. }
  92. };
  93. target.LayoutManager.ExecuteInitialLayoutPass(target);
  94. Assert.Equal(new Rect(0, 0, 321, 432), target.Bounds);
  95. }
  96. }
  97. [Fact]
  98. public void Width_And_Height_Should_Not_Be_Set_After_Layout_Pass()
  99. {
  100. using (UnitTestApplication.Start(TestServices.StyledWindow))
  101. {
  102. var impl = new Mock<ITopLevelImpl>();
  103. impl.Setup(x => x.ClientSize).Returns(new Size(123, 456));
  104. var target = new TestTopLevel(impl.Object);
  105. target.LayoutManager.ExecuteLayoutPass();
  106. Assert.Equal(double.NaN, target.Width);
  107. Assert.Equal(double.NaN, target.Height);
  108. }
  109. }
  110. [Fact]
  111. public void Width_And_Height_Should_Be_Set_After_Window_Resize_Notification()
  112. {
  113. using (UnitTestApplication.Start(TestServices.StyledWindow))
  114. {
  115. var impl = new Mock<ITopLevelImpl>();
  116. impl.SetupAllProperties();
  117. impl.Setup(x => x.ClientSize).Returns(new Size(123, 456));
  118. // The user has resized the window, so we can no longer auto-size.
  119. var target = new TestTopLevel(impl.Object);
  120. impl.Object.Resized(new Size(100, 200));
  121. Assert.Equal(100, target.Width);
  122. Assert.Equal(200, target.Height);
  123. }
  124. }
  125. [Fact]
  126. public void Impl_Close_Should_Call_Raise_Closed_Event()
  127. {
  128. using (UnitTestApplication.Start(TestServices.StyledWindow))
  129. {
  130. var impl = new Mock<ITopLevelImpl>();
  131. impl.SetupAllProperties();
  132. bool raised = false;
  133. var target = new TestTopLevel(impl.Object);
  134. target.Closed += (s, e) => raised = true;
  135. impl.Object.Closed();
  136. Assert.True(raised);
  137. }
  138. }
  139. [Fact]
  140. public void Impl_Close_Should_Raise_DetachedFromLogicalTree_Event()
  141. {
  142. using (UnitTestApplication.Start(TestServices.StyledWindow))
  143. {
  144. var impl = new Mock<ITopLevelImpl>();
  145. impl.SetupAllProperties();
  146. var target = new TestTopLevel(impl.Object);
  147. var raised = 0;
  148. target.DetachedFromLogicalTree += (s, e) =>
  149. {
  150. Assert.Same(target, e.Root);
  151. Assert.Same(target, e.Source);
  152. Assert.Null(e.Parent);
  153. ++raised;
  154. };
  155. impl.Object.Closed();
  156. Assert.Equal(1, raised);
  157. }
  158. }
  159. [Fact]
  160. public void Impl_Input_Should_Pass_Input_To_InputManager()
  161. {
  162. var inputManagerMock = new Mock<IInputManager>();
  163. var services = TestServices.StyledWindow.With(inputManager: inputManagerMock.Object);
  164. using (UnitTestApplication.Start(services))
  165. {
  166. var impl = new Mock<ITopLevelImpl>();
  167. impl.SetupAllProperties();
  168. var target = new TestTopLevel(impl.Object);
  169. var input = new RawKeyEventArgs(
  170. new Mock<IKeyboardDevice>().Object,
  171. 0,
  172. target,
  173. RawKeyEventType.KeyDown,
  174. Key.A, RawInputModifiers.None);
  175. impl.Object.Input(input);
  176. inputManagerMock.Verify(x => x.ProcessInput(input));
  177. }
  178. }
  179. [Fact]
  180. public void Adding_Top_Level_As_Child_Should_Throw_Exception()
  181. {
  182. using (UnitTestApplication.Start(TestServices.StyledWindow))
  183. {
  184. var impl = new Mock<ITopLevelImpl>();
  185. impl.SetupAllProperties();
  186. var target = new TestTopLevel(impl.Object);
  187. var child = new TestTopLevel(impl.Object);
  188. target.Template = CreateTemplate();
  189. target.Content = child;
  190. target.ApplyTemplate();
  191. Assert.Throws<InvalidOperationException>(() => target.Presenter.ApplyTemplate());
  192. }
  193. }
  194. [Fact]
  195. public void Adding_Resource_To_Application_Should_Raise_ResourcesChanged()
  196. {
  197. using (UnitTestApplication.Start(TestServices.StyledWindow))
  198. {
  199. var impl = new Mock<ITopLevelImpl>();
  200. impl.SetupAllProperties();
  201. var target = new TestTopLevel(impl.Object);
  202. var raised = false;
  203. target.ResourcesChanged += (_, __) => raised = true;
  204. Application.Current.Resources.Add("foo", "bar");
  205. Assert.True(raised);
  206. }
  207. }
  208. [Fact]
  209. public void Close_Should_Notify_MouseDevice()
  210. {
  211. using (UnitTestApplication.Start(TestServices.StyledWindow))
  212. {
  213. var impl = new Mock<ITopLevelImpl>();
  214. var mouseDevice = new Mock<IMouseDevice>();
  215. impl.SetupAllProperties();
  216. impl.Setup(x => x.MouseDevice).Returns(mouseDevice.Object);
  217. var target = new TestTopLevel(impl.Object);
  218. impl.Object.Closed();
  219. mouseDevice.Verify(x => x.TopLevelClosed(target));
  220. }
  221. }
  222. [Fact]
  223. public void Reacts_To_Changes_In_Global_Styles()
  224. {
  225. using (UnitTestApplication.Start(TestServices.StyledWindow))
  226. {
  227. var impl = new Mock<ITopLevelImpl>();
  228. impl.SetupGet(x => x.Scaling).Returns(1);
  229. var child = new Border { Classes = { "foo" } };
  230. var target = new TestTopLevel(impl.Object)
  231. {
  232. Template = CreateTemplate(),
  233. Content = child,
  234. };
  235. target.LayoutManager.ExecuteInitialLayoutPass(target);
  236. Assert.Equal(new Thickness(0), child.BorderThickness);
  237. var style = new Style(x => x.OfType<Border>().Class("foo"))
  238. {
  239. Setters =
  240. {
  241. new Setter(Border.BorderThicknessProperty, new Thickness(2))
  242. }
  243. };
  244. Application.Current.Styles.Add(style);
  245. target.LayoutManager.ExecuteInitialLayoutPass(target);
  246. Assert.Equal(new Thickness(2), child.BorderThickness);
  247. Application.Current.Styles.Remove(style);
  248. Assert.Equal(new Thickness(0), child.BorderThickness);
  249. }
  250. }
  251. private FuncControlTemplate<TestTopLevel> CreateTemplate()
  252. {
  253. return new FuncControlTemplate<TestTopLevel>((x, scope) =>
  254. new ContentPresenter
  255. {
  256. Name = "PART_ContentPresenter",
  257. [!ContentPresenter.ContentProperty] = x[!ContentControl.ContentProperty],
  258. }.RegisterInNameScope(scope));
  259. }
  260. private class TestTopLevel : TopLevel
  261. {
  262. private readonly ILayoutManager _layoutManager;
  263. public bool IsClosed { get; private set; }
  264. public TestTopLevel(ITopLevelImpl impl, ILayoutManager layoutManager = null)
  265. : base(impl)
  266. {
  267. _layoutManager = layoutManager ?? new LayoutManager();
  268. }
  269. protected override ILayoutManager CreateLayoutManager() => _layoutManager;
  270. }
  271. }
  272. }