From f829137f73b9c6f858449c44d315650ef1f53c06 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Fri, 5 Mar 2021 16:04:23 +0000 Subject: [PATCH] add unit tests for window close when os button is clicked --- .../WindowTests.cs | 109 +++++++++++++++++- 1 file changed, 107 insertions(+), 2 deletions(-) diff --git a/tests/Avalonia.Controls.UnitTests/WindowTests.cs b/tests/Avalonia.Controls.UnitTests/WindowTests.cs index 6d9b319a3e..4822c07fa1 100644 --- a/tests/Avalonia.Controls.UnitTests/WindowTests.cs +++ b/tests/Avalonia.Controls.UnitTests/WindowTests.cs @@ -138,7 +138,112 @@ namespace Avalonia.Controls.UnitTests } [Fact] - public void Child_windows_should_be_closed_before_parent() + public void Child_windows_should_be_closed_before_parent_OSCloseButton() + { + 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; + }; + + 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); + + var cancel = window.PlatformImpl.Closing(); + + Assert.Equal(false, cancel); + + Assert.Equal(2, windowClosing); + Assert.Equal(1, childClosing); + Assert.Equal(4, windowClosed); + Assert.Equal(3, childClosed); + } + } + + [Fact] + public void Child_windows_must_not_close_before_parent_has_chance_to_Cancel_OSCloseButton() + { + 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); + + var cancel = window.PlatformImpl.Closing(); + + Assert.Equal(true, cancel); + + Assert.Equal(2, windowClosing); + Assert.Equal(1, childClosing); + Assert.Equal(0, windowClosed); + Assert.Equal(0, childClosed); + } + } + + [Fact] + public void Child_windows_should_be_closed_before_parent_Programatically() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { @@ -188,7 +293,7 @@ namespace Avalonia.Controls.UnitTests } [Fact] - public void Child_windows_must_not_close_before_parent_has_chance_to_Cancel() + public void Child_windows_must_not_close_before_parent_has_chance_to_Cancel_Programatically() { using (UnitTestApplication.Start(TestServices.StyledWindow)) {