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); } }
private static void TextBox_PropertyChanged(object sender, AvaloniaPropertyChangedEventArgs e) { TextBox textBox = sender as TextBox; if (e.Property == TextBox.TextProperty) { string oldText = e.OldValue as string == null ? string.Empty : (string)e.OldValue; string newText = e.NewValue as string == null ? string.Empty : (string)e.NewValue; _subject.AsObservable().ObserveWithHistory(x => _myHistory.Add(oldText), newText, _stackHistory); _stackHistory.Snapshot(() => { textBox.PropertyChanged -= TextBox_PropertyChanged; //don't allow to switchback to initial value if (string.IsNullOrEmpty(oldText) == false) { textBox.Text = oldText; } textBox.SelectionStart = textBox.SelectionEnd = textBox.Text.Length + 1; textBox.PropertyChanged += TextBox_PropertyChanged; }, () => { textBox.PropertyChanged -= TextBox_PropertyChanged; textBox.Text = newText; textBox.PropertyChanged += TextBox_PropertyChanged; }); } }
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 First_Snapshot_Should_Push_One_Undo_State() { var target = new StackHistory(); using (var helper = new HistoryHelper(target)) { target.Snapshot(() => { }, () => { }); Assert.Single(target.Undos); Assert.Empty(target.Redos); Assert.Equal(new bool[] { true }, helper.CanUndos.ToArray()); Assert.Equal(new bool[] { }, helper.CanRedos.ToArray()); Assert.Equal(new bool[] { 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(_ => { })); } }
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); }