WindowTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 window = new Window();
  55. var task = window.ShowDialog();
  56. Assert.True(window.IsVisible);
  57. }
  58. }
  59. [Fact]
  60. public void IsVisible_Should_Be_False_After_Hide()
  61. {
  62. using (UnitTestApplication.Start(TestServices.StyledWindow))
  63. {
  64. var window = new Window();
  65. window.Show();
  66. window.Hide();
  67. Assert.False(window.IsVisible);
  68. }
  69. }
  70. [Fact]
  71. public void IsVisible_Should_Be_False_After_Close()
  72. {
  73. using (UnitTestApplication.Start(TestServices.StyledWindow))
  74. {
  75. var window = new Window();
  76. window.Show();
  77. window.Close();
  78. Assert.False(window.IsVisible);
  79. }
  80. }
  81. [Fact]
  82. public void IsVisible_Should_Be_False_After_Impl_Signals_Close()
  83. {
  84. var windowImpl = new Mock<IWindowImpl>();
  85. windowImpl.SetupProperty(x => x.Closed);
  86. windowImpl.Setup(x => x.Scaling).Returns(1);
  87. var services = TestServices.StyledWindow.With(
  88. windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object));
  89. using (UnitTestApplication.Start(services))
  90. {
  91. var window = new Window();
  92. window.Show();
  93. windowImpl.Object.Closed();
  94. Assert.False(window.IsVisible);
  95. }
  96. }
  97. [Fact]
  98. public void Show_Should_Add_Window_To_OpenWindows()
  99. {
  100. using (UnitTestApplication.Start(TestServices.StyledWindow))
  101. {
  102. ClearOpenWindows();
  103. var window = new Window();
  104. window.Show();
  105. Assert.Equal(new[] { window }, Application.Current.Windows);
  106. }
  107. }
  108. [Fact]
  109. public void Window_Should_Be_Added_To_OpenWindows_Only_Once()
  110. {
  111. using (UnitTestApplication.Start(TestServices.StyledWindow))
  112. {
  113. ClearOpenWindows();
  114. var window = new Window();
  115. window.Show();
  116. window.Show();
  117. window.IsVisible = true;
  118. Assert.Equal(new[] { window }, Application.Current.Windows);
  119. window.Close();
  120. }
  121. }
  122. [Fact]
  123. public void Close_Should_Remove_Window_From_OpenWindows()
  124. {
  125. using (UnitTestApplication.Start(TestServices.StyledWindow))
  126. {
  127. ClearOpenWindows();
  128. var window = new Window();
  129. window.Show();
  130. window.Close();
  131. Assert.Empty(Application.Current.Windows);
  132. }
  133. }
  134. [Fact]
  135. public void Impl_Closing_Should_Remove_Window_From_OpenWindows()
  136. {
  137. var windowImpl = new Mock<IWindowImpl>();
  138. windowImpl.SetupProperty(x => x.Closed);
  139. windowImpl.Setup(x => x.Scaling).Returns(1);
  140. var services = TestServices.StyledWindow.With(
  141. windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object));
  142. using (UnitTestApplication.Start(services))
  143. {
  144. ClearOpenWindows();
  145. var window = new Window();
  146. window.Show();
  147. windowImpl.Object.Closed();
  148. Assert.Empty(Application.Current.Windows);
  149. }
  150. }
  151. [Fact]
  152. public void Closing_Should_Only_Be_Invoked_Once()
  153. {
  154. using (UnitTestApplication.Start(TestServices.StyledWindow))
  155. {
  156. var window = new Window();
  157. var count = 0;
  158. window.Closing +=
  159. (sender, e) =>
  160. {
  161. count++;
  162. };
  163. window.Show();
  164. window.Close();
  165. Assert.Equal(1, count);
  166. }
  167. }
  168. [Fact]
  169. public void Showing_Should_Start_Renderer()
  170. {
  171. using (UnitTestApplication.Start(TestServices.StyledWindow))
  172. {
  173. var renderer = new Mock<IRenderer>();
  174. var target = new Window(CreateImpl(renderer));
  175. target.Show();
  176. renderer.Verify(x => x.Start(), Times.Once);
  177. }
  178. }
  179. [Fact]
  180. public void ShowDialog_Should_Start_Renderer()
  181. {
  182. using (UnitTestApplication.Start(TestServices.StyledWindow))
  183. {
  184. var renderer = new Mock<IRenderer>();
  185. var target = new Window(CreateImpl(renderer));
  186. target.Show();
  187. renderer.Verify(x => x.Start(), Times.Once);
  188. }
  189. }
  190. [Fact]
  191. public void Hiding_Should_Stop_Renderer()
  192. {
  193. using (UnitTestApplication.Start(TestServices.StyledWindow))
  194. {
  195. var renderer = new Mock<IRenderer>();
  196. var target = new Window(CreateImpl(renderer));
  197. target.Show();
  198. target.Hide();
  199. renderer.Verify(x => x.Stop(), Times.Once);
  200. }
  201. }
  202. [Fact]
  203. public async Task ShowDialog_With_ValueType_Returns_Default_When_Closed()
  204. {
  205. using (UnitTestApplication.Start(TestServices.StyledWindow))
  206. {
  207. var windowImpl = new Mock<IWindowImpl>();
  208. windowImpl.SetupProperty(x => x.Closed);
  209. windowImpl.Setup(x => x.Scaling).Returns(1);
  210. var target = new Window(windowImpl.Object);
  211. var task = target.ShowDialog<bool>();
  212. windowImpl.Object.Closed();
  213. var result = await task;
  214. Assert.False(result);
  215. }
  216. }
  217. [Fact]
  218. public void Window_Should_Be_Centered_When_WindowStartupLocation_Is_CenterScreen()
  219. {
  220. var screen1 = new Mock<Screen>(new Rect(new Size(1920, 1080)), new Rect(new Size(1920, 1040)), true);
  221. var screen2 = new Mock<Screen>(new Rect(new Size(1366, 768)), new Rect(new Size(1366, 728)), false);
  222. var screens = new Mock<IScreenImpl>();
  223. screens.Setup(x => x.AllScreens).Returns(new Screen[] { screen1.Object, screen2.Object });
  224. var windowImpl = new Mock<IWindowImpl>();
  225. windowImpl.SetupProperty(x => x.Position);
  226. windowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
  227. windowImpl.Setup(x => x.Scaling).Returns(1);
  228. windowImpl.Setup(x => x.Screen).Returns(screens.Object);
  229. using (UnitTestApplication.Start(TestServices.StyledWindow))
  230. {
  231. var window = new Window(windowImpl.Object);
  232. window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  233. window.Position = new Point(60, 40);
  234. window.Show();
  235. var expectedPosition = new Point(
  236. screen1.Object.WorkingArea.Size.Width / 2 - window.ClientSize.Width / 2,
  237. screen1.Object.WorkingArea.Size.Height / 2 - window.ClientSize.Height / 2);
  238. Assert.Equal(window.Position, expectedPosition);
  239. }
  240. }
  241. [Fact]
  242. public void Window_Should_Be_Centered_Relative_To_Owner_When_WindowStartupLocation_Is_CenterOwner()
  243. {
  244. var parentWindowImpl = new Mock<IWindowImpl>();
  245. parentWindowImpl.SetupProperty(x => x.Position);
  246. parentWindowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
  247. parentWindowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1920, 1080));
  248. parentWindowImpl.Setup(x => x.Scaling).Returns(1);
  249. var windowImpl = new Mock<IWindowImpl>();
  250. windowImpl.SetupProperty(x => x.Position);
  251. windowImpl.Setup(x => x.ClientSize).Returns(new Size(320, 200));
  252. windowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1920, 1080));
  253. windowImpl.Setup(x => x.Scaling).Returns(1);
  254. var parentWindowServices = TestServices.StyledWindow.With(
  255. windowingPlatform: new MockWindowingPlatform(() => parentWindowImpl.Object));
  256. var windowServices = TestServices.StyledWindow.With(
  257. windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object));
  258. using (UnitTestApplication.Start(parentWindowServices))
  259. {
  260. var parentWindow = new Window();
  261. parentWindow.Position = new Point(60, 40);
  262. parentWindow.Show();
  263. using (UnitTestApplication.Start(windowServices))
  264. {
  265. var window = new Window();
  266. window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
  267. window.Position = new Point(60, 40);
  268. window.Owner = parentWindow;
  269. window.Show();
  270. var expectedPosition = new Point(
  271. parentWindow.Position.X + parentWindow.ClientSize.Width / 2 - window.ClientSize.Width / 2,
  272. parentWindow.Position.Y + parentWindow.ClientSize.Height / 2 - window.ClientSize.Height / 2);
  273. Assert.Equal(window.Position, expectedPosition);
  274. }
  275. }
  276. }
  277. private IWindowImpl CreateImpl(Mock<IRenderer> renderer)
  278. {
  279. return Mock.Of<IWindowImpl>(x =>
  280. x.Scaling == 1 &&
  281. x.CreateRenderer(It.IsAny<IRenderRoot>()) == renderer.Object);
  282. }
  283. private void ClearOpenWindows()
  284. {
  285. // HACK: We really need a decent way to have "statics" that can be scoped to
  286. // AvaloniaLocator scopes.
  287. Application.Current.Windows.Clear();
  288. }
  289. }
  290. }