DocumentTests.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using System;
  2. using System.Windows.Media;
  3. using PixiEditor.Models.Controllers;
  4. using PixiEditor.Models.DataHolders;
  5. using PixiEditor.Models.Enums;
  6. using PixiEditor.Models.Position;
  7. using Xunit;
  8. namespace PixiEditorTests.ModelsTests.DataHoldersTests
  9. {
  10. [Collection("Application collection")]
  11. public class DocumentTests
  12. {
  13. [Theory]
  14. [InlineData(10, 10, 20, 20)]
  15. [InlineData(1, 2, 5, 8)]
  16. [InlineData(20, 20, 10, 10)] // TODO Anchor
  17. public void TestResizeCanvasResizesProperly(int oldWidth, int oldHeight, int newWidth, int newHeight)
  18. {
  19. Document document = new Document(oldWidth, oldHeight);
  20. document.ResizeCanvas(newWidth, newHeight, AnchorPoint.Top | AnchorPoint.Left);
  21. Assert.Equal(newHeight, document.Height);
  22. Assert.Equal(newWidth, document.Width);
  23. }
  24. [Theory]
  25. [InlineData(10, 10, 20, 20)]
  26. [InlineData(5, 8, 10, 16)]
  27. public void TestResizeWorks(int oldWidth, int oldHeight, int newWidth, int newHeight)
  28. {
  29. Document document = new Document(oldWidth, oldHeight);
  30. document.Resize(newWidth, newHeight);
  31. Assert.Equal(newHeight, document.Height);
  32. Assert.Equal(newWidth, document.Width);
  33. }
  34. [Theory]
  35. [InlineData(10, 10, 0, 0)]
  36. [InlineData(50, 50, 10, 49)]
  37. public void TestThatClipCanvasWorksForSingleLayer(int initialWidth, int initialHeight, int additionalPixelX, int additionalPixelY)
  38. {
  39. Document document = new Document(initialWidth, initialHeight);
  40. BitmapManager manager = new BitmapManager
  41. {
  42. ActiveDocument = document
  43. };
  44. manager.ActiveDocument.AddNewLayer("test");
  45. manager.ActiveLayer.SetPixel(
  46. new Coordinates(
  47. (int)Math.Ceiling(initialWidth / 2f),
  48. (int)Math.Ceiling(initialHeight / 2f)), Colors.Black);
  49. manager.ActiveLayer.SetPixel(new Coordinates(additionalPixelX, additionalPixelY), Colors.Black);
  50. document.ClipCanvas();
  51. Assert.Equal(manager.ActiveLayer.Width, document.Width);
  52. Assert.Equal(manager.ActiveLayer.Height, document.Height);
  53. }
  54. [Theory]
  55. [InlineData(10, 10, 0, 0)]
  56. [InlineData(50, 50, 15, 23)]
  57. [InlineData(3, 3, 1, 1)]
  58. [InlineData(1, 1, 0, 0)]
  59. public void TestThatClipCanvasWorksForMultipleLayers(int initialWidth, int initialHeight, int secondLayerPixelX, int secondLayerPixelY)
  60. {
  61. Document document = new Document(initialWidth, initialHeight);
  62. BitmapManager manager = new BitmapManager
  63. {
  64. ActiveDocument = document
  65. };
  66. manager.ActiveDocument.AddNewLayer("test");
  67. manager.ActiveLayer.SetPixel(
  68. new Coordinates(
  69. (int)Math.Ceiling(initialWidth / 2f),
  70. (int)Math.Ceiling(initialHeight / 2f)), Colors.Black); // Set pixel in center
  71. manager.ActiveDocument.AddNewLayer("test2");
  72. manager.ActiveLayer.SetPixel(new Coordinates(secondLayerPixelX, secondLayerPixelY), Colors.Black);
  73. document.ClipCanvas();
  74. int totalWidth = Math.Abs(manager.ActiveDocument.Layers[1].OffsetX +
  75. manager.ActiveDocument.Layers[1].Width - (manager.ActiveDocument.Layers[0].OffsetX +
  76. manager.ActiveDocument.Layers[0].Width)) + 1;
  77. int totalHeight = Math.Abs(manager.ActiveDocument.Layers[1].OffsetY +
  78. manager.ActiveDocument.Layers[1].Height - (manager.ActiveDocument.Layers[0].OffsetY +
  79. manager.ActiveDocument.Layers[0].Height)) + 1;
  80. Assert.Equal(totalWidth, document.Width);
  81. Assert.Equal(totalHeight, document.Height);
  82. }
  83. [Theory]
  84. [InlineData(10, 10)]
  85. [InlineData(11, 11)]
  86. [InlineData(25, 17)]
  87. public void TestThatCenterContentCentersContentForSingleLayer(int docWidth, int docHeight)
  88. {
  89. Document doc = new Document(docWidth, docHeight);
  90. BitmapManager manager = new BitmapManager
  91. {
  92. ActiveDocument = doc
  93. };
  94. manager.ActiveDocument.AddNewLayer("test");
  95. manager.ActiveLayer.SetPixel(new Coordinates(0, 0), Colors.Green);
  96. doc.CenterContent();
  97. Assert.Equal(Math.Floor(docWidth / 2f), manager.ActiveLayer.OffsetX);
  98. Assert.Equal(Math.Floor(docHeight / 2f), manager.ActiveLayer.OffsetY);
  99. }
  100. [Theory]
  101. [InlineData(10, 10)]
  102. [InlineData(11, 11)]
  103. [InlineData(25, 17)]
  104. public void TestThatCenterContentCentersContentForMultipleLayers(int docWidth, int docHeight)
  105. {
  106. Document doc = new Document(docWidth, docHeight);
  107. BitmapManager manager = new BitmapManager
  108. {
  109. ActiveDocument = doc
  110. };
  111. manager.ActiveDocument.AddNewLayer("test");
  112. manager.ActiveLayer.SetPixel(new Coordinates(0, 0), Colors.Green);
  113. manager.ActiveDocument.AddNewLayer("test2");
  114. manager.ActiveLayer.SetPixel(new Coordinates(1, 1), Colors.Green);
  115. doc.CenterContent();
  116. int midWidth = (int)Math.Floor(docWidth / 2f);
  117. int midHeight = (int)Math.Floor(docHeight / 2f);
  118. Assert.Equal(midWidth - 1, manager.ActiveDocument.Layers[0].OffsetX);
  119. Assert.Equal(midHeight - 1, manager.ActiveDocument.Layers[0].OffsetY);
  120. Assert.Equal(midWidth, manager.ActiveDocument.Layers[1].OffsetX);
  121. Assert.Equal(midHeight, manager.ActiveDocument.Layers[1].OffsetY);
  122. }
  123. [Fact]
  124. public void TestThatSetNextActiveLayerSetsLayerBelow()
  125. {
  126. Document doc = new Document(10, 10);
  127. doc.Layers.Add(new PixiEditor.Models.Layers.Layer("Test"));
  128. doc.Layers.Add(new PixiEditor.Models.Layers.Layer("Test 2"));
  129. doc.SetActiveLayer(1);
  130. doc.SetNextLayerAsActive(1);
  131. Assert.False(doc.Layers[1].IsActive);
  132. Assert.True(doc.Layers[0].IsActive);
  133. }
  134. [Fact]
  135. public void TestThatAddNewLayerAddsUndoChange()
  136. {
  137. Document document = new Document(10, 10);
  138. document.AddNewLayer("Test");
  139. document.AddNewLayer("Test2");
  140. Assert.Single(document.UndoManager.UndoStack);
  141. }
  142. [Fact]
  143. public void TestThatAddNewLayerUndoProcessWorks()
  144. {
  145. Document document = new Document(10, 10);
  146. document.AddNewLayer("Test");
  147. document.AddNewLayer("Test2");
  148. document.UndoManager.Undo();
  149. Assert.Single(document.Layers);
  150. }
  151. [Fact]
  152. public void TestThatAddNewLayerRedoProcessWorks()
  153. {
  154. Document document = new Document(10, 10);
  155. document.AddNewLayer("Test");
  156. document.AddNewLayer("Test2");
  157. document.UndoManager.Undo();
  158. document.UndoManager.Redo();
  159. Assert.Equal(2, document.Layers.Count);
  160. }
  161. [Fact]
  162. public void TestThatRemoveLayerUndoProcessWorks()
  163. {
  164. Document document = new Document(10, 10);
  165. document.AddNewLayer("Test");
  166. document.AddNewLayer("Test2");
  167. document.RemoveLayer(1);
  168. document.UndoManager.Undo();
  169. Assert.Equal(2, document.Layers.Count);
  170. }
  171. [Fact]
  172. public void TestThatRemoveLayerRedoProcessWorks()
  173. {
  174. Document document = new Document(10, 10);
  175. document.AddNewLayer("Test");
  176. document.AddNewLayer("Test2");
  177. document.RemoveLayer(1);
  178. document.UndoManager.Undo();
  179. document.UndoManager.Redo();
  180. Assert.Single(document.Layers);
  181. }
  182. [Theory]
  183. [InlineData(2, 0, 1)]
  184. [InlineData(2, 1, -1)]
  185. [InlineData(3, 1, 1)]
  186. [InlineData(3, 2, -2)]
  187. [InlineData(10, 9, -5)]
  188. public void TestThatMoveLayerIndexByWorks(int layersAmount, int index, int amount)
  189. {
  190. Document document = new Document(10, 10);
  191. for (int i = 0; i < layersAmount; i++)
  192. {
  193. document.AddNewLayer("Layer " + i);
  194. }
  195. Guid oldGuid = document.Layers[index].LayerGuid;
  196. document.MoveLayerIndexBy(index, amount);
  197. Assert.Equal(oldGuid, document.Layers[index + amount].LayerGuid);
  198. }
  199. [Fact]
  200. public void TestThatMoveLayerIndexByUndoProcessWorks()
  201. {
  202. Document document = new Document(10, 10);
  203. document.AddNewLayer("Test");
  204. document.AddNewLayer("Test2");
  205. document.MoveLayerIndexBy(0, 1);
  206. document.UndoManager.Undo();
  207. Assert.Equal("Test2", document.Layers[1].Name);
  208. Assert.Equal("Test", document.Layers[0].Name);
  209. }
  210. [Fact]
  211. public void TestThatMoveLayerIndexByRedoProcessWorks()
  212. {
  213. Document document = new Document(10, 10);
  214. document.AddNewLayer("Test");
  215. document.AddNewLayer("Test2");
  216. document.MoveLayerIndexBy(0, 1);
  217. document.UndoManager.Undo();
  218. document.UndoManager.Redo();
  219. Assert.Equal("Test", document.Layers[1].Name);
  220. Assert.Equal("Test2", document.Layers[0].Name);
  221. }
  222. }
  223. }