123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- using System;
- using PixiEditor.Models.Controllers;
- using PixiEditor.Models.DataHolders;
- using Xunit;
- namespace PixiEditorTests.ModelsTests.ControllersTests
- {
- public class UndoManagerTests
- {
- public int ExampleProperty { get; set; } = 1;
- public TestPropertyClass TestPropClass { get; set; } = new TestPropertyClass();
- public UndoManagerTests()
- {
- PrepareUndoManagerForTest();
- }
- [Fact]
- public void TestSetRoot()
- {
- PrepareUndoManagerForTest();
- UndoManager.SetMainRoot(null);
- UndoManager.SetMainRoot(this);
- Assert.Equal(this, UndoManager.MainRoot);
- }
- [Fact]
- public void TestAddToUndoStack()
- {
- PrepareUndoManagerForTest();
- UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
- Assert.True(UndoManager.UndoStack.Count == 1);
- Assert.True((int)UndoManager.UndoStack.Peek().OldValue == ExampleProperty);
- }
- [Fact]
- public void TestThatUndoAddsToRedoStack()
- {
- PrepareUndoManagerForTest();
- UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
- UndoManager.Undo();
- Assert.True(UndoManager.RedoStack.Count == 1);
- }
- [Fact]
- public void TestUndo()
- {
- PrepareUndoManagerForTest();
- UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, 55));
- ExampleProperty = 55;
- UndoManager.Undo();
- Assert.True((int)UndoManager.RedoStack.Peek().OldValue == ExampleProperty);
- }
- [Fact]
- public void TestThatRedoAddsToUndoStack()
- {
- PrepareUndoManagerForTest();
- UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
- UndoManager.Undo();
- UndoManager.Redo();
- Assert.True(UndoManager.UndoStack.Count == 1);
- }
- [Fact]
- public void TestRedo()
- {
- PrepareUndoManagerForTest();
- ExampleProperty = 55;
- UndoManager.AddUndoChange(new Change("ExampleProperty", 1, ExampleProperty));
- UndoManager.Undo();
- UndoManager.Redo();
- Assert.True((int)UndoManager.UndoStack.Peek().NewValue == ExampleProperty);
- }
- [Fact]
- public void TestThatUndoManagerUndoAndRedoWithCustomRootCorrectly()
- {
- PrepareUndoManagerForTest();
- TestPropertyClass testProp = new TestPropertyClass();
- int newVal = 5;
- testProp.IntProperty = newVal;
- UndoManager.AddUndoChange(new Change("IntProperty", 0, newVal, root: testProp));
- Assert.Equal(newVal, testProp.IntProperty);
-
- UndoManager.Undo();
- Assert.Equal(0, testProp.IntProperty);
- UndoManager.Redo();
- Assert.Equal(newVal, testProp.IntProperty);
- }
- [Fact]
- public void TestThatMixedProcessOfUndoAndRedoWorks()
- {
- PrepareUndoManagerForTest();
- int newVal = 5;
- UndoManager.AddUndoChange(
- new Change("ExampleProperty",
- ReverseProcess,
- new object[]{ExampleProperty},
- newVal));
- ExampleProperty = newVal;
- Assert.Equal(newVal, ExampleProperty);
- UndoManager.Undo();
- Assert.Equal(1, ExampleProperty);
- UndoManager.Redo();
- Assert.Equal(newVal, ExampleProperty);
- }
- [Fact]
- public void TestThatProcessBasedUndoAndRedoWorks()
- {
- PrepareUndoManagerForTest();
- int newVal = 5;
- UndoManager.AddUndoChange(new Change(ReverseProcess, new object[]{ExampleProperty}, ReverseProcess,
- new object[]{newVal}));
- ExampleProperty = newVal;
- Assert.Equal(newVal, ExampleProperty);
- UndoManager.Undo();
- Assert.Equal(1, ExampleProperty);
- UndoManager.Redo();
- Assert.Equal(newVal, ExampleProperty);
- }
- [Fact]
- public void TestThatNestedPropertyUndoWorks()
- {
- PrepareUndoManagerForTest();
- int newVal = 5;
- UndoManager.AddUndoChange(new Change("TestPropClass.IntProperty", TestPropClass.IntProperty,
- newVal));
- TestPropClass.IntProperty = newVal;
- Assert.Equal(newVal, TestPropClass.IntProperty);
- UndoManager.Undo();
- Assert.Equal(0, TestPropClass.IntProperty);
- UndoManager.Redo();
- Assert.Equal(newVal, TestPropClass.IntProperty);
- }
- private void ReverseProcess(object[] args)
- {
- ExampleProperty = (int)args[0];
- }
- private void PrepareUndoManagerForTest()
- {
- UndoManager.SetMainRoot(this);
- UndoManager.UndoStack.Clear();
- UndoManager.RedoStack.Clear();
- ExampleProperty = 1;
- TestPropClass = new TestPropertyClass {IntProperty = 0};
- }
- }
- public class TestPropertyClass
- {
- public int IntProperty { get; set; } = 0;
- }
- }
|