StorageBasedChangeTests.cs 6.1 KB

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