public void execute_async_composes_actions_appropriately_for_long_durations()
        {
            var delayService = new DelayServiceMock();
            var speechService = new SpeechServiceMock();
            var performedActions = new List<string>();

            delayService
                .When(x => x.DelayAsync(It.IsAny<TimeSpan>(), It.IsAny<CancellationToken>()))
                .Do<TimeSpan, CancellationToken>((duration, ct) => performedActions.Add("Delayed for " + duration))
                .Return(Observable.Return(Unit.Default));

            speechService
                .When(x => x.SpeakAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()))
                .Do<string, CancellationToken>((speechText, cty) => performedActions.Add("Saying '" + speechText + "'"))
                .Return(Observable.Return(Unit.Default));

            var sut = new WaitWithPromptActionBuilder()
                .WithDelayService(delayService)
                .WithSpeechService(speechService)
                .WithDuration(TimeSpan.FromSeconds(5))
                .WithPromptSpeechText("break")
                .Build();

            sut.ExecuteAsync(new ExecutionContext());

            Assert.Equal(7, performedActions.Count);
            Assert.Equal("Saying 'break'", performedActions[0]);
            Assert.Equal("Delayed for 00:00:01", performedActions[1]);
            Assert.Equal("Delayed for 00:00:01", performedActions[2]);
            Assert.Equal("Delayed for 00:00:01", performedActions[3]);
            Assert.Equal("Saying 'ready?'", performedActions[4]);
            Assert.Equal("Delayed for 00:00:01", performedActions[5]);
            Assert.Equal("Delayed for 00:00:01", performedActions[6]);
        }
        public void execute_composes_actions_appropriately_for_short_durations()
        {
            var delayService = new DelayServiceMock();
            var speechService = new SpeechServiceMock();
            var performedActions = new List<string>();

            delayService
                .When(x => x.Delay(It.IsAny<TimeSpan>()))
                .Do<TimeSpan>(duration => performedActions.Add("Delayed for " + duration))
                .Return(Observable.Return(Unit.Default));

            speechService
                .When(x => x.Speak(It.IsAny<string>()))
                .Do<string>(speechText => performedActions.Add("Saying '" + speechText + "'"))
                .Return(Observable.Return(Unit.Default));

            var sut = new WaitWithPromptActionBuilder()
                .WithDelayService(delayService)
                .WithSpeechService(speechService)
                .WithDuration(TimeSpan.FromSeconds(1))
                .WithPromptSpeechText("break")
                .Build();

            sut
                .Execute(new ExecutionContext())
                .Subscribe();

            Assert.Equal(2, performedActions.Count);
            Assert.Equal("Saying 'break'", performedActions[0]);
            Assert.Equal("Delayed for 00:00:01", performedActions[1]);
        }
        public void execute_async_skips_ahead_if_the_context_has_skip_ahead(int delayInMs, int skipInMs, int expectedDelayInMs)
        {
            var delayService = new DelayServiceMock();
            var totalDelay = TimeSpan.Zero;

            delayService
                .When(x => x.DelayAsync(It.IsAny<TimeSpan>(), It.IsAny<CancellationToken>()))
                .Do<TimeSpan, CancellationToken>((t, ct) => totalDelay += t)
                .Return(Observable.Return(Unit.Default));

            var sut = new WaitActionBuilder()
                .WithDelayService(delayService)
                .WithDelay(TimeSpan.FromMilliseconds(delayInMs))
                .Build();

            sut.ExecuteAsync(new ExecutionContext(TimeSpan.FromMilliseconds(skipInMs)));

            Assert.Equal(TimeSpan.FromMilliseconds(expectedDelayInMs), totalDelay);
        }
        public void execute_breaks_for_the_specified_delay(int delayInMs)
        {
            var delayService = new DelayServiceMock();
            var totalDelay = TimeSpan.Zero;

            delayService
                .When(x => x.Delay(It.IsAny<TimeSpan>()))
                .Do<TimeSpan>(t => totalDelay += t)
                .Return(Observable.Return(Unit.Default));

            var sut = new WaitActionBuilder()
                .WithDelayService(delayService)
                .WithDelay(TimeSpan.FromMilliseconds(delayInMs))
                .Build();

            sut
                .Execute(new ExecutionContext())
                .Subscribe();

            Assert.Equal(TimeSpan.FromMilliseconds(delayInMs), totalDelay);
        }
        public void execute_composes_the_appropriate_actions()
        {
            var audioService = new AudioServiceMock();
            var delayService = new DelayServiceMock();
            var actionsPerformed = new List<string>();

            audioService
                .When(x => x.Play(It.IsAny<string>()))
                .Do<string>((resource) => actionsPerformed.Add("Played audio resource " + resource))
                .Return(Observable.Return(Unit.Default));

            delayService
                .When(x => x.Delay(It.IsAny<TimeSpan>()))
                .Do<TimeSpan>(period => actionsPerformed.Add("Delayed for " + period))
                .Return(Observable.Return(Unit.Default));

            var sut = new MetronomeActionBuilder()
                .WithAudioService(audioService)
                .WithDelayService(delayService)
                .WithMetronomeTick(new MetronomeTick(TimeSpan.Zero, MetronomeTickType.Bell))
                .WithMetronomeTick(new MetronomeTick(TimeSpan.FromMilliseconds(10)))
                .WithMetronomeTick(new MetronomeTick(TimeSpan.FromMilliseconds(20)))
                .WithMetronomeTick(new MetronomeTick(TimeSpan.FromMilliseconds(50), MetronomeTickType.Bell))
                .WithMetronomeTick(new MetronomeTick(TimeSpan.FromMilliseconds(30), MetronomeTickType.None))
                .Build();

            sut
                .Execute(new ExecutionContext())
                .Subscribe();

            Assert.Equal(8, actionsPerformed.Count);
            Assert.Equal("Played audio resource MetronomeBell", actionsPerformed[0]);
            Assert.Equal("Delayed for 00:00:00.0100000", actionsPerformed[1]);
            Assert.Equal("Played audio resource MetronomeClick", actionsPerformed[2]);
            Assert.Equal("Delayed for 00:00:00.0200000", actionsPerformed[3]);
            Assert.Equal("Played audio resource MetronomeClick", actionsPerformed[4]);
            Assert.Equal("Delayed for 00:00:00.0500000", actionsPerformed[5]);
            Assert.Equal("Played audio resource MetronomeBell", actionsPerformed[6]);
            Assert.Equal("Delayed for 00:00:00.0300000", actionsPerformed[7]);
        }
        public void execute_async_pauses_if_context_is_paused()
        {
            var delayService = new DelayServiceMock();
            var delayCallCount = 0;

            using (var context = new ExecutionContext())
            {
                delayService
                    .When(x => x.DelayAsync(It.IsAny<TimeSpan>(), It.IsAny<CancellationToken>()))
                    .Do(() => context.IsPaused = delayCallCount++ == 2)
                    .Return(Observable.Return(Unit.Default));

                var sut = new WaitActionBuilder()
                    .WithDelayService(delayService)
                    .WithDelay(TimeSpan.FromSeconds(50))
                    .Build();

                sut.ExecuteAsync(context);

                Assert.True(context.IsPaused);
                delayService
                    .Verify(x => x.DelayAsync(It.IsAny<TimeSpan>(), It.IsAny<CancellationToken>()))
                    .WasCalledExactly(times: 3);
            }
        }
        public void execute_async_reports_progress_correctly_even_if_the_skip_ahead_exceeds_the_wait_duration()
        {
            var delayService = new DelayServiceMock(MockBehavior.Loose);
            var sut = new WaitActionBuilder()
                .WithDelayService(delayService)
                .WithDelay(TimeSpan.FromMilliseconds(50))
                .Build();

            using (var context = new ExecutionContext(TimeSpan.FromMilliseconds(100)))
            {
                Assert.Equal(TimeSpan.Zero, context.Progress);

                sut.ExecuteAsync(context);

                Assert.Equal(TimeSpan.FromMilliseconds(50), context.Progress);
            }
        }
        public void execute_async_reports_progress()
        {
            var delayService = new DelayServiceMock(MockBehavior.Loose);
            var sut = new WaitActionBuilder()
                .WithDelayService(delayService)
                .WithDelay(TimeSpan.FromMilliseconds(50))
                .Build();

            using (var context = new ExecutionContext())
            {
                Assert.Equal(TimeSpan.Zero, context.Progress);

                sut.ExecuteAsync(context);

                Assert.Equal(TimeSpan.FromMilliseconds(50), context.Progress);
            }
        }
        public void execute_async_skips_ahead_if_the_context_has_skip_ahead_even_if_the_context_is_paused(int delayInMs, int skipInMs)
        {
            var delayService = new DelayServiceMock();
            var totalDelay = TimeSpan.Zero;

            delayService
                .When(x => x.DelayAsync(It.IsAny<TimeSpan>(), It.IsAny<CancellationToken>()))
                .Do<TimeSpan, CancellationToken>((t, ct) => totalDelay += t)
                .Return(Observable.Return(Unit.Default));

            var sut = new WaitActionBuilder()
                .WithDelayService(delayService)
                .WithDelay(TimeSpan.FromMilliseconds(delayInMs))
                .Build();

            using (var context = new ExecutionContext(TimeSpan.FromMilliseconds(skipInMs)) { IsPaused = true })
            {
                var progress = context
                    .WhenAnyValue(x => x.Progress)
                    .Skip(1)
                    .CreateCollection();

                sut.ExecuteAsync(context);

                Assert.Equal(TimeSpan.FromMilliseconds(skipInMs), progress.First());
            }
        }