#region Apache License 2.0
/*
Nuclex .NET Framework
Copyright (C) 2002-2024 Markus Ewald / Nuclex Development Labs
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#endregion // Apache License 2.0
#if UNITTEST
using System;
using NUnit.Framework;
namespace Nuclex.Windows.Forms.ViewModels {
/// Unit test for the dialog view model
[TestFixture]
public class DialogViewModelTest {
#region class DialogViewModelSubscriber
/// Subscriber for the events offered by a dialog view model
private class DialogViewModelSubscriber {
/// Indicates that the user has accepted the dialog
public void Confirmed(object sender, EventArgs arguments) {
++this.confirmCallCount;
}
/// Indicates that the user has cancelled the dialog
public void Cancelled(object sender, EventArgs arguments) {
++this.cancelCallCount;
}
/// Indicates that the dialog was simply closed
public void Submitted(object sender, EventArgs arguments) {
++this.submitCallCount;
}
/// How many times the Confirmed() method was called
public int ConfirmCallCount {
get { return this.confirmCallCount; }
}
/// How many times the Cancelled() method was called
public int CancelCallCount {
get { return this.cancelCallCount; }
}
/// How many times the Submitted() method was called
public int SubmitCallCount {
get { return this.submitCallCount; }
}
/// How many times the Confirmed() method was called
private int confirmCallCount;
/// How many times the Cancelled() method was called
private int cancelCallCount;
/// How many times the Submitted() method was called
private int submitCallCount;
}
#endregion // class DialogViewModelSubscriber
/// Verifies that the dialog view model has a default constructor
[Test]
public void HasDefaultConstructor() {
Assert.DoesNotThrow(
delegate() { new DialogViewModel(); }
);
}
///
/// Verifies that calling Confirm() on the dialog view model triggers
/// the 'Confirmed' event
///
[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);
}
///
/// Verifies that calling Cancel() on the dialog view model triggers
/// the 'Cancelled' event
///
[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);
}
///
/// Verifies that calling Submitm() on the dialog view model triggers
/// the 'Submitted' event
///
[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);
}
/// Constructs a new subscriber for the dialog view model's events
/// View model a subscriber will be created for
/// A subscriber for the events of the specified view model
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
#endif // UNITTEST