UndoManagerTests.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using PixiEditor.Models.Controllers;
  2. using PixiEditor.Models.DataHolders;
  3. using Xunit;
  4. namespace PixiEditorTests.ModelsTests.ControllersTests
  5. {
  6. public class UndoManagerTests
  7. {
  8. public UndoManagerTests()
  9. {
  10. PrepareUndoManagerForTest();
  11. }
  12. public int ExampleProperty { get; set; } = 1;
  13. public TestPropertyClass TestPropClass { get; set; } = new TestPropertyClass();
  14. [Fact]
  15. public void TestSetRoot()
  16. {
  17. PrepareUndoManagerForTest();
  18. UndoManager undoManager = new UndoManager(this);
  19. Assert.Equal(this, undoManager.MainRoot);
  20. }
  21. [Fact]
  22. public void TestAddToUndoStack()
  23. {
  24. PrepareUndoManagerForTest();
  25. UndoManager undoManager = new UndoManager(this);
  26. undoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
  27. Assert.True(undoManager.UndoStack.Count == 1);
  28. Assert.True((int)undoManager.UndoStack.Peek().OldValue == ExampleProperty);
  29. }
  30. [Fact]
  31. public void TestThatUndoAddsToRedoStack()
  32. {
  33. PrepareUndoManagerForTest();
  34. UndoManager undoManager = new UndoManager(this);
  35. undoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
  36. undoManager.Undo();
  37. Assert.True(undoManager.RedoStack.Count == 1);
  38. }
  39. [Fact]
  40. public void TestUndo()
  41. {
  42. PrepareUndoManagerForTest();
  43. UndoManager undoManager = new UndoManager(this);
  44. undoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, 55));
  45. ExampleProperty = 55;
  46. undoManager.Undo();
  47. Assert.True((int)undoManager.RedoStack.Peek().OldValue == ExampleProperty);
  48. }
  49. [Fact]
  50. public void TestThatRedoAddsToUndoStack()
  51. {
  52. PrepareUndoManagerForTest();
  53. UndoManager undoManager = new UndoManager(this);
  54. undoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
  55. undoManager.Undo();
  56. undoManager.Redo();
  57. Assert.True(undoManager.UndoStack.Count == 1);
  58. }
  59. [Fact]
  60. public void TestRedo()
  61. {
  62. PrepareUndoManagerForTest();
  63. UndoManager undoManager = new UndoManager(this);
  64. ExampleProperty = 55;
  65. undoManager.AddUndoChange(new Change("ExampleProperty", 1, ExampleProperty));
  66. undoManager.Undo();
  67. undoManager.Redo();
  68. Assert.True((int)undoManager.UndoStack.Peek().NewValue == ExampleProperty);
  69. }
  70. [Fact]
  71. public void TestThatUndoManagerUndoAndRedoWithCustomRootCorrectly()
  72. {
  73. PrepareUndoManagerForTest();
  74. UndoManager undoManager = new UndoManager(this);
  75. TestPropertyClass testProp = new TestPropertyClass();
  76. int newVal = 5;
  77. testProp.IntProperty = newVal;
  78. undoManager.AddUndoChange(new Change("IntProperty", 0, newVal, root: testProp));
  79. Assert.Equal(newVal, testProp.IntProperty);
  80. undoManager.Undo();
  81. Assert.Equal(0, testProp.IntProperty);
  82. undoManager.Redo();
  83. Assert.Equal(newVal, testProp.IntProperty);
  84. }
  85. [Fact]
  86. public void TestThatMixedProcessOfUndoAndRedoWorks()
  87. {
  88. PrepareUndoManagerForTest();
  89. UndoManager undoManager = new UndoManager(this);
  90. int newVal = 5;
  91. undoManager.AddUndoChange(
  92. new Change(
  93. "ExampleProperty",
  94. ReverseProcess,
  95. new object[] { ExampleProperty },
  96. newVal));
  97. ExampleProperty = newVal;
  98. Assert.Equal(newVal, ExampleProperty);
  99. undoManager.Undo();
  100. Assert.Equal(1, ExampleProperty);
  101. undoManager.Redo();
  102. Assert.Equal(newVal, ExampleProperty);
  103. }
  104. [Fact]
  105. public void TestThatProcessBasedUndoAndRedoWorks()
  106. {
  107. PrepareUndoManagerForTest();
  108. UndoManager undoManager = new UndoManager(this);
  109. int newVal = 5;
  110. undoManager.AddUndoChange(new Change(
  111. ReverseProcess,
  112. new object[] { ExampleProperty },
  113. ReverseProcess,
  114. new object[] { newVal }));
  115. ExampleProperty = newVal;
  116. Assert.Equal(newVal, ExampleProperty);
  117. undoManager.Undo();
  118. Assert.Equal(1, ExampleProperty);
  119. undoManager.Redo();
  120. Assert.Equal(newVal, ExampleProperty);
  121. }
  122. [Fact]
  123. public void TestThatNestedPropertyUndoWorks()
  124. {
  125. PrepareUndoManagerForTest();
  126. UndoManager undoManager = new UndoManager(this);
  127. int newVal = 5;
  128. undoManager.AddUndoChange(new Change("TestPropClass.IntProperty", TestPropClass.IntProperty, newVal));
  129. TestPropClass.IntProperty = newVal;
  130. Assert.Equal(newVal, TestPropClass.IntProperty);
  131. undoManager.Undo();
  132. Assert.Equal(0, TestPropClass.IntProperty);
  133. undoManager.Redo();
  134. Assert.Equal(newVal, TestPropClass.IntProperty);
  135. }
  136. private void ReverseProcess(object[] args)
  137. {
  138. ExampleProperty = (int)args[0];
  139. }
  140. private void PrepareUndoManagerForTest()
  141. {
  142. ExampleProperty = 1;
  143. TestPropClass = new TestPropertyClass { IntProperty = 0 };
  144. }
  145. }
  146. }