StorageBasedChangeTests.cs 6.1 KB

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