123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128 |
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using Avalonia.Media;
- using Avalonia.Platform;
- using Avalonia.Rendering;
- using Avalonia.Rendering.Composition;
- using Avalonia.UnitTests;
- using Moq;
- using Xunit;
- namespace Avalonia.Controls.UnitTests
- {
- public class WindowTests
- {
- [Fact]
- public void Setting_Title_Should_Set_Impl_Title()
- {
- var windowImpl = new Mock<IWindowImpl>();
- windowImpl.Setup(r => r.Compositor).Returns(RendererMocks.CreateDummyCompositor());
- var windowingPlatform = new MockWindowingPlatform(() => windowImpl.Object);
- using (UnitTestApplication.Start(new TestServices(windowingPlatform: windowingPlatform)))
- {
- var target = new Window();
- target.Title = "Hello World";
- windowImpl.Verify(x => x.SetTitle("Hello World"));
- }
- }
- [Fact]
- public void IsVisible_Should_Initially_Be_False()
- {
- using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
- {
- var window = new Window();
- Assert.False(window.IsVisible);
- }
- }
- [Fact]
- public void IsVisible_Should_Be_True_After_Show()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var window = new Window();
- window.Show();
- Assert.True(window.IsVisible);
- }
- }
- [Fact]
- public void IsVisible_Should_Be_True_After_ShowDialog()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var parent = new Window();
- parent.Show();
- var window = new Window();
- var task = window.ShowDialog(parent);
- Assert.True(window.IsVisible);
- }
- }
- [Fact]
- public void IsVisible_Should_Be_False_After_Hide()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var window = new Window();
- window.Show();
- window.Hide();
- Assert.False(window.IsVisible);
- }
- }
- [Fact]
- public void IsVisible_Should_Be_False_After_Close()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var window = new Window();
- window.Show();
- window.Close();
- Assert.False(window.IsVisible);
- }
- }
- [Fact]
- public void IsVisible_Should_Be_False_After_Impl_Signals_Close()
- {
- var windowImpl = new Mock<IWindowImpl>();
- windowImpl.Setup(r => r.Compositor).Returns(RendererMocks.CreateDummyCompositor());
- windowImpl.SetupProperty(x => x.Closed);
- windowImpl.Setup(x => x.DesktopScaling).Returns(1);
- windowImpl.Setup(x => x.RenderScaling).Returns(1);
- var services = TestServices.StyledWindow.With(
- windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object));
- using (UnitTestApplication.Start(services))
- {
- var window = new Window();
- window.Show();
- windowImpl.Object.Closed();
- Assert.False(window.IsVisible);
- }
- }
- [Fact]
- public void Closing_Should_Only_Be_Invoked_Once()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var window = new Window();
- var count = 0;
- window.Closing +=
- (sender, e) =>
- {
- count++;
- };
- window.Show();
- window.Close();
- Assert.Equal(1, count);
- }
- }
-
- [Theory]
- [InlineData(true)]
- [InlineData(false)]
- public void Child_windows_should_be_closed_before_parent(bool programmaticClose)
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var window = new Window();
- var child = new Window();
- int count = 0;
- int windowClosing = 0;
- int childClosing = 0;
- int windowClosed = 0;
- int childClosed = 0;
- window.Closing += (sender, e) =>
- {
- Assert.Equal(WindowCloseReason.WindowClosing, e.CloseReason);
- Assert.Equal(programmaticClose, e.IsProgrammatic);
- count++;
- windowClosing = count;
- };
-
- child.Closing += (sender, e) =>
- {
- Assert.Equal(WindowCloseReason.OwnerWindowClosing, e.CloseReason);
- Assert.Equal(programmaticClose, e.IsProgrammatic);
- count++;
- childClosing = count;
- };
-
- window.Closed += (sender, e) =>
- {
- count++;
- windowClosed = count;
- };
-
- child.Closed += (sender, e) =>
- {
- count++;
- childClosed = count;
- };
- window.Show();
- child.Show(window);
- if (programmaticClose)
- {
- window.Close();
- }
- else
- {
- var cancel = window.PlatformImpl.Closing(WindowCloseReason.WindowClosing);
- Assert.Equal(false, cancel);
- }
- Assert.Equal(2, windowClosing);
- Assert.Equal(1, childClosing);
- Assert.Equal(4, windowClosed);
- Assert.Equal(3, childClosed);
- }
- }
-
- [Theory]
- [InlineData(true)]
- [InlineData(false)]
- public void Child_windows_must_not_close_before_parent_has_chance_to_Cancel_OSCloseButton(bool programmaticClose)
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var window = new Window();
- var child = new Window();
- int count = 0;
- int windowClosing = 0;
- int childClosing = 0;
- int windowClosed = 0;
- int childClosed = 0;
- window.Closing += (sender, e) =>
- {
- count++;
- windowClosing = count;
- e.Cancel = true;
- };
-
- child.Closing += (sender, e) =>
- {
- count++;
- childClosing = count;
- };
-
- window.Closed += (sender, e) =>
- {
- count++;
- windowClosed = count;
- };
-
- child.Closed += (sender, e) =>
- {
- count++;
- childClosed = count;
- };
- window.Show();
- child.Show(window);
-
- if (programmaticClose)
- {
- window.Close();
- }
- else
- {
- var cancel = window.PlatformImpl.Closing(WindowCloseReason.WindowClosing);
- Assert.Equal(true, cancel);
- }
- Assert.Equal(2, windowClosing);
- Assert.Equal(1, childClosing);
- Assert.Equal(0, windowClosed);
- Assert.Equal(0, childClosed);
- }
- }
- [Fact]
- public void Showing_Should_Start_Renderer()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var target = new Window(CreateImpl());
- target.Show();
- Assert.True(MediaContext.Instance.IsTopLevelActive(target));
- }
- }
- [Fact]
- public void ShowDialog_Should_Start_Renderer()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var parent = new Window();
- var target = new Window(CreateImpl());
- parent.Show();
- target.ShowDialog<object>(parent);
- Assert.True(MediaContext.Instance.IsTopLevelActive(target));
- }
- }
- [Fact]
- public void ShowDialog_Should_Raise_Opened()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var parent = new Window();
- var target = new Window();
- var raised = false;
- parent.Show();
- target.Opened += (s, e) => raised = true;
- target.ShowDialog<object>(parent);
- Assert.True(raised);
- }
- }
- [Fact]
- public void Hiding_Should_Stop_Renderer()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var target = new Window(CreateImpl());
- target.Show();
- target.Hide();
- Assert.False(MediaContext.Instance.IsTopLevelActive(target));
- }
- }
- [Fact]
- public async Task ShowDialog_With_ValueType_Returns_Default_When_Closed()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var parent = new Window();
- var windowImpl = new Mock<IWindowImpl>();
- windowImpl.Setup(x => x.Compositor).Returns(RendererMocks.CreateDummyCompositor());
- windowImpl.SetupProperty(x => x.Closed);
- windowImpl.Setup(x => x.DesktopScaling).Returns(1);
- windowImpl.Setup(x => x.RenderScaling).Returns(1);
- parent.Show();
- var target = new Window(windowImpl.Object);
- var task = target.ShowDialog<bool>(parent);
- windowImpl.Object.Closed();
- var result = await task;
- Assert.False(result);
- }
- }
- [Fact]
- public void Calling_Show_On_Closed_Window_Should_Throw()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var target = new Window();
- target.Show();
- target.Close();
- var openedRaised = false;
- target.Opened += (s, e) => openedRaised = true;
- var ex = Assert.Throws<InvalidOperationException>(() => target.Show());
- Assert.Equal("Cannot re-show a closed window.", ex.Message);
- Assert.False(openedRaised);
- }
- }
- [Fact]
- public async Task Calling_ShowDialog_On_Closed_Window_Should_Throw()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var parent = new Window();
- var windowImpl = new Mock<IWindowImpl>();
- windowImpl.Setup(x => x.Compositor).Returns(RendererMocks.CreateDummyCompositor());
- windowImpl.SetupProperty(x => x.Closed);
- windowImpl.Setup(x => x.DesktopScaling).Returns(1);
- windowImpl.Setup(x => x.RenderScaling).Returns(1);
- parent.Show();
- var target = new Window(windowImpl.Object);
- var task = target.ShowDialog<bool>(parent);
- windowImpl.Object.Closed();
- await task;
- var openedRaised = false;
- target.Opened += (s, e) => openedRaised = true;
- var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => target.ShowDialog<bool>(parent));
- Assert.Equal("Cannot re-show a closed window.", ex.Message);
- Assert.False(openedRaised);
- }
- }
- [Fact]
- public void Calling_Show_With_Closed_Parent_Window_Should_Throw()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var parent = new Window();
- var target = new Window();
- parent.Close();
- var ex = Assert.Throws<InvalidOperationException>(() => target.Show(parent));
- Assert.Equal("Cannot show a window with a closed owner.", ex.Message);
- }
- }
- [Fact]
- public async Task Calling_ShowDialog_With_Closed_Parent_Window_Should_Throw()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var parent = new Window();
- var target = new Window();
- parent.Close();
- var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => target.ShowDialog(parent));
- Assert.Equal("Cannot show a window with a closed owner.", ex.Message);
- }
- }
- [Fact]
- public void Calling_Show_With_Invisible_Parent_Window_Should_Throw()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var parent = new Window();
- var target = new Window();
- var ex = Assert.Throws<InvalidOperationException>(() => target.Show(parent));
- Assert.Equal("Cannot show window with non-visible owner.", ex.Message);
- }
- }
- [Fact]
- public async Task Calling_ShowDialog_With_Invisible_Parent_Window_Should_Throw()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var parent = new Window();
- var target = new Window();
- var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => target.ShowDialog(parent));
- Assert.Equal("Cannot show window with non-visible owner.", ex.Message);
- }
- }
- [Fact]
- public void Calling_Show_With_Self_As_Parent_Window_Should_Throw()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var target = new Window();
- var ex = Assert.Throws<InvalidOperationException>(() => target.Show(target));
- Assert.Equal("A Window cannot be its own owner.", ex.Message);
- }
- }
- [Fact]
- public async Task Calling_ShowDialog_With_Self_As_Parent_Window_Should_Throw()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var target = new Window();
- var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => target.ShowDialog(target));
- Assert.Equal("A Window cannot be its own owner.", ex.Message);
- }
- }
- [Fact]
- public void Hiding_Parent_Window_Should_Close_Children()
- {
- using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
- {
- var parent = new Window();
- var child = new Window();
- parent.Show();
- child.Show(parent);
- parent.Hide();
- Assert.False(parent.IsVisible);
- Assert.False(child.IsVisible);
- }
- }
- [Fact]
- public void Hiding_Parent_Window_Should_Close_Dialog_Children()
- {
- using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
- {
- var parent = new Window();
- var child = new Window();
- parent.Show();
- child.ShowDialog(parent);
- parent.Hide();
- Assert.False(parent.IsVisible);
- Assert.False(child.IsVisible);
- }
- }
- [Fact]
- public void Window_Should_Not_Be_Centered_When_WindowStartupLocation_Is_CenterScreen_And_Window_Is_Hidden_And_Shown()
- {
- var screen1 = new Mock<Screen>(1.0, new PixelRect(new PixelSize(1920, 1080)), new PixelRect(new PixelSize(1920, 1040)), true);
- var screens = new Mock<IScreenImpl>();
- screens.Setup(x => x.AllScreens).Returns(new Screen[] { screen1.Object });
- screens.Setup(x => x.ScreenFromPoint(It.IsAny<PixelPoint>())).Returns(screen1.Object);
- var windowImpl = MockWindowingPlatform.CreateWindowMock();
- windowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
- windowImpl.Setup(x => x.DesktopScaling).Returns(1);
- windowImpl.Setup(x => x.RenderScaling).Returns(1);
- windowImpl.Setup(x => x.TryGetFeature(It.Is<Type>(t => t == typeof(IScreenImpl)))).Returns(screens.Object);
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var window = new Window(windowImpl.Object)
- {
- WindowStartupLocation = WindowStartupLocation.CenterScreen
- };
- window.Show();
- var expected = new PixelPoint(150, 400);
- window.Position = expected;
- window.IsVisible = false;
- window.IsVisible = true;
- Assert.Equal(expected, window.Position);
- }
- }
- [Fact]
- public void Window_Should_Be_Centered_When_WindowStartupLocation_Is_CenterScreen()
- {
- var screen1 = new Mock<Screen>(1.0, new PixelRect(new PixelSize(1920, 1080)), new PixelRect(new PixelSize(1920, 1040)), true);
- var screen2 = new Mock<Screen>(1.0, new PixelRect(new PixelSize(1366, 768)), new PixelRect(new PixelSize(1366, 728)), false);
- var screens = new Mock<IScreenImpl>();
- screens.Setup(x => x.AllScreens).Returns(new Screen[] { screen1.Object, screen2.Object });
- screens.Setup(x => x.ScreenFromPoint(It.IsAny<PixelPoint>())).Returns(screen1.Object);
-
- var windowImpl = MockWindowingPlatform.CreateWindowMock();
- windowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
- windowImpl.Setup(x => x.DesktopScaling).Returns(1);
- windowImpl.Setup(x => x.RenderScaling).Returns(1);
- windowImpl.Setup(x => x.TryGetFeature(It.Is<Type>(t => t == typeof(IScreenImpl)))).Returns(screens.Object);
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var window = new Window(windowImpl.Object);
- window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
- window.Position = new PixelPoint(60, 40);
- window.Show();
- var expectedPosition = new PixelPoint(
- (int)(screen1.Object.WorkingArea.Size.Width / 2 - window.ClientSize.Width / 2),
- (int)(screen1.Object.WorkingArea.Size.Height / 2 - window.ClientSize.Height / 2));
- Assert.Equal(window.Position, expectedPosition);
- }
- }
-
- [Fact]
- public void Window_Should_Be_Sized_To_MinSize_If_InitialSize_Less_Than_MinSize()
- {
- var screen1 = new Mock<Screen>(1.75, new PixelRect(new PixelSize(1920, 1080)), new PixelRect(new PixelSize(1920, 966)), true);
- var screens = new Mock<IScreenImpl>();
- screens.Setup(x => x.AllScreens).Returns(new Screen[] { screen1.Object });
- screens.Setup(x => x.ScreenFromPoint(It.IsAny<PixelPoint>())).Returns(screen1.Object);
-
- var windowImpl = MockWindowingPlatform.CreateWindowMock(400, 300);
- windowImpl.Setup(x => x.DesktopScaling).Returns(1.75);
- windowImpl.Setup(x => x.RenderScaling).Returns(1.75);
- windowImpl.Setup(x => x.TryGetFeature(It.Is<Type>(t => t == typeof(IScreenImpl)))).Returns(screens.Object);
-
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var window = new Window(windowImpl.Object);
- window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
- window.MinWidth = 720;
- window.MinHeight = 480;
- window.Show();
-
- Assert.Equal(new PixelPoint(330, 63), window.Position);
- Assert.Equal(new Size(720, 480), window.Bounds.Size);
- }
- }
- [Fact]
- public void Window_Should_Be_Centered_Relative_To_Owner_When_WindowStartupLocation_Is_CenterOwner()
- {
- var parentWindowImpl = MockWindowingPlatform.CreateWindowMock();
- parentWindowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
- parentWindowImpl.Setup(x => x.MaxAutoSizeHint).Returns(new Size(1920, 1080));
- parentWindowImpl.Setup(x => x.DesktopScaling).Returns(1);
- parentWindowImpl.Setup(x => x.RenderScaling).Returns(1);
- var windowImpl = MockWindowingPlatform.CreateWindowMock();
- windowImpl.Setup(x => x.ClientSize).Returns(new Size(320, 200));
- windowImpl.Setup(x => x.MaxAutoSizeHint).Returns(new Size(1920, 1080));
- windowImpl.Setup(x => x.DesktopScaling).Returns(1);
- windowImpl.Setup(x => x.RenderScaling).Returns(1);
- var parentWindowServices = TestServices.StyledWindow.With(
- windowingPlatform: new MockWindowingPlatform(() => parentWindowImpl.Object));
- var windowServices = TestServices.StyledWindow.With(
- windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object));
- using (UnitTestApplication.Start(parentWindowServices))
- {
- var parentWindow = new Window();
- parentWindow.Position = new PixelPoint(60, 40);
- parentWindow.Show();
- using (UnitTestApplication.Start(windowServices))
- {
- var window = new Window();
- window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
- window.Position = new PixelPoint(60, 40);
- window.ShowDialog(parentWindow);
- var expectedPosition = new PixelPoint(
- (int)(parentWindow.Position.X + parentWindow.ClientSize.Width / 2 - window.ClientSize.Width / 2),
- (int)(parentWindow.Position.Y + parentWindow.ClientSize.Height / 2 - window.ClientSize.Height / 2));
- Assert.Equal(window.Position, expectedPosition);
- }
- }
- }
- [Fact]
- public void Window_Topmost_By_Default_Should_Configure_PlatformImpl_When_Constructed()
- {
- var windowImpl = MockWindowingPlatform.CreateWindowMock();
- var windowServices = TestServices.StyledWindow.With(
- windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object));
- using (UnitTestApplication.Start(windowServices))
- {
- var window = new TopmostWindow();
- Assert.True(window.Topmost);
- windowImpl.Verify(i => i.SetTopmost(true));
- }
- }
- public class SizingTests
- {
- [Fact]
- public void Child_Should_Be_Measured_With_Width_And_Height_If_SizeToContent_Is_Manual()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var child = new ChildControl();
- var target = new Window
- {
- Width = 100,
- Height = 50,
- SizeToContent = SizeToContent.Manual,
- Content = child
- };
- Show(target);
- Assert.Equal(1, child.MeasureSizes.Count);
- Assert.Equal(new Size(100, 50), child.MeasureSizes[0]);
- }
- }
- [Fact]
- public void Child_Should_Be_Measured_With_ClientSize_If_SizeToContent_Is_Manual_And_No_Width_Height_Specified()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var windowImpl = MockWindowingPlatform.CreateWindowMock();
- windowImpl.Setup(x => x.ClientSize).Returns(new Size(550, 450));
- var child = new ChildControl();
- var target = new Window(windowImpl.Object)
- {
- SizeToContent = SizeToContent.Manual,
- Content = child
- };
- Show(target);
- Assert.Equal(1, child.MeasureSizes.Count);
- Assert.Equal(new Size(550, 450), child.MeasureSizes[0]);
- }
- }
- [Fact]
- public void Child_Should_Be_Measured_With_MaxAutoSizeHint_If_SizeToContent_Is_WidthAndHeight()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var windowImpl = MockWindowingPlatform.CreateWindowMock();
- windowImpl.Setup(x => x.MaxAutoSizeHint).Returns(new Size(1200, 1000));
- var child = new ChildControl();
- var target = new Window(windowImpl.Object)
- {
- Width = 100,
- Height = 50,
- SizeToContent = SizeToContent.WidthAndHeight,
- Content = child
- };
- target.Show();
- Assert.Equal(1, child.MeasureSizes.Count);
- Assert.Equal(new Size(1200, 1000), child.MeasureSizes[0]);
- }
- }
- [Fact]
- public void Should_Not_Have_Offset_On_Bounds_When_Content_Larger_Than_Max_Window_Size()
- {
- // Issue #3784.
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var windowImpl = MockWindowingPlatform.CreateWindowMock();
- var clientSize = new Size(200, 200);
- var maxClientSize = new Size(480, 480);
- windowImpl.Setup(x => x.Resize(It.IsAny<Size>(), It.IsAny<WindowResizeReason>()))
- .Callback<Size, WindowResizeReason>((size, reason) =>
- {
- clientSize = size.Constrain(maxClientSize);
- windowImpl.Object.Resized?.Invoke(clientSize, reason);
- });
- windowImpl.Setup(x => x.ClientSize).Returns(() => clientSize);
- var child = new Canvas
- {
- Width = 400,
- Height = 800,
- };
- var target = new Window(windowImpl.Object)
- {
- SizeToContent = SizeToContent.WidthAndHeight,
- Content = child
- };
- Show(target);
- Assert.Equal(new Size(400, 480), target.Bounds.Size);
- // Issue #3784 causes this to be (0, 160) which makes no sense as Window has no
- // parent control to be offset against.
- Assert.Equal(new Point(0, 0), target.Bounds.Position);
- }
- }
- [Fact]
- public void Width_Height_Should_Not_Be_NaN_After_Show_With_SizeToContent_Manual()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var child = new Canvas
- {
- Width = 400,
- Height = 800,
- };
- var target = new Window()
- {
- SizeToContent = SizeToContent.Manual,
- Content = child
- };
- Show(target);
- // Values come from MockWindowingPlatform defaults.
- Assert.Equal(800, target.Width);
- Assert.Equal(600, target.Height);
- }
- }
- [Fact]
- public void Width_Height_Should_Not_Be_NaN_After_Show_With_SizeToContent_WidthAndHeight()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var child = new Canvas
- {
- Width = 400,
- Height = 800,
- };
- var target = new Window()
- {
- SizeToContent = SizeToContent.WidthAndHeight,
- Content = child
- };
- target.GetObservable(Window.WidthProperty).Subscribe(x => { });
- Show(target);
- Assert.Equal(400, target.Width);
- Assert.Equal(800, target.Height);
- }
- }
- [Fact]
- public void MaxWidth_And_MaxHeight_Should_Be_Respected_With_SizeToContent_WidthAndHeight()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var child = new ChildControl();
- var target = new Window()
- {
- SizeToContent = SizeToContent.WidthAndHeight,
- MaxWidth = 300,
- MaxHeight = 700,
- Content = child,
- };
- Show(target);
- Assert.Equal(new[] { new Size(300, 700) }, child.MeasureSizes);
- }
- }
- [Fact]
- public void SizeToContent_Should_Not_Be_Lost_On_Show()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var child = new Canvas
- {
- Width = 400,
- Height = 800,
- };
- var target = new Window()
- {
- SizeToContent = SizeToContent.WidthAndHeight,
- Content = child
- };
- Show(target);
- Assert.Equal(SizeToContent.WidthAndHeight, target.SizeToContent);
- }
- }
- [Fact]
- public void SizeToContent_Should_Not_Be_Lost_On_Scaling_Change()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var child = new Canvas
- {
- Width = 209,
- Height = 117,
- };
- var target = new Window()
- {
- SizeToContent = SizeToContent.WidthAndHeight,
- Content = child
- };
- Show(target);
- // Size before and after DPI change is a real-world example, with size after DPI
- // change coming from Win32 WM_DPICHANGED.
- target.PlatformImpl.ScalingChanged(1.5);
- target.PlatformImpl.Resized(
- new Size(210.66666666666666, 118.66666666666667),
- WindowResizeReason.DpiChange);
- Assert.Equal(SizeToContent.WidthAndHeight, target.SizeToContent);
- }
- }
- [Fact]
- public void Width_Height_Should_Be_Updated_When_SizeToContent_Is_WidthAndHeight()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var child = new Canvas
- {
- Width = 400,
- Height = 800,
- };
- var target = new Window()
- {
- SizeToContent = SizeToContent.WidthAndHeight,
- Content = child
- };
- Show(target);
- Assert.Equal(400, target.Width);
- Assert.Equal(800, target.Height);
- child.Width = 410;
- target.LayoutManager.ExecuteLayoutPass();
- Assert.Equal(410, target.Width);
- Assert.Equal(800, target.Height);
- Assert.Equal(SizeToContent.WidthAndHeight, target.SizeToContent);
- }
- }
- [Fact]
- public void Setting_Width_Should_Resize_WindowImpl()
- {
- // Issue #3796
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var target = new Window()
- {
- Width = 400,
- Height = 800,
- };
- Show(target);
- Assert.Equal(400, target.Width);
- Assert.Equal(800, target.Height);
- target.Width = 410;
- target.LayoutManager.ExecuteLayoutPass();
- var windowImpl = Mock.Get(target.PlatformImpl);
- windowImpl.Verify(x => x.Resize(new Size(410, 800), WindowResizeReason.Application));
- Assert.Equal(410, target.Width);
- }
- }
- [Fact]
- public void User_Resize_Of_Window_Width_Should_Reset_SizeToContent()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var target = new Window()
- {
- SizeToContent = SizeToContent.WidthAndHeight,
- Content = new Canvas
- {
- Width = 400,
- Height = 800,
- },
- };
- Show(target);
- Assert.Equal(400, target.Width);
- Assert.Equal(800, target.Height);
- target.PlatformImpl.Resized(new Size(410, 800), WindowResizeReason.User);
- Assert.Equal(410, target.Width);
- Assert.Equal(800, target.Height);
- Assert.Equal(SizeToContent.Height, target.SizeToContent);
- }
- }
- [Fact]
- public void User_Resize_Of_Window_Height_Should_Reset_SizeToContent()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var target = new Window()
- {
- SizeToContent = SizeToContent.WidthAndHeight,
- Content = new Canvas
- {
- Width = 400,
- Height = 800,
- },
- };
- Show(target);
- Assert.Equal(400, target.Width);
- Assert.Equal(800, target.Height);
- target.PlatformImpl.Resized(new Size(400, 810), WindowResizeReason.User);
- Assert.Equal(400, target.Width);
- Assert.Equal(810, target.Height);
- Assert.Equal(SizeToContent.Width, target.SizeToContent);
- }
- }
- [Fact]
- public void Window_Resize_Should_Not_Reset_SizeToContent_If_CanResize_False()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var target = new Window()
- {
- SizeToContent = SizeToContent.WidthAndHeight,
- CanResize = false,
- Content = new Canvas
- {
- Width = 400,
- Height = 800,
- },
- };
- Show(target);
- Assert.Equal(400, target.Width);
- Assert.Equal(800, target.Height);
- target.PlatformImpl.Resized(new Size(410, 810), WindowResizeReason.Unspecified);
- Assert.Equal(400, target.Width);
- Assert.Equal(800, target.Height);
- Assert.Equal(SizeToContent.WidthAndHeight, target.SizeToContent);
- }
- }
-
- [Fact]
- public void IsVisible_Should_Open_Window()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var target = new Window();
- var raised = false;
-
- target.Opened += (s, e) => raised = true;
- target.IsVisible = true;
- Assert.True(raised);
- }
- }
-
- [Fact]
- public void IsVisible_Should_Close_DialogWindow()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var parent = new Window();
- parent.Show();
-
- var target = new Window();
-
- var raised = false;
- var task = target.ShowDialog<bool>(parent);
-
- target.Closed += (sender, args) => raised = true;
- target.IsVisible = false;
- Assert.True(raised);
-
- Assert.False(task.Result);
- }
- }
-
- protected virtual void Show(Window window)
- {
- window.Show();
- }
- }
- public class DialogSizingTests : SizingTests
- {
- protected override void Show(Window window)
- {
- var owner = new Window();
- owner.Show();
- window.ShowDialog(owner);
- }
- }
- private static IWindowImpl CreateImpl()
- {
- var compositor = RendererMocks.CreateDummyCompositor();
- return Mock.Of<IWindowImpl>(x => x.RenderScaling == 1 &&
- x.Compositor == compositor);
- }
- private class ChildControl : Control
- {
- public List<Size> MeasureSizes { get; } = new List<Size>();
- protected override Size MeasureOverride(Size availableSize)
- {
- MeasureSizes.Add(availableSize);
- return base.MeasureOverride(availableSize);
- }
- }
- private class TopmostWindow : Window
- {
- static TopmostWindow()
- {
- TopmostProperty.OverrideDefaultValue<TopmostWindow>(true);
- }
- }
- }
- }
|