StorageBasedChangeTests.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using PixiEditor.Models.Controllers;
  2. using PixiEditor.Models.DataHolders;
  3. using PixiEditor.Models.Layers;
  4. using PixiEditor.Models.Undo;
  5. using PixiEditorTests.ModelsTests.LayersTests;
  6. using SkiaSharp;
  7. using System;
  8. using System.Collections.ObjectModel;
  9. using System.IO;
  10. using Xunit;
  11. namespace PixiEditorTests.ModelsTests.UndoTests
  12. {
  13. public class StorageBasedChangeTests
  14. {
  15. private const string UndoStoreLocation = "undoStack";
  16. public StorageBasedChangeTests()
  17. {
  18. if (!Directory.Exists(UndoStoreLocation))
  19. {
  20. Directory.CreateDirectory(UndoStoreLocation);
  21. }
  22. }
  23. public Document GenerateTestDocument()
  24. {
  25. Document testDocument = new Document(10, 10);
  26. Surface testBitmap = new Surface(10, 10);
  27. Surface testBitmap2 = new Surface(5, 8);
  28. testBitmap.SetSRGBPixel(0, 0, SKColors.Black);
  29. testBitmap2.SetSRGBPixel(4, 4, SKColors.Blue);
  30. Random random = new Random();
  31. testDocument.Layers = new WpfObservableRangeCollection<Layer>()
  32. {
  33. new Layer("Test layer" + random.Next(int.MinValue, int.MaxValue), testBitmap),
  34. new Layer("Test layer 2" + random.Next(int.MinValue, int.MaxValue), testBitmap2) { Offset = new System.Windows.Thickness(2, 3, 0, 0) }
  35. };
  36. return testDocument;
  37. }
  38. [Fact]
  39. public void TestThatConstructorGeneratesUndoLayersProperly()
  40. {
  41. Document testDocument = GenerateTestDocument();
  42. StorageBasedChange change = new StorageBasedChange(testDocument, testDocument.Layers, UndoStoreLocation);
  43. Assert.Equal(testDocument.Layers.Count, change.StoredLayers.Length);
  44. for (int i = 0; i < change.StoredLayers.Length; i++)
  45. {
  46. Layer testLayer = testDocument.Layers[i];
  47. UndoLayer layer = change.StoredLayers[i];
  48. Assert.Equal(testLayer.Name, layer.Name);
  49. Assert.Equal(testLayer.Width, layer.Width);
  50. Assert.Equal(testLayer.Height, layer.Height);
  51. Assert.Equal(testLayer.IsActive, layer.IsActive);
  52. Assert.Equal(testLayer.IsVisible, layer.IsVisible);
  53. Assert.Equal(testLayer.OffsetX, layer.OffsetX);
  54. Assert.Equal(testLayer.OffsetY, layer.OffsetY);
  55. Assert.Equal(testLayer.MaxWidth, layer.MaxWidth);
  56. Assert.Equal(testLayer.MaxHeight, layer.MaxHeight);
  57. Assert.Equal(testLayer.Opacity, layer.Opacity);
  58. }
  59. }
  60. [Fact]
  61. public void TestThatSaveLayersOnDeviceSavesLayers()
  62. {
  63. Document document = GenerateTestDocument();
  64. StorageBasedChange change = new StorageBasedChange(document, document.Layers, UndoStoreLocation);
  65. foreach (var layer in change.StoredLayers)
  66. {
  67. Assert.True(File.Exists(layer.StoredPngLayerName));
  68. File.Delete(layer.StoredPngLayerName);
  69. }
  70. }
  71. [Fact]
  72. public void TestThatLoadLayersFromDeviceLoadsLayers()
  73. {
  74. Document document = GenerateTestDocument();
  75. StorageBasedChange change = new StorageBasedChange(document, document.Layers, UndoStoreLocation);
  76. Layer[] layers = change.LoadLayersFromDevice();
  77. Assert.Equal(document.Layers.Count, layers.Length);
  78. for (int i = 0; i < document.Layers.Count; i++)
  79. {
  80. Layer expected = document.Layers[i];
  81. Layer actual = layers[i];
  82. LayersTestHelper.LayersAreEqual(expected, actual);
  83. }
  84. }
  85. [Fact]
  86. public void TestThatUndoInvokesLoadFromDeviceAndExecutesProcess()
  87. {
  88. Document document = GenerateTestDocument();
  89. StorageBasedChange change = new StorageBasedChange(document, document.Layers, UndoStoreLocation);
  90. bool undoInvoked = false;
  91. Action<Layer[], UndoLayer[]> testUndoProcess = (layers, data) =>
  92. {
  93. undoInvoked = true;
  94. Assert.Equal(document.Layers.Count, layers.Length);
  95. Assert.Equal(document.Layers.Count, data.Length);
  96. foreach (var undoLayer in data)
  97. {
  98. Assert.False(File.Exists(undoLayer.StoredPngLayerName));
  99. }
  100. };
  101. Action<object[]> testRedoProcess = parameters => { };
  102. Change undoChange = change.ToChange(testUndoProcess, testRedoProcess, null);
  103. UndoManager manager = new UndoManager(this);
  104. manager.AddUndoChange(undoChange);
  105. manager.Undo();
  106. Assert.True(undoInvoked);
  107. }
  108. [Fact]
  109. public void TestThatRedoInvokesSaveToDeviceAndExecutesProcess()
  110. {
  111. Document document = GenerateTestDocument();
  112. StorageBasedChange change = new StorageBasedChange(document, document.Layers, UndoStoreLocation);
  113. bool redoInvoked = false;
  114. Action<Layer[], UndoLayer[]> testUndoProcess = (layers, data) => { };
  115. Action<object[]> testRedoProcess = parameters =>
  116. {
  117. redoInvoked = true;
  118. foreach (var undoLayer in change.StoredLayers)
  119. {
  120. Assert.True(File.Exists(undoLayer.StoredPngLayerName));
  121. Assert.NotNull(parameters);
  122. Assert.Single(parameters);
  123. Assert.IsType<int>(parameters[0]);
  124. Assert.Equal(2, parameters[0]);
  125. }
  126. };
  127. Change undoChange = change.ToChange(testUndoProcess, testRedoProcess, new object[] { 2 });
  128. UndoManager manager = new UndoManager(this);
  129. manager.AddUndoChange(undoChange);
  130. manager.Undo();
  131. manager.Redo();
  132. Assert.True(redoInvoked);
  133. }
  134. }
  135. }