| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- // -----------------------------------------------------------------------
- // <copyright file="WindowTests.cs" company="Steven Kirk">
- // Copyright 2015 MIT Licence. See licence.md for more information.
- // </copyright>
- // -----------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using Avalonia.Platform;
- using Avalonia.Rendering;
- 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>();
- 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 window = new Window();
- var task = window.ShowDialog();
- 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.SetupProperty(x => x.Closed);
- windowImpl.Setup(x => x.Scaling).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 Show_Should_Add_Window_To_OpenWindows()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- ClearOpenWindows();
- var window = new Window();
- window.Show();
- Assert.Equal(new[] { window }, Application.Current.Windows);
- }
- }
- [Fact]
- public void Window_Should_Be_Added_To_OpenWindows_Only_Once()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- ClearOpenWindows();
- var window = new Window();
- window.Show();
- window.Show();
- window.IsVisible = true;
- Assert.Equal(new[] { window }, Application.Current.Windows);
- window.Close();
- }
- }
- [Fact]
- public void Close_Should_Remove_Window_From_OpenWindows()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- ClearOpenWindows();
- var window = new Window();
- window.Show();
- window.Close();
- Assert.Empty(Application.Current.Windows);
- }
- }
- [Fact]
- public void Impl_Closing_Should_Remove_Window_From_OpenWindows()
- {
- var windowImpl = new Mock<IWindowImpl>();
- windowImpl.SetupProperty(x => x.Closed);
- windowImpl.Setup(x => x.Scaling).Returns(1);
- var services = TestServices.StyledWindow.With(
- windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object));
- using (UnitTestApplication.Start(services))
- {
- ClearOpenWindows();
- var window = new Window();
- window.Show();
- windowImpl.Object.Closed();
- Assert.Empty(Application.Current.Windows);
- }
- }
- [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);
- }
- }
- [Fact]
- public void Showing_Should_Start_Renderer()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var renderer = new Mock<IRenderer>();
- var target = new Window(CreateImpl(renderer));
- target.Show();
- renderer.Verify(x => x.Start(), Times.Once);
- }
- }
- [Fact]
- public void ShowDialog_Should_Start_Renderer()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var renderer = new Mock<IRenderer>();
- var target = new Window(CreateImpl(renderer));
- target.Show();
- renderer.Verify(x => x.Start(), Times.Once);
- }
- }
- [Fact]
- public void Hiding_Should_Stop_Renderer()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var renderer = new Mock<IRenderer>();
- var target = new Window(CreateImpl(renderer));
- target.Show();
- target.Hide();
- renderer.Verify(x => x.Stop(), Times.Once);
- }
- }
- [Fact]
- public async Task ShowDialog_With_ValueType_Returns_Default_When_Closed()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var windowImpl = new Mock<IWindowImpl>();
- windowImpl.SetupProperty(x => x.Closed);
- windowImpl.Setup(x => x.Scaling).Returns(1);
- var target = new Window(windowImpl.Object);
- var task = target.ShowDialog<bool>();
- windowImpl.Object.Closed();
- var result = await task;
- Assert.False(result);
- }
- }
- [Fact]
- public void Window_Should_Be_Centered_When_WindowStartupLocation_Is_CenterScreen()
- {
- var screen1 = new Mock<Screen>(new Rect(new Size(1920, 1080)), new Rect(new Size(1920, 1040)), true);
- var screen2 = new Mock<Screen>(new Rect(new Size(1366, 768)), new Rect(new Size(1366, 728)), false);
- var screens = new Mock<IScreenImpl>();
- screens.Setup(x => x.AllScreens).Returns(new Screen[] { screen1.Object, screen2.Object });
- var windowImpl = new Mock<IWindowImpl>();
- windowImpl.SetupProperty(x => x.Position);
- windowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
- windowImpl.Setup(x => x.Scaling).Returns(1);
- windowImpl.Setup(x => x.Screen).Returns(screens.Object);
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var window = new Window(windowImpl.Object);
- window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
- window.Position = new Point(60, 40);
- window.Show();
- var expectedPosition = new Point(
- screen1.Object.WorkingArea.Size.Width / 2 - window.ClientSize.Width / 2,
- screen1.Object.WorkingArea.Size.Height / 2 - window.ClientSize.Height / 2);
- Assert.Equal(window.Position, expectedPosition);
- }
- }
- [Fact]
- public void Window_Should_Be_Centered_Relative_To_Owner_When_WindowStartupLocation_Is_CenterOwner()
- {
- var parentWindowImpl = new Mock<IWindowImpl>();
- parentWindowImpl.SetupProperty(x => x.Position);
- parentWindowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
- parentWindowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1920, 1080));
- parentWindowImpl.Setup(x => x.Scaling).Returns(1);
- var windowImpl = new Mock<IWindowImpl>();
- windowImpl.SetupProperty(x => x.Position);
- windowImpl.Setup(x => x.ClientSize).Returns(new Size(320, 200));
- windowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1920, 1080));
- windowImpl.Setup(x => x.Scaling).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 Point(60, 40);
- parentWindow.Show();
- using (UnitTestApplication.Start(windowServices))
- {
- var window = new Window();
- window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
- window.Position = new Point(60, 40);
- window.Owner = parentWindow;
- window.Show();
- var expectedPosition = new Point(
- parentWindow.Position.X + parentWindow.ClientSize.Width / 2 - window.ClientSize.Width / 2,
- parentWindow.Position.Y + parentWindow.ClientSize.Height / 2 - window.ClientSize.Height / 2);
- Assert.Equal(window.Position, expectedPosition);
- }
- }
- }
- private IWindowImpl CreateImpl(Mock<IRenderer> renderer)
- {
- return Mock.Of<IWindowImpl>(x =>
- x.Scaling == 1 &&
- x.CreateRenderer(It.IsAny<IRenderRoot>()) == renderer.Object);
- }
- private void ClearOpenWindows()
- {
- // HACK: We really need a decent way to have "statics" that can be scoped to
- // AvaloniaLocator scopes.
- Application.Current.Windows.Clear();
- }
- }
- }
|