Browse Source

Don't allow using a closed window as a parent/owner.

pull/6198/head
Steven Kirk 5 years ago
parent
commit
ea3f85e126
  1. 10
      src/Avalonia.Controls/Window.cs
  2. 30
      tests/Avalonia.Controls.UnitTests/WindowTests.cs

10
src/Avalonia.Controls/Window.cs

@ -635,6 +635,11 @@ namespace Avalonia.Controls
throw new InvalidOperationException("Cannot re-show a closed window."); throw new InvalidOperationException("Cannot re-show a closed window.");
} }
if (parent != null && parent.PlatformImpl == null)
{
throw new InvalidOperationException("Cannot Show a Window with a closed parent.");
}
if (IsVisible) if (IsVisible)
{ {
return; return;
@ -709,6 +714,11 @@ namespace Avalonia.Controls
throw new ArgumentNullException(nameof(owner)); throw new ArgumentNullException(nameof(owner));
} }
if (owner.PlatformImpl == null)
{
throw new InvalidOperationException("Cannot Show a Window with a closed owner.");
}
if (IsVisible) if (IsVisible)
{ {
throw new InvalidOperationException("The window is already being shown."); throw new InvalidOperationException("The window is already being shown.");

30
tests/Avalonia.Controls.UnitTests/WindowTests.cs

@ -387,6 +387,36 @@ namespace Avalonia.Controls.UnitTests
} }
} }
[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 parent.", 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] [Fact]
public void Window_Should_Be_Centered_When_WindowStartupLocation_Is_CenterScreen() public void Window_Should_Be_Centered_When_WindowStartupLocation_Is_CenterScreen()
{ {

Loading…
Cancel
Save