DocumentTests.cs 10 KB

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