UndoManagerTests.cs 5.3 KB

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