LayerStructureTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using PixiEditor.Models.DataHolders;
  2. using PixiEditor.Models.Layers;
  3. using System;
  4. using Xunit;
  5. namespace PixiEditorTests.ModelsTests.DataHoldersTests
  6. {
  7. public class LayerStructureTests
  8. {
  9. [Fact]
  10. public void TestThatAddNewGroupAddsNewGroup()
  11. {
  12. using Document doc = new Document(1, 1);
  13. doc.Layers.Add(new("_testLayer", 1, 1));
  14. var testLayer = doc.Layers[^1];
  15. doc.LayerStructure.AddNewGroup("test", testLayer.GuidValue);
  16. Assert.Single(doc.LayerStructure.Groups);
  17. Assert.Equal(testLayer.GuidValue, doc.LayerStructure.Groups[0].StartLayerGuid);
  18. Assert.Equal(testLayer.GuidValue, doc.LayerStructure.Groups[0].EndLayerGuid);
  19. }
  20. [Fact]
  21. public void TestThatAddNewGroupAddsNewGroupAsASubgroup()
  22. {
  23. using Document doc = new Document(1, 1);
  24. doc.Layers.Add(new("_testLayer", 1, 1));
  25. var testLayer = doc.Layers[^1];
  26. doc.LayerStructure.AddNewGroup("test", testLayer.GuidValue);
  27. doc.LayerStructure.AddNewGroup("test1", testLayer.GuidValue);
  28. Assert.Single(doc.LayerStructure.Groups);
  29. Assert.Single(doc.LayerStructure.Groups[0].Subgroups);
  30. Assert.Equal(testLayer.GuidValue, doc.LayerStructure.Groups[0].StartLayerGuid);
  31. Assert.Equal(testLayer.GuidValue, doc.LayerStructure.Groups[0].EndLayerGuid);
  32. Assert.Equal(testLayer.GuidValue, doc.LayerStructure.Groups[0].Subgroups[0].StartLayerGuid);
  33. Assert.Equal(testLayer.GuidValue, doc.LayerStructure.Groups[0].Subgroups[0].EndLayerGuid);
  34. }
  35. [Fact]
  36. public void TestThatMoveGroupMovesSwapsLayerPlacesWithOtherGroup()
  37. {
  38. using Document doc = new Document(1, 1);
  39. doc.Layers.Add(new Layer("_testLayer", 1, 1));
  40. doc.Layers.Add(new Layer("_testLayer1", 1, 1));
  41. var testLayer = doc.Layers[0];
  42. var testLayer1 = doc.Layers[^1];
  43. doc.LayerStructure.AddNewGroup("test", testLayer.GuidValue);
  44. doc.LayerStructure.AddNewGroup("test1", testLayer1.GuidValue);
  45. Assert.Equal(0, doc.Layers.IndexOf(testLayer));
  46. Assert.Equal(1, doc.Layers.IndexOf(testLayer1));
  47. doc.LayerStructure.MoveGroup(doc.LayerStructure.Groups[0].GroupGuid, 1);
  48. Assert.Equal(1, doc.Layers.IndexOf(testLayer));
  49. Assert.Equal(0, doc.Layers.IndexOf(testLayer1));
  50. }
  51. [Fact]
  52. public void TestThatIsChildOfDetectsNestedGroupCorrectly()
  53. {
  54. using var doc = new Document(1, 1);
  55. LayerStructure ls = new LayerStructure(doc);
  56. Layer testLayer = new Layer("tst", 1, 1);
  57. ls.Groups.Add(new GuidStructureItem("group 1", testLayer.GuidValue));
  58. ls.Groups[0].Subgroups.Add(new GuidStructureItem("group 1 nested", testLayer.GuidValue));
  59. Assert.True(ls.IsChildOf(ls.Groups[0].Subgroups[0], ls.Groups[0]));
  60. Assert.False(ls.IsChildOf(ls.Groups[0], ls.Groups[0].Subgroups[0]));
  61. }
  62. [Fact]
  63. public void TestThatIsChildOfDetectsNestedLayersCorrectly()
  64. {
  65. using var doc = new Document(1, 1);
  66. doc.Layers.Add(new Layer("tst", 1, 1));
  67. Guid testLayerGuid = doc.Layers[0].GuidValue;
  68. LayerStructure ls = new LayerStructure(doc);
  69. ls.AddNewGroup("Test group", testLayerGuid);
  70. ls.AddNewGroup("Test group nested", testLayerGuid);
  71. Assert.True(ls.IsChildOf(testLayerGuid, ls.Groups[0]));
  72. Assert.True(ls.IsChildOf(testLayerGuid, ls.Groups[0].Subgroups[0]));
  73. }
  74. [Fact]
  75. public void TestThatGroupContainsOnlyLayerDetectsOnlySingleLayerCorrectly()
  76. {
  77. using var doc = new Document(1, 1);
  78. doc.Layers.Add(new Layer("layer", 1, 1));
  79. var guid = doc.Layers[0].GuidValue;
  80. doc.LayerStructure.AddNewGroup("layer group", guid);
  81. Assert.True(LayerStructure.GroupContainsOnlyLayer(guid, doc.LayerStructure.Groups[0]));
  82. }
  83. [Fact]
  84. public void TestThatGroupContainsOnlyLayerDetectsOnlySingleLayerThatIsNested()
  85. {
  86. using var doc = new Document(1, 1);
  87. doc.Layers.Add(new Layer("layer", 1, 1));
  88. var guid = doc.Layers[0].GuidValue;
  89. doc.LayerStructure.AddNewGroup("layer group", guid);
  90. doc.LayerStructure.AddNewGroup("layer group nested", guid);
  91. Assert.False(LayerStructure.GroupContainsOnlyLayer(guid, doc.LayerStructure.Groups[0]));
  92. Assert.True(LayerStructure.GroupContainsOnlyLayer(guid, doc.LayerStructure.Groups[0].Subgroups[0]));
  93. }
  94. [Fact]
  95. public void TestThatCloneReturnsSameLayerStructure()
  96. {
  97. using Document doc = new(1, 1);
  98. doc.Layers.Add(new("Test", 1, 1));
  99. doc.Layers.Add(new("Test2", 1, 1));
  100. LayerStructure structure = new(doc);
  101. structure.AddNewGroup("Test group", doc.Layers[0].GuidValue);
  102. var clone = structure.CloneGroups();
  103. Assert.Equal(structure.Groups.Count, clone.Count);
  104. Assert.Single(clone);
  105. Assert.Equal(structure.Groups[0].GroupGuid, clone[0].GroupGuid);
  106. }
  107. [Fact]
  108. public void TestThatGetGroupByGuidReturnsNullForNonExistingGroup()
  109. {
  110. using Document doc = new(1, 1);
  111. doc.Layers.Add(new("Test", 1, 1));
  112. Assert.Null(doc.LayerStructure.GetGroupByGuid(null));
  113. Assert.Null(doc.LayerStructure.GetGroupByGuid(Guid.NewGuid()));
  114. }
  115. [Fact]
  116. public void TestThatGetGroupByGuidReturnsGroupCorrectly()
  117. {
  118. using Document doc = new(1, 1);
  119. doc.Layers.Add(new("Test", 1, 1));
  120. var group = doc.LayerStructure.AddNewGroup("Test group", doc.Layers[0].GuidValue);
  121. Assert.Equal(group.GroupGuid, doc.LayerStructure.GetGroupByGuid(group.GroupGuid).GroupGuid);
  122. }
  123. [Fact]
  124. public void TestThatPreMoveReassignBoundsMakesNestedGroupEmptyAndRemovesItAndParent()
  125. {
  126. using Document doc = new(1, 1);
  127. doc.Layers.Add(new("Test", 1, 1));
  128. doc.LayerStructure.AddNewGroup("Test group", doc.Layers[0].GuidValue);
  129. var group1 = doc.LayerStructure.AddNewGroup("Test group nested", doc.Layers[0].GuidValue);
  130. doc.LayerStructure.Unassign(new GroupData(group1.GroupGuid), doc.Layers[0].GuidValue);
  131. Assert.Empty(doc.LayerStructure.Groups);
  132. }
  133. [Fact]
  134. public void TestThatPostMoveReassignBoundsAssignsNewLayerToGroup()
  135. {
  136. using Document doc = new(1, 1);
  137. doc.Layers.Add(new("Test", 1, 1));
  138. doc.LayerStructure.AddNewGroup("Test group", doc.Layers[0].GuidValue);
  139. var group1 = doc.LayerStructure.AddNewGroup("Test group nested", doc.Layers[0].GuidValue);
  140. doc.Layers.Add(new("Test 1", 1, 1));
  141. var firstLayer = doc.Layers[0];
  142. var layer = doc.Layers[^1];
  143. doc.LayerStructure.Assign(new GroupData(group1.GroupGuid), layer.GuidValue);
  144. Assert.Single(doc.LayerStructure.Groups);
  145. Assert.Single(doc.LayerStructure.Groups[0].Subgroups);
  146. Assert.Equal(layer.GuidValue, doc.LayerStructure.Groups[0].Subgroups[0].EndLayerGuid);
  147. Assert.Equal(firstLayer.GuidValue, doc.LayerStructure.Groups[0].Subgroups[0].StartLayerGuid);
  148. Assert.Equal(layer.GuidValue, doc.LayerStructure.Groups[0].EndLayerGuid);
  149. Assert.Equal(firstLayer.GuidValue, doc.LayerStructure.Groups[0].StartLayerGuid);
  150. }
  151. [Fact]
  152. public void TestThatAssignParentAssignsParent()
  153. {
  154. using Document doc = new(1, 1);
  155. doc.Layers.Add(new Layer("Test", 1, 1));
  156. var firstLayer = doc.Layers[0];
  157. doc.LayerStructure.AddNewGroup("Test group", doc.Layers[0].GuidValue);
  158. doc.Layers.Add(new Layer("Test 1", 1, 1));
  159. var layer = doc.Layers[^1];
  160. doc.LayerStructure.AssignParent(doc.Layers[^1].GuidValue, doc.LayerStructure.Groups[0].GroupGuid);
  161. Assert.Equal(layer.GuidValue, doc.LayerStructure.Groups[0].EndLayerGuid);
  162. Assert.Equal(firstLayer.GuidValue, doc.LayerStructure.Groups[0].StartLayerGuid);
  163. }
  164. [Fact]
  165. public void TestThatAssignParentDeAssignsParentOnNull()
  166. {
  167. using Document doc = new(1, 1);
  168. doc.Layers.Add(new Layer("Test", 1, 1));
  169. var firstLayer = doc.Layers[0];
  170. doc.LayerStructure.AddNewGroup("Test group", doc.Layers[0].GuidValue);
  171. doc.Layers.Add(new Layer("Test 1", 1, 1));
  172. var layer = doc.Layers[^1];
  173. doc.LayerStructure.AssignParent(layer.GuidValue, doc.LayerStructure.Groups[0].GroupGuid);
  174. doc.LayerStructure.AssignParent(layer.GuidValue, null);
  175. Assert.Equal(firstLayer.GuidValue, doc.LayerStructure.Groups[0].EndLayerGuid);
  176. Assert.Equal(firstLayer.GuidValue, doc.LayerStructure.Groups[0].StartLayerGuid);
  177. }
  178. [Fact]
  179. public void TestThatGetGroupLayersReturnsAllLayersInGroup()
  180. {
  181. using Document doc = new(1, 1);
  182. doc.Layers.Add(new Layer("Test", 1, 1));
  183. doc.Layers.Add(new Layer("Test 1", 1, 1));
  184. doc.Layers.Add(new Layer("Test 2", 1, 1));
  185. doc.Layers.Add(new Layer("Test 3", 1, 1));
  186. doc.LayerStructure.AddNewGroup("Test group", doc.Layers[0].GuidValue);
  187. doc.LayerStructure.AssignParent(doc.Layers[1].GuidValue, doc.LayerStructure.Groups[0].GroupGuid);
  188. doc.LayerStructure.AssignParent(doc.Layers[2].GuidValue, doc.LayerStructure.Groups[0].GroupGuid);
  189. doc.LayerStructure.AddNewGroup("Test group", doc.Layers[2].GuidValue);
  190. var layersInGroup = doc.LayerStructure.GetGroupLayers(doc.LayerStructure.Groups[0]);
  191. Assert.Equal(3, layersInGroup.Count);
  192. Assert.Contains(doc.Layers[0], layersInGroup);
  193. Assert.Contains(doc.Layers[1], layersInGroup);
  194. Assert.Contains(doc.Layers[2], layersInGroup);
  195. }
  196. }
  197. }