2007-07-09 21:41:21 +00:00
|
|
|
#region CPL License
|
|
|
|
/*
|
|
|
|
Nuclex Framework
|
2008-01-07 18:04:02 +00:00
|
|
|
Copyright (C) 2002-2008 Nuclex Development Labs
|
2007-07-09 21:41:21 +00:00
|
|
|
|
|
|
|
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
|
2007-07-24 20:15:19 +00:00
|
|
|
|
2007-07-09 21:41:21 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
#if UNITTEST
|
|
|
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
using NMock2;
|
|
|
|
|
|
|
|
namespace Nuclex.Support.Tracking {
|
|
|
|
|
2008-06-11 20:28:08 +00:00
|
|
|
/// <summary>Unit Test for the progress tracker class</summary>
|
2007-07-09 21:41:21 +00:00
|
|
|
[TestFixture]
|
2008-06-11 20:28:08 +00:00
|
|
|
public class ProgressTrackerTest {
|
2007-07-09 21:41:21 +00:00
|
|
|
|
2008-06-11 20:28:08 +00:00
|
|
|
#region interface IProgressTrackerSubscriber
|
2007-07-09 21:41:21 +00:00
|
|
|
|
2008-06-11 20:28:08 +00:00
|
|
|
/// <summary>Interface used to test the progress tracker</summary>
|
|
|
|
public interface IProgressTrackerSubscriber {
|
2007-07-09 21:41:21 +00:00
|
|
|
|
2008-06-11 20:28:08 +00:00
|
|
|
/// <summary>Called when the tracked progress changes</summary>
|
|
|
|
/// <param name="sender">Progress tracker whose progress has changed</param>
|
2007-07-09 21:41:21 +00:00
|
|
|
/// <param name="e">Contains the new progress achieved</param>
|
2008-03-27 18:45:09 +00:00
|
|
|
void ProgressChanged(object sender, ProgressReportEventArgs e);
|
2007-07-09 21:41:21 +00:00
|
|
|
|
2008-06-11 20:28:08 +00:00
|
|
|
/// <summary>Called when the progress tracker's idle state changes</summary>
|
|
|
|
/// <param name="sender">Progress tracker whose idle state has changed</param>
|
2007-07-09 21:41:21 +00:00
|
|
|
/// <param name="e">Contains the new idle state of the tracker</param>
|
|
|
|
void IdleStateChanged(object sender, IdleStateEventArgs e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-06-11 20:28:08 +00:00
|
|
|
#endregion // interface IProgressTrackerSubscriber
|
2007-07-09 21:41:21 +00:00
|
|
|
|
|
|
|
#region class ProgressUpdateEventArgsMatcher
|
|
|
|
|
|
|
|
/// <summary>Compares two ProgressUpdateEventArgsInstances for NMock validation</summary>
|
2008-03-27 18:45:09 +00:00
|
|
|
private class ProgressReportEventArgsMatcher : Matcher {
|
2007-07-09 21:41:21 +00:00
|
|
|
|
|
|
|
/// <summary>Initializes a new ProgressUpdateEventArgsMatcher </summary>
|
|
|
|
/// <param name="expected">Expected progress update event arguments</param>
|
2008-03-27 18:45:09 +00:00
|
|
|
public ProgressReportEventArgsMatcher(ProgressReportEventArgs expected) {
|
2007-07-09 21:41:21 +00:00
|
|
|
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) {
|
2008-03-26 21:03:49 +00:00
|
|
|
ProgressReportEventArgs actual = (actualAsObject as ProgressReportEventArgs);
|
2007-07-09 21:41:21 +00:00
|
|
|
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>
|
2008-03-26 21:03:49 +00:00
|
|
|
private ProgressReportEventArgs expected;
|
2007-07-09 21:41:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion // class ProgressUpdateEventArgsMatcher
|
|
|
|
|
2008-12-03 18:58:20 +00:00
|
|
|
#region class TestTransaction
|
2007-07-09 21:41:21 +00:00
|
|
|
|
2008-12-03 18:58:20 +00:00
|
|
|
/// <summary>Transaction used for testing in this unit test</summary>
|
|
|
|
private class TestTransaction : Transaction, IProgressReporter {
|
2008-03-26 21:03:49 +00:00
|
|
|
|
|
|
|
/// <summary>will be triggered to report when progress has been achieved</summary>
|
|
|
|
public event EventHandler<ProgressReportEventArgs> AsyncProgressChanged;
|
2007-07-09 21:41:21 +00:00
|
|
|
|
2008-12-03 18:58:20 +00:00
|
|
|
/// <summary>Changes the testing transaction's indicated progress</summary>
|
|
|
|
/// <param name="progress">New progress to be reported by the testing transaction</param>
|
2007-07-09 21:41:21 +00:00
|
|
|
public void ChangeProgress(float progress) {
|
2008-03-26 21:03:49 +00:00
|
|
|
OnAsyncProgressChanged(progress);
|
2007-07-09 21:41:21 +00:00
|
|
|
}
|
|
|
|
|
2008-12-03 18:58:20 +00:00
|
|
|
/// <summary>Transitions the transaction into the ended state</summary>
|
2007-07-09 21:41:21 +00:00
|
|
|
public void End() {
|
|
|
|
OnAsyncEnded();
|
|
|
|
}
|
|
|
|
|
2008-03-26 21:03:49 +00:00
|
|
|
/// <summary>Fires the progress update event</summary>
|
|
|
|
/// <param name="progress">Progress to report (ranging from 0.0 to 1.0)</param>
|
|
|
|
/// <remarks>
|
2008-12-03 18:58:20 +00:00
|
|
|
/// Informs the observers of this transaction about the achieved progress.
|
2008-03-26 21:03:49 +00:00
|
|
|
/// </remarks>
|
|
|
|
protected virtual void OnAsyncProgressChanged(float progress) {
|
|
|
|
OnAsyncProgressChanged(new ProgressReportEventArgs(progress));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Fires the progress update event</summary>
|
|
|
|
/// <param name="eventArguments">Progress to report (ranging from 0.0 to 1.0)</param>
|
|
|
|
/// <remarks>
|
2008-12-03 18:58:20 +00:00
|
|
|
/// Informs the observers of this transaction about the achieved progress.
|
|
|
|
/// Allows for classes derived from the transaction class to easily provide
|
2008-03-26 21:03:49 +00:00
|
|
|
/// a custom event arguments class that has been derived from the
|
2008-12-03 18:58:20 +00:00
|
|
|
/// transaction's ProgressUpdateEventArgs class.
|
2008-03-26 21:03:49 +00:00
|
|
|
/// </remarks>
|
|
|
|
protected virtual void OnAsyncProgressChanged(ProgressReportEventArgs eventArguments) {
|
|
|
|
EventHandler<ProgressReportEventArgs> copy = AsyncProgressChanged;
|
|
|
|
if(copy != null)
|
|
|
|
copy(this, eventArguments);
|
|
|
|
}
|
|
|
|
|
2007-07-09 21:41:21 +00:00
|
|
|
}
|
|
|
|
|
2008-12-05 19:28:39 +00:00
|
|
|
#endregion // class TestTransaction
|
|
|
|
|
|
|
|
#region class EvilTransaction
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Transaction that tries to emulate a thread given a progress report at
|
|
|
|
/// a very inconvenient time ;)
|
|
|
|
/// </summary>
|
|
|
|
private class EvilTransaction : Transaction, IProgressReporter {
|
|
|
|
|
|
|
|
/// <summary>will be triggered to report when progress has been achieved</summary>
|
|
|
|
public event EventHandler<ProgressReportEventArgs> AsyncProgressChanged {
|
|
|
|
add {}
|
|
|
|
remove {
|
|
|
|
// Send a progress update right when the subscriber is trying to unsubscribe
|
|
|
|
value(this, new ProgressReportEventArgs(0.5f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion // class EvilTransaction
|
2007-07-09 21:41:21 +00:00
|
|
|
|
|
|
|
/// <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() {
|
2008-12-03 18:58:20 +00:00
|
|
|
using(ProgressTracker tracker = new ProgressTracker()) {
|
|
|
|
IProgressTrackerSubscriber mockedSubscriber = mockSubscriber(tracker);
|
|
|
|
|
|
|
|
TestTransaction test1 = new TestTransaction();
|
|
|
|
TestTransaction test2 = new TestTransaction();
|
|
|
|
|
|
|
|
// Step 1
|
|
|
|
{
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
|
|
Method("IdleStateChanged").
|
|
|
|
WithAnyArguments();
|
|
|
|
|
|
|
|
// Since the progress is already at 0, these redundant reports are optional
|
|
|
|
Expect.Between(0, 2).On(mockedSubscriber).
|
|
|
|
Method("ProgressChanged").
|
|
|
|
With(
|
|
|
|
new Matcher[] {
|
|
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
|
|
|
|
new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(0.0f))
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
tracker.Track(test1);
|
|
|
|
tracker.Track(test2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2
|
|
|
|
{
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
|
|
Method("ProgressChanged").
|
|
|
|
With(
|
|
|
|
new Matcher[] {
|
|
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
|
|
|
|
new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(0.25f))
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
test1.ChangeProgress(0.5f);
|
|
|
|
}
|
|
|
|
}
|
2007-07-09 21:41:21 +00:00
|
|
|
|
|
|
|
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2008-12-03 18:58:20 +00:00
|
|
|
/// Validates that the tracker only removes transactions when the whole
|
2007-07-09 21:41:21 +00:00
|
|
|
/// tracking list has reached the 'ended' state.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
2008-12-03 18:58:20 +00:00
|
|
|
/// If the tracker would remove ended transactions right when they finished,
|
2007-07-09 21:41:21 +00:00
|
|
|
/// the total progress would jump back each time. This is unwanted, of course.
|
|
|
|
/// </remarks>
|
|
|
|
[Test]
|
|
|
|
public void TestDelayedRemoval() {
|
2008-12-03 18:58:20 +00:00
|
|
|
using(ProgressTracker tracker = new ProgressTracker()) {
|
|
|
|
IProgressTrackerSubscriber mockedSubscriber = mockSubscriber(tracker);
|
|
|
|
|
|
|
|
TestTransaction test1 = new TestTransaction();
|
|
|
|
TestTransaction test2 = new TestTransaction();
|
|
|
|
|
|
|
|
// Step 1
|
|
|
|
{
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
|
|
Method("IdleStateChanged").
|
|
|
|
WithAnyArguments();
|
|
|
|
|
|
|
|
// This is optional. The tracker's progress is currently 0, so there's no need
|
|
|
|
// to send out superfluous progress reports.
|
|
|
|
Expect.Between(0, 2).On(mockedSubscriber).
|
|
|
|
Method("ProgressChanged").
|
|
|
|
With(
|
|
|
|
new Matcher[] {
|
|
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
|
|
|
|
new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(0.0f))
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
tracker.Track(test1);
|
|
|
|
tracker.Track(test2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2
|
|
|
|
{
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
|
|
Method("ProgressChanged").
|
|
|
|
With(
|
|
|
|
new Matcher[] {
|
|
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
|
|
|
|
new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(0.25f))
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Total progress should be 0.25 after this call (two transactions, one with
|
|
|
|
// 0% progress and one with 50% progress)
|
|
|
|
test1.ChangeProgress(0.5f);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 3
|
|
|
|
{
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
|
|
Method("ProgressChanged").
|
|
|
|
With(
|
|
|
|
new Matcher[] {
|
|
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
|
|
|
|
new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(0.75f))
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Total progress should be 0.75 after this call (one transaction at 100%,
|
|
|
|
// the other one at 50%). If the second transaction would be removed by the tracker,
|
|
|
|
// (which would be inappropriate!) the progress would falsely jump to 0.5 instead.
|
|
|
|
test2.End();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 4
|
|
|
|
{
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
|
|
Method("ProgressChanged").
|
|
|
|
With(
|
|
|
|
new Matcher[] {
|
|
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
|
|
|
|
new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(1.0f))
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
|
|
Method("IdleStateChanged").
|
|
|
|
WithAnyArguments();
|
|
|
|
|
|
|
|
test1.End();
|
|
|
|
}
|
|
|
|
}
|
2007-07-12 22:02:23 +00:00
|
|
|
|
|
|
|
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2008-12-03 18:58:20 +00:00
|
|
|
/// Validates that the tracker behaves correctly if it is fed with transactions
|
2007-07-12 22:02:23 +00:00
|
|
|
/// that have already ended.
|
|
|
|
/// </summary>
|
|
|
|
[Test]
|
2008-12-03 18:58:20 +00:00
|
|
|
public void TestSoleEndedTransaction() {
|
|
|
|
using(ProgressTracker tracker = new ProgressTracker()) {
|
|
|
|
IProgressTrackerSubscriber mockedSubscriber = mockSubscriber(tracker);
|
2008-12-07 19:36:29 +00:00
|
|
|
|
|
|
|
Expect.Never.On(mockedSubscriber).Method("IdleStateChanged").WithAnyArguments();
|
|
|
|
Expect.Never.On(mockedSubscriber).Method("ProgressChanged").WithAnyArguments();
|
2007-07-12 22:02:23 +00:00
|
|
|
|
2008-12-03 18:58:20 +00:00
|
|
|
tracker.Track(Transaction.EndedDummy);
|
|
|
|
}
|
2007-07-12 22:02:23 +00:00
|
|
|
|
|
|
|
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2008-12-03 18:58:20 +00:00
|
|
|
/// Validates that the tracker behaves correctly if it is fed with transactions
|
|
|
|
/// that have already ended in addition to transactions that are actively executing.
|
2007-07-12 22:02:23 +00:00
|
|
|
/// </summary>
|
|
|
|
[Test]
|
2008-12-03 18:58:20 +00:00
|
|
|
public void TestEndedTransaction() {
|
|
|
|
using(ProgressTracker tracker = new ProgressTracker()) {
|
|
|
|
IProgressTrackerSubscriber mockedSubscriber = mockSubscriber(tracker);
|
|
|
|
|
|
|
|
TestTransaction test1 = new TestTransaction();
|
|
|
|
|
|
|
|
// Step 1
|
|
|
|
{
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
|
|
Method("IdleStateChanged").
|
|
|
|
WithAnyArguments();
|
|
|
|
|
|
|
|
Expect.Between(0, 1).On(mockedSubscriber).
|
|
|
|
Method("ProgressChanged").
|
|
|
|
With(
|
|
|
|
new Matcher[] {
|
|
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
|
|
|
|
new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(0.0f))
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
tracker.Track(test1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2
|
|
|
|
{
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
|
|
Method("ProgressChanged").
|
|
|
|
With(
|
|
|
|
new Matcher[] {
|
|
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
|
|
|
|
new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(0.5f))
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
tracker.Track(Transaction.EndedDummy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 3
|
|
|
|
{
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
|
|
Method("ProgressChanged").
|
|
|
|
With(
|
|
|
|
new Matcher[] {
|
|
|
|
new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
|
|
|
|
new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(1.0f))
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
Expect.Once.On(mockedSubscriber).
|
|
|
|
Method("IdleStateChanged").
|
|
|
|
WithAnyArguments();
|
|
|
|
|
|
|
|
test1.End();
|
|
|
|
}
|
|
|
|
}
|
2007-07-09 21:41:21 +00:00
|
|
|
|
|
|
|
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
|
|
|
}
|
|
|
|
|
2007-07-12 22:16:11 +00:00
|
|
|
/// <summary>
|
2008-12-03 18:58:20 +00:00
|
|
|
/// Tries to provoke a deadlock by re-entering the tracker from one of its own events
|
2007-07-12 22:16:11 +00:00
|
|
|
/// </summary>
|
|
|
|
[Test]
|
|
|
|
public void TestProvokedDeadlock() {
|
2008-12-03 18:58:20 +00:00
|
|
|
using(ProgressTracker tracker = new ProgressTracker()) {
|
|
|
|
TestTransaction test1 = new TestTransaction();
|
|
|
|
tracker.Track(test1);
|
2007-07-12 22:16:11 +00:00
|
|
|
|
2008-12-03 18:58:20 +00:00
|
|
|
tracker.AsyncIdleStateChanged +=
|
|
|
|
(EventHandler<IdleStateEventArgs>)delegate(object sender, IdleStateEventArgs arguments) {
|
|
|
|
tracker.Track(Transaction.EndedDummy);
|
|
|
|
};
|
2007-07-12 22:16:11 +00:00
|
|
|
|
2008-12-03 18:58:20 +00:00
|
|
|
test1.End();
|
|
|
|
}
|
2007-07-12 22:16:11 +00:00
|
|
|
}
|
|
|
|
|
2008-12-05 19:28:39 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Tests whether the progress tracker enters and leaves the idle state correctly
|
|
|
|
/// when a transaction is removed via Untrack()
|
|
|
|
/// </summary>
|
|
|
|
[Test]
|
|
|
|
public void TestIdleWithUntrack() {
|
|
|
|
using(ProgressTracker tracker = new ProgressTracker()) {
|
|
|
|
TestTransaction test1 = new TestTransaction();
|
|
|
|
|
|
|
|
Assert.IsTrue(tracker.Idle);
|
|
|
|
|
|
|
|
tracker.Track(test1);
|
|
|
|
|
|
|
|
Assert.IsFalse(tracker.Idle);
|
|
|
|
|
|
|
|
tracker.Untrack(test1);
|
|
|
|
|
|
|
|
Assert.IsTrue(tracker.Idle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Tests whether the progress tracker enters and leaves the idle state correctly
|
|
|
|
/// when a transaction is removed the transaction finishing
|
|
|
|
/// </summary>
|
|
|
|
[Test]
|
|
|
|
public void TestIdleWithAutoRemoval() {
|
|
|
|
using(ProgressTracker tracker = new ProgressTracker()) {
|
|
|
|
TestTransaction test1 = new TestTransaction();
|
|
|
|
|
|
|
|
Assert.IsTrue(tracker.Idle);
|
|
|
|
|
|
|
|
tracker.Track(test1);
|
|
|
|
|
|
|
|
Assert.IsFalse(tracker.Idle);
|
|
|
|
|
|
|
|
test1.End();
|
|
|
|
|
|
|
|
Assert.IsTrue(tracker.Idle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Tests whether the progress tracker enters and leaves the idle state correctly
|
|
|
|
/// when a transaction is removed via Untrack()
|
|
|
|
/// </summary>
|
|
|
|
[Test]
|
|
|
|
public void TestProgressWithUntrack() {
|
|
|
|
using(ProgressTracker tracker = new ProgressTracker()) {
|
|
|
|
TestTransaction test1 = new TestTransaction();
|
|
|
|
TestTransaction test2 = new TestTransaction();
|
|
|
|
tracker.Track(test1);
|
|
|
|
tracker.Track(test2);
|
|
|
|
|
|
|
|
Assert.AreEqual(0.0f, tracker.Progress);
|
|
|
|
|
|
|
|
test1.ChangeProgress(0.5f);
|
|
|
|
|
|
|
|
Assert.AreEqual(0.25f, tracker.Progress);
|
|
|
|
|
|
|
|
tracker.Untrack(test2);
|
|
|
|
|
|
|
|
Assert.AreEqual(0.5f, tracker.Progress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Verifies that the progress tracker throws an exception if it is instructed
|
|
|
|
/// to untrack a transaction it doesn't know about
|
|
|
|
/// </summary>
|
|
|
|
[Test, ExpectedException(typeof(ArgumentException))]
|
|
|
|
public void TestThrowOnUntrackNonTrackedTransaction() {
|
|
|
|
using(ProgressTracker tracker = new ProgressTracker()) {
|
|
|
|
TestTransaction test1 = new TestTransaction();
|
|
|
|
|
|
|
|
tracker.Untrack(test1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Verifies that the progress tracker throws an exception if it is instructed
|
|
|
|
/// to untrack a transaction it doesn't know about
|
|
|
|
/// </summary>
|
|
|
|
[Test]
|
|
|
|
public void TestProgressReportDuringUnsubscribe() {
|
|
|
|
using(ProgressTracker tracker = new ProgressTracker()) {
|
|
|
|
EvilTransaction evil = new EvilTransaction();
|
|
|
|
tracker.Track(evil);
|
|
|
|
tracker.Untrack(evil);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Verifies that the progress tracker doesn't choke on a transaction being tracked
|
|
|
|
/// multiple times.
|
|
|
|
/// </summary>
|
|
|
|
[Test]
|
|
|
|
public void TestMultiTrackedTransaction() {
|
|
|
|
using(ProgressTracker tracker = new ProgressTracker()) {
|
|
|
|
TestTransaction test = new TestTransaction();
|
|
|
|
tracker.Track(test);
|
|
|
|
tracker.Track(test);
|
|
|
|
tracker.Track(test);
|
|
|
|
tracker.Untrack(test);
|
|
|
|
tracker.Untrack(test);
|
|
|
|
tracker.Untrack(test);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-09 21:41:21 +00:00
|
|
|
/// <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>
|
2008-06-11 20:28:08 +00:00
|
|
|
private IProgressTrackerSubscriber mockSubscriber(ProgressTracker tracker) {
|
|
|
|
IProgressTrackerSubscriber mockedSubscriber =
|
|
|
|
this.mockery.NewMock<IProgressTrackerSubscriber>();
|
2007-07-09 21:41:21 +00:00
|
|
|
|
|
|
|
tracker.AsyncIdleStateChanged +=
|
|
|
|
new EventHandler<IdleStateEventArgs>(mockedSubscriber.IdleStateChanged);
|
|
|
|
|
2008-03-26 21:20:52 +00:00
|
|
|
tracker.AsyncProgressChanged +=
|
2008-03-27 18:45:09 +00:00
|
|
|
new EventHandler<ProgressReportEventArgs>(mockedSubscriber.ProgressChanged);
|
2007-07-09 21:41:21 +00:00
|
|
|
|
|
|
|
return mockedSubscriber;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Mock object factory</summary>
|
|
|
|
private Mockery mockery;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Nuclex.Support.Tracking
|
|
|
|
|
|
|
|
#endif // UNITTEST
|