• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "compile/Image.h"
18 
19 #include "test/Test.h"
20 
21 namespace aapt {
22 
23 // Pixels are in RGBA_8888 packing.
24 
25 #define RED "\xff\x00\x00\xff"
26 #define BLUE "\x00\x00\xff\xff"
27 #define GREEN "\xff\x00\x00\xff"
28 #define GR_70 "\xff\x00\x00\xb3"
29 #define GR_50 "\xff\x00\x00\x80"
30 #define GR_20 "\xff\x00\x00\x33"
31 #define BLACK "\x00\x00\x00\xff"
32 #define WHITE "\xff\xff\xff\xff"
33 #define TRANS "\x00\x00\x00\x00"
34 
35 static uint8_t* k2x2[] = {
36     (uint8_t*)WHITE WHITE, (uint8_t*)WHITE WHITE,
37 };
38 
39 static uint8_t* kMixedNeutralColor3x3[] = {
40     (uint8_t*)WHITE BLACK TRANS, (uint8_t*)TRANS RED TRANS,
41     (uint8_t*)WHITE WHITE WHITE,
42 };
43 
44 static uint8_t* kTransparentNeutralColor3x3[] = {
45     (uint8_t*)TRANS BLACK TRANS, (uint8_t*)BLACK RED BLACK,
46     (uint8_t*)TRANS BLACK TRANS,
47 };
48 
49 static uint8_t* kSingleStretch7x6[] = {
50     (uint8_t*)WHITE WHITE BLACK BLACK BLACK WHITE WHITE,
51     (uint8_t*)WHITE RED RED RED RED RED WHITE,
52     (uint8_t*)BLACK RED RED RED RED RED WHITE,
53     (uint8_t*)BLACK RED RED RED RED RED WHITE,
54     (uint8_t*)WHITE RED RED RED RED RED WHITE,
55     (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE WHITE,
56 };
57 
58 static uint8_t* kMultipleStretch10x7[] = {
59     (uint8_t*)WHITE WHITE BLACK WHITE BLACK BLACK WHITE BLACK WHITE WHITE,
60     (uint8_t*)BLACK RED BLUE RED BLUE BLUE RED BLUE RED WHITE,
61     (uint8_t*)BLACK RED BLUE RED BLUE BLUE RED BLUE RED WHITE,
62     (uint8_t*)WHITE RED BLUE RED BLUE BLUE RED BLUE RED WHITE,
63     (uint8_t*)BLACK RED BLUE RED BLUE BLUE RED BLUE RED WHITE,
64     (uint8_t*)BLACK RED BLUE RED BLUE BLUE RED BLUE RED WHITE,
65     (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE,
66 };
67 
68 static uint8_t* kPadding6x5[] = {
69     (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE,
70     (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE,
71     (uint8_t*)WHITE WHITE WHITE WHITE WHITE BLACK,
72     (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE,
73     (uint8_t*)WHITE WHITE BLACK BLACK WHITE WHITE,
74 };
75 
76 static uint8_t* kLayoutBoundsWrongEdge3x3[] = {
77     (uint8_t*)WHITE RED WHITE, (uint8_t*)RED WHITE WHITE,
78     (uint8_t*)WHITE WHITE WHITE,
79 };
80 
81 static uint8_t* kLayoutBoundsNotEdgeAligned5x5[] = {
82     (uint8_t*)WHITE WHITE WHITE WHITE WHITE,
83     (uint8_t*)WHITE WHITE WHITE WHITE WHITE,
84     (uint8_t*)WHITE WHITE WHITE WHITE RED,
85     (uint8_t*)WHITE WHITE WHITE WHITE WHITE,
86     (uint8_t*)WHITE WHITE RED WHITE WHITE,
87 };
88 
89 static uint8_t* kLayoutBounds5x5[] = {
90     (uint8_t*)WHITE WHITE WHITE WHITE WHITE,
91     (uint8_t*)WHITE WHITE WHITE WHITE RED,
92     (uint8_t*)WHITE WHITE WHITE WHITE WHITE,
93     (uint8_t*)WHITE WHITE WHITE WHITE RED,
94     (uint8_t*)WHITE RED WHITE RED WHITE,
95 };
96 
97 static uint8_t* kAsymmetricLayoutBounds5x5[] = {
98     (uint8_t*)WHITE WHITE WHITE WHITE WHITE,
99     (uint8_t*)WHITE WHITE WHITE WHITE RED,
100     (uint8_t*)WHITE WHITE WHITE WHITE WHITE,
101     (uint8_t*)WHITE WHITE WHITE WHITE WHITE,
102     (uint8_t*)WHITE RED WHITE WHITE WHITE,
103 };
104 
105 static uint8_t* kPaddingAndLayoutBounds5x5[] = {
106     (uint8_t*)WHITE WHITE WHITE WHITE WHITE,
107     (uint8_t*)WHITE WHITE WHITE WHITE RED,
108     (uint8_t*)WHITE WHITE WHITE WHITE BLACK,
109     (uint8_t*)WHITE WHITE WHITE WHITE RED,
110     (uint8_t*)WHITE RED BLACK RED WHITE,
111 };
112 
113 static uint8_t* kColorfulImage5x5[] = {
114     (uint8_t*)WHITE BLACK WHITE BLACK WHITE,
115     (uint8_t*)BLACK RED BLUE GREEN WHITE,
116     (uint8_t*)BLACK RED GREEN GREEN WHITE,
117     (uint8_t*)WHITE TRANS BLUE GREEN WHITE,
118     (uint8_t*)WHITE WHITE WHITE WHITE WHITE,
119 };
120 
121 static uint8_t* kOutlineOpaque10x10[] = {
122     (uint8_t*)WHITE BLACK BLACK BLACK BLACK BLACK BLACK BLACK BLACK WHITE,
123     (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE,
124     (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE,
125     (uint8_t*)WHITE TRANS TRANS GREEN GREEN GREEN GREEN TRANS TRANS WHITE,
126     (uint8_t*)WHITE TRANS TRANS GREEN GREEN GREEN GREEN TRANS TRANS WHITE,
127     (uint8_t*)WHITE TRANS TRANS GREEN GREEN GREEN GREEN TRANS TRANS WHITE,
128     (uint8_t*)WHITE TRANS TRANS GREEN GREEN GREEN GREEN TRANS TRANS WHITE,
129     (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE,
130     (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE,
131     (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE,
132 };
133 
134 static uint8_t* kOutlineTranslucent10x10[] = {
135     (uint8_t*)WHITE BLACK BLACK BLACK BLACK BLACK BLACK BLACK BLACK WHITE,
136     (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE,
137     (uint8_t*)WHITE TRANS TRANS GR_20 GR_20 GR_20 GR_20 TRANS TRANS WHITE,
138     (uint8_t*)WHITE TRANS TRANS GR_50 GR_50 GR_50 GR_50 TRANS TRANS WHITE,
139     (uint8_t*)WHITE TRANS GR_20 GR_50 GR_70 GR_70 GR_50 GR_20 TRANS WHITE,
140     (uint8_t*)WHITE TRANS GR_20 GR_50 GR_70 GR_70 GR_50 GR_20 TRANS WHITE,
141     (uint8_t*)WHITE TRANS TRANS GR_50 GR_50 GR_50 GR_50 TRANS TRANS WHITE,
142     (uint8_t*)WHITE TRANS TRANS GR_20 GR_20 GR_20 GR_20 TRANS TRANS WHITE,
143     (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE,
144     (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE,
145 };
146 
147 static uint8_t* kOutlineOffsetTranslucent12x10[] = {
148     (uint8_t*)
149         WHITE WHITE WHITE BLACK BLACK BLACK BLACK BLACK BLACK BLACK BLACK WHITE,
150     (uint8_t*)
151         WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE,
152     (uint8_t*)
153         WHITE TRANS TRANS TRANS TRANS GR_20 GR_20 GR_20 GR_20 TRANS TRANS WHITE,
154     (uint8_t*)
155         WHITE TRANS TRANS TRANS TRANS GR_50 GR_50 GR_50 GR_50 TRANS TRANS WHITE,
156     (uint8_t*)
157         WHITE TRANS TRANS TRANS GR_20 GR_50 GR_70 GR_70 GR_50 GR_20 TRANS WHITE,
158     (uint8_t*)
159         WHITE TRANS TRANS TRANS GR_20 GR_50 GR_70 GR_70 GR_50 GR_20 TRANS WHITE,
160     (uint8_t*)
161         WHITE TRANS TRANS TRANS TRANS GR_50 GR_50 GR_50 GR_50 TRANS TRANS WHITE,
162     (uint8_t*)
163         WHITE TRANS TRANS TRANS TRANS GR_20 GR_20 GR_20 GR_20 TRANS TRANS WHITE,
164     (uint8_t*)
165         WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE,
166     (uint8_t*)
167         WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE,
168 };
169 
170 static uint8_t* kOutlineRadius5x5[] = {
171     (uint8_t*)WHITE BLACK BLACK BLACK WHITE,
172     (uint8_t*)BLACK TRANS GREEN TRANS WHITE,
173     (uint8_t*)BLACK GREEN GREEN GREEN WHITE,
174     (uint8_t*)BLACK TRANS GREEN TRANS WHITE,
175     (uint8_t*)WHITE WHITE WHITE WHITE WHITE,
176 };
177 
178 static uint8_t* kStretchAndPadding5x5[] = {
179     (uint8_t*)WHITE WHITE BLACK WHITE WHITE, (uint8_t*)WHITE RED RED RED WHITE,
180     (uint8_t*)BLACK RED RED RED BLACK,       (uint8_t*)WHITE RED RED RED WHITE,
181     (uint8_t*)WHITE WHITE BLACK WHITE WHITE,
182 };
183 
TEST(NinePatchTest,Minimum3x3)184 TEST(NinePatchTest, Minimum3x3) {
185   std::string err;
186   EXPECT_EQ(nullptr, NinePatch::Create(k2x2, 2, 2, &err));
187   EXPECT_FALSE(err.empty());
188 }
189 
TEST(NinePatchTest,MixedNeutralColors)190 TEST(NinePatchTest, MixedNeutralColors) {
191   std::string err;
192   EXPECT_EQ(nullptr, NinePatch::Create(kMixedNeutralColor3x3, 3, 3, &err));
193   EXPECT_FALSE(err.empty());
194 }
195 
TEST(NinePatchTest,TransparentNeutralColor)196 TEST(NinePatchTest, TransparentNeutralColor) {
197   std::string err;
198   EXPECT_NE(nullptr,
199             NinePatch::Create(kTransparentNeutralColor3x3, 3, 3, &err));
200 }
201 
TEST(NinePatchTest,SingleStretchRegion)202 TEST(NinePatchTest, SingleStretchRegion) {
203   std::string err;
204   std::unique_ptr<NinePatch> nine_patch =
205       NinePatch::Create(kSingleStretch7x6, 7, 6, &err);
206   ASSERT_NE(nullptr, nine_patch);
207 
208   ASSERT_EQ(1u, nine_patch->horizontal_stretch_regions.size());
209   ASSERT_EQ(1u, nine_patch->vertical_stretch_regions.size());
210 
211   EXPECT_EQ(Range(1, 4), nine_patch->horizontal_stretch_regions.front());
212   EXPECT_EQ(Range(1, 3), nine_patch->vertical_stretch_regions.front());
213 }
214 
TEST(NinePatchTest,MultipleStretchRegions)215 TEST(NinePatchTest, MultipleStretchRegions) {
216   std::string err;
217   std::unique_ptr<NinePatch> nine_patch =
218       NinePatch::Create(kMultipleStretch10x7, 10, 7, &err);
219   ASSERT_NE(nullptr, nine_patch);
220 
221   ASSERT_EQ(3u, nine_patch->horizontal_stretch_regions.size());
222   ASSERT_EQ(2u, nine_patch->vertical_stretch_regions.size());
223 
224   EXPECT_EQ(Range(1, 2), nine_patch->horizontal_stretch_regions[0]);
225   EXPECT_EQ(Range(3, 5), nine_patch->horizontal_stretch_regions[1]);
226   EXPECT_EQ(Range(6, 7), nine_patch->horizontal_stretch_regions[2]);
227 
228   EXPECT_EQ(Range(0, 2), nine_patch->vertical_stretch_regions[0]);
229   EXPECT_EQ(Range(3, 5), nine_patch->vertical_stretch_regions[1]);
230 }
231 
TEST(NinePatchTest,InferPaddingFromStretchRegions)232 TEST(NinePatchTest, InferPaddingFromStretchRegions) {
233   std::string err;
234   std::unique_ptr<NinePatch> nine_patch =
235       NinePatch::Create(kMultipleStretch10x7, 10, 7, &err);
236   ASSERT_NE(nullptr, nine_patch);
237   EXPECT_EQ(Bounds(1, 0, 1, 0), nine_patch->padding);
238 }
239 
TEST(NinePatchTest,Padding)240 TEST(NinePatchTest, Padding) {
241   std::string err;
242   std::unique_ptr<NinePatch> nine_patch =
243       NinePatch::Create(kPadding6x5, 6, 5, &err);
244   ASSERT_NE(nullptr, nine_patch);
245   EXPECT_EQ(Bounds(1, 1, 1, 1), nine_patch->padding);
246 }
247 
TEST(NinePatchTest,LayoutBoundsAreOnWrongEdge)248 TEST(NinePatchTest, LayoutBoundsAreOnWrongEdge) {
249   std::string err;
250   EXPECT_EQ(nullptr, NinePatch::Create(kLayoutBoundsWrongEdge3x3, 3, 3, &err));
251   EXPECT_FALSE(err.empty());
252 }
253 
TEST(NinePatchTest,LayoutBoundsMustTouchEdges)254 TEST(NinePatchTest, LayoutBoundsMustTouchEdges) {
255   std::string err;
256   EXPECT_EQ(nullptr,
257             NinePatch::Create(kLayoutBoundsNotEdgeAligned5x5, 5, 5, &err));
258   EXPECT_FALSE(err.empty());
259 }
260 
TEST(NinePatchTest,LayoutBounds)261 TEST(NinePatchTest, LayoutBounds) {
262   std::string err;
263   std::unique_ptr<NinePatch> nine_patch =
264       NinePatch::Create(kLayoutBounds5x5, 5, 5, &err);
265   ASSERT_NE(nullptr, nine_patch);
266   EXPECT_EQ(Bounds(1, 1, 1, 1), nine_patch->layout_bounds);
267 
268   nine_patch = NinePatch::Create(kAsymmetricLayoutBounds5x5, 5, 5, &err);
269   ASSERT_NE(nullptr, nine_patch);
270   EXPECT_EQ(Bounds(1, 1, 0, 0), nine_patch->layout_bounds);
271 }
272 
TEST(NinePatchTest,PaddingAndLayoutBounds)273 TEST(NinePatchTest, PaddingAndLayoutBounds) {
274   std::string err;
275   std::unique_ptr<NinePatch> nine_patch =
276       NinePatch::Create(kPaddingAndLayoutBounds5x5, 5, 5, &err);
277   ASSERT_NE(nullptr, nine_patch);
278   EXPECT_EQ(Bounds(1, 1, 1, 1), nine_patch->padding);
279   EXPECT_EQ(Bounds(1, 1, 1, 1), nine_patch->layout_bounds);
280 }
281 
TEST(NinePatchTest,RegionColorsAreCorrect)282 TEST(NinePatchTest, RegionColorsAreCorrect) {
283   std::string err;
284   std::unique_ptr<NinePatch> nine_patch =
285       NinePatch::Create(kColorfulImage5x5, 5, 5, &err);
286   ASSERT_NE(nullptr, nine_patch);
287 
288   std::vector<uint32_t> expected_colors = {
289       NinePatch::PackRGBA((uint8_t*)RED),
290       (uint32_t)android::Res_png_9patch::NO_COLOR,
291       NinePatch::PackRGBA((uint8_t*)GREEN),
292       (uint32_t)android::Res_png_9patch::TRANSPARENT_COLOR,
293       NinePatch::PackRGBA((uint8_t*)BLUE),
294       NinePatch::PackRGBA((uint8_t*)GREEN),
295   };
296   EXPECT_EQ(expected_colors, nine_patch->region_colors);
297 }
298 
TEST(NinePatchTest,OutlineFromOpaqueImage)299 TEST(NinePatchTest, OutlineFromOpaqueImage) {
300   std::string err;
301   std::unique_ptr<NinePatch> nine_patch =
302       NinePatch::Create(kOutlineOpaque10x10, 10, 10, &err);
303   ASSERT_NE(nullptr, nine_patch);
304   EXPECT_EQ(Bounds(2, 2, 2, 2), nine_patch->outline);
305   EXPECT_EQ(0x000000ffu, nine_patch->outline_alpha);
306   EXPECT_EQ(0.0f, nine_patch->outline_radius);
307 }
308 
TEST(NinePatchTest,OutlineFromTranslucentImage)309 TEST(NinePatchTest, OutlineFromTranslucentImage) {
310   std::string err;
311   std::unique_ptr<NinePatch> nine_patch =
312       NinePatch::Create(kOutlineTranslucent10x10, 10, 10, &err);
313   ASSERT_NE(nullptr, nine_patch);
314   EXPECT_EQ(Bounds(3, 3, 3, 3), nine_patch->outline);
315   EXPECT_EQ(0x000000b3u, nine_patch->outline_alpha);
316   EXPECT_EQ(0.0f, nine_patch->outline_radius);
317 }
318 
TEST(NinePatchTest,OutlineFromOffCenterImage)319 TEST(NinePatchTest, OutlineFromOffCenterImage) {
320   std::string err;
321   std::unique_ptr<NinePatch> nine_patch =
322       NinePatch::Create(kOutlineOffsetTranslucent12x10, 12, 10, &err);
323   ASSERT_NE(nullptr, nine_patch);
324 
325   // TODO(adamlesinski): The old AAPT algorithm searches from the outside to the
326   // middle for each inset. If the outline is shifted, the search may not find a
327   // closer bounds.
328   // This check should be:
329   //   EXPECT_EQ(Bounds(5, 3, 3, 3), ninePatch->outline);
330   // but until I know what behavior I'm breaking, I will leave it at the
331   // incorrect:
332   EXPECT_EQ(Bounds(4, 3, 3, 3), nine_patch->outline);
333 
334   EXPECT_EQ(0x000000b3u, nine_patch->outline_alpha);
335   EXPECT_EQ(0.0f, nine_patch->outline_radius);
336 }
337 
TEST(NinePatchTest,OutlineRadius)338 TEST(NinePatchTest, OutlineRadius) {
339   std::string err;
340   std::unique_ptr<NinePatch> nine_patch =
341       NinePatch::Create(kOutlineRadius5x5, 5, 5, &err);
342   ASSERT_NE(nullptr, nine_patch);
343   EXPECT_EQ(Bounds(0, 0, 0, 0), nine_patch->outline);
344   EXPECT_EQ(3.4142f, nine_patch->outline_radius);
345 }
346 
BigEndianOne(uint8_t * cursor)347 ::testing::AssertionResult BigEndianOne(uint8_t* cursor) {
348   if (cursor[0] == 0 && cursor[1] == 0 && cursor[2] == 0 && cursor[3] == 1) {
349     return ::testing::AssertionSuccess();
350   }
351   return ::testing::AssertionFailure() << "Not BigEndian 1";
352 }
353 
TEST(NinePatchTest,SerializePngEndianness)354 TEST(NinePatchTest, SerializePngEndianness) {
355   std::string err;
356   std::unique_ptr<NinePatch> nine_patch =
357       NinePatch::Create(kStretchAndPadding5x5, 5, 5, &err);
358   ASSERT_NE(nullptr, nine_patch);
359 
360   size_t len;
361   std::unique_ptr<uint8_t[]> data = nine_patch->SerializeBase(&len);
362   ASSERT_NE(nullptr, data);
363   ASSERT_NE(0u, len);
364 
365   // Skip past wasDeserialized + numXDivs + numYDivs + numColors + xDivsOffset +
366   // yDivsOffset
367   // (12 bytes)
368   uint8_t* cursor = data.get() + 12;
369 
370   // Check that padding is big-endian. Expecting value 1.
371   EXPECT_TRUE(BigEndianOne(cursor));
372   EXPECT_TRUE(BigEndianOne(cursor + 4));
373   EXPECT_TRUE(BigEndianOne(cursor + 8));
374   EXPECT_TRUE(BigEndianOne(cursor + 12));
375 }
376 
377 }  // namespace aapt
378