UndoManagerTests.cs 5.5 KB

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