UndoManagerTests.cs 5.4 KB

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