UndoManagerTests.cs 5.8 KB

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