WindowTests.cs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Avalonia.Platform;
  5. using Avalonia.Rendering;
  6. using Avalonia.UnitTests;
  7. using Moq;
  8. using Xunit;
  9. namespace Avalonia.Controls.UnitTests
  10. {
  11. public class WindowTests
  12. {
  13. [Fact]
  14. public void Setting_Title_Should_Set_Impl_Title()
  15. {
  16. var windowImpl = new Mock<IWindowImpl>();
  17. windowImpl.Setup(r => r.CreateRenderer(It.IsAny<IRenderRoot>()))
  18. .Returns(RendererMocks.CreateRenderer().Object);
  19. var windowingPlatform = new MockWindowingPlatform(() => windowImpl.Object);
  20. using (UnitTestApplication.Start(new TestServices(windowingPlatform: windowingPlatform)))
  21. {
  22. var target = new Window();
  23. target.Title = "Hello World";
  24. windowImpl.Verify(x => x.SetTitle("Hello World"));
  25. }
  26. }
  27. [Fact]
  28. public void IsVisible_Should_Initially_Be_False()
  29. {
  30. using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
  31. {
  32. var window = new Window();
  33. Assert.False(window.IsVisible);
  34. }
  35. }
  36. [Fact]
  37. public void IsVisible_Should_Be_True_After_Show()
  38. {
  39. using (UnitTestApplication.Start(TestServices.StyledWindow))
  40. {
  41. var window = new Window();
  42. window.Show();
  43. Assert.True(window.IsVisible);
  44. }
  45. }
  46. [Fact]
  47. public void IsVisible_Should_Be_True_After_ShowDialog()
  48. {
  49. using (UnitTestApplication.Start(TestServices.StyledWindow))
  50. {
  51. var parent = new Window();
  52. parent.Show();
  53. var window = new Window();
  54. var task = window.ShowDialog(parent);
  55. Assert.True(window.IsVisible);
  56. }
  57. }
  58. [Fact]
  59. public void IsVisible_Should_Be_False_After_Hide()
  60. {
  61. using (UnitTestApplication.Start(TestServices.StyledWindow))
  62. {
  63. var window = new Window();
  64. window.Show();
  65. window.Hide();
  66. Assert.False(window.IsVisible);
  67. }
  68. }
  69. [Fact]
  70. public void IsVisible_Should_Be_False_After_Close()
  71. {
  72. using (UnitTestApplication.Start(TestServices.StyledWindow))
  73. {
  74. var window = new Window();
  75. window.Show();
  76. window.Close();
  77. Assert.False(window.IsVisible);
  78. }
  79. }
  80. [Fact]
  81. public void IsVisible_Should_Be_False_After_Impl_Signals_Close()
  82. {
  83. var windowImpl = new Mock<IWindowImpl>();
  84. windowImpl.Setup(x => x.CreateRenderer(It.IsAny<IRenderRoot>()))
  85. .Returns(() => RendererMocks.CreateRenderer().Object);
  86. windowImpl.SetupProperty(x => x.Closed);
  87. windowImpl.Setup(x => x.DesktopScaling).Returns(1);
  88. windowImpl.Setup(x => x.RenderScaling).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. [Theory]
  117. [InlineData(true)]
  118. [InlineData(false)]
  119. public void Child_windows_should_be_closed_before_parent(bool programmaticClose)
  120. {
  121. using (UnitTestApplication.Start(TestServices.StyledWindow))
  122. {
  123. var window = new Window();
  124. var child = new Window();
  125. int count = 0;
  126. int windowClosing = 0;
  127. int childClosing = 0;
  128. int windowClosed = 0;
  129. int childClosed = 0;
  130. window.Closing += (sender, e) =>
  131. {
  132. Assert.Equal(WindowCloseReason.WindowClosing, e.CloseReason);
  133. Assert.Equal(programmaticClose, e.IsProgrammatic);
  134. count++;
  135. windowClosing = count;
  136. };
  137. child.Closing += (sender, e) =>
  138. {
  139. Assert.Equal(WindowCloseReason.OwnerWindowClosing, e.CloseReason);
  140. Assert.Equal(programmaticClose, e.IsProgrammatic);
  141. count++;
  142. childClosing = count;
  143. };
  144. window.Closed += (sender, e) =>
  145. {
  146. count++;
  147. windowClosed = count;
  148. };
  149. child.Closed += (sender, e) =>
  150. {
  151. count++;
  152. childClosed = count;
  153. };
  154. window.Show();
  155. child.Show(window);
  156. if (programmaticClose)
  157. {
  158. window.Close();
  159. }
  160. else
  161. {
  162. var cancel = window.PlatformImpl.Closing(WindowCloseReason.WindowClosing);
  163. Assert.Equal(false, cancel);
  164. }
  165. Assert.Equal(2, windowClosing);
  166. Assert.Equal(1, childClosing);
  167. Assert.Equal(4, windowClosed);
  168. Assert.Equal(3, childClosed);
  169. }
  170. }
  171. [Theory]
  172. [InlineData(true)]
  173. [InlineData(false)]
  174. public void Child_windows_must_not_close_before_parent_has_chance_to_Cancel_OSCloseButton(bool programmaticClose)
  175. {
  176. using (UnitTestApplication.Start(TestServices.StyledWindow))
  177. {
  178. var window = new Window();
  179. var child = new Window();
  180. int count = 0;
  181. int windowClosing = 0;
  182. int childClosing = 0;
  183. int windowClosed = 0;
  184. int childClosed = 0;
  185. window.Closing += (sender, e) =>
  186. {
  187. count++;
  188. windowClosing = count;
  189. e.Cancel = true;
  190. };
  191. child.Closing += (sender, e) =>
  192. {
  193. count++;
  194. childClosing = count;
  195. };
  196. window.Closed += (sender, e) =>
  197. {
  198. count++;
  199. windowClosed = count;
  200. };
  201. child.Closed += (sender, e) =>
  202. {
  203. count++;
  204. childClosed = count;
  205. };
  206. window.Show();
  207. child.Show(window);
  208. if (programmaticClose)
  209. {
  210. window.Close();
  211. }
  212. else
  213. {
  214. var cancel = window.PlatformImpl.Closing(WindowCloseReason.WindowClosing);
  215. Assert.Equal(true, cancel);
  216. }
  217. Assert.Equal(2, windowClosing);
  218. Assert.Equal(1, childClosing);
  219. Assert.Equal(0, windowClosed);
  220. Assert.Equal(0, childClosed);
  221. }
  222. }
  223. [Fact]
  224. public void Showing_Should_Start_Renderer()
  225. {
  226. using (UnitTestApplication.Start(TestServices.StyledWindow))
  227. {
  228. var renderer = RendererMocks.CreateRenderer();
  229. var target = new Window(CreateImpl(renderer));
  230. target.Show();
  231. renderer.Verify(x => x.Start(), Times.Once);
  232. }
  233. }
  234. [Fact]
  235. public void ShowDialog_Should_Start_Renderer()
  236. {
  237. using (UnitTestApplication.Start(TestServices.StyledWindow))
  238. {
  239. var parent = new Window();
  240. var renderer = RendererMocks.CreateRenderer();
  241. var target = new Window(CreateImpl(renderer));
  242. parent.Show();
  243. target.ShowDialog<object>(parent);
  244. renderer.Verify(x => x.Start(), Times.Once);
  245. }
  246. }
  247. [Fact]
  248. public void ShowDialog_Should_Raise_Opened()
  249. {
  250. using (UnitTestApplication.Start(TestServices.StyledWindow))
  251. {
  252. var parent = new Window();
  253. var target = new Window();
  254. var raised = false;
  255. parent.Show();
  256. target.Opened += (s, e) => raised = true;
  257. target.ShowDialog<object>(parent);
  258. Assert.True(raised);
  259. }
  260. }
  261. [Fact]
  262. public void Hiding_Should_Stop_Renderer()
  263. {
  264. using (UnitTestApplication.Start(TestServices.StyledWindow))
  265. {
  266. var renderer = RendererMocks.CreateRenderer();
  267. var target = new Window(CreateImpl(renderer));
  268. target.Show();
  269. target.Hide();
  270. renderer.Verify(x => x.Stop(), Times.Once);
  271. }
  272. }
  273. [Fact]
  274. public async Task ShowDialog_With_ValueType_Returns_Default_When_Closed()
  275. {
  276. using (UnitTestApplication.Start(TestServices.StyledWindow))
  277. {
  278. var parent = new Window();
  279. var windowImpl = new Mock<IWindowImpl>();
  280. windowImpl.Setup(x => x.CreateRenderer(It.IsAny<IRenderRoot>()))
  281. .Returns(() => RendererMocks.CreateRenderer().Object);
  282. windowImpl.SetupProperty(x => x.Closed);
  283. windowImpl.Setup(x => x.DesktopScaling).Returns(1);
  284. windowImpl.Setup(x => x.RenderScaling).Returns(1);
  285. parent.Show();
  286. var target = new Window(windowImpl.Object);
  287. var task = target.ShowDialog<bool>(parent);
  288. windowImpl.Object.Closed();
  289. var result = await task;
  290. Assert.False(result);
  291. }
  292. }
  293. [Fact]
  294. public void Calling_Show_On_Closed_Window_Should_Throw()
  295. {
  296. using (UnitTestApplication.Start(TestServices.StyledWindow))
  297. {
  298. var target = new Window();
  299. target.Show();
  300. target.Close();
  301. var openedRaised = false;
  302. target.Opened += (s, e) => openedRaised = true;
  303. var ex = Assert.Throws<InvalidOperationException>(() => target.Show());
  304. Assert.Equal("Cannot re-show a closed window.", ex.Message);
  305. Assert.False(openedRaised);
  306. }
  307. }
  308. [Fact]
  309. public async Task Calling_ShowDialog_On_Closed_Window_Should_Throw()
  310. {
  311. using (UnitTestApplication.Start(TestServices.StyledWindow))
  312. {
  313. var parent = new Window();
  314. var windowImpl = new Mock<IWindowImpl>();
  315. windowImpl.Setup(x => x.CreateRenderer(It.IsAny<IRenderRoot>()))
  316. .Returns(() => RendererMocks.CreateRenderer().Object);
  317. windowImpl.SetupProperty(x => x.Closed);
  318. windowImpl.Setup(x => x.DesktopScaling).Returns(1);
  319. windowImpl.Setup(x => x.RenderScaling).Returns(1);
  320. parent.Show();
  321. var target = new Window(windowImpl.Object);
  322. var task = target.ShowDialog<bool>(parent);
  323. windowImpl.Object.Closed();
  324. await task;
  325. var openedRaised = false;
  326. target.Opened += (s, e) => openedRaised = true;
  327. var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => target.ShowDialog<bool>(parent));
  328. Assert.Equal("Cannot re-show a closed window.", ex.Message);
  329. Assert.False(openedRaised);
  330. }
  331. }
  332. [Fact]
  333. public void Calling_Show_With_Closed_Parent_Window_Should_Throw()
  334. {
  335. using (UnitTestApplication.Start(TestServices.StyledWindow))
  336. {
  337. var parent = new Window();
  338. var target = new Window();
  339. parent.Close();
  340. var ex = Assert.Throws<InvalidOperationException>(() => target.Show(parent));
  341. Assert.Equal("Cannot show a window with a closed owner.", ex.Message);
  342. }
  343. }
  344. [Fact]
  345. public async Task Calling_ShowDialog_With_Closed_Parent_Window_Should_Throw()
  346. {
  347. using (UnitTestApplication.Start(TestServices.StyledWindow))
  348. {
  349. var parent = new Window();
  350. var target = new Window();
  351. parent.Close();
  352. var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => target.ShowDialog(parent));
  353. Assert.Equal("Cannot show a window with a closed owner.", ex.Message);
  354. }
  355. }
  356. [Fact]
  357. public void Calling_Show_With_Invisible_Parent_Window_Should_Throw()
  358. {
  359. using (UnitTestApplication.Start(TestServices.StyledWindow))
  360. {
  361. var parent = new Window();
  362. var target = new Window();
  363. var ex = Assert.Throws<InvalidOperationException>(() => target.Show(parent));
  364. Assert.Equal("Cannot show window with non-visible owner.", ex.Message);
  365. }
  366. }
  367. [Fact]
  368. public async Task Calling_ShowDialog_With_Invisible_Parent_Window_Should_Throw()
  369. {
  370. using (UnitTestApplication.Start(TestServices.StyledWindow))
  371. {
  372. var parent = new Window();
  373. var target = new Window();
  374. var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => target.ShowDialog(parent));
  375. Assert.Equal("Cannot show window with non-visible owner.", ex.Message);
  376. }
  377. }
  378. [Fact]
  379. public void Calling_Show_With_Self_As_Parent_Window_Should_Throw()
  380. {
  381. using (UnitTestApplication.Start(TestServices.StyledWindow))
  382. {
  383. var target = new Window();
  384. var ex = Assert.Throws<InvalidOperationException>(() => target.Show(target));
  385. Assert.Equal("A Window cannot be its own owner.", ex.Message);
  386. }
  387. }
  388. [Fact]
  389. public async Task Calling_ShowDialog_With_Self_As_Parent_Window_Should_Throw()
  390. {
  391. using (UnitTestApplication.Start(TestServices.StyledWindow))
  392. {
  393. var target = new Window();
  394. var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => target.ShowDialog(target));
  395. Assert.Equal("A Window cannot be its own owner.", ex.Message);
  396. }
  397. }
  398. [Fact]
  399. public void Hiding_Parent_Window_Should_Close_Children()
  400. {
  401. using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
  402. {
  403. var parent = new Window();
  404. var child = new Window();
  405. parent.Show();
  406. child.Show(parent);
  407. parent.Hide();
  408. Assert.False(parent.IsVisible);
  409. Assert.False(child.IsVisible);
  410. }
  411. }
  412. [Fact]
  413. public void Hiding_Parent_Window_Should_Close_Dialog_Children()
  414. {
  415. using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
  416. {
  417. var parent = new Window();
  418. var child = new Window();
  419. parent.Show();
  420. child.ShowDialog(parent);
  421. parent.Hide();
  422. Assert.False(parent.IsVisible);
  423. Assert.False(child.IsVisible);
  424. }
  425. }
  426. [Fact]
  427. public void Window_Should_Be_Centered_When_WindowStartupLocation_Is_CenterScreen()
  428. {
  429. var screen1 = new Mock<Screen>(1.0, new PixelRect(new PixelSize(1920, 1080)), new PixelRect(new PixelSize(1920, 1040)), true);
  430. var screen2 = new Mock<Screen>(1.0, new PixelRect(new PixelSize(1366, 768)), new PixelRect(new PixelSize(1366, 728)), false);
  431. var screens = new Mock<IScreenImpl>();
  432. screens.Setup(x => x.AllScreens).Returns(new Screen[] { screen1.Object, screen2.Object });
  433. screens.Setup(x => x.ScreenFromPoint(It.IsAny<PixelPoint>())).Returns(screen1.Object);
  434. var windowImpl = MockWindowingPlatform.CreateWindowMock();
  435. windowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
  436. windowImpl.Setup(x => x.DesktopScaling).Returns(1);
  437. windowImpl.Setup(x => x.RenderScaling).Returns(1);
  438. windowImpl.Setup(x => x.Screen).Returns(screens.Object);
  439. using (UnitTestApplication.Start(TestServices.StyledWindow))
  440. {
  441. var window = new Window(windowImpl.Object);
  442. window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  443. window.Position = new PixelPoint(60, 40);
  444. window.Show();
  445. var expectedPosition = new PixelPoint(
  446. (int)(screen1.Object.WorkingArea.Size.Width / 2 - window.ClientSize.Width / 2),
  447. (int)(screen1.Object.WorkingArea.Size.Height / 2 - window.ClientSize.Height / 2));
  448. Assert.Equal(window.Position, expectedPosition);
  449. }
  450. }
  451. [Fact]
  452. public void Window_Should_Be_Sized_To_MinSize_If_InitialSize_Less_Than_MinSize()
  453. {
  454. var screen1 = new Mock<Screen>(1.75, new PixelRect(new PixelSize(1920, 1080)), new PixelRect(new PixelSize(1920, 966)), true);
  455. var screens = new Mock<IScreenImpl>();
  456. screens.Setup(x => x.AllScreens).Returns(new Screen[] { screen1.Object });
  457. screens.Setup(x => x.ScreenFromPoint(It.IsAny<PixelPoint>())).Returns(screen1.Object);
  458. var windowImpl = MockWindowingPlatform.CreateWindowMock(400, 300);
  459. windowImpl.Setup(x => x.DesktopScaling).Returns(1.75);
  460. windowImpl.Setup(x => x.RenderScaling).Returns(1.75);
  461. windowImpl.Setup(x => x.Screen).Returns(screens.Object);
  462. using (UnitTestApplication.Start(TestServices.StyledWindow))
  463. {
  464. var window = new Window(windowImpl.Object);
  465. window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  466. window.MinWidth = 720;
  467. window.MinHeight = 480;
  468. window.Show();
  469. Assert.Equal(new PixelPoint(330, 63), window.Position);
  470. Assert.Equal(new Size(720, 480), window.Bounds.Size);
  471. }
  472. }
  473. [Fact]
  474. public void Window_Should_Be_Centered_Relative_To_Owner_When_WindowStartupLocation_Is_CenterOwner()
  475. {
  476. var parentWindowImpl = MockWindowingPlatform.CreateWindowMock();
  477. parentWindowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
  478. parentWindowImpl.Setup(x => x.MaxAutoSizeHint).Returns(new Size(1920, 1080));
  479. parentWindowImpl.Setup(x => x.DesktopScaling).Returns(1);
  480. parentWindowImpl.Setup(x => x.RenderScaling).Returns(1);
  481. var windowImpl = MockWindowingPlatform.CreateWindowMock();
  482. windowImpl.Setup(x => x.ClientSize).Returns(new Size(320, 200));
  483. windowImpl.Setup(x => x.MaxAutoSizeHint).Returns(new Size(1920, 1080));
  484. windowImpl.Setup(x => x.DesktopScaling).Returns(1);
  485. windowImpl.Setup(x => x.RenderScaling).Returns(1);
  486. var parentWindowServices = TestServices.StyledWindow.With(
  487. windowingPlatform: new MockWindowingPlatform(() => parentWindowImpl.Object));
  488. var windowServices = TestServices.StyledWindow.With(
  489. windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object));
  490. using (UnitTestApplication.Start(parentWindowServices))
  491. {
  492. var parentWindow = new Window();
  493. parentWindow.Position = new PixelPoint(60, 40);
  494. parentWindow.Show();
  495. using (UnitTestApplication.Start(windowServices))
  496. {
  497. var window = new Window();
  498. window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
  499. window.Position = new PixelPoint(60, 40);
  500. window.ShowDialog(parentWindow);
  501. var expectedPosition = new PixelPoint(
  502. (int)(parentWindow.Position.X + parentWindow.ClientSize.Width / 2 - window.ClientSize.Width / 2),
  503. (int)(parentWindow.Position.Y + parentWindow.ClientSize.Height / 2 - window.ClientSize.Height / 2));
  504. Assert.Equal(window.Position, expectedPosition);
  505. }
  506. }
  507. }
  508. public class SizingTests
  509. {
  510. [Fact]
  511. public void Child_Should_Be_Measured_With_Width_And_Height_If_SizeToContent_Is_Manual()
  512. {
  513. using (UnitTestApplication.Start(TestServices.StyledWindow))
  514. {
  515. var child = new ChildControl();
  516. var target = new Window
  517. {
  518. Width = 100,
  519. Height = 50,
  520. SizeToContent = SizeToContent.Manual,
  521. Content = child
  522. };
  523. Show(target);
  524. Assert.Equal(1, child.MeasureSizes.Count);
  525. Assert.Equal(new Size(100, 50), child.MeasureSizes[0]);
  526. }
  527. }
  528. [Fact]
  529. public void Child_Should_Be_Measured_With_ClientSize_If_SizeToContent_Is_Manual_And_No_Width_Height_Specified()
  530. {
  531. using (UnitTestApplication.Start(TestServices.StyledWindow))
  532. {
  533. var windowImpl = MockWindowingPlatform.CreateWindowMock();
  534. windowImpl.Setup(x => x.ClientSize).Returns(new Size(550, 450));
  535. var child = new ChildControl();
  536. var target = new Window(windowImpl.Object)
  537. {
  538. SizeToContent = SizeToContent.Manual,
  539. Content = child
  540. };
  541. Show(target);
  542. Assert.Equal(1, child.MeasureSizes.Count);
  543. Assert.Equal(new Size(550, 450), child.MeasureSizes[0]);
  544. }
  545. }
  546. [Fact]
  547. public void Child_Should_Be_Measured_With_MaxAutoSizeHint_If_SizeToContent_Is_WidthAndHeight()
  548. {
  549. using (UnitTestApplication.Start(TestServices.StyledWindow))
  550. {
  551. var windowImpl = MockWindowingPlatform.CreateWindowMock();
  552. windowImpl.Setup(x => x.MaxAutoSizeHint).Returns(new Size(1200, 1000));
  553. var child = new ChildControl();
  554. var target = new Window(windowImpl.Object)
  555. {
  556. Width = 100,
  557. Height = 50,
  558. SizeToContent = SizeToContent.WidthAndHeight,
  559. Content = child
  560. };
  561. target.Show();
  562. Assert.Equal(1, child.MeasureSizes.Count);
  563. Assert.Equal(new Size(1200, 1000), child.MeasureSizes[0]);
  564. }
  565. }
  566. [Fact]
  567. public void Should_Not_Have_Offset_On_Bounds_When_Content_Larger_Than_Max_Window_Size()
  568. {
  569. // Issue #3784.
  570. using (UnitTestApplication.Start(TestServices.StyledWindow))
  571. {
  572. var windowImpl = MockWindowingPlatform.CreateWindowMock();
  573. var clientSize = new Size(200, 200);
  574. var maxClientSize = new Size(480, 480);
  575. windowImpl.Setup(x => x.Resize(It.IsAny<Size>(), It.IsAny<PlatformResizeReason>()))
  576. .Callback<Size, PlatformResizeReason>((size, reason) =>
  577. {
  578. clientSize = size.Constrain(maxClientSize);
  579. windowImpl.Object.Resized?.Invoke(clientSize, reason);
  580. });
  581. windowImpl.Setup(x => x.ClientSize).Returns(() => clientSize);
  582. var child = new Canvas
  583. {
  584. Width = 400,
  585. Height = 800,
  586. };
  587. var target = new Window(windowImpl.Object)
  588. {
  589. SizeToContent = SizeToContent.WidthAndHeight,
  590. Content = child
  591. };
  592. Show(target);
  593. Assert.Equal(new Size(400, 480), target.Bounds.Size);
  594. // Issue #3784 causes this to be (0, 160) which makes no sense as Window has no
  595. // parent control to be offset against.
  596. Assert.Equal(new Point(0, 0), target.Bounds.Position);
  597. }
  598. }
  599. [Fact]
  600. public void Width_Height_Should_Not_Be_NaN_After_Show_With_SizeToContent_Manual()
  601. {
  602. using (UnitTestApplication.Start(TestServices.StyledWindow))
  603. {
  604. var child = new Canvas
  605. {
  606. Width = 400,
  607. Height = 800,
  608. };
  609. var target = new Window()
  610. {
  611. SizeToContent = SizeToContent.Manual,
  612. Content = child
  613. };
  614. Show(target);
  615. // Values come from MockWindowingPlatform defaults.
  616. Assert.Equal(800, target.Width);
  617. Assert.Equal(600, target.Height);
  618. }
  619. }
  620. [Fact]
  621. public void Width_Height_Should_Not_Be_NaN_After_Show_With_SizeToContent_WidthAndHeight()
  622. {
  623. using (UnitTestApplication.Start(TestServices.StyledWindow))
  624. {
  625. var child = new Canvas
  626. {
  627. Width = 400,
  628. Height = 800,
  629. };
  630. var target = new Window()
  631. {
  632. SizeToContent = SizeToContent.WidthAndHeight,
  633. Content = child
  634. };
  635. target.GetObservable(Window.WidthProperty).Subscribe(x => { });
  636. Show(target);
  637. Assert.Equal(400, target.Width);
  638. Assert.Equal(800, target.Height);
  639. }
  640. }
  641. [Fact]
  642. public void MaxWidth_And_MaxHeight_Should_Be_Respected_With_SizeToContent_WidthAndHeight()
  643. {
  644. using (UnitTestApplication.Start(TestServices.StyledWindow))
  645. {
  646. var child = new ChildControl();
  647. var target = new Window()
  648. {
  649. SizeToContent = SizeToContent.WidthAndHeight,
  650. MaxWidth = 300,
  651. MaxHeight = 700,
  652. Content = child,
  653. };
  654. Show(target);
  655. Assert.Equal(new[] { new Size(300, 700) }, child.MeasureSizes);
  656. }
  657. }
  658. [Fact]
  659. public void SizeToContent_Should_Not_Be_Lost_On_Show()
  660. {
  661. using (UnitTestApplication.Start(TestServices.StyledWindow))
  662. {
  663. var child = new Canvas
  664. {
  665. Width = 400,
  666. Height = 800,
  667. };
  668. var target = new Window()
  669. {
  670. SizeToContent = SizeToContent.WidthAndHeight,
  671. Content = child
  672. };
  673. Show(target);
  674. Assert.Equal(SizeToContent.WidthAndHeight, target.SizeToContent);
  675. }
  676. }
  677. [Fact]
  678. public void SizeToContent_Should_Not_Be_Lost_On_Scaling_Change()
  679. {
  680. using (UnitTestApplication.Start(TestServices.StyledWindow))
  681. {
  682. var child = new Canvas
  683. {
  684. Width = 209,
  685. Height = 117,
  686. };
  687. var target = new Window()
  688. {
  689. SizeToContent = SizeToContent.WidthAndHeight,
  690. Content = child
  691. };
  692. Show(target);
  693. // Size before and after DPI change is a real-world example, with size after DPI
  694. // change coming from Win32 WM_DPICHANGED.
  695. target.PlatformImpl.ScalingChanged(1.5);
  696. target.PlatformImpl.Resized(
  697. new Size(210.66666666666666, 118.66666666666667),
  698. PlatformResizeReason.DpiChange);
  699. Assert.Equal(SizeToContent.WidthAndHeight, target.SizeToContent);
  700. }
  701. }
  702. [Fact]
  703. public void Width_Height_Should_Be_Updated_When_SizeToContent_Is_WidthAndHeight()
  704. {
  705. using (UnitTestApplication.Start(TestServices.StyledWindow))
  706. {
  707. var child = new Canvas
  708. {
  709. Width = 400,
  710. Height = 800,
  711. };
  712. var target = new Window()
  713. {
  714. SizeToContent = SizeToContent.WidthAndHeight,
  715. Content = child
  716. };
  717. Show(target);
  718. Assert.Equal(400, target.Width);
  719. Assert.Equal(800, target.Height);
  720. child.Width = 410;
  721. target.LayoutManager.ExecuteLayoutPass();
  722. Assert.Equal(410, target.Width);
  723. Assert.Equal(800, target.Height);
  724. Assert.Equal(SizeToContent.WidthAndHeight, target.SizeToContent);
  725. }
  726. }
  727. [Fact]
  728. public void Setting_Width_Should_Resize_WindowImpl()
  729. {
  730. // Issue #3796
  731. using (UnitTestApplication.Start(TestServices.StyledWindow))
  732. {
  733. var target = new Window()
  734. {
  735. Width = 400,
  736. Height = 800,
  737. };
  738. Show(target);
  739. Assert.Equal(400, target.Width);
  740. Assert.Equal(800, target.Height);
  741. target.Width = 410;
  742. target.LayoutManager.ExecuteLayoutPass();
  743. var windowImpl = Mock.Get(target.PlatformImpl);
  744. windowImpl.Verify(x => x.Resize(new Size(410, 800), PlatformResizeReason.Application));
  745. Assert.Equal(410, target.Width);
  746. }
  747. }
  748. [Fact]
  749. public void User_Resize_Of_Window_Width_Should_Reset_SizeToContent()
  750. {
  751. using (UnitTestApplication.Start(TestServices.StyledWindow))
  752. {
  753. var target = new Window()
  754. {
  755. SizeToContent = SizeToContent.WidthAndHeight,
  756. Content = new Canvas
  757. {
  758. Width = 400,
  759. Height = 800,
  760. },
  761. };
  762. Show(target);
  763. Assert.Equal(400, target.Width);
  764. Assert.Equal(800, target.Height);
  765. target.PlatformImpl.Resized(new Size(410, 800), PlatformResizeReason.User);
  766. Assert.Equal(410, target.Width);
  767. Assert.Equal(800, target.Height);
  768. Assert.Equal(SizeToContent.Height, target.SizeToContent);
  769. }
  770. }
  771. [Fact]
  772. public void User_Resize_Of_Window_Height_Should_Reset_SizeToContent()
  773. {
  774. using (UnitTestApplication.Start(TestServices.StyledWindow))
  775. {
  776. var target = new Window()
  777. {
  778. SizeToContent = SizeToContent.WidthAndHeight,
  779. Content = new Canvas
  780. {
  781. Width = 400,
  782. Height = 800,
  783. },
  784. };
  785. Show(target);
  786. Assert.Equal(400, target.Width);
  787. Assert.Equal(800, target.Height);
  788. target.PlatformImpl.Resized(new Size(400, 810), PlatformResizeReason.User);
  789. Assert.Equal(400, target.Width);
  790. Assert.Equal(810, target.Height);
  791. Assert.Equal(SizeToContent.Width, target.SizeToContent);
  792. }
  793. }
  794. [Fact]
  795. public void Window_Resize_Should_Not_Reset_SizeToContent_If_CanResize_False()
  796. {
  797. using (UnitTestApplication.Start(TestServices.StyledWindow))
  798. {
  799. var target = new Window()
  800. {
  801. SizeToContent = SizeToContent.WidthAndHeight,
  802. CanResize = false,
  803. Content = new Canvas
  804. {
  805. Width = 400,
  806. Height = 800,
  807. },
  808. };
  809. Show(target);
  810. Assert.Equal(400, target.Width);
  811. Assert.Equal(800, target.Height);
  812. target.PlatformImpl.Resized(new Size(410, 810), PlatformResizeReason.Unspecified);
  813. Assert.Equal(400, target.Width);
  814. Assert.Equal(800, target.Height);
  815. Assert.Equal(SizeToContent.WidthAndHeight, target.SizeToContent);
  816. }
  817. }
  818. [Fact]
  819. public void IsVisible_Should_Open_Window()
  820. {
  821. using (UnitTestApplication.Start(TestServices.StyledWindow))
  822. {
  823. var target = new Window();
  824. var raised = false;
  825. target.Opened += (s, e) => raised = true;
  826. target.IsVisible = true;
  827. Assert.True(raised);
  828. }
  829. }
  830. [Fact]
  831. public void IsVisible_Should_Close_DialogWindow()
  832. {
  833. using (UnitTestApplication.Start(TestServices.StyledWindow))
  834. {
  835. var parent = new Window();
  836. parent.Show();
  837. var target = new Window();
  838. var raised = false;
  839. var task = target.ShowDialog<bool>(parent);
  840. target.Closed += (sender, args) => raised = true;
  841. target.IsVisible = false;
  842. Assert.True(raised);
  843. Assert.False(task.Result);
  844. }
  845. }
  846. protected virtual void Show(Window window)
  847. {
  848. window.Show();
  849. }
  850. }
  851. public class DialogSizingTests : SizingTests
  852. {
  853. protected override void Show(Window window)
  854. {
  855. var owner = new Window();
  856. owner.Show();
  857. window.ShowDialog(owner);
  858. }
  859. }
  860. private static IWindowImpl CreateImpl(Mock<IRenderer> renderer)
  861. {
  862. return Mock.Of<IWindowImpl>(x =>
  863. x.RenderScaling == 1 &&
  864. x.CreateRenderer(It.IsAny<IRenderRoot>()) == renderer.Object);
  865. }
  866. private class ChildControl : Control
  867. {
  868. public List<Size> MeasureSizes { get; } = new List<Size>();
  869. protected override Size MeasureOverride(Size availableSize)
  870. {
  871. MeasureSizes.Add(availableSize);
  872. return base.MeasureOverride(availableSize);
  873. }
  874. }
  875. }
  876. }