using System; using System.Linq; using NUnit.Framework; namespace Chernobyl.ComponentModel { [TestFixture, Description("Tests for the TaskEnumerable type.")] public class TaskEnumerableTests : TaskTests { [Test, Description("A test to insure the TaskEnumerable.Canceled event " + "is raised and the TaskEnumerable.IsCanceled is true when all of " + "tasks contained by the TaskEnumerable are canceled.")] public void CancelAllTasksTest() { ITask[] tasks = new ITask[] { new TestingTask(), new TestingTask(), new TestingTask() }; TaskEnumerable taskEnumerable = new TaskEnumerable(tasks); CanceledChecker canceledChecker = new CanceledChecker(); taskEnumerable.Canceled += canceledChecker.OnCanceled; foreach (ITask task in tasks) task.Cancel(); Assert.IsTrue(canceledChecker.OnCanceledInvoked, "TaskEnumerable.Canceled was not invoked even though all of " + "the ITask instances it contains were canceled."); Assert.IsTrue(taskEnumerable.IsCanceled, "TaskEnumerable.IsCanceled is false even though all of " + "the ITask instances it contains were canceled."); } [Test, Description("A test that ensure the TaskEnumerable.ProgressPercentage " + "property is set to the proper value when one of its contained ITask " + "instances makes a certain amount of progress.")] public void ProgressPercentageTest() { TestingTask containedTask = new TestingTask(); ITask[] tasks = new ITask[] { containedTask, new TestingTask(), new TestingTask(), new TestingTask() }; TaskEnumerable taskEnumerable = new TaskEnumerable(tasks); containedTask.SetProgressPercentage(100); Assert.AreEqual(25, taskEnumerable.ProgressPercentage, "One task of four was set to 100% progress completion but the " + "TaskEnumerable.ProgressPercentage was not 25%."); } /// /// A helper type used to check if the /// event has been invoked. /// class CanceledChecker { public void OnCanceled(object sender, EventArgs e) { OnCanceledInvoked = true; } public bool OnCanceledInvoked { get; set; } } protected override ITask CreateIncompleteTask() { ITask[] tasks = new ITask[] { new TestingTask(), new TestingTask(), new TestingTask() }; return new TaskEnumerable(tasks); } protected override ITask CreateCompleteTask() { TestingTask[] tasks = new[] { new TestingTask(), new TestingTask(), new TestingTask() }; foreach (TestingTask task in tasks) task.SetProgressPercentage(100); return new TaskEnumerable(tasks); } protected override void MakeProgress(ITask task) { TaskEnumerable taskEnumerable = (TaskEnumerable) task; ((TestingTask)taskEnumerable.First()).SetProgressPercentage(50); } } }