1 // Aseprite Document Library
2 // Copyright (c) 2001-2016 David Capello
3 //
4 // This file is released under the terms of the MIT license.
5 // Read LICENSE.txt for more information.
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include <gtest/gtest.h>
12 
13 #include "doc/cel.h"
14 #include "doc/cels_range.h"
15 #include "doc/layer.h"
16 #include "doc/pixel_format.h"
17 #include "doc/sprite.h"
18 
19 using namespace doc;
20 
TEST(Sprite,Layers)21 TEST(Sprite, Layers)
22 {
23   Sprite* spr = new Sprite(IMAGE_RGB, 32, 32, 256);
24 
25   LayerImage* lay1 = new LayerImage(spr);
26   LayerImage* lay2 = new LayerImage(spr);
27   LayerImage* lay3 = new LayerImage(spr);
28   LayerImage* lay4 = new LayerImage(spr);
29   LayerImage* lay5 = new LayerImage(spr);
30   LayerImage* lay6 = new LayerImage(spr);
31   LayerImage* lay7 = new LayerImage(spr);
32   LayerGroup* grp1 = new LayerGroup(spr);
33   LayerGroup* grp2 = new LayerGroup(spr);
34   LayerGroup* grp3 = new LayerGroup(spr);
35 
36   grp1->setVisible(false);
37   lay5->setVisible(false);
38   grp2->setCollapsed(true);
39 
40   ;; grp2->addLayer(lay5);
41   ;;;; grp3->addLayer(lay7);
42   ;;;; grp3->addLayer(lay6);
43   ;; grp2->addLayer(grp3);
44   spr->root()->addLayer(grp2);
45   spr->root()->addLayer(lay2);
46   ;; grp1->addLayer(lay4);
47   ;; grp1->addLayer(lay3);
48   spr->root()->addLayer(grp1);
49   spr->root()->addLayer(lay1);
50 
51   auto all = spr->allLayers();
52   ASSERT_EQ(10, all.size());
53   EXPECT_EQ(lay5, all[0]);
54   EXPECT_EQ(lay7, all[1]);
55   EXPECT_EQ(lay6, all[2]);
56   EXPECT_EQ(grp3, all[3]);
57   EXPECT_EQ(grp2, all[4]);
58   EXPECT_EQ(lay2, all[5]);
59   EXPECT_EQ(lay4, all[6]);
60   EXPECT_EQ(lay3, all[7]);
61   EXPECT_EQ(grp1, all[8]);
62   EXPECT_EQ(lay1, all[9]);
63 
64   auto vis = spr->allVisibleLayers();
65   ASSERT_EQ(6, vis.size());
66   EXPECT_EQ(lay7, vis[0]);
67   EXPECT_EQ(lay6, vis[1]);
68   EXPECT_EQ(grp3, vis[2]);
69   EXPECT_EQ(grp2, vis[3]);
70   EXPECT_EQ(lay2, vis[4]);
71   EXPECT_EQ(lay1, vis[5]);
72 
73   auto bro = spr->allBrowsableLayers();
74   ASSERT_EQ(6, bro.size());
75   EXPECT_EQ(grp2, bro[0]);
76   EXPECT_EQ(lay2, bro[1]);
77   EXPECT_EQ(lay4, bro[2]);
78   EXPECT_EQ(lay3, bro[3]);
79   EXPECT_EQ(grp1, bro[4]);
80   EXPECT_EQ(lay1, bro[5]);
81 }
82 
83 //            frames
84 //            0 1 2
85 // root
86 // - lay1:    A~~~B
87 // - lay2:    C D~E
88 // - grp1:
89 //   - lay3:  F G~H
TEST(Sprite,CelsRange)90 TEST(Sprite, CelsRange)
91 {
92   Sprite* spr = new Sprite(IMAGE_RGB, 32, 32, 256);
93   spr->setTotalFrames(3);
94 
95   LayerImage* lay1 = new LayerImage(spr);
96   LayerImage* lay2 = new LayerImage(spr);
97   LayerGroup* grp1 = new LayerGroup(spr);
98   LayerImage* lay3 = new LayerImage(spr);
99   spr->root()->addLayer(lay1);
100   spr->root()->addLayer(lay2);
101   spr->root()->addLayer(grp1);
102   grp1->addLayer(lay3);
103 
104   ImageRef imgA(Image::create(IMAGE_RGB, 32, 32));
105   Cel* celA = new Cel(frame_t(0), imgA);
106   Cel* celB = Cel::createLink(celA);
107   celB->setFrame(frame_t(2));
108   lay1->addCel(celA);
109   lay1->addCel(celB);
110 
111   ImageRef imgC(Image::create(IMAGE_RGB, 32, 32));
112   Cel* celC = new Cel(frame_t(0), imgC);
113   Cel* celD = Cel::createCopy(celC);
114   Cel* celE = Cel::createLink(celD);
115   celD->setFrame(frame_t(1));
116   celE->setFrame(frame_t(2));
117   lay2->addCel(celC);
118   lay2->addCel(celD);
119   lay2->addCel(celE);
120 
121   ImageRef imgF(Image::create(IMAGE_RGB, 32, 32));
122   ImageRef imgG(Image::create(IMAGE_RGB, 32, 32));
123   Cel* celF = new Cel(frame_t(0), imgF);
124   Cel* celG = new Cel(frame_t(1), imgG);
125   Cel* celH = Cel::createLink(celG);
126   celH->setFrame(frame_t(2));
127   lay3->addCel(celF);
128   lay3->addCel(celG);
129   lay3->addCel(celH);
130 
131   int i = 0;
132   for (Cel* cel : spr->cels()) {
133     switch (i) {
134       case 0: EXPECT_EQ(cel, celA); break;
135       case 1: EXPECT_EQ(cel, celB); break;
136       case 2: EXPECT_EQ(cel, celC); break;
137       case 3: EXPECT_EQ(cel, celD); break;
138       case 4: EXPECT_EQ(cel, celE); break;
139       case 5: EXPECT_EQ(cel, celF); break;
140       case 6: EXPECT_EQ(cel, celG); break;
141       case 7: EXPECT_EQ(cel, celH); break;
142     }
143     ++i;
144   }
145   EXPECT_EQ(8, i);
146 
147   i = 0;
148   for (Cel* cel : spr->uniqueCels()) {
149     switch (i) {
150       case 0: EXPECT_EQ(cel, celA); break;
151       case 1: EXPECT_EQ(cel, celC); break;
152       case 2: EXPECT_EQ(cel, celD); break;
153       case 3: EXPECT_EQ(cel, celF); break;
154       case 4: EXPECT_EQ(cel, celG); break;
155     }
156     ++i;
157   }
158   EXPECT_EQ(5, i);
159 
160   i = 0;
161   for (Cel* cel : spr->cels(frame_t(0))) {
162     switch (i) {
163       case 0: EXPECT_EQ(cel, celA); break;
164       case 1: EXPECT_EQ(cel, celC); break;
165       case 2: EXPECT_EQ(cel, celF); break;
166     }
167     ++i;
168   }
169   EXPECT_EQ(3, i);
170 
171   i = 0;
172   for (Cel* cel : spr->cels(frame_t(1))) {
173     switch (i) {
174       case 0: EXPECT_EQ(cel, celD); break;
175       case 1: EXPECT_EQ(cel, celG); break;
176     }
177     ++i;
178   }
179   EXPECT_EQ(2, i);
180 
181   i = 0;
182   for (Cel* cel : spr->cels(frame_t(2))) {
183     switch (i) {
184       case 0: EXPECT_EQ(cel, celB); break;
185       case 1: EXPECT_EQ(cel, celE); break;
186       case 2: EXPECT_EQ(cel, celH); break;
187     }
188     ++i;
189   }
190   EXPECT_EQ(3, i);
191 }
192 
main(int argc,char ** argv)193 int main(int argc, char** argv)
194 {
195   ::testing::InitGoogleTest(&argc, argv);
196   return RUN_ALL_TESTS();
197 }
198