using System; using NUnit.Framework; namespace Chernobyl.ComponentModel { /// /// Tests for implementations of the type. /// public abstract class TaskTests { [Test, Description("Test to ensure the ITask.Canceled event is raised " + "when ITask.Cancel() is invoked and ITask.ProgressPercentage is " + "less than 100.")] public void CanceledRaisedWhenProgressPercentageIsLessThan100() { ITask task = CreateIncompleteTask(); task.Canceled += (sender, e) => { if (task.ProgressPercentage < 100) Assert.Pass(); Assert.Fail("ITask.Canceled was raised even " + "though ITask.ProgressPercentage is equal " + "to 100."); }; task.Cancel(); Assert.Fail("ITask.Canceled was not raised even though " + "ITask.Cancel() was invoked."); } [Test, Description("Test to ensure that ITask.ProgressPercentage is equal " + "to 0 when ITask.Cancel() has been invoked.")] public void ProgressPercentageEquals0WhenCancelIsInvoked() { ITask task = CreateIncompleteTask(); MakeProgress(task); Assert.Greater(task.ProgressPercentage, 0, "ITask.ProgressPercentage " + "is greater than 0 even though work was supposed to be done on it."); Assert.Less(task.ProgressPercentage, 100, "ITask.ProgressPercentage " + "is 100 even though the task was supposed to be incomplete."); task.Cancel(); Assert.AreEqual(0, task.ProgressPercentage, "ITask.ProgressPercentage " + "is not 0 even though ITask.Cancel() was invoked on the ITask."); } [Test, Description("Test to ensure the ITask.Canceled event is not raised " + "when ITask.Cancel() is invoked and ITask.ProgressPercentage is " + "equal to 100.")] public void CanceledNotRaisedWhenProgressPercentageIsEqualTo100() { ITask task = CreateCompleteTask(); Assert.AreEqual(100, task.ProgressPercentage, "ITask.ProgressPercentage " + "is not 100 even though the task was supposed to be completed."); task.Canceled += (sender, e) => Assert.Fail("ITask.Canceled was raised " + "even though ITask.ProgressPercentage is equal to 100."); task.Cancel(); } [Test, Description("Test to ensure the ITask.ProgressChanged event is " + "raised and the ProgressChangedEventArgs.ProgressPercentage returned " + "by that event reflects the value in ITask.ProgressPercentage.")] public void ProgressChangedRaisedAndProgressChangedEventArgsIsValid() { ITask task = CreateIncompleteTask(); task.ProgressChanged += (sender, e) => { if (task.ProgressPercentage == e.ProgressPercentage) Assert.Pass(); Assert.Fail("ITask.ProgressChanged was raised but " + "the ProgressChangedEventArgs.ProgressPercentage was not " + "equal to ITask.ProgressPercentage."); }; MakeProgress(task); Assert.Fail("ITask.ProgressChanged was not raised even though " + "ITask.Cancel() was invoked."); } [Test, Description("Test to ensure the ITask.Canceled is raised only " + "once if ITask.Cancel() is invoked twice.")] public void CanceledNotRaisedOnSecondCallToCancel() { ITask task = CreateIncompleteTask(); task.Canceled += (sender, e) => Console.Write("TaskTests.CanceledNotRaisedOnSecondCallToCancel"); task.Cancel(); task.Canceled += (sender, e) => Assert.Fail("ITask.Canceled was " + "raised even though the ITask was already canceled."); task.Cancel(); } [Test, Description("Test to ensure the ITask.IsCanceled is false before " + "ITask.Cancel() is invoked and true after it is invoked.")] public void IsCanceledIsValidDependingOnCallToCancel() { ITask task = CreateIncompleteTask(); Assert.AreEqual(false, task.IsCanceled, "ITask.IsCanceled " + "is not false even though the task was supposed to be " + "and not canceled."); task.Cancel(); Assert.AreEqual(true, task.IsCanceled, "ITask.IsCanceled " + "is not true even though ITask.Cancel() was invoked on the task."); } /// /// Creates fully incomplete, not canceled, that is /// to be tested. This method should always create a new task and never /// reuse the same /// . /// /// The that is to be tested. protected abstract ITask CreateIncompleteTask(); /// /// Creates a fully completed, not canceled, that is /// to be tested. This method should always create a new task and never /// reuse the same /// . /// /// The that is to be tested. protected abstract ITask CreateCompleteTask(); /// /// Causes the passed in to make progress on the /// work it is doing. Note that the progress made should not be equal to /// 100% according to . /// /// The that is to continue work /// on whatever it is doing. protected abstract void MakeProgress(ITask task); } }