Преглед изворни кода

Don't allow self as parent/owner window.

Steven Kirk пре 4 година
родитељ
комит
69852a56f5
2 измењених фајлова са 39 додато и 2 уклоњено
  1. 15 2
      src/Avalonia.Controls/Window.cs
  2. 24 0
      tests/Avalonia.Controls.UnitTests/WindowTests.cs

+ 15 - 2
src/Avalonia.Controls/Window.cs

@@ -635,9 +635,17 @@ namespace Avalonia.Controls
                 throw new InvalidOperationException("Cannot re-show a closed window.");
             }
 
-            if (parent != null && parent.PlatformImpl == null)
+            if (parent != null)
             {
-                throw new InvalidOperationException("Cannot Show a Window with a closed parent.");
+                if (parent.PlatformImpl == null)
+                {
+                    throw new InvalidOperationException("Cannot Show a Window with a closed parent.");
+                }
+
+                if (parent == this)
+                {
+                    throw new InvalidOperationException("A Window cannot be its own parent.");
+                }
             }
 
             if (IsVisible)
@@ -719,6 +727,11 @@ namespace Avalonia.Controls
                 throw new InvalidOperationException("Cannot Show a Window with a closed owner.");
             }
 
+            if (owner == this)
+            {
+                throw new InvalidOperationException("A Window cannot be its own owner.");
+            }
+
             if (IsVisible)
             {
                 throw new InvalidOperationException("The window is already being shown.");

+ 24 - 0
tests/Avalonia.Controls.UnitTests/WindowTests.cs

@@ -417,6 +417,30 @@ namespace Avalonia.Controls.UnitTests
             }
         }
 
+        [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 parent.", 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 Window_Should_Be_Centered_When_WindowStartupLocation_Is_CenterScreen()
         {