WindowTests.cs 33 KB

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