WindowTests.cs 13 KB

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