123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- using PixiEditor.Models.Controllers;
- using PixiEditor.Models.DataHolders;
- using Xunit;
- namespace PixiEditorTests.ModelsTests.ControllersTests
- {
- public class UndoManagerTests
- {
- public UndoManagerTests()
- {
- PrepareUndoManagerForTest();
- }
- public int ExampleProperty { get; set; } = 1;
- public TestPropertyClass TestPropClass { get; set; } = new TestPropertyClass();
- [Fact]
- public void TestSetRoot()
- {
- PrepareUndoManagerForTest();
- UndoManager undoManager = new UndoManager(this);
- Assert.Equal(this, undoManager.MainRoot);
- }
- [Fact]
- public void TestAddToUndoStack()
- {
- PrepareUndoManagerForTest();
- UndoManager undoManager = new UndoManager(this);
- 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 undoManager = new UndoManager(this);
- undoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
- undoManager.Undo();
- Assert.True(undoManager.RedoStack.Count == 1);
- }
- [Fact]
- public void TestUndo()
- {
- PrepareUndoManagerForTest();
- UndoManager undoManager = new UndoManager(this);
- 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 undoManager = new UndoManager(this);
- undoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
- undoManager.Undo();
- undoManager.Redo();
- Assert.True(undoManager.UndoStack.Count == 1);
- }
- [Fact]
- public void TestRedo()
- {
- PrepareUndoManagerForTest();
- UndoManager undoManager = new UndoManager(this);
- 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();
- UndoManager undoManager = new UndoManager(this);
- 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();
- UndoManager undoManager = new UndoManager(this);
- 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();
- UndoManager undoManager = new UndoManager(this);
- 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();
- UndoManager undoManager = new UndoManager(this);
- 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()
- {
- ExampleProperty = 1;
- TestPropClass = new TestPropertyClass { IntProperty = 0 };
- }
- }
- }
|