WindowTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // -----------------------------------------------------------------------
  2. // <copyright file="WindowTests.cs" company="Steven Kirk">
  3. // Copyright 2015 MIT Licence. See licence.md for more information.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Threading.Tasks;
  9. using Avalonia.Platform;
  10. using Avalonia.Rendering;
  11. using Avalonia.UnitTests;
  12. using Moq;
  13. using Xunit;
  14. namespace Avalonia.Controls.UnitTests
  15. {
  16. public class WindowTests
  17. {
  18. [Fact]
  19. public void Setting_Title_Should_Set_Impl_Title()
  20. {
  21. var windowImpl = new Mock<IWindowImpl>();
  22. var windowingPlatform = new MockWindowingPlatform(() => windowImpl.Object);
  23. using (UnitTestApplication.Start(new TestServices(windowingPlatform: windowingPlatform)))
  24. {
  25. var target = new Window();
  26. target.Title = "Hello World";
  27. windowImpl.Verify(x => x.SetTitle("Hello World"));
  28. }
  29. }
  30. [Fact]
  31. public void IsVisible_Should_Initially_Be_False()
  32. {
  33. using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
  34. {
  35. var window = new Window();
  36. Assert.False(window.IsVisible);
  37. }
  38. }
  39. [Fact]
  40. public void IsVisible_Should_Be_True_After_Show()
  41. {
  42. using (UnitTestApplication.Start(TestServices.StyledWindow))
  43. {
  44. var window = new Window();
  45. window.Show();
  46. Assert.True(window.IsVisible);
  47. }
  48. }
  49. [Fact]
  50. public void IsVisible_Should_Be_True_After_ShowDialog()
  51. {
  52. using (UnitTestApplication.Start(TestServices.StyledWindow))
  53. {
  54. var parent = new Window();
  55. parent.Show();
  56. var window = new Window();
  57. var task = window.ShowDialog(parent);
  58. Assert.True(window.IsVisible);
  59. }
  60. }
  61. [Fact]
  62. public void IsVisible_Should_Be_False_After_Hide()
  63. {
  64. using (UnitTestApplication.Start(TestServices.StyledWindow))
  65. {
  66. var window = new Window();
  67. window.Show();
  68. window.Hide();
  69. Assert.False(window.IsVisible);
  70. }
  71. }
  72. [Fact]
  73. public void IsVisible_Should_Be_False_After_Close()
  74. {
  75. using (UnitTestApplication.Start(TestServices.StyledWindow))
  76. {
  77. var window = new Window();
  78. window.Show();
  79. window.Close();
  80. Assert.False(window.IsVisible);
  81. }
  82. }
  83. [Fact]
  84. public void IsVisible_Should_Be_False_After_Impl_Signals_Close()
  85. {
  86. var windowImpl = new Mock<IWindowImpl>();
  87. windowImpl.SetupProperty(x => x.Closed);
  88. windowImpl.Setup(x => x.Scaling).Returns(1);
  89. var services = TestServices.StyledWindow.With(
  90. windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object));
  91. using (UnitTestApplication.Start(services))
  92. {
  93. var window = new Window();
  94. window.Show();
  95. windowImpl.Object.Closed();
  96. Assert.False(window.IsVisible);
  97. }
  98. }
  99. [Fact]
  100. public void Closing_Should_Only_Be_Invoked_Once()
  101. {
  102. using (UnitTestApplication.Start(TestServices.StyledWindow))
  103. {
  104. var window = new Window();
  105. var count = 0;
  106. window.Closing +=
  107. (sender, e) =>
  108. {
  109. count++;
  110. };
  111. window.Show();
  112. window.Close();
  113. Assert.Equal(1, count);
  114. }
  115. }
  116. [Fact]
  117. public void Showing_Should_Start_Renderer()
  118. {
  119. using (UnitTestApplication.Start(TestServices.StyledWindow))
  120. {
  121. var renderer = new Mock<IRenderer>();
  122. var target = new Window(CreateImpl(renderer));
  123. target.Show();
  124. renderer.Verify(x => x.Start(), Times.Once);
  125. }
  126. }
  127. [Fact]
  128. public void ShowDialog_Should_Start_Renderer()
  129. {
  130. using (UnitTestApplication.Start(TestServices.StyledWindow))
  131. {
  132. var parent = Mock.Of<IWindowImpl>();
  133. var renderer = new Mock<IRenderer>();
  134. var target = new Window(CreateImpl(renderer));
  135. target.ShowDialog<object>(parent);
  136. renderer.Verify(x => x.Start(), Times.Once);
  137. }
  138. }
  139. [Fact]
  140. public void ShowDialog_Should_Raise_Opened()
  141. {
  142. using (UnitTestApplication.Start(TestServices.StyledWindow))
  143. {
  144. var parent = Mock.Of<IWindowImpl>();
  145. var target = new Window();
  146. var raised = false;
  147. target.Opened += (s, e) => raised = true;
  148. target.ShowDialog<object>(parent);
  149. Assert.True(raised);
  150. }
  151. }
  152. [Fact]
  153. public void Hiding_Should_Stop_Renderer()
  154. {
  155. using (UnitTestApplication.Start(TestServices.StyledWindow))
  156. {
  157. var renderer = new Mock<IRenderer>();
  158. var target = new Window(CreateImpl(renderer));
  159. target.Show();
  160. target.Hide();
  161. renderer.Verify(x => x.Stop(), Times.Once);
  162. }
  163. }
  164. [Fact]
  165. public async Task ShowDialog_With_ValueType_Returns_Default_When_Closed()
  166. {
  167. using (UnitTestApplication.Start(TestServices.StyledWindow))
  168. {
  169. var parent = new Mock<IWindowImpl>();
  170. var windowImpl = new Mock<IWindowImpl>();
  171. windowImpl.SetupProperty(x => x.Closed);
  172. windowImpl.Setup(x => x.Scaling).Returns(1);
  173. var target = new Window(windowImpl.Object);
  174. var task = target.ShowDialog<bool>(parent.Object);
  175. windowImpl.Object.Closed();
  176. var result = await task;
  177. Assert.False(result);
  178. }
  179. }
  180. [Fact]
  181. public void Calling_Show_On_Closed_Window_Should_Throw()
  182. {
  183. using (UnitTestApplication.Start(TestServices.StyledWindow))
  184. {
  185. var target = new Window();
  186. target.Show();
  187. target.Close();
  188. var openedRaised = false;
  189. target.Opened += (s, e) => openedRaised = true;
  190. var ex = Assert.Throws<InvalidOperationException>(() => target.Show());
  191. Assert.Equal("Cannot re-show a closed window.", ex.Message);
  192. Assert.False(openedRaised);
  193. }
  194. }
  195. [Fact]
  196. public async Task Calling_ShowDialog_On_Closed_Window_Should_Throw()
  197. {
  198. using (UnitTestApplication.Start(TestServices.StyledWindow))
  199. {
  200. var parent = new Mock<IWindowImpl>();
  201. var windowImpl = new Mock<IWindowImpl>();
  202. windowImpl.SetupProperty(x => x.Closed);
  203. windowImpl.Setup(x => x.Scaling).Returns(1);
  204. var target = new Window(windowImpl.Object);
  205. var task = target.ShowDialog<bool>(parent.Object);
  206. windowImpl.Object.Closed();
  207. await task;
  208. var openedRaised = false;
  209. target.Opened += (s, e) => openedRaised = true;
  210. var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => target.ShowDialog<bool>(parent.Object));
  211. Assert.Equal("Cannot re-show a closed window.", ex.Message);
  212. Assert.False(openedRaised);
  213. }
  214. }
  215. [Fact]
  216. public void Window_Should_Be_Centered_When_WindowStartupLocation_Is_CenterScreen()
  217. {
  218. var screen1 = new Mock<Screen>(1.0, new PixelRect(new PixelSize(1920, 1080)), new PixelRect(new PixelSize(1920, 1040)), true);
  219. var screen2 = new Mock<Screen>(1.0, new PixelRect(new PixelSize(1366, 768)), new PixelRect(new PixelSize(1366, 728)), false);
  220. var screens = new Mock<IScreenImpl>();
  221. screens.Setup(x => x.AllScreens).Returns(new Screen[] { screen1.Object, screen2.Object });
  222. var windowImpl = MockWindowingPlatform.CreateWindowMock();
  223. windowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
  224. windowImpl.Setup(x => x.Scaling).Returns(1);
  225. windowImpl.Setup(x => x.Screen).Returns(screens.Object);
  226. using (UnitTestApplication.Start(TestServices.StyledWindow))
  227. {
  228. var window = new Window(windowImpl.Object);
  229. window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  230. window.Position = new PixelPoint(60, 40);
  231. window.Show();
  232. var expectedPosition = new PixelPoint(
  233. (int)(screen1.Object.WorkingArea.Size.Width / 2 - window.ClientSize.Width / 2),
  234. (int)(screen1.Object.WorkingArea.Size.Height / 2 - window.ClientSize.Height / 2));
  235. Assert.Equal(window.Position, expectedPosition);
  236. }
  237. }
  238. [Fact]
  239. public void Window_Should_Be_Centered_Relative_To_Owner_When_WindowStartupLocation_Is_CenterOwner()
  240. {
  241. var parentWindowImpl = MockWindowingPlatform.CreateWindowMock();
  242. parentWindowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
  243. parentWindowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1920, 1080));
  244. parentWindowImpl.Setup(x => x.Scaling).Returns(1);
  245. var windowImpl = MockWindowingPlatform.CreateWindowMock();
  246. windowImpl.Setup(x => x.ClientSize).Returns(new Size(320, 200));
  247. windowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1920, 1080));
  248. windowImpl.Setup(x => x.Scaling).Returns(1);
  249. var parentWindowServices = TestServices.StyledWindow.With(
  250. windowingPlatform: new MockWindowingPlatform(() => parentWindowImpl.Object));
  251. var windowServices = TestServices.StyledWindow.With(
  252. windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object));
  253. using (UnitTestApplication.Start(parentWindowServices))
  254. {
  255. var parentWindow = new Window();
  256. parentWindow.Position = new PixelPoint(60, 40);
  257. parentWindow.Show();
  258. using (UnitTestApplication.Start(windowServices))
  259. {
  260. var window = new Window();
  261. window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
  262. window.Position = new PixelPoint(60, 40);
  263. window.Owner = parentWindow;
  264. window.Show();
  265. var expectedPosition = new PixelPoint(
  266. (int)(parentWindow.Position.X + parentWindow.ClientSize.Width / 2 - window.ClientSize.Width / 2),
  267. (int)(parentWindow.Position.Y + parentWindow.ClientSize.Height / 2 - window.ClientSize.Height / 2));
  268. Assert.Equal(window.Position, expectedPosition);
  269. }
  270. }
  271. }
  272. [Fact]
  273. public void Child_Should_Be_Measured_With_Width_And_Height_If_SizeToContent_Is_Manual()
  274. {
  275. using (UnitTestApplication.Start(TestServices.StyledWindow))
  276. {
  277. var child = new ChildControl();
  278. var target = new Window
  279. {
  280. Width = 100,
  281. Height = 50,
  282. SizeToContent = SizeToContent.Manual,
  283. Content = child
  284. };
  285. target.Show();
  286. Assert.Equal(new Size(100, 50), child.MeasureSize);
  287. }
  288. }
  289. [Fact]
  290. public void Child_Should_Be_Measured_With_Infinity_If_SizeToContent_Is_WidthAndHeight()
  291. {
  292. using (UnitTestApplication.Start(TestServices.StyledWindow))
  293. {
  294. var child = new ChildControl();
  295. var target = new Window
  296. {
  297. Width = 100,
  298. Height = 50,
  299. SizeToContent = SizeToContent.WidthAndHeight,
  300. Content = child
  301. };
  302. target.Show();
  303. Assert.Equal(Size.Infinity, child.MeasureSize);
  304. }
  305. }
  306. private IWindowImpl CreateImpl(Mock<IRenderer> renderer)
  307. {
  308. return Mock.Of<IWindowImpl>(x =>
  309. x.Scaling == 1 &&
  310. x.CreateRenderer(It.IsAny<IRenderRoot>()) == renderer.Object);
  311. }
  312. private class ChildControl : Control
  313. {
  314. public Size MeasureSize { get; private set; }
  315. protected override Size MeasureOverride(Size availableSize)
  316. {
  317. MeasureSize = availableSize;
  318. return base.MeasureOverride(availableSize);
  319. }
  320. }
  321. }
  322. }