UndoManagerTests.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using PixiEditor.Models.Controllers;
  2. using PixiEditor.Models.Undo;
  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. using UndoManager undoManager = new UndoManager(this);
  19. Assert.Equal(this, undoManager.MainRoot);
  20. }
  21. [Fact]
  22. public void TestAddToUndoStack()
  23. {
  24. PrepareUndoManagerForTest();
  25. using UndoManager undoManager = new UndoManager(this);
  26. using var change = new Change("ExampleProperty", ExampleProperty, ExampleProperty);
  27. undoManager.AddUndoChange(change);
  28. Assert.True(undoManager.UndoStack.Count == 1);
  29. Assert.True((int)undoManager.UndoStack.Peek().OldValue == ExampleProperty);
  30. }
  31. [Fact]
  32. public void TestThatUndoAddsToRedoStack()
  33. {
  34. PrepareUndoManagerForTest();
  35. using UndoManager undoManager = new UndoManager(this);
  36. using var change = new Change("ExampleProperty", ExampleProperty, ExampleProperty);
  37. undoManager.AddUndoChange(change);
  38. undoManager.Undo();
  39. Assert.True(undoManager.RedoStack.Count == 1);
  40. }
  41. [Fact]
  42. public void TestUndo()
  43. {
  44. PrepareUndoManagerForTest();
  45. using UndoManager undoManager = new UndoManager(this);
  46. using var change = new Change("ExampleProperty", ExampleProperty, 55);
  47. undoManager.AddUndoChange(change);
  48. ExampleProperty = 55;
  49. undoManager.Undo();
  50. Assert.True((int)undoManager.RedoStack.Peek().OldValue == ExampleProperty);
  51. }
  52. [Fact]
  53. public void TestThatRedoAddsToUndoStack()
  54. {
  55. PrepareUndoManagerForTest();
  56. using UndoManager undoManager = new UndoManager(this);
  57. using var change = new Change("ExampleProperty", ExampleProperty, ExampleProperty);
  58. undoManager.AddUndoChange(change);
  59. undoManager.Undo();
  60. undoManager.Redo();
  61. Assert.True(undoManager.UndoStack.Count == 1);
  62. }
  63. [Fact]
  64. public void TestRedo()
  65. {
  66. PrepareUndoManagerForTest();
  67. using UndoManager undoManager = new UndoManager(this);
  68. ExampleProperty = 55;
  69. using var change = new Change("ExampleProperty", 1, ExampleProperty);
  70. undoManager.AddUndoChange(change);
  71. undoManager.Undo();
  72. undoManager.Redo();
  73. Assert.True((int)undoManager.UndoStack.Peek().NewValue == ExampleProperty);
  74. }
  75. [Fact]
  76. public void TestThatUndoManagerUndoAndRedoWithCustomRootCorrectly()
  77. {
  78. PrepareUndoManagerForTest();
  79. using UndoManager undoManager = new UndoManager(this);
  80. TestPropertyClass testProp = new TestPropertyClass();
  81. int newVal = 5;
  82. testProp.IntProperty = newVal;
  83. using var change = new Change("IntProperty", 0, newVal, root: testProp);
  84. undoManager.AddUndoChange(change);
  85. Assert.Equal(newVal, testProp.IntProperty);
  86. undoManager.Undo();
  87. Assert.Equal(0, testProp.IntProperty);
  88. undoManager.Redo();
  89. Assert.Equal(newVal, testProp.IntProperty);
  90. }
  91. [Fact]
  92. public void TestThatMixedProcessOfUndoAndRedoWorks()
  93. {
  94. PrepareUndoManagerForTest();
  95. using UndoManager undoManager = new UndoManager(this);
  96. int newVal = 5;
  97. using var change = new Change(
  98. "ExampleProperty",
  99. ReverseProcess,
  100. new object[] { ExampleProperty },
  101. newVal);
  102. undoManager.AddUndoChange(change);
  103. ExampleProperty = newVal;
  104. Assert.Equal(newVal, ExampleProperty);
  105. undoManager.Undo();
  106. Assert.Equal(1, ExampleProperty);
  107. undoManager.Redo();
  108. Assert.Equal(newVal, ExampleProperty);
  109. }
  110. [Fact]
  111. public void TestThatProcessBasedUndoAndRedoWorks()
  112. {
  113. PrepareUndoManagerForTest();
  114. using UndoManager undoManager = new UndoManager(this);
  115. int newVal = 5;
  116. using var change = new Change(
  117. ReverseProcess,
  118. new object[] { ExampleProperty },
  119. ReverseProcess,
  120. new object[] { newVal });
  121. undoManager.AddUndoChange(change);
  122. ExampleProperty = newVal;
  123. Assert.Equal(newVal, ExampleProperty);
  124. undoManager.Undo();
  125. Assert.Equal(1, ExampleProperty);
  126. undoManager.Redo();
  127. Assert.Equal(newVal, ExampleProperty);
  128. }
  129. [Fact]
  130. public void TestThatNestedPropertyUndoWorks()
  131. {
  132. PrepareUndoManagerForTest();
  133. using UndoManager undoManager = new UndoManager(this);
  134. int newVal = 5;
  135. using var change = new Change("TestPropClass.IntProperty", TestPropClass.IntProperty, newVal);
  136. undoManager.AddUndoChange(change);
  137. TestPropClass.IntProperty = newVal;
  138. Assert.Equal(newVal, TestPropClass.IntProperty);
  139. undoManager.Undo();
  140. Assert.Equal(0, TestPropClass.IntProperty);
  141. undoManager.Redo();
  142. Assert.Equal(newVal, TestPropClass.IntProperty);
  143. }
  144. [Fact]
  145. public void TestThatFindRootProcessWorks()
  146. {
  147. PrepareUndoManagerForTest();
  148. using UndoManager undoManager = new UndoManager(this);
  149. using var change1 = new Change("IntProperty", 0, 5, FindRootProcess, null);
  150. undoManager.AddUndoChange(change1);
  151. Change change = undoManager.UndoStack.Peek();
  152. Assert.Equal(TestPropClass, change.FindRootProcess(change.FindRootProcessArgs));
  153. }
  154. [Fact]
  155. public void TestThatUndoForFindRootProcessWorks()
  156. {
  157. PrepareUndoManagerForTest();
  158. using UndoManager undoManager = new UndoManager(this);
  159. using var change = new Change("IntProperty", 0, 5, FindRootProcess, null);
  160. undoManager.AddUndoChange(change);
  161. TestPropClass.IntProperty = 5;
  162. undoManager.Undo();
  163. Assert.Equal(0, TestPropClass.IntProperty);
  164. }
  165. [Fact]
  166. public void TestThatUndoAndRedoForFindRootProcessWorks()
  167. {
  168. PrepareUndoManagerForTest();
  169. using UndoManager undoManager = new UndoManager(this);
  170. using var change = new Change("IntProperty", 0, 5, FindRootProcess, null);
  171. undoManager.AddUndoChange(change);
  172. TestPropClass.IntProperty = 5;
  173. undoManager.Undo();
  174. Assert.Equal(0, TestPropClass.IntProperty);
  175. undoManager.Redo();
  176. Assert.Equal(5, TestPropClass.IntProperty);
  177. }
  178. private object FindRootProcess(object[] args)
  179. {
  180. return TestPropClass;
  181. }
  182. private void ReverseProcess(object[] args)
  183. {
  184. ExampleProperty = (int)args[0];
  185. }
  186. private void PrepareUndoManagerForTest()
  187. {
  188. ExampleProperty = 1;
  189. TestPropClass = new TestPropertyClass { IntProperty = 0 };
  190. }
  191. }
  192. }