Added some 'dialog view models' that fire events when a dialog is to be closed (this allows ok and cancel buttons to be normally bound, allowing the view model to be unit tested and to run code upon receiving an ok click, the event then signaling to the dialog that it should close with the specified result)

git-svn-id: file:///srv/devel/repo-conversion/nuwi@42 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2019-02-06 07:30:41 +00:00
parent 4ef5dd9430
commit e728a182c5
5 changed files with 440 additions and 0 deletions

View File

@ -55,6 +55,14 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Source\ViewModels\DialogViewModel.cs" />
<Compile Include="Source\ViewModels\DialogViewModel.Test.cs">
<DependentUpon>DialogViewModel.cs</DependentUpon>
</Compile>
<Compile Include="Source\ViewModels\ThreadedDialogViewModel.cs" />
<Compile Include="Source\ViewModels\ThreadedDialogViewModel.Test.cs">
<DependentUpon>ThreadedDialogViewModel.cs</DependentUpon>
</Compile>
<Compile Include="Source\ViewModels\ThreadedViewModel.cs" /> <Compile Include="Source\ViewModels\ThreadedViewModel.cs" />
<Compile Include="Source\ViewModels\ThreadedViewModel.Test.cs"> <Compile Include="Source\ViewModels\ThreadedViewModel.Test.cs">
<DependentUpon>ThreadedViewModel.cs</DependentUpon> <DependentUpon>ThreadedViewModel.cs</DependentUpon>

View File

@ -0,0 +1,132 @@
using System;
using NUnit.Framework;
namespace Nuclex.Windows.Forms.ViewModels {
/// <summary>Unit test for the dialog view model</summary>
[TestFixture]
public class DialogViewModelTest {
#region class DialogViewModelSubscriber
/// <summary>Subscriber for the events offered by a dialog view model</summary>
private class DialogViewModelSubscriber {
/// <summary>Indicates that the user has accepted the dialog</summary>
public void Confirmed(object sender, EventArgs arguments) {
++this.confirmCallCount;
}
/// <summary>Indicates that the user has cancelled the dialog</summary>
public void Cancelled(object sender, EventArgs arguments) {
++this.cancelCallCount;
}
/// <summary>Indicates that the dialog was simply closed</summary>
public void Submitted(object sender, EventArgs arguments) {
++this.submitCallCount;
}
/// <summary>How many times the Confirmed() method was called</summary>
public int ConfirmCallCount {
get { return this.confirmCallCount; }
}
/// <summary>How many times the Cancelled() method was called</summary>
public int CancelCallCount {
get { return this.cancelCallCount; }
}
/// <summary>How many times the Submitted() method was called</summary>
public int SubmitCallCount {
get { return this.submitCallCount; }
}
/// <summary>How many times the Confirmed() method was called</summary>
private int confirmCallCount;
/// <summary>How many times the Cancelled() method was called</summary>
private int cancelCallCount;
/// <summary>How many times the Submitted() method was called</summary>
private int submitCallCount;
}
#endregion // class DialogViewModelSubscriber
/// <summary>Verifies that the dialog view model has a default constructor</summary>
[Test]
public void HasDefaultConstructor() {
Assert.DoesNotThrow(
delegate() { new DialogViewModel(); }
);
}
/// <summary>
/// Verifies that calling Confirm() on the dialog view model triggers
/// the 'Confirmed' event
/// </summary>
[Test]
public void ConfirmTriggersConfirmedEvent() {
var viewModel = new DialogViewModel();
var subscriber = createSubscriber(viewModel);
Assert.AreEqual(0, subscriber.ConfirmCallCount);
Assert.AreEqual(0, subscriber.CancelCallCount);
Assert.AreEqual(0, subscriber.SubmitCallCount);
viewModel.Confirm();
Assert.AreEqual(1, subscriber.ConfirmCallCount);
Assert.AreEqual(0, subscriber.CancelCallCount);
Assert.AreEqual(0, subscriber.SubmitCallCount);
}
/// <summary>
/// Verifies that calling Cancel() on the dialog view model triggers
/// the 'Cancelled' event
/// </summary>
[Test]
public void CancelTriggersCancelledEvent() {
var viewModel = new DialogViewModel();
var subscriber = createSubscriber(viewModel);
Assert.AreEqual(0, subscriber.ConfirmCallCount);
Assert.AreEqual(0, subscriber.CancelCallCount);
Assert.AreEqual(0, subscriber.SubmitCallCount);
viewModel.Cancel();
Assert.AreEqual(0, subscriber.ConfirmCallCount);
Assert.AreEqual(1, subscriber.CancelCallCount);
Assert.AreEqual(0, subscriber.SubmitCallCount);
}
/// <summary>
/// Verifies that calling Submitm() on the dialog view model triggers
/// the 'Submitted' event
/// </summary>
[Test]
public void SubmitTriggersSubmittedEvent() {
var viewModel = new DialogViewModel();
var subscriber = createSubscriber(viewModel);
Assert.AreEqual(0, subscriber.ConfirmCallCount);
Assert.AreEqual(0, subscriber.CancelCallCount);
Assert.AreEqual(0, subscriber.SubmitCallCount);
viewModel.Submit();
Assert.AreEqual(0, subscriber.ConfirmCallCount);
Assert.AreEqual(0, subscriber.CancelCallCount);
Assert.AreEqual(1, subscriber.SubmitCallCount);
}
/// <summary>Constructs a new subscriber for the dialog view model's events</summary>
/// <param name="viewModel">View model a subscriber will be created for</param>
/// <returns>A subscriber for the events of the specified view model</returns>
private DialogViewModelSubscriber createSubscriber(DialogViewModel viewModel) {
var subscriber = new DialogViewModelSubscriber();
viewModel.Confirmed += subscriber.Confirmed;
viewModel.Canceled += subscriber.Cancelled;
viewModel.Submitted += subscriber.Submitted;
return subscriber;
}
}
} // namespace Nuclex.Windows.Forms.ViewModels

View File

@ -0,0 +1,76 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2019 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using Nuclex.Support;
namespace Nuclex.Windows.Forms.ViewModels {
/// <summary>Base class for the view model of dialogs (typically modal ones)</summary>
public class DialogViewModel : Observable {
/// <summary>Indicates that the view should close with a positive result</summary>
/// <remarks>
/// This event typically corresponds to the 'Ok' button in a dialog.
/// </remarks>
public event EventHandler Confirmed;
/// <summary>Indicates that the view should close with a negative result</summary>
/// <remarks>
/// This event typically corresponds to the 'Cancel' button in a dialog.
/// </remarks>
public event EventHandler Canceled;
/// <summary>Indicates that the view should close</summary>
/// <remarks>
/// This closes the view with a neutral result, used when the view doesn't follow
/// an ok/cancel scheme or the result is transmitted in some other way.
/// </remarks>
public event EventHandler Submitted;
/// <summary>
/// Indicates that the dialog should be closed with a positive outcome
/// </summary>
public virtual void Confirm() {
if(Confirmed != null) {
Confirmed(this, EventArgs.Empty);
}
}
/// <summary>
/// Indicates that the dialog should be closed with a negative outcome
/// </summary>
public virtual void Cancel() {
if(Canceled != null) {
Canceled(this, EventArgs.Empty);
}
}
/// <summary>Indicates that the dialog should be closed</summary>
public virtual void Submit() {
if(Submitted != null) {
Submitted(this, EventArgs.Empty);
}
}
}
} // namespace Nuclex.Windows.Forms.ViewModels

View File

@ -0,0 +1,150 @@
using System;
using NUnit.Framework;
namespace Nuclex.Windows.Forms.ViewModels {
/// <summary>Unit test for the threaded dialog view model</summary>
[TestFixture]
public class ThreadedDialogViewModelTest {
#region class DialogViewModelSubscriber
/// <summary>Subscriber for the events offered by a dialog view model</summary>
private class DialogViewModelSubscriber {
/// <summary>Indicates that the user has accepted the dialog</summary>
public void Confirmed(object sender, EventArgs arguments) {
++this.confirmCallCount;
}
/// <summary>Indicates that the user has cancelled the dialog</summary>
public void Cancelled(object sender, EventArgs arguments) {
++this.cancelCallCount;
}
/// <summary>Indicates that the dialog was simply closed</summary>
public void Submitted(object sender, EventArgs arguments) {
++this.submitCallCount;
}
/// <summary>How many times the Confirmed() method was called</summary>
public int ConfirmCallCount {
get { return this.confirmCallCount; }
}
/// <summary>How many times the Cancelled() method was called</summary>
public int CancelCallCount {
get { return this.cancelCallCount; }
}
/// <summary>How many times the Submitted() method was called</summary>
public int SubmitCallCount {
get { return this.submitCallCount; }
}
/// <summary>How many times the Confirmed() method was called</summary>
private int confirmCallCount;
/// <summary>How many times the Cancelled() method was called</summary>
private int cancelCallCount;
/// <summary>How many times the Submitted() method was called</summary>
private int submitCallCount;
}
#endregion // class DialogViewModelSubscriber
#region class TestViewModel
private class TestViewModel : ThreadedDialogViewModel {
public Exception ReportedError {
get { return this.reportedError; }
}
protected override void ReportError(Exception exception) {
this.reportedError = exception;
}
private Exception reportedError;
}
#endregion // class TestViewModel
/// <summary>Verifies that the dialog view model has a default constructor</summary>
[Test]
public void HasDefaultConstructor() {
Assert.DoesNotThrow(
delegate() { new TestViewModel(); }
);
}
/// <summary>
/// Verifies that calling Confirm() on the dialog view model triggers
/// the 'Confirmed' event
/// </summary>
[Test]
public void ConfirmTriggersConfirmedEvent() {
var viewModel = new TestViewModel();
var subscriber = createSubscriber(viewModel);
Assert.AreEqual(0, subscriber.ConfirmCallCount);
Assert.AreEqual(0, subscriber.CancelCallCount);
Assert.AreEqual(0, subscriber.SubmitCallCount);
viewModel.Confirm();
Assert.AreEqual(1, subscriber.ConfirmCallCount);
Assert.AreEqual(0, subscriber.CancelCallCount);
Assert.AreEqual(0, subscriber.SubmitCallCount);
}
/// <summary>
/// Verifies that calling Cancel() on the dialog view model triggers
/// the 'Cancelled' event
/// </summary>
[Test]
public void CancelTriggersCancelledEvent() {
var viewModel = new TestViewModel();
var subscriber = createSubscriber(viewModel);
Assert.AreEqual(0, subscriber.ConfirmCallCount);
Assert.AreEqual(0, subscriber.CancelCallCount);
Assert.AreEqual(0, subscriber.SubmitCallCount);
viewModel.Cancel();
Assert.AreEqual(0, subscriber.ConfirmCallCount);
Assert.AreEqual(1, subscriber.CancelCallCount);
Assert.AreEqual(0, subscriber.SubmitCallCount);
}
/// <summary>
/// Verifies that calling Submitm() on the dialog view model triggers
/// the 'Submitted' event
/// </summary>
[Test]
public void SubmitTriggersSubmittedEvent() {
var viewModel = new TestViewModel();
var subscriber = createSubscriber(viewModel);
Assert.AreEqual(0, subscriber.ConfirmCallCount);
Assert.AreEqual(0, subscriber.CancelCallCount);
Assert.AreEqual(0, subscriber.SubmitCallCount);
viewModel.Submit();
Assert.AreEqual(0, subscriber.ConfirmCallCount);
Assert.AreEqual(0, subscriber.CancelCallCount);
Assert.AreEqual(1, subscriber.SubmitCallCount);
}
/// <summary>Constructs a new subscriber for the dialog view model's events</summary>
/// <param name="viewModel">View model a subscriber will be created for</param>
/// <returns>A subscriber for the events of the specified view model</returns>
private DialogViewModelSubscriber createSubscriber(ThreadedDialogViewModel viewModel) {
var subscriber = new DialogViewModelSubscriber();
viewModel.Confirmed += subscriber.Confirmed;
viewModel.Canceled += subscriber.Cancelled;
viewModel.Submitted += subscriber.Submitted;
return subscriber;
}
}
} // namespace Nuclex.Windows.Forms.ViewModels

View File

@ -0,0 +1,74 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2019 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
namespace Nuclex.Windows.Forms.ViewModels {
/// <summary>View model for a dialog that can execute tasks in a background thread</summary>
public abstract class ThreadedDialogViewModel : ThreadedViewModel {
/// <summary>Indicates that the view should close with a positive result</summary>
/// <remarks>
/// This event typically corresponds to the 'Ok' button in a dialog.
/// </remarks>
public event EventHandler Confirmed;
/// <summary>Indicates that the view should close with a negative result</summary>
/// <remarks>
/// This event typically corresponds to the 'Cancel' button in a dialog.
/// </remarks>
public event EventHandler Canceled;
/// <summary>Indicates that the view should close</summary>
/// <remarks>
/// This closes the view with a neutral result, used when the view doesn't follow
/// an ok/cancel scheme or the result is transmitted in some other way.
/// </remarks>
public event EventHandler Submitted;
/// <summary>
/// Indicates that the dialog should be closed with a positive outcome
/// </summary>
public virtual void Confirm() {
if(Confirmed != null) {
Confirmed(this, EventArgs.Empty);
}
}
/// <summary>
/// Indicates that the dialog should be closed with a negative outcome
/// </summary>
public virtual void Cancel() {
if(Canceled != null) {
Canceled(this, EventArgs.Empty);
}
}
/// <summary>Indicates that the dialog should be closed</summary>
public virtual void Submit() {
if(Submitted != null) {
Submitted(this, EventArgs.Empty);
}
}
}
} // namespace Nuclex.Windows.Forms.ViewModels