Esempio n. 1
0
        public void ExerciseService_Stop_SignalsActiveSubExerciseChnaged()
        {
            // Arrange
            var actualOldSubExercise       = SubExercise.Undefined;
            var actualNewSubExercise       = SubExercise.Undefined;
            var stubTimerService           = Substitute.For <ITimerService>();
            var stubExerciseTrackerService = Substitute.For <IExerciseTrackerService>();
            var unitUnderTest = ExerciseService.ExerciseServiceFromString(string.Empty, CreateTestExerciseConfiguration(), stubTimerService, stubExerciseTrackerService);

            unitUnderTest.Run();

            for (var i = 0; i < 193; i++)
            {
                stubTimerService.Elapsed += Raise.Event();
            }

            unitUnderTest.ActiveSubExerciseChanged += (sender, args) =>
            {
                actualOldSubExercise = args.OldSubExercise;
                actualNewSubExercise = args.NewSubExercise;
            };

            // Act
            unitUnderTest.Stop();

            // Assert
            Assert.AreEqual(SubExercise.ShortLeft, actualOldSubExercise);
            Assert.AreEqual(SubExercise.Undefined, actualNewSubExercise);
        }
Esempio n. 2
0
        public void ExerciseService_TimerTick_SignalsActiveSubExerciseChanged(int elapsedTicks, SubExercise expectedOldSubExercise, SubExercise expectedNewSubExercise)
        {
            // Arrange
            var  actualOldSubExercise       = SubExercise.Undefined;
            var  actualNewSubExercise       = SubExercise.Undefined;
            var  stubTimerService           = Substitute.For <ITimerService>();
            var  stubExerciseTrackerService = Substitute.For <IExerciseTrackerService>();
            var  unitUnderTest = ExerciseService.ExerciseServiceFromString(string.Empty, CreateTestExerciseConfiguration(), stubTimerService, stubExerciseTrackerService);
            bool handled       = false;

            unitUnderTest.Run();

            for (var i = 0; i < elapsedTicks - 1; i++)
            {
                stubTimerService.Elapsed += Raise.Event();
            }

            unitUnderTest.ActiveSubExerciseChanged += (sender, args) =>
            {
                if (!handled)
                {
                    actualOldSubExercise = args.OldSubExercise;
                    actualNewSubExercise = args.NewSubExercise;
                    handled = true;
                }
            };

            // Act
            stubTimerService.Elapsed += Raise.Event();

            // Assert
            Assert.AreEqual(expectedOldSubExercise, actualOldSubExercise);
            Assert.AreEqual(expectedNewSubExercise, actualNewSubExercise);
        }
Esempio n. 3
0
        public void ExerciseService_PrepareExerciseServiceFromJsonWithEmptyString_CreatesDefault()
        {
            // Arrange
            var eventThrown           = false;
            var exerciseConfiguration = new ExerciseConfiguration
            {
                NoOfRepetitions     = 8,
                DurationPerStance   = 10,
                PreparationDuration = 3
            };

            Exercise actualExercise   = null;
            var      expectedExercise = new Exercise(exerciseConfiguration);

            var stubTimerService           = Substitute.For <ITimerService>();
            var stubExerciseTrackerService = Substitute.For <IExerciseTrackerService>();

            var unitUnderTest = ExerciseService.ExerciseServiceFromString(string.Empty, exerciseConfiguration, stubTimerService, stubExerciseTrackerService);

            unitUnderTest.ExerciseChanged += (sender, args) =>
            {
                eventThrown    = true;
                actualExercise = args.Exercise;
            };

            // Act
            unitUnderTest.PrepareForNewExercise();

            // Assert
            // TODO: Move this to a sepparate test
            Assert.IsTrue(eventThrown, "Expected exercise changed event thrown");
            Assert.AreEqual(expectedExercise.ToString(), actualExercise.ToString());
        }
Esempio n. 4
0
        public void ExerciseService_TimerTick_ExerciseChangedCorrect(int elapsedTicks, string testCaseDescription, string expectedExerciseState)
        {
            // Arrange
            var      stubTimerService           = Substitute.For <ITimerService>();
            var      stubExerciseTrackerService = Substitute.For <IExerciseTrackerService>();
            var      unitUnderTest  = ExerciseService.ExerciseServiceFromString(string.Empty, CreateTestExerciseConfiguration(), stubTimerService, stubExerciseTrackerService);
            Exercise actualExercise = null;
            bool     handled        = false;

            unitUnderTest.Run();

            for (var i = 0; i < elapsedTicks - 1; i++)
            {
                stubTimerService.Elapsed += Raise.Event();
            }

            unitUnderTest.ExerciseChanged += (sender, args) =>
            {
                if (!handled)
                {
                    actualExercise = args.Exercise;
                    handled        = true;
                }
            };

            // Act
            stubTimerService.Elapsed += Raise.Event();

            // Assert
            Assert.AreEqual(expectedExerciseState, actualExercise.ToString(), $"TestCase {testCaseDescription} failed");
        }
Esempio n. 5
0
        private void CreateExerciseService(BaseBundle savedInstance)
        {
            var exerciseState = savedInstance?.GetString(SavedExerciseStateKey, string.Empty);

            // Create the exercise service
            mExerciseService = ExerciseService.ExerciseServiceFromString(exerciseState, new ExerciseConfiguration(), new TimerService(), mExerciseTrackerService);

            mExerciseService.ExerciseChanged          += ExerciseServiceOnExerciseChanged;
            mExerciseService.ActiveSubExerciseChanged += ExerciseServiceOnActiveSubExerciseChanged;
            mExerciseService.StateChanged             += ExerciseServiceOnStateChanged;
        }
Esempio n. 6
0
        public void ExerciseService_IsRunning_ReturnsFalseWhenNotRunning()
        {
            // Arrange
            var exerciseConfiguration      = CreateTestExerciseConfiguration();
            var stubTimerService           = Substitute.For <ITimerService>();
            var stubExerciseTrackerService = Substitute.For <IExerciseTrackerService>();
            var unitUnderTest = ExerciseService.ExerciseServiceFromString(string.Empty, exerciseConfiguration, stubTimerService, stubExerciseTrackerService);

            stubTimerService.IsRunning.Returns(false);

            // Act and assert
            Assert.IsFalse(unitUnderTest.IsRunning);
        }
Esempio n. 7
0
        public void ExerciseService_Start_StartsTimer()
        {
            // Arrange
            var exerciseConfiguration      = CreateTestExerciseConfiguration();
            var mockTimerService           = Substitute.For <ITimerService>();
            var stubExerciseTrackerService = Substitute.For <IExerciseTrackerService>();
            var unitUnderTest = ExerciseService.ExerciseServiceFromString(string.Empty, exerciseConfiguration, mockTimerService, stubExerciseTrackerService);

            // Act
            unitUnderTest.Run();

            // Assert
            mockTimerService.Received(1).Start(Arg.Any <int>());
        }
Esempio n. 8
0
        public void ExerciseService_StateToString_ReturnsExerciseToString()
        {
            // Arrange
            var exerciseConfiguration      = CreateTestExerciseConfiguration();
            var stubExerciseTImer          = Substitute.For <ITimerService>();
            var stubExerciseTrackerService = Substitute.For <IExerciseTrackerService>();
            var unitUnderTest  = ExerciseService.ExerciseServiceFromString(string.Empty, exerciseConfiguration, stubExerciseTImer, stubExerciseTrackerService);
            var expectedResult = new Exercise(exerciseConfiguration).ToString();

            // Act
            var actualResult = unitUnderTest.StateToString();

            // Assert
            Assert.AreEqual(expectedResult, actualResult);
        }
Esempio n. 9
0
        public void ExerciseService_Prepare_SignalsExerciseChanged()
        {
            // Arrange
            var stubTimerService           = Substitute.For <ITimerService>();
            var stubExerciseTrackerService = Substitute.For <IExerciseTrackerService>();
            var unitUnderTest        = ExerciseService.ExerciseServiceFromString(string.Empty, CreateTestExerciseConfiguration(), stubTimerService, stubExerciseTrackerService);
            var receivedChangedEvent = new AutoResetEvent(false);

            unitUnderTest.ExerciseChanged += (sender, args) => receivedChangedEvent.Set();

            // Act
            unitUnderTest.PrepareForNewExercise();

            // Assert
            Assert.IsTrue(receivedChangedEvent.WaitOne(5000));
        }
Esempio n. 10
0
        public void ExerciseService_PrepareExerciseServiceFromInvalidString_CreatedDefault()
        {
            // Arrange
            var exerciseConfiguration      = CreateTestExerciseConfiguration();
            var stubExerciseTimer          = Substitute.For <ITimerService>();
            var stubExerciseTrackerService = Substitute.For <IExerciseTrackerService>();
            var invalidString  = "Invalid string";
            var expectedResult = new Exercise(exerciseConfiguration).ToString();

            // Act
            var unitUnderTest = ExerciseService.ExerciseServiceFromString(invalidString, exerciseConfiguration, stubExerciseTimer, stubExerciseTrackerService);
            var actualResult  = unitUnderTest.StateToString();

            // Assert
            Assert.AreEqual(expectedResult, actualResult);
        }
Esempio n. 11
0
        public void ExerciseService_TimerTick_SignalExerciseChanged()
        {
            // Arrange
            var stubTimerService           = Substitute.For <ITimerService>();
            var stubExerciseTrackerService = Substitute.For <IExerciseTrackerService>();
            var unitUnderTest        = ExerciseService.ExerciseServiceFromString(string.Empty, CreateTestExerciseConfiguration(), stubTimerService, stubExerciseTrackerService);
            var receivedChangedEvent = new AutoResetEvent(false);

            unitUnderTest.ExerciseChanged += (sender, args) => receivedChangedEvent.Set();

            unitUnderTest.Run();

            // Act
            stubTimerService.Elapsed += Raise.Event();
            // Assert
            Assert.IsTrue(receivedChangedEvent.WaitOne(5000), "Expected to receive changed event");
        }
Esempio n. 12
0
        public void ExerciseService_Prepare_ExerciseReset()
        {
            // Arrange
            var exerciseConfiguration      = CreateTestExerciseConfiguration();
            var stubTimerService           = Substitute.For <ITimerService>();
            var stubExerciseTrackerService = Substitute.For <IExerciseTrackerService>();

            // TODO: Replace string.Empty with saved state
            var      unitUnderTest    = ExerciseService.ExerciseServiceFromString(string.Empty, exerciseConfiguration, stubTimerService, stubExerciseTrackerService);
            var      expectedExercise = new Exercise(exerciseConfiguration);
            Exercise actualExercise   = null;

            unitUnderTest.ExerciseChanged += (sender, args) => actualExercise = args.Exercise;

            // Act
            unitUnderTest.PrepareForNewExercise();

            // Assert
            Assert.AreEqual(expectedExercise.ToString(), actualExercise?.ToString());
        }