diff options
author | Lluis Sanchez <lluis@xamarin.com> | 2013-11-14 15:20:09 +0400 |
---|---|---|
committer | Lluis Sanchez <lluis@xamarin.com> | 2013-11-14 15:20:09 +0400 |
commit | 2cf479a5b8c8b9592e1ecff7ffc3c2a05cfed055 (patch) | |
tree | 7bb83332fd3c5dde01c14cfe993e3982a5a68053 /Testing | |
parent | 0b819f24b153bf3e562085faba1b8c6b6f6d7363 (diff) |
Window and dialog closing improvements
Add Closed event to Window. Properly handle closing of dialogs.
A dialog is now always "closed" when Respond is called. The CloseRequested
and Closed events are fired. A respond can be canceled by a CloseRequested
handler. Added unit tests to verify all this semantic.
Diffstat (limited to 'Testing')
-rw-r--r-- | Testing/GtkTestRunner.csproj | 1 | ||||
-rw-r--r-- | Testing/Tests/DialogTests.cs | 171 | ||||
-rw-r--r-- | Testing/Tests/WindowTests.cs | 33 |
3 files changed, 205 insertions, 0 deletions
diff --git a/Testing/GtkTestRunner.csproj b/Testing/GtkTestRunner.csproj index a7233e20..80707448 100644 --- a/Testing/GtkTestRunner.csproj +++ b/Testing/GtkTestRunner.csproj @@ -109,6 +109,7 @@ <Compile Include="Tests\InternalChildrenTests.cs" /> <Compile Include="Tests\LinkLabelTests.cs" /> <Compile Include="Tests\FormattedTextTests.cs" /> + <Compile Include="Tests\DialogTests.cs" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <ItemGroup> diff --git a/Testing/Tests/DialogTests.cs b/Testing/Tests/DialogTests.cs new file mode 100644 index 00000000..0d99603a --- /dev/null +++ b/Testing/Tests/DialogTests.cs @@ -0,0 +1,171 @@ +// +// Dialog.cs +// +// Author: +// Lluis Sanchez <lluis@xamarin.com> +// +// Copyright (c) 2013 Xamarin Inc +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using NUnit.Framework; + +namespace Xwt +{ + public class DialogTests: XwtTest + { + [Test] + public void TwoResponds () + { + // When Respond is called twice before leaving an event handler, the last call is the good one + + using (var win = new Dialog ()) { + Application.TimeoutInvoke (10, delegate { + win.Respond (Command.Ok); + win.Respond (Command.Apply); + return false; + }); + var cmd = win.Run (); + Assert.AreEqual (Command.Apply, cmd); + } + } + + [Test] + public void RespondFiresClose () + { + // The close event is always fired after running a dialog + + bool closed = false; + using (var win = new Dialog ()) { + win.Buttons.Add (new DialogButton (Command.Ok)); + win.Closed += (sender, e) => closed = true; + Application.TimeoutInvoke (10, delegate { + win.Respond (Command.Apply); + return false; + }); + var cmd = win.Run (); + Assert.AreEqual (Command.Apply, cmd); + Assert.IsTrue (closed, "Close event not fired"); + } + } + + [Test] + public void RespondCanBeCanceled () + { + // Is it possible to cancel a Response call + + using (var win = new Dialog ()) { + win.Buttons.Add (new DialogButton (Command.Ok)); + win.CloseRequested += HandleCloseRequested1; + Application.TimeoutInvoke (10, delegate { + win.Respond (Command.Ok); + Application.TimeoutInvoke (10, delegate { + win.CloseRequested -= HandleCloseRequested1; + win.Respond (Command.Apply); + return false; + }); + return false; + }); + var cmd = win.Run (); + Assert.AreEqual (Command.Apply, cmd, "Close not canceled"); + } + } + + void HandleCloseRequested1 (object sender, CloseRequestedEventArgs args) + { + args.AllowClose = false; + } + + [Test] + public void Close () + { + // The Close method can be used to stop running a dialog + + bool closing = false, closed = false; + using (var win = new Dialog ()) { + win.Buttons.Add (new DialogButton (Command.Ok)); + win.CloseRequested += delegate(object sender, CloseRequestedEventArgs args) { + Assert.IsTrue (args.AllowClose); + closing = true; + }; + win.Closed += (sender, e) => closed = true; + Application.TimeoutInvoke (10, delegate { + win.Close (); + return false; + }); + var cmd = win.Run (); + Assert.IsNull (cmd); + Assert.IsTrue (closing, "CloseRequested event not fired"); + Assert.IsTrue (closed, "Window not closed"); + } + } + + [Test] + public void RespondOnClosing () + { + // Respond can be used in a CloseRequest handler to provide a result for the Run method + + using (var win = new Dialog ()) { + win.Buttons.Add (new DialogButton (Command.Ok)); + win.CloseRequested += delegate(object sender, CloseRequestedEventArgs args) { + win.Respond (Command.Apply); + }; + Application.TimeoutInvoke (10, delegate { + win.Close (); + return false; + }); + var cmd = win.Run (); + Assert.AreEqual (Command.Apply, cmd); + } + } + + [Test] + public void RespondOnClosingWithCancel () + { + // If Respond if called on a CloseRequest, but the close is canceled, the respose call is ignored + + using (var win = new Dialog ()) { + bool closeCanceled = false; + win.Buttons.Add (new DialogButton (Command.Ok)); + win.CloseRequested += HandleCloseRequested; + Application.TimeoutInvoke (10, delegate { + win.Close (); + Application.TimeoutInvoke (10, delegate { + win.CloseRequested -= HandleCloseRequested; + closeCanceled = true; + win.Close (); + return false; + }); + return false; + }); + var cmd = win.Run (); + Assert.IsTrue (closeCanceled, "Close not canceled"); + Assert.IsNull (cmd); + } + } + + void HandleCloseRequested (object sender, CloseRequestedEventArgs args) + { + var dlg = (Dialog)sender; + dlg.Respond (Command.Apply); + args.AllowClose = false; + } + } +} + diff --git a/Testing/Tests/WindowTests.cs b/Testing/Tests/WindowTests.cs index e683b51a..0ce7182e 100644 --- a/Testing/Tests/WindowTests.cs +++ b/Testing/Tests/WindowTests.cs @@ -185,6 +185,39 @@ namespace Xwt Assert.AreEqual (200, test.ScreenBounds.Height); } } + + [Test] + public void Close () + { + using (var win = new Window ()) { + ShowWindow (win); + bool closing = false, closed = false; + win.CloseRequested += delegate(object sender, CloseRequestedEventArgs args) { + Assert.IsTrue (args.AllowClose); + closing = true; + }; + win.Closed += (sender, e) => closed = true; + win.Close (); + Assert.IsTrue (closing, "CloseRequested event not fired"); + Assert.IsTrue (closed, "Window not closed"); + } + } + + [Test] + public void CloseCancel () + { + bool closed = false; + using (var win = new Window ()) { + ShowWindow (win); + win.CloseRequested += delegate(object sender, CloseRequestedEventArgs args) { + args.AllowClose = false; + }; + win.Closed += (sender, e) => closed = true; + win.Close (); + Assert.IsFalse (closed, "Window should not be closed"); + } + Assert.IsFalse (closed, "Window should not be closed"); + } } public class SquareBox: Canvas |