public void CanCancelCountingTest()
        {
            var target = new CounterViewModel();
            var countStarted = new AutoResetEvent(false);
            var countCompleted = new AutoResetEvent(false);
            target.CountStarted += () => countStarted.Set();
            target.CountCompleted += () => countCompleted.Set();

            Assert.IsFalse(target.CanCancelCounting(), "Cancel command is not disabled");

            target.CountTo = 5;
            target.StartCounting();

            Assert.IsTrue(countStarted.WaitOne(1000));

            Assert.IsTrue(target.CanCancelCounting(), "Cancel command is not enabled");

            Assert.IsTrue(countCompleted.WaitOne(1000));

            Assert.IsFalse(target.CanCancelCounting(), "Cancel command is not disabled");
        }
        public void StatusTest()
        {
            var target = new CounterViewModel();
            var countStarted = new AutoResetEvent(false);
            var countCompleted = new AutoResetEvent(false);
            target.CountStarted += () => countStarted.Set();
            target.CountCompleted += () => countCompleted.Set();
            var expected = new[]
                {
                    "Count Started",
                    "Count is 1",
                    "Count Completed"
                };
            var actual = new string[3];
            var index = 0;
            var propertyChangedNotified = false;
            target.PropertyChanged += (o, e) =>
                {
                    if (e.PropertyName == "Status")
                    {
                        actual[index] = ((CounterViewModel)o).Status;
                        Assert.AreEqual(expected[index], actual[index]);
                        index++;
                        propertyChangedNotified = true;
                    }
                };

            Assert.AreEqual("Ready!", target.Status);

            target.CountTo = 1;
            target.StartCounting();

            Assert.IsTrue(countCompleted.WaitOne(1000));

            // Verify that the PropertyChanged event was fired
            Assert.IsTrue(propertyChangedNotified);
            Assert.AreEqual(3, index);
        }
        public void StartCountingTest()
        {
            var target = new CounterViewModel();
            var countCompleted = new AutoResetEvent(false);
            target.CountCompleted += () => countCompleted.Set();

            target.CountTo = 5;
            target.StartCounting();

            Assert.IsTrue(countCompleted.WaitOne(1000));
            Assert.AreEqual(target.CountTo, target.CurrentCount, "Count did not complete");
        }
        public void IsCountingTest()
        {
            var target = new CounterViewModel();
            var countStarted = new AutoResetEvent(false);
            var countCompleted = new AutoResetEvent(false);
            target.CountStarted += () => countStarted.Set();
            target.CountCompleted += () => countCompleted.Set();
            var actual = new bool[2];
            var actualIndex = 0;
            var propertyChangedNotified = false;
            target.PropertyChanged += (o, e) =>
                {
                    if (e.PropertyName == "IsCounting")
                    {
                        actual[actualIndex] = ((CounterViewModel)o).IsCounting;
                        actualIndex++;
                        propertyChangedNotified = true;
                    }
                };

            const int expected = 1;

            target.CountTo = expected;
            target.StartCounting();

            Assert.IsTrue(countStarted.WaitOne(1000));
            // IsCounting will be updated twice, true at start, then false when done
            Assert.IsTrue(actual[0]);

            Assert.IsTrue(countCompleted.WaitOne(1000));
            Assert.IsFalse(actual[1]);

            // Verify that the PropertyChanged event was fired
            Assert.IsTrue(propertyChangedNotified);
        }
        public void CurrentCountTest()
        {
            var target = new CounterViewModel();
            var countCompleted = new AutoResetEvent(false);
            target.CountCompleted += () => countCompleted.Set();
            var actual = 0;
            var propertyChangedNotified = false;
            target.PropertyChanged += (o, e) =>
                {
                    if (e.PropertyName == "CurrentCount")
                    {
                        actual = ((CounterViewModel)o).CurrentCount;
                        propertyChangedNotified = true;
                    }
                };

            const int expected = 2;

            target.CountTo = expected;
            target.StartCounting();

            Assert.IsTrue(countCompleted.WaitOne(1000));

            // Verify CurrentCount did update
            Assert.AreEqual(expected, actual);

            // Verify that the PropertyChanged event was fired
            Assert.IsTrue(propertyChangedNotified);
        }
        public void CountToTest()
        {
            var target = new CounterViewModel();
            var countCompleted = new AutoResetEvent(false);
            target.CountCompleted += () => countCompleted.Set();

            var propertyChangedNotified = false;
            target.PropertyChanged += (o, e) =>
                {
                    if (e.PropertyName == "CountTo")
                    {
                        propertyChangedNotified = true;
                    }
                };

            const int expected = 1;

            target.CountTo = expected;
            target.StartCounting();

            Assert.IsTrue(countCompleted.WaitOne(1000));

            // Verify the we did count to CountTo
            Assert.AreEqual(expected, target.CurrentCount);

            // Verify that the PropertyChanged event was fired
            Assert.IsTrue(propertyChangedNotified);
        }
        public void CancelCountingTest()
        {
            var target = new CounterViewModel();
            var countStarted = new AutoResetEvent(false);
            var countCanceled = new AutoResetEvent(false);
            target.CountStarted += () => countStarted.Set();
            target.CountCanceled += () => countCanceled.Set();

            target.CountTo = 5;
            target.StartCounting();

            Assert.IsTrue(countStarted.WaitOne(1000));

            target.CancelCounting();

            Assert.IsTrue(countCanceled.WaitOne(1000));
            Assert.AreNotEqual(target.CountTo, target.CurrentCount, "Count finished - did not cancel");
        }