WindowTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 windowImpl = Mock.Of<IWindowImpl>(x => x.Scaling == 1);
  186. var target = new Window(windowImpl);
  187. target.Show();
  188. target.Close();
  189. var openedRaised = false;
  190. target.Opened += (s, e) => openedRaised = true;
  191. var ex = Assert.Throws<InvalidOperationException>(() => target.Show());
  192. Assert.Equal("Cannot re-show a closed window.", ex.Message);
  193. Assert.False(openedRaised);
  194. }
  195. }
  196. [Fact]
  197. public async Task Calling_ShowDialog_On_Closed_Window_Should_Throw()
  198. {
  199. using (UnitTestApplication.Start(TestServices.StyledWindow))
  200. {
  201. var parent = new Mock<IWindowImpl>();
  202. var windowImpl = new Mock<IWindowImpl>();
  203. windowImpl.SetupProperty(x => x.Closed);
  204. windowImpl.Setup(x => x.Scaling).Returns(1);
  205. var target = new Window(windowImpl.Object);
  206. var task = target.ShowDialog<bool>(parent.Object);
  207. windowImpl.Object.Closed();
  208. await task;
  209. var openedRaised = false;
  210. target.Opened += (s, e) => openedRaised = true;
  211. var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => target.ShowDialog<bool>(parent.Object));
  212. Assert.Equal("Cannot re-show a closed window.", ex.Message);
  213. Assert.False(openedRaised);
  214. }
  215. }
  216. [Fact]
  217. public void Window_Should_Be_Centered_When_WindowStartupLocation_Is_CenterScreen()
  218. {
  219. var screen1 = new Mock<Screen>(new PixelRect(new PixelSize(1920, 1080)), new PixelRect(new PixelSize(1920, 1040)), true);
  220. var screen2 = new Mock<Screen>(new PixelRect(new PixelSize(1366, 768)), new PixelRect(new PixelSize(1366, 728)), false);
  221. var screens = new Mock<IScreenImpl>();
  222. screens.Setup(x => x.AllScreens).Returns(new Screen[] { screen1.Object, screen2.Object });
  223. var windowImpl = MockWindowingPlatform.CreateWindowMock();
  224. windowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
  225. windowImpl.Setup(x => x.Scaling).Returns(1);
  226. windowImpl.Setup(x => x.Screen).Returns(screens.Object);
  227. using (UnitTestApplication.Start(TestServices.StyledWindow))
  228. {
  229. var window = new Window(windowImpl.Object);
  230. window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  231. window.Position = new PixelPoint(60, 40);
  232. window.Show();
  233. var expectedPosition = new PixelPoint(
  234. (int)(screen1.Object.WorkingArea.Size.Width / 2 - window.ClientSize.Width / 2),
  235. (int)(screen1.Object.WorkingArea.Size.Height / 2 - window.ClientSize.Height / 2));
  236. Assert.Equal(window.Position, expectedPosition);
  237. }
  238. }
  239. [Fact]
  240. public void Window_Should_Be_Centered_Relative_To_Owner_When_WindowStartupLocation_Is_CenterOwner()
  241. {
  242. var parentWindowImpl = MockWindowingPlatform.CreateWindowMock();
  243. parentWindowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
  244. parentWindowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1920, 1080));
  245. parentWindowImpl.Setup(x => x.Scaling).Returns(1);
  246. var windowImpl = MockWindowingPlatform.CreateWindowMock();
  247. windowImpl.Setup(x => x.ClientSize).Returns(new Size(320, 200));
  248. windowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1920, 1080));
  249. windowImpl.Setup(x => x.Scaling).Returns(1);
  250. var parentWindowServices = TestServices.StyledWindow.With(
  251. windowingPlatform: new MockWindowingPlatform(() => parentWindowImpl.Object));
  252. var windowServices = TestServices.StyledWindow.With(
  253. windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object));
  254. using (UnitTestApplication.Start(parentWindowServices))
  255. {
  256. var parentWindow = new Window();
  257. parentWindow.Position = new PixelPoint(60, 40);
  258. parentWindow.Show();
  259. using (UnitTestApplication.Start(windowServices))
  260. {
  261. var window = new Window();
  262. window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
  263. window.Position = new PixelPoint(60, 40);
  264. window.Owner = parentWindow;
  265. window.Show();
  266. var expectedPosition = new PixelPoint(
  267. (int)(parentWindow.Position.X + parentWindow.ClientSize.Width / 2 - window.ClientSize.Width / 2),
  268. (int)(parentWindow.Position.Y + parentWindow.ClientSize.Height / 2 - window.ClientSize.Height / 2));
  269. Assert.Equal(window.Position, expectedPosition);
  270. }
  271. }
  272. }
  273. private IWindowImpl CreateImpl(Mock<IRenderer> renderer)
  274. {
  275. return Mock.Of<IWindowImpl>(x =>
  276. x.Scaling == 1 &&
  277. x.CreateRenderer(It.IsAny<IRenderRoot>()) == renderer.Object);
  278. }
  279. }
  280. }