d59753b98d
git-svn-id: file:///srv/devel/repo-conversion/nusu@59 d2e56fa2-650e-0410-a79f-9358c0239efd
349 lines
12 KiB
C#
349 lines
12 KiB
C#
#region CPL License
|
|
/*
|
|
Nuclex Framework
|
|
Copyright (C) 2002-2008 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 System.Collections.Generic;
|
|
using System.IO;
|
|
|
|
#if UNITTEST
|
|
|
|
using NUnit.Framework;
|
|
using NMock2;
|
|
|
|
namespace Nuclex.Support.Tracking {
|
|
|
|
/// <summary>Unit Test for the progression tracker class</summary>
|
|
[TestFixture]
|
|
public class ProgressionTrackerTest {
|
|
|
|
#region interface IProgressionTrackerSubscriber
|
|
|
|
/// <summary>Interface used to test the progression tracker.</summary>
|
|
public interface IProgressionTrackerSubscriber {
|
|
|
|
/// <summary>Called when the progression tracker's progress changes</summary>
|
|
/// <param name="sender">Progression tracker whose progress has changed</param>
|
|
/// <param name="e">Contains the new progress achieved</param>
|
|
void ProgressUpdated(object sender, ProgressUpdateEventArgs e);
|
|
|
|
/// <summary>Called when the progression tracker's idle state changes</summary>
|
|
/// <param name="sender">Progression tracker whose idle state has changed</param>
|
|
/// <param name="e">Contains the new idle state of the tracker</param>
|
|
void IdleStateChanged(object sender, IdleStateEventArgs e);
|
|
|
|
}
|
|
|
|
#endregion // interface IProgressionTrackerSubscriber
|
|
|
|
#region class ProgressUpdateEventArgsMatcher
|
|
|
|
/// <summary>Compares two ProgressUpdateEventArgsInstances for NMock validation</summary>
|
|
private class ProgressUpdateEventArgsMatcher : Matcher {
|
|
|
|
/// <summary>Initializes a new ProgressUpdateEventArgsMatcher </summary>
|
|
/// <param name="expected">Expected progress update event arguments</param>
|
|
public ProgressUpdateEventArgsMatcher(ProgressUpdateEventArgs expected) {
|
|
this.expected = expected;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called by NMock to verfiy the ProgressUpdateEventArgs match the expected value
|
|
/// </summary>
|
|
/// <param name="actualAsObject">Actual value to compare to the expected value</param>
|
|
/// <returns>
|
|
/// True if the actual value matches the expected value; otherwise false
|
|
/// </returns>
|
|
public override bool Matches(object actualAsObject) {
|
|
ProgressUpdateEventArgs actual = (actualAsObject as ProgressUpdateEventArgs);
|
|
if(actual == null)
|
|
return false;
|
|
|
|
return (actual.Progress == this.expected.Progress);
|
|
}
|
|
|
|
/// <summary>Creates a string representation of the expected value</summary>
|
|
/// <param name="writer">Writer to write the string representation into</param>
|
|
public override void DescribeTo(TextWriter writer) {
|
|
writer.Write(this.expected.Progress.ToString());
|
|
}
|
|
|
|
/// <summary>Expected progress update event args value</summary>
|
|
private ProgressUpdateEventArgs expected;
|
|
|
|
}
|
|
|
|
#endregion // class ProgressUpdateEventArgsMatcher
|
|
|
|
#region class TestProgression
|
|
|
|
/// <summary>Progression used for testing in this unit test</summary>
|
|
private class TestProgression : Progression {
|
|
|
|
/// <summary>Changes the testing progression's indicated progress</summary>
|
|
/// <param name="progress">
|
|
/// New progress to be reported by the testing progression
|
|
/// </param>
|
|
public void ChangeProgress(float progress) {
|
|
OnAsyncProgressUpdated(progress);
|
|
}
|
|
|
|
/// <summary>Transitions the progression into the ended state</summary>
|
|
public void End() {
|
|
OnAsyncEnded();
|
|
}
|
|
|
|
}
|
|
|
|
#endregion // class TestProgression
|
|
|
|
/// <summary>Initialization routine executed before each test is run</summary>
|
|
[SetUp]
|
|
public void Setup() {
|
|
this.mockery = new Mockery();
|
|
}
|
|
|
|
/// <summary>Validates that the tracker properly sums the progress</summary>
|
|
[Test]
|
|
public void TestSummedProgress() {
|
|
ProgressionTracker tracker = new ProgressionTracker();
|
|
|
|
IProgressionTrackerSubscriber mockedSubscriber = mockSubscriber(tracker);
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
Method("IdleStateChanged").
|
|
WithAnyArguments();
|
|
|
|
Expect.Exactly(2).On(mockedSubscriber).
|
|
Method("ProgressUpdated").
|
|
With(
|
|
new Matcher[] {
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressionTracker)),
|
|
new ProgressUpdateEventArgsMatcher(new ProgressUpdateEventArgs(0.0f))
|
|
}
|
|
);
|
|
|
|
TestProgression test1 = new TestProgression();
|
|
tracker.Track(test1);
|
|
TestProgression test2 = new TestProgression();
|
|
tracker.Track(test2);
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
Method("ProgressUpdated").
|
|
With(
|
|
new Matcher[] {
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressionTracker)),
|
|
new ProgressUpdateEventArgsMatcher(new ProgressUpdateEventArgs(0.25f))
|
|
}
|
|
);
|
|
|
|
test1.ChangeProgress(0.5f);
|
|
|
|
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates that the tracker only removes progressions when the whole
|
|
/// tracking list has reached the 'ended' state.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// If the tracker would remove ended progressions right when they finished,
|
|
/// the total progress would jump back each time. This is unwanted, of course.
|
|
/// </remarks>
|
|
[Test]
|
|
public void TestDelayedRemoval() {
|
|
ProgressionTracker tracker = new ProgressionTracker();
|
|
|
|
IProgressionTrackerSubscriber mockedSubscriber = mockSubscriber(tracker);
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
Method("IdleStateChanged").
|
|
WithAnyArguments();
|
|
|
|
Expect.Exactly(2).On(mockedSubscriber).
|
|
Method("ProgressUpdated").
|
|
With(
|
|
new Matcher[] {
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressionTracker)),
|
|
new ProgressUpdateEventArgsMatcher(new ProgressUpdateEventArgs(0.0f))
|
|
}
|
|
);
|
|
|
|
TestProgression test1 = new TestProgression();
|
|
tracker.Track(test1);
|
|
TestProgression test2 = new TestProgression();
|
|
tracker.Track(test2);
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
Method("ProgressUpdated").
|
|
With(
|
|
new Matcher[] {
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressionTracker)),
|
|
new ProgressUpdateEventArgsMatcher(new ProgressUpdateEventArgs(0.25f))
|
|
}
|
|
);
|
|
|
|
test1.ChangeProgress(0.5f);
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
Method("ProgressUpdated").
|
|
With(
|
|
new Matcher[] {
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressionTracker)),
|
|
new ProgressUpdateEventArgsMatcher(new ProgressUpdateEventArgs(0.75f))
|
|
}
|
|
);
|
|
|
|
// Total progress should be 0.75 after this call (one progression at 1.0,
|
|
// the other one at 0.5). If the second progression would be removed,
|
|
// the progress would jump to 0.5 instead.
|
|
test2.End();
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
Method("ProgressUpdated").
|
|
With(
|
|
new Matcher[] {
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressionTracker)),
|
|
new ProgressUpdateEventArgsMatcher(new ProgressUpdateEventArgs(1.0f))
|
|
}
|
|
);
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
Method("IdleStateChanged").
|
|
WithAnyArguments();
|
|
|
|
test1.End();
|
|
|
|
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates that the tracker behaves correctly if it is fed with progressions
|
|
/// that have already ended.
|
|
/// </summary>
|
|
[Test]
|
|
public void TestSoleEndedProgression() {
|
|
ProgressionTracker tracker = new ProgressionTracker();
|
|
|
|
IProgressionTrackerSubscriber mockedSubscriber = mockSubscriber(tracker);
|
|
|
|
tracker.Track(Progression.EndedDummy);
|
|
|
|
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates that the tracker behaves correctly if it is fed with progressions
|
|
/// that have already ended in addition to progressions that are actively executing.
|
|
/// </summary>
|
|
[Test]
|
|
public void TestEndedProgression() {
|
|
ProgressionTracker tracker = new ProgressionTracker();
|
|
|
|
IProgressionTrackerSubscriber mockedSubscriber = mockSubscriber(tracker);
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
Method("IdleStateChanged").
|
|
WithAnyArguments();
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
Method("ProgressUpdated").
|
|
With(
|
|
new Matcher[] {
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressionTracker)),
|
|
new ProgressUpdateEventArgsMatcher(new ProgressUpdateEventArgs(0.0f))
|
|
}
|
|
);
|
|
|
|
TestProgression test1 = new TestProgression();
|
|
tracker.Track(test1);
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
Method("ProgressUpdated").
|
|
With(
|
|
new Matcher[] {
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressionTracker)),
|
|
new ProgressUpdateEventArgsMatcher(new ProgressUpdateEventArgs(0.5f))
|
|
}
|
|
);
|
|
|
|
tracker.Track(Progression.EndedDummy);
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
Method("ProgressUpdated").
|
|
With(
|
|
new Matcher[] {
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressionTracker)),
|
|
new ProgressUpdateEventArgsMatcher(new ProgressUpdateEventArgs(1.0f))
|
|
}
|
|
);
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
Method("IdleStateChanged").
|
|
WithAnyArguments();
|
|
|
|
test1.End();
|
|
|
|
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tries to provoke a deadlock by re-entering the tracker from one of
|
|
/// its own events.
|
|
/// </summary>
|
|
[Test]
|
|
public void TestProvokedDeadlock() {
|
|
ProgressionTracker tracker = new ProgressionTracker();
|
|
|
|
TestProgression test1 = new TestProgression();
|
|
tracker.Track(test1);
|
|
|
|
tracker.AsyncIdleStateChanged +=
|
|
(EventHandler<IdleStateEventArgs>)delegate(object sender, IdleStateEventArgs arguments) {
|
|
tracker.Track(Progression.EndedDummy);
|
|
};
|
|
|
|
test1.End();
|
|
}
|
|
|
|
/// <summary>Mocks a subscriber for the events of a tracker</summary>
|
|
/// <param name="tracker">Tracker to mock an event subscriber for</param>
|
|
/// <returns>The mocked event subscriber</returns>
|
|
private IProgressionTrackerSubscriber mockSubscriber(ProgressionTracker tracker) {
|
|
IProgressionTrackerSubscriber mockedSubscriber =
|
|
this.mockery.NewMock<IProgressionTrackerSubscriber>();
|
|
|
|
tracker.AsyncIdleStateChanged +=
|
|
new EventHandler<IdleStateEventArgs>(mockedSubscriber.IdleStateChanged);
|
|
|
|
tracker.AsyncProgressUpdated +=
|
|
new EventHandler<ProgressUpdateEventArgs>(mockedSubscriber.ProgressUpdated);
|
|
|
|
return mockedSubscriber;
|
|
}
|
|
|
|
/// <summary>Mock object factory</summary>
|
|
private Mockery mockery;
|
|
|
|
}
|
|
|
|
} // namespace Nuclex.Support.Tracking
|
|
|
|
#endif // UNITTEST
|