Ejemplo n.º 1
0
        public void ObserveWithHistory_Sets_CurrentValue()
        {
            var history = new StackHistory();

            using (var subject = new Subject <int>())
            {
                var target       = new List <int>();
                var initialValue = 10;

                using (subject.AsObservable().ObserveWithHistory(x => target.Add(x), currentValue: initialValue, history: history))
                {
                    subject.OnNext(initialValue); // empty -> 10 (the initial state of variable)
                    subject.OnNext(2);            // empty -> 10 -> 2
                    subject.OnNext(3);            // empty -> 10 -> 2 -> 3

                    history.Undo();               // 3 -> 2
                    history.Undo();               // 2 -> 10 (finally restores initial state)

                    Assert.Equal(0, history.Undos.Count);
                    Assert.Equal(2, history.Redos.Count);
                    Assert.Equal(new int[] { 2, 10 }, target);

                    history.Redo(); // 10 -> 2
                    history.Redo(); // 2 -> 3

                    Assert.Equal(2, history.Undos.Count);
                    Assert.Equal(0, history.Redos.Count);
                    Assert.Equal(new int[] { 2, 10, 2, 3 }, target);
                }
            }
        }
Ejemplo n.º 2
0
        public void ReplaceWithHistory_Replaces_Items_List_Middle()
        {
            var history  = new StackHistory();
            var target   = new ObservableCollection <Item>();
            var item0    = new Item("item0");
            var item1    = new Item("item1");
            var item2    = new Item("item2");
            var item3    = new Item("item3");
            var replace1 = new Item("replace1");
            var replace2 = new Item("replace2");

            target.Add(item0);
            target.Add(item1);
            target.Add(item2);
            target.Add(item3);
            Assert.Equal(4, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(item1, target[1]);
            Assert.Equal(item2, target[2]);
            Assert.Equal(item3, target[3]);

            target.ReplaceWithHistory(1, replace1, history);
            target.ReplaceWithHistory(2, replace2, history);
            Assert.Equal(4, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(replace1, target[1]);
            Assert.Equal(replace2, target[2]);
            Assert.Equal(item3, target[3]);

            history.Undo();
            Assert.Equal(4, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(replace1, target[1]);
            Assert.Equal(item2, target[2]);
            Assert.Equal(item3, target[3]);

            history.Undo();
            Assert.Equal(4, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(item1, target[1]);
            Assert.Equal(item2, target[2]);
            Assert.Equal(item3, target[3]);

            history.Redo();
            Assert.Equal(4, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(replace1, target[1]);
            Assert.Equal(item2, target[2]);
            Assert.Equal(item3, target[3]);

            history.Redo();
            Assert.Equal(4, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(replace1, target[1]);
            Assert.Equal(replace2, target[2]);
            Assert.Equal(item3, target[3]);
        }
Ejemplo n.º 3
0
        public void RemoveWithHistory_Removes_Index_Item_List_Middle()
        {
            var history = new StackHistory();
            var target  = new ObservableCollection <Item>();
            var item0   = new Item("item0");
            var item1   = new Item("item1");
            var item2   = new Item("item2");

            target.Add(item0);
            target.Add(item1);
            target.Add(item2);
            Assert.Equal(3, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(item1, target[1]);
            Assert.Equal(item2, target[2]);

            target.RemoveWithHistory(1, history);
            Assert.Equal(2, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(item2, target[1]);

            target.RemoveWithHistory(1, history);
            Assert.Equal(1, target.Count);
            Assert.Equal(item0, target[0]);

            target.RemoveWithHistory(0, history);
            Assert.Equal(0, target.Count);

            history.Undo();
            Assert.Equal(1, target.Count);
            Assert.Equal(item0, target[0]);

            history.Undo();
            Assert.Equal(2, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(item2, target[1]);

            history.Undo();
            Assert.Equal(3, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(item1, target[1]);
            Assert.Equal(item2, target[2]);

            history.Redo();
            Assert.Equal(2, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(item2, target[1]);

            history.Redo();
            Assert.Equal(1, target.Count);
            Assert.Equal(item0, target[0]);

            history.Redo();
            Assert.Equal(0, target.Count);
        }
Ejemplo n.º 4
0
        public void ClearWithHistory_Removes_All_Items_Single_Item()
        {
            var history = new StackHistory();
            var target  = new ObservableCollection <Item>();
            var item0   = new Item("item0");

            target.Add(item0);
            Assert.Equal(1, target.Count);
            Assert.Equal(0, history.Undos.Count);
            Assert.Equal(0, history.Redos.Count);

            target.ClearWithHistory(history);
            Assert.Equal(0, target.Count);
            Assert.Equal(1, history.Undos.Count);
            Assert.Equal(0, history.Redos.Count);

            history.Undo();
            Assert.Equal(1, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(0, history.Undos.Count);
            Assert.Equal(1, history.Redos.Count);

            history.Redo();
            Assert.Equal(0, target.Count);
            Assert.Equal(1, history.Undos.Count);
            Assert.Equal(0, history.Redos.Count);
        }
        public void Invoking_Redo_Should_Invoke_Redo_Action_And_Push_State_To_Undos()
        {
            int undoCount = 0;
            int redoCount = 0;
            var target    = new StackHistory();

            using (var helper = new HistoryHelper(target))
            {
                target.Snapshot(() => undoCount++, () => redoCount++);
                var undo1   = target.Undos.Peek();
                var result1 = target.Undo();
                var redo1   = target.Redos.Peek();
                var result2 = target.Redo();
                Assert.Single(target.Undos);
                Assert.Empty(target.Redos);
                Assert.True(result1);
                Assert.True(result2);
                Assert.Equal(new bool[] { true, false, true }, helper.CanUndos.ToArray());
                Assert.Equal(new bool[] { true, false }, helper.CanRedos.ToArray());
                Assert.Equal(new bool[] { true, true, true }, helper.CanClears.ToArray());

                var undo2 = target.Undos.Peek();
                Assert.Equal(undo1, undo2);
                Assert.Equal(undo1, redo1);
                Assert.Equal(1, undoCount);
                Assert.Equal(1, redoCount);
                Assert.True(result1);
            }
        }
Ejemplo n.º 6
0
        public void AddWithHistory_Adds_Items_As_Last()
        {
            var history = new StackHistory();
            var target  = new ObservableCollection <Item>();
            var item0   = new Item("item0");
            var item1   = new Item("item1");
            var item2   = new Item("item2");

            target.AddWithHistory(item0, history);
            target.AddWithHistory(item1, history);
            target.AddWithHistory(item2, history);
            Assert.Equal(3, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(item1, target[1]);
            Assert.Equal(item2, target[2]);

            history.Undo();
            Assert.Equal(2, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(item1, target[1]);

            history.Undo();
            Assert.Single(target);
            Assert.Equal(item0, target[0]);

            history.Undo();
            Assert.Empty(target);

            history.Redo();
            Assert.Single(target);
            Assert.Equal(item0, target[0]);

            history.Redo();
            Assert.Equal(2, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(item1, target[1]);

            history.Redo();
            Assert.Equal(3, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(item1, target[1]);
            Assert.Equal(item2, target[2]);
        }
Ejemplo n.º 7
0
        public void InsertWithHistory_Inserts_Items_List_Head()
        {
            var history = new StackHistory();
            var target  = new ObservableCollection <Item>();
            var item0   = new Item("item0");
            var item1   = new Item("item1");
            var item2   = new Item("item2");

            target.InsertWithHistory(0, item0, history);
            target.InsertWithHistory(0, item1, history);
            target.InsertWithHistory(0, item2, history);
            Assert.Equal(3, target.Count);
            Assert.Equal(item2, target[0]);
            Assert.Equal(item1, target[1]);
            Assert.Equal(item0, target[2]);

            history.Undo();
            Assert.Equal(2, target.Count);
            Assert.Equal(item1, target[0]);
            Assert.Equal(item0, target[1]);

            history.Undo();
            Assert.Equal(1, target.Count);
            Assert.Equal(item0, target[0]);

            history.Undo();
            Assert.Equal(0, target.Count);

            history.Redo();
            Assert.Equal(1, target.Count);
            Assert.Equal(item0, target[0]);

            history.Redo();
            Assert.Equal(2, target.Count);
            Assert.Equal(item1, target[0]);
            Assert.Equal(item0, target[1]);

            history.Redo();
            Assert.Equal(3, target.Count);
            Assert.Equal(item2, target[0]);
            Assert.Equal(item1, target[1]);
            Assert.Equal(item0, target[2]);
        }
        public void Invoking_Undo_Should_Not_Throw_When_Undos_Are_Empty()
        {
            var target = new StackHistory();

            using (var helper = new HistoryHelper(target))
            {
                var result = target.Undo();
                Assert.False(result);
                Assert.Equal(new bool[] { }, helper.CanUndos.ToArray());
                Assert.Equal(new bool[] { }, helper.CanRedos.ToArray());
                Assert.Equal(new bool[] { }, helper.CanClears.ToArray());
            }
        }
        private static void TextBoxRollbackValue(object sender, KeyEventArgs e)
        {
            var textbox = sender as TextBox;

            if (textbox == null)
            {
                return;
            }

            if (e.Key == Key.Escape)
            {
                var result = _stackHistory.Undo();

                e.Handled = true;
            }
        }
        public void Undo_Sets_IsPaused_True_While_Invoking_Undo_Redo_State()
        {
            var target = new StackHistory();

            target.Snapshot(
                undo: () => Assert.True(target.IsPaused),
                redo: () => Assert.True(target.IsPaused));

            Assert.False(target.IsPaused);
            target.Undo();
            Assert.False(target.IsPaused);

            Assert.False(target.IsPaused);
            target.Redo();
            Assert.False(target.IsPaused);
        }
Ejemplo n.º 11
0
        public void AddWithHistory_Adds_Item_Empty_List()
        {
            var history = new StackHistory();
            var target  = new ObservableCollection <Item>();
            var item0   = new Item("item0");

            target.AddWithHistory(item0, history);
            Assert.Single(target);
            Assert.Equal(item0, target[0]);

            history.Undo();
            Assert.Empty(target);

            history.Redo();
            Assert.Single(target);
            Assert.Equal(item0, target[0]);
        }
Ejemplo n.º 12
0
        public void InsertWithHistory_Inserts_Item_Empty_List()
        {
            var history = new StackHistory();
            var target  = new ObservableCollection <Item>();
            var item0   = new Item("item0");

            target.InsertWithHistory(0, item0, history);
            Assert.Equal(1, target.Count);
            Assert.Equal(item0, target[0]);

            history.Undo();
            Assert.Equal(0, target.Count);

            history.Redo();
            Assert.Equal(1, target.Count);
            Assert.Equal(item0, target[0]);
        }
Ejemplo n.º 13
0
        public void RemoveWithHistory_Removes_Index_Empty_List()
        {
            var history = new StackHistory();
            var target  = new ObservableCollection <Item>();
            var item0   = new Item("item0");

            target.Add(item0);
            Assert.Equal(1, target.Count);
            Assert.Equal(item0, target[0]);

            target.RemoveWithHistory(0, history);
            Assert.Equal(0, target.Count);

            history.Undo();
            Assert.Equal(1, target.Count);
            Assert.Equal(item0, target[0]);

            history.Redo();
            Assert.Equal(0, target.Count);
        }
Ejemplo n.º 14
0
        public void ReplaceWithHistory_Replaces_Item_Empty_List()
        {
            var history = new StackHistory();
            var target  = new ObservableCollection <Item>();
            var item0   = new Item("item0");
            var item1   = new Item("item1");

            target.Add(item0);
            Assert.Single(target);
            Assert.Equal(item0, target[0]);

            target.ReplaceWithHistory(0, item1, history);
            Assert.Single(target);
            Assert.Equal(item1, target[0]);

            history.Undo();
            Assert.Single(target);
            Assert.Equal(item0, target[0]);

            history.Redo();
            Assert.Single(target);
            Assert.Equal(item1, target[0]);
        }
        public void Snapshot_Should_Clear_Redos()
        {
            var target = new StackHistory();

            using (var helper = new HistoryHelper(target))
            {
                target.Snapshot(() => { }, () => { });
                Assert.Single(target.Undos);
                Assert.Empty(target.Redos);

                var result = target.Undo();
                Assert.Empty(target.Undos);
                Assert.Single(target.Redos);
                Assert.True(result);

                target.Snapshot(() => { }, () => { });
                Assert.Single(target.Undos);
                Assert.Empty(target.Redos);
                Assert.Equal(new bool[] { true, false, true }, helper.CanUndos.ToArray());
                Assert.Equal(new bool[] { true, false }, helper.CanRedos.ToArray());
                Assert.Equal(new bool[] { true, true, true }, helper.CanClears.ToArray());
            }
        }
        public void Dispose_Should_Release_Allocated_Resources()
        {
            var target = new StackHistory();

            using (var helper = new HistoryHelper(target))
            {
                target.Snapshot(() => { }, () => { });
                target.Snapshot(() => { }, () => { });
                var result = target.Undo();
                Assert.Single(target.Undos);
                Assert.Single(target.Redos);
                Assert.True(result);

                target.Dispose();

                Assert.Empty(target.Undos);
                Assert.Empty(target.Redos);

                Assert.Throws <ObjectDisposedException>(() => target.CanUndo.Subscribe(_ => { }));
                Assert.Throws <ObjectDisposedException>(() => target.CanRedo.Subscribe(_ => { }));
                Assert.Throws <ObjectDisposedException>(() => target.CanClear.Subscribe(_ => { }));
            }
        }
        public void Dispose_Should_Release_Allocated_Resources()
        {
            var target = new StackHistory();

            using (var helper = new HistoryHelper(target))
            {
                target.Snapshot(() => { }, () => { });
                target.Snapshot(() => { }, () => { });
                var result = target.Undo();
                Assert.Equal(1, target.Undos.Count);
                Assert.Equal(1, target.Redos.Count);
                Assert.Equal(true, result);

                target.Dispose();

                Assert.Null(target.Undos);
                Assert.Null(target.Redos);

                Assert.Throws(typeof(ObjectDisposedException), () => target.CanUndo.Subscribe(_ => { }));
                Assert.Throws(typeof(ObjectDisposedException), () => target.CanRedo.Subscribe(_ => { }));
                Assert.Throws(typeof(ObjectDisposedException), () => target.CanClear.Subscribe(_ => { }));
            }
        }
Ejemplo n.º 18
0
        public void ClearWithHistory_Removes_All_Items_Multiple_Item()
        {
            var history = new StackHistory();
            var target  = new ObservableCollection <Item>();
            var item0   = new Item("item0");
            var item1   = new Item("item1");
            var item2   = new Item("item2");

            target.Add(item0);
            target.Add(item1);
            target.Add(item2);
            Assert.Equal(3, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(item1, target[1]);
            Assert.Equal(item2, target[2]);
            Assert.Empty(history.Undos);
            Assert.Empty(history.Redos);

            target.ClearWithHistory(history);
            Assert.Empty(target);
            Assert.Single(history.Undos);
            Assert.Empty(history.Redos);

            history.Undo();
            Assert.Equal(3, target.Count);
            Assert.Equal(item0, target[0]);
            Assert.Equal(item1, target[1]);
            Assert.Equal(item2, target[2]);
            Assert.Empty(history.Undos);
            Assert.Single(history.Redos);

            history.Redo();
            Assert.Empty(target);
            Assert.Single(history.Undos);
            Assert.Empty(history.Redos);
        }
        public LineShapeViewModel(LineShape line, IHistory history)
        {
            Disposable = new CompositeDisposable();

            var lineHistoryScope = new StackHistory().AddTo(this.Disposable);

            this.Name = line.ToReactivePropertyAsSynchronized(l => l.Name)
                        .SetValidateNotifyError(name => string.IsNullOrWhiteSpace(name) ? "Name can not be null or whitespace." : null)
                        .AddTo(this.Disposable);

            var startInitialValue = new PointShapeViewModel(line.Start, lineHistoryScope).AddTo(this.Disposable);

            this.Start = new ReactiveProperty <PointShapeViewModel>(startInitialValue)
                         .SetValidateNotifyError(start => start == null ? "Point can not be null." : null)
                         .AddTo(this.Disposable);

            var endInitialValue = new PointShapeViewModel(line.End, lineHistoryScope).AddTo(this.Disposable);

            this.End = new ReactiveProperty <PointShapeViewModel>(endInitialValue)
                       .SetValidateNotifyError(end => end == null ? "Point can not be null." : null)
                       .AddTo(this.Disposable);

            this.Name.ObserveWithHistory(name => line.Name = name, line.Name, lineHistoryScope).AddTo(this.Disposable);

            this.DeleteCommand = new ReactiveCommand();
            this.DeleteCommand.Subscribe((x) => Delete(line, history)).AddTo(this.Disposable);

            UndoCommand = new ReactiveCommand(lineHistoryScope.CanUndo, false);
            UndoCommand.Subscribe(_ => lineHistoryScope.Undo()).AddTo(this.Disposable);

            RedoCommand = new ReactiveCommand(lineHistoryScope.CanRedo, false);
            RedoCommand.Subscribe(_ => lineHistoryScope.Redo()).AddTo(this.Disposable);

            ClearCommand = new ReactiveCommand(lineHistoryScope.CanClear, false);
            ClearCommand.Subscribe(_ => lineHistoryScope.Clear()).AddTo(this.Disposable);
        }