Пример #1
0
        public BlockTimerWindow(BlockItem Model)
        {
            this.InitializeComponent();

            ViewModel = new BlockTimerViewModel(Model);

            // N.B: This is a bit of a hack - the completion of this observable
            // happens regardless of the value; I tried to use OnError for this
            // instead but it actually ended up throwing the exception.
            ViewModel.TimerState
                .Where(x => x == BlockTimerViewState.ShouldCancel)
                .Subscribe(
                    _ => Dispatcher.BeginInvoke(new Action(() => Close())),
                    () => Dispatcher.BeginInvoke(new Action(() => Close())));

            Observable.Merge(
                    Observable.FromEvent<SizeChangedEventArgs>(this, "SizeChanged").Select(_ => new Unit()),
                    ViewModel.WhenAny(x => x.ProgressPercentage, _ => new Unit()))
                .Select(_ => progressParentBorder.ActualWidth * ViewModel.ProgressPercentage)
                .Subscribe(x => progressBorder.Width = x);
        }
        public void ProgressBarShouldntMoveDuringAPause()
        {
            (new TestScheduler()).With(sched => {
                double lastPercentage = -1.0;
                var fixture = new BlockTimerViewModel(new BlockItem() { Description = "Test Item" });

                fixture.WhenAny(x => x.ProgressPercentage, x => x.Value).Subscribe(x => lastPercentage = x);

                fixture.Start.Execute(null);

                // At the beginning we should be zero
                sched.RunToMilliseconds(10);
                lastPercentage.AssertWithinEpsilonOf(0.0);

                // Run to exactly half of the work time 25 mins / 2
                sched.RunToMilliseconds((12 * 60 + 30) * 1000);
                lastPercentage.AssertWithinEpsilonOf(0.5);

                // Simulate hitting the Pause button
                fixture.Pause.Execute(null);

                // Run to 20 minutes; the progress bar shouldn't have moved
                // since we were paused
                sched.RunToMilliseconds(20 * 60 * 1000);
                lastPercentage.AssertWithinEpsilonOf(0.5);

                fixture.Start.Execute(null);

                // Move to 25 minutes; the progress bar should've moved 5
                // minutes worth (remember, since we were paused from 12min
                // to 20min
                sched.RunToMilliseconds(25 * 60 * 1000);
                lastPercentage.AssertWithinEpsilonOf(0.5 + 0.2);
            });
        }
        public void ProgressBarValueIsAccurate()
        {
            (new TestScheduler()).With(sched => {
                double lastPercentage = -1.0;
                var fixture = new BlockTimerViewModel(new BlockItem() { Description = "Test Item" });

                fixture.WhenAny(x => x.ProgressPercentage, x => x.Value).Subscribe(x => lastPercentage = x);

                fixture.Start.Execute(null);

                // At the beginning we should be zero
                sched.RunToMilliseconds(10);
                lastPercentage.AssertWithinEpsilonOf(0.0);

                // Run to exactly half of the work time 25 mins / 2
                sched.RunToMilliseconds((12 * 60 + 30) * 1000);
                lastPercentage.AssertWithinEpsilonOf(0.5);

                // Run to a little before the end, should be near 1.0
                sched.RunToMilliseconds(25 * 60 * 1000 - 10);
                lastPercentage.AssertWithinEpsilonOf(1.0);

                // Step to the beginning of the break, we should've moved back
                // to zero
                sched.RunToMilliseconds(25 * 60 * 1000 + 1010);
                lastPercentage.AssertWithinEpsilonOf(0.0);

                // Finally run to the end of the break
                sched.RunToMilliseconds(30 * 60 * 1000 - 10);
                lastPercentage.AssertWithinEpsilonOf(1.0);
            });
        }