WindowTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 Showing_Should_Start_Renderer()
  153. {
  154. using (UnitTestApplication.Start(TestServices.StyledWindow))
  155. {
  156. var renderer = new Mock<IRenderer>();
  157. var target = new Window(CreateImpl(renderer));
  158. target.Show();
  159. renderer.Verify(x => x.Start(), Times.Once);
  160. }
  161. }
  162. [Fact]
  163. public void ShowDialog_Should_Start_Renderer()
  164. {
  165. using (UnitTestApplication.Start(TestServices.StyledWindow))
  166. {
  167. var renderer = new Mock<IRenderer>();
  168. var target = new Window(CreateImpl(renderer));
  169. target.Show();
  170. renderer.Verify(x => x.Start(), Times.Once);
  171. }
  172. }
  173. [Fact]
  174. public void Hiding_Should_Stop_Renderer()
  175. {
  176. using (UnitTestApplication.Start(TestServices.StyledWindow))
  177. {
  178. var renderer = new Mock<IRenderer>();
  179. var target = new Window(CreateImpl(renderer));
  180. target.Show();
  181. target.Hide();
  182. renderer.Verify(x => x.Stop(), Times.Once);
  183. }
  184. }
  185. [Fact]
  186. public async Task ShowDialog_With_ValueType_Returns_Default_When_Closed()
  187. {
  188. using (UnitTestApplication.Start(TestServices.StyledWindow))
  189. {
  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>();
  195. windowImpl.Object.Closed();
  196. var result = await task;
  197. Assert.False(result);
  198. }
  199. }
  200. [Fact]
  201. public void Window_Should_Be_Centered_When_WindowStartupLocation_Is_CenterScreen()
  202. {
  203. var screen1 = new Mock<Screen>(new Rect(new Size(1920, 1080)), new Rect(new Size(1920, 1040)), true);
  204. var screen2 = new Mock<Screen>(new Rect(new Size(1366, 768)), new Rect(new Size(1366, 728)), false);
  205. var screens = new Mock<IScreenImpl>();
  206. screens.Setup(x => x.AllScreens).Returns(new Screen[] { screen1.Object, screen2.Object });
  207. var windowImpl = new Mock<IWindowImpl>();
  208. windowImpl.SetupProperty(x => x.Position);
  209. windowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
  210. windowImpl.Setup(x => x.Scaling).Returns(1);
  211. windowImpl.Setup(x => x.Screen).Returns(screens.Object);
  212. using (UnitTestApplication.Start(TestServices.StyledWindow))
  213. {
  214. var window = new Window(windowImpl.Object);
  215. window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  216. window.Position = new Point(60, 40);
  217. window.Show();
  218. var expectedPosition = new Point(
  219. screen1.Object.WorkingArea.Size.Width / 2 - window.ClientSize.Width / 2,
  220. screen1.Object.WorkingArea.Size.Height / 2 - window.ClientSize.Height / 2);
  221. Assert.Equal(window.Position, expectedPosition);
  222. }
  223. }
  224. [Fact]
  225. public void Window_Should_Be_Centered_Relative_To_Owner_When_WindowStartupLocation_Is_CenterOwner()
  226. {
  227. var parentWindowImpl = new Mock<IWindowImpl>();
  228. parentWindowImpl.SetupProperty(x => x.Position);
  229. parentWindowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
  230. parentWindowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1920, 1080));
  231. parentWindowImpl.Setup(x => x.Scaling).Returns(1);
  232. var windowImpl = new Mock<IWindowImpl>();
  233. windowImpl.SetupProperty(x => x.Position);
  234. windowImpl.Setup(x => x.ClientSize).Returns(new Size(320, 200));
  235. windowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1920, 1080));
  236. windowImpl.Setup(x => x.Scaling).Returns(1);
  237. var parentWindowServices = TestServices.StyledWindow.With(
  238. windowingPlatform: new MockWindowingPlatform(() => parentWindowImpl.Object));
  239. var windowServices = TestServices.StyledWindow.With(
  240. windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object));
  241. using (UnitTestApplication.Start(parentWindowServices))
  242. {
  243. var parentWindow = new Window();
  244. parentWindow.Position = new Point(60, 40);
  245. parentWindow.Show();
  246. using (UnitTestApplication.Start(windowServices))
  247. {
  248. var window = new Window();
  249. window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
  250. window.Position = new Point(60, 40);
  251. window.Owner = parentWindow;
  252. window.Show();
  253. var expectedPosition = new Point(
  254. parentWindow.Position.X + parentWindow.ClientSize.Width / 2 - window.ClientSize.Width / 2,
  255. parentWindow.Position.Y + parentWindow.ClientSize.Height / 2 - window.ClientSize.Height / 2);
  256. Assert.Equal(window.Position, expectedPosition);
  257. }
  258. }
  259. }
  260. private IWindowImpl CreateImpl(Mock<IRenderer> renderer)
  261. {
  262. return Mock.Of<IWindowImpl>(x =>
  263. x.Scaling == 1 &&
  264. x.CreateRenderer(It.IsAny<IRenderRoot>()) == renderer.Object);
  265. }
  266. private void ClearOpenWindows()
  267. {
  268. // HACK: We really need a decent way to have "statics" that can be scoped to
  269. // AvaloniaLocator scopes.
  270. Application.Current.Windows.Clear();
  271. }
  272. }
  273. }