public void CurrentCycleCannotBeGreaterThanTargetValue()
        {
            var tested = new TestedTransferProgress();

            tested.Start(200);
            Assert.Throws <ArgumentOutOfRangeException>(() => tested.ReportProgress(201));
        }
        public void ResetResetsCompletly()
        {
            // Arrange
            var tested = new TestedTransferProgress();

            // Act
            tested.Restart(2);
            tested.ReportProgress();
            tested.ReportProgress();
            tested.Reset();

            Assert.True(tested.IsIdle);
            Assert.False(tested.IsRunning);
            Assert.False(tested.UsedAtLestOnce);
            Assert.Equal(default(TimeSpan), tested.AverageCycleDuration);
            Assert.Equal(0, tested.AverageCycleStep);
            Assert.Equal(0, tested.CompletedPercent);
            Assert.Equal(0, tested.CompletedRawValue);
            Assert.Equal(0, tested.CurrentCycle);
            Assert.Equal(0, tested.CurrentRawValue);
            Assert.Equal(0, tested.LastCycleStep);
            Assert.Equal(0, tested.PreviousRawValue);
            Assert.Equal(0, tested.RemainingCyclesEstimate);
            Assert.Equal(100, tested.RemainingPercent);
            Assert.Equal(0, tested.RemainingRawValue);
            Assert.Equal(default(TimeSpan), tested.RemainingTimeEstimate);
            Assert.Equal(default(TimeSpan), tested.Elapsed);
            Assert.Equal(0, tested.TargetCycleEstimate);
            Assert.Equal(0, tested.TargetRawValue);
        }
        public void TransferProgressHasValidStateAfterCreation()
        {
            var tested = new TestedTransferProgress();

            Assert.Equal(0, tested.AverageBitrateBps);
            Assert.Equal(0, tested.BitrateBps);
            Assert.True(tested.IsIdle);
            Assert.False(tested.IsRunning);
            Assert.False(tested.UsedAtLestOnce);
            Assert.Equal(default(TimeSpan), tested.AverageCycleDuration);
            Assert.Equal(0, tested.AverageCycleStep);
            Assert.Equal(0, tested.CompletedPercent);
            Assert.Equal(0, tested.CompletedRawValue);
            Assert.Equal(0, tested.CurrentCycle);
            Assert.Equal(0, tested.CurrentRawValue);
            Assert.Equal(0, tested.LastCycleStep);
            Assert.Equal(0, tested.PreviousRawValue);
            Assert.Equal(0, tested.RemainingCyclesEstimate);
            Assert.Equal(100, tested.RemainingPercent);
            Assert.Equal(0, tested.RemainingRawValue);
            Assert.Equal(default(TimeSpan), tested.RemainingTimeEstimate);
            Assert.Equal(default(TimeSpan), tested.Elapsed);
            Assert.Equal(0, tested.TargetCycleEstimate);
            Assert.Equal(0, tested.TargetRawValue);
        }
        public void ReportProgressTest2()
        {
            const int numberOfIterations = 10;
            var       tested             = new TestedTransferProgress();

            Thread.Sleep(1);
            Assert.Equal(0L, tested.Elapsed.TotalMilliseconds);
            Assert.False(tested.IsRunning);
            tested.Restart(numberOfIterations);
            const long remainingMsPrevious = long.MaxValue;
            var        elapsedMs           = 0.0;

            for (var i = 0; i < numberOfIterations; ++i)
            {
                Assert.True(tested.IsRunning);
                var from = i;
                var to   = i + 1;
                Thread.Sleep(1);
                var remainingMs = tested.RemainingTimeEstimate.TotalMilliseconds;
                tested.ReportProgress();
                elapsedMs = tested.Elapsed.TotalMilliseconds;
                Assert.True(remainingMsPrevious > remainingMs);
            }

            Assert.False(tested.IsRunning);
            Assert.Equal(elapsedMs, tested.Elapsed.TotalMilliseconds);
        }
        public void ProgressCannotRegress()
        {
            var tested = new TestedTransferProgress();

            tested.Start(200);

            tested.ReportProgress(100);
            Assert.Throws <ArgumentException>(() => tested.ReportProgress(99));
        }
        public void TestedTransferProgressCompletedPercentNeverGoesAbove100()
        {
            var tested = new TestedTransferProgress();

            tested.Start(1);
            tested.ReportProgress();
            try { tested.ReportProgress(); } catch { }
            Assert.Equal(100, tested.CompletedPercent);
        }
        public void PauseDoesNothingWhenNotRunning()
        {
            var tested = new TestedTransferProgress();

            Assert.False(tested.IsRunning);
            Assert.True(tested.IsIdle);
            tested.Pause();
            Assert.False(tested.IsRunning);
            Assert.True(tested.IsIdle);
        }
        public void CurrentCycleStopsIncrementingAfterCompletion()
        {
            var tested = new TestedTransferProgress();

            tested.Start(200);
            tested.ReportProgress(200);
            try { tested.ReportProgress(200); } catch { }
            try { tested.ReportProgress(200); } catch { }
            try { tested.ReportProgress(200); } catch { }
            try { tested.ReportProgress(200); } catch { }

            Assert.Equal(1, tested.CurrentCycle);
        }
        public void ReportingProgressProperlyUpdatesRemaingPercent(int iterations)
        {
            var tested = new TestedTransferProgress();

            tested.Start(iterations);
            var step = 100 / iterations;

            for (int i = 0; i < iterations; ++i)
            {
                tested.ReportProgress();
                var expectedCompletedPercent = step * (i + 1);
                var expectedRemaingPercent   = 100 - expectedCompletedPercent;
                Assert.Equal(expectedRemaingPercent, tested.RemainingPercent, 1);
            }
        }
        public void UnpauseUnpauses()
        {
            var tested = new TestedTransferProgress();

            tested.Start(2);

            tested.ReportProgress();
            tested.Pause();

            tested.UnPause();
            var elapsed = tested.Elapsed;

            Thread.Sleep(1);
            Assert.True(tested.Elapsed > elapsed);
        }
        public void ReportingProgressWithArguementProperlyUpdatesCurrentCycle()
        {
            var tested = new TestedTransferProgress();

            tested.Start(200);

            Assert.Equal(0, tested.CurrentCycle);

            tested.ReportProgress(100);

            Assert.Equal(1, tested.CurrentCycle);

            tested.ReportProgress(200);

            Assert.Equal(2, tested.CurrentCycle);
        }
        public void ReportingProgressWithArguementProperlyUpdatesRemainingRawValue()
        {
            var tested = new TestedTransferProgress();

            tested.Start(200);

            Assert.Equal(200, tested.RemainingRawValue, 1);

            tested.ReportProgress(100);

            Assert.Equal(100, tested.RemainingRawValue, 1);

            tested.ReportProgress(200);

            Assert.Equal(0, tested.RemainingRawValue, 1);
        }
        public void PausePauses()
        {
            var tested = new TestedTransferProgress();

            tested.Start(2);

            tested.ReportProgress();
            tested.Pause();

            var elapsed = tested.Elapsed;

            Thread.Sleep(1);

            Assert.Equal(elapsed, tested.Elapsed);
            Assert.False(tested.IsRunning);
            Assert.True(tested.IsIdle);
        }
        public void AverageBitrateBpsCalculatesAverageTransferPerSecond()
        {
            // Arrange
            const int bytesToTransfer  = 10000;
            const int cycles           = 2;
            const int bytesChunk       = 10000 / cycles;
            var       bytesTransferred = 0;
            var       tested           = new TestedTransferProgress();

            tested.Start(bytesToTransfer);

            // Act & Assert
            for (int i = 0; i < cycles; ++i)
            {
                bytesTransferred += bytesChunk;
                tested.ReportProgress(bytesTransferred);
                Assert.True(tested.AverageBitrateBps > 0);
            }
        }
        public void FinishTest()
        {
            const int numberOfIterations = 5;
            var       tested             = new TestedTransferProgress();

            Assert.False(tested.IsRunning);
            tested.Restart(numberOfIterations * 2);
            for (var i = 0; i < numberOfIterations; ++i)
            {
                Assert.True(tested.IsRunning);
                tested.ReportProgress();
            }
            Assert.False(tested.IsIdle);
            Assert.True(tested.IsRunning);
            tested.Pause();

            Assert.False(tested.IsRunning);
            Assert.True(tested.IsIdle);
        }
        public void LastCycleStepIsCorrect()
        {
            var tested = new TestedTransferProgress();

            tested.Start(200);

            Assert.Equal(0, tested.LastCycleStep);

            tested.ReportProgress(100);

            Assert.Equal(100, tested.LastCycleStep);

            tested.ReportProgress(150);

            Assert.Equal(50, tested.LastCycleStep);

            tested.ReportProgress(200);

            Assert.Equal(50, tested.LastCycleStep);
        }
        public void AverageCycleStepIsCorrect()
        {
            var tested = new TestedTransferProgress();

            tested.Start(200);

            Assert.Equal(0, tested.AverageCycleStep);

            tested.ReportProgress(100);

            Assert.Equal(100, tested.AverageCycleStep);

            tested.ReportProgress(150);

            Assert.Equal(75.0, tested.AverageCycleStep, 1);

            tested.ReportProgress(200);

            Assert.Equal(66.7, tested.AverageCycleStep, 1);
        }
        public void ReportingProgressRaisesPropertyChangedEvents()
        {
            var tested = new TestedTransferProgress();

            tested.Start(2);

            var receivedEvents = new HashSet <string>();

            tested.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
            {
                receivedEvents.Add(e.PropertyName);
            };

            tested.ReportProgress();

            Assert.Contains(nameof(tested.AverageCycleDuration), receivedEvents);
            Assert.Contains(nameof(tested.AverageCycleStep), receivedEvents);
            Assert.Contains(nameof(tested.CompletedPercent), receivedEvents);
            Assert.Contains(nameof(tested.CompletedRawValue), receivedEvents);
            Assert.Contains(nameof(tested.CurrentCycle), receivedEvents);
            Assert.Contains(nameof(tested.CurrentRawValue), receivedEvents);
            Assert.Contains(nameof(tested.Elapsed), receivedEvents);
            Assert.Contains(nameof(tested.IsIdle), receivedEvents);
            Assert.Contains(nameof(tested.IsRunning), receivedEvents);
            Assert.Contains(nameof(tested.LastCycleStep), receivedEvents);
            Assert.Contains(nameof(tested.PreviousRawValue), receivedEvents);
            Assert.Contains(nameof(tested.RemainingCyclesEstimate), receivedEvents);
            Assert.Contains(nameof(tested.RemainingPercent), receivedEvents);
            Assert.Contains(nameof(tested.RemainingRawValue), receivedEvents);
            Assert.Contains(nameof(tested.RemainingTimeEstimate), receivedEvents);
            Assert.Contains(nameof(tested.UsedAtLestOnce), receivedEvents);
            Assert.Contains(nameof(tested.LastCycleTotalMillisecondsElapsed), receivedEvents);
            Assert.Contains(nameof(tested.CurrentCycleDuration), receivedEvents);
            Assert.Contains(nameof(tested.LastCycleDurationMs), receivedEvents);
            Assert.Contains(nameof(tested.AverageBitrateBps), receivedEvents);
            Assert.Contains(nameof(tested.BitrateBps), receivedEvents);

            //Assert.Contains(nameof(tested.TargetCycleEstimate), receivedEvents);
            //Assert.Contains(nameof(tested.TargetRawValue), receivedEvents);
        }
        public void TargetCycleEstimateIsCorrectEnough()
        {
            var tested = new TestedTransferProgress();

            tested.Start(200);

            Assert.Equal(200, tested.TargetCycleEstimate);

            tested.ReportProgress(99);

            Assert.Equal(2, tested.TargetCycleEstimate, 1);

            tested.ReportProgress(100);

            Assert.Equal(4, tested.TargetCycleEstimate, 1);
            tested.ReportProgress(101);

            Assert.Equal(5.9, tested.TargetCycleEstimate, 1);

            tested.ReportProgress(200);

            Assert.Equal(4, tested.TargetCycleEstimate, 1);
        }
        public void RemainingCyclesEstimateIsCorrectEnough()
        {
            var tested = new TestedTransferProgress();

            tested.Start(200);

            Assert.Equal(200, tested.RemainingCyclesEstimate);

            tested.ReportProgress(99);

            Assert.Equal(1, tested.RemainingCyclesEstimate, 1);

            tested.ReportProgress(100);

            Assert.Equal(2, tested.RemainingCyclesEstimate, 1);

            tested.ReportProgress(101);

            Assert.Equal(2.9, tested.RemainingCyclesEstimate, 1);

            tested.ReportProgress(200);

            Assert.Equal(0, tested.RemainingCyclesEstimate, 1);
        }
        public void ThrowsExceptionWhenTargetValueInvalid()
        {
            var tested = new TestedTransferProgress();

            Assert.Throws <ArgumentOutOfRangeException>(() => { tested.Start(-100); });
        }
        public void ReportProgressThrowExceptionWhenTargetIsZero()
        {
            var tested = new TestedTransferProgress();

            Assert.Throws <InvalidOperationException>(() => { tested.ReportProgress(); });
        }
        public void ThrowsExceptionWhenNotStarted2()
        {
            var tested = new TestedTransferProgress();

            Assert.Throws <InvalidOperationException>(() => { tested.ReportProgress(); });
        }