WindowTests.cs 36 KB

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