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 "androidfw/Image.h"
18 #include "androidfw/ResourceTypes.h"
19 #include "gtest/gtest.h"
20
21 namespace android {
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,
37 (uint8_t*)WHITE WHITE,
38 };
39
40 static uint8_t* kMixedNeutralColor3x3[] = {
41 (uint8_t*)WHITE BLACK TRANS,
42 (uint8_t*)TRANS RED TRANS,
43 (uint8_t*)WHITE WHITE WHITE,
44 };
45
46 static uint8_t* kTransparentNeutralColor3x3[] = {
47 (uint8_t*)TRANS BLACK TRANS,
48 (uint8_t*)BLACK RED BLACK,
49 (uint8_t*)TRANS BLACK TRANS,
50 };
51
52 static uint8_t* kSingleStretch7x6[] = {
53 (uint8_t*)WHITE WHITE BLACK BLACK BLACK WHITE WHITE,
54 (uint8_t*)WHITE RED RED RED RED RED WHITE,
55 (uint8_t*)BLACK RED RED RED RED RED WHITE,
56 (uint8_t*)BLACK RED RED RED RED RED WHITE,
57 (uint8_t*)WHITE RED RED RED RED RED WHITE,
58 (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE WHITE,
59 };
60
61 static uint8_t* kMultipleStretch10x7[] = {
62 (uint8_t*)WHITE WHITE BLACK WHITE BLACK BLACK WHITE BLACK WHITE 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 RED BLUE RED BLUE BLUE RED BLUE RED WHITE,
66 (uint8_t*)BLACK RED BLUE RED BLUE BLUE RED BLUE RED WHITE,
67 (uint8_t*)BLACK RED BLUE RED BLUE BLUE RED BLUE RED WHITE,
68 (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE,
69 };
70
71 static uint8_t* kPadding6x5[] = {
72 (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE, (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE,
73 (uint8_t*)WHITE WHITE WHITE WHITE WHITE BLACK, (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE,
74 (uint8_t*)WHITE WHITE BLACK BLACK WHITE WHITE,
75 };
76
77 static uint8_t* kLayoutBoundsWrongEdge3x3[] = {
78 (uint8_t*)WHITE RED WHITE,
79 (uint8_t*)RED WHITE WHITE,
80 (uint8_t*)WHITE WHITE WHITE,
81 };
82
83 static uint8_t* kLayoutBoundsNotEdgeAligned5x5[] = {
84 (uint8_t*)WHITE WHITE WHITE WHITE WHITE, (uint8_t*)WHITE WHITE WHITE WHITE WHITE,
85 (uint8_t*)WHITE WHITE WHITE WHITE RED, (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, (uint8_t*)WHITE WHITE WHITE WHITE RED,
91 (uint8_t*)WHITE WHITE WHITE WHITE WHITE, (uint8_t*)WHITE WHITE WHITE WHITE RED,
92 (uint8_t*)WHITE RED WHITE RED WHITE,
93 };
94
95 static uint8_t* kAsymmetricLayoutBounds5x5[] = {
96 (uint8_t*)WHITE WHITE WHITE WHITE WHITE, (uint8_t*)WHITE WHITE WHITE WHITE RED,
97 (uint8_t*)WHITE WHITE WHITE WHITE WHITE, (uint8_t*)WHITE WHITE WHITE WHITE WHITE,
98 (uint8_t*)WHITE RED WHITE WHITE WHITE,
99 };
100
101 static uint8_t* kPaddingAndLayoutBounds5x5[] = {
102 (uint8_t*)WHITE WHITE WHITE WHITE WHITE, (uint8_t*)WHITE WHITE WHITE WHITE RED,
103 (uint8_t*)WHITE WHITE WHITE WHITE BLACK, (uint8_t*)WHITE WHITE WHITE WHITE RED,
104 (uint8_t*)WHITE RED BLACK RED WHITE,
105 };
106
107 static uint8_t* kColorfulImage5x5[] = {
108 (uint8_t*)WHITE BLACK WHITE BLACK WHITE, (uint8_t*)BLACK RED BLUE GREEN WHITE,
109 (uint8_t*)BLACK RED GREEN GREEN WHITE, (uint8_t*)WHITE TRANS BLUE GREEN WHITE,
110 (uint8_t*)WHITE WHITE WHITE WHITE WHITE,
111 };
112
113 static uint8_t* kOutlineOpaque10x10[] = {
114 (uint8_t*)WHITE BLACK BLACK BLACK BLACK BLACK BLACK BLACK BLACK WHITE,
115 (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE,
116 (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE,
117 (uint8_t*)WHITE TRANS TRANS GREEN GREEN GREEN GREEN TRANS TRANS WHITE,
118 (uint8_t*)WHITE TRANS TRANS GREEN GREEN GREEN GREEN TRANS TRANS WHITE,
119 (uint8_t*)WHITE TRANS TRANS GREEN GREEN GREEN GREEN TRANS TRANS WHITE,
120 (uint8_t*)WHITE TRANS TRANS GREEN GREEN GREEN GREEN TRANS TRANS WHITE,
121 (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE,
122 (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE,
123 (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE,
124 };
125
126 static uint8_t* kOutlineTranslucent10x10[] = {
127 (uint8_t*)WHITE BLACK BLACK BLACK BLACK BLACK BLACK BLACK BLACK WHITE,
128 (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE,
129 (uint8_t*)WHITE TRANS TRANS GR_20 GR_20 GR_20 GR_20 TRANS TRANS WHITE,
130 (uint8_t*)WHITE TRANS TRANS GR_50 GR_50 GR_50 GR_50 TRANS TRANS WHITE,
131 (uint8_t*)WHITE TRANS GR_20 GR_50 GR_70 GR_70 GR_50 GR_20 TRANS WHITE,
132 (uint8_t*)WHITE TRANS GR_20 GR_50 GR_70 GR_70 GR_50 GR_20 TRANS WHITE,
133 (uint8_t*)WHITE TRANS TRANS GR_50 GR_50 GR_50 GR_50 TRANS TRANS WHITE,
134 (uint8_t*)WHITE TRANS TRANS GR_20 GR_20 GR_20 GR_20 TRANS TRANS WHITE,
135 (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE,
136 (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE,
137 };
138
139 static uint8_t* kOutlineOffsetTranslucent12x10[] = {
140 (uint8_t*)WHITE WHITE WHITE BLACK BLACK BLACK BLACK BLACK BLACK BLACK BLACK WHITE,
141 (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE,
142 (uint8_t*)WHITE TRANS TRANS TRANS TRANS GR_20 GR_20 GR_20 GR_20 TRANS TRANS WHITE,
143 (uint8_t*)WHITE TRANS TRANS TRANS TRANS GR_50 GR_50 GR_50 GR_50 TRANS TRANS WHITE,
144 (uint8_t*)WHITE TRANS TRANS TRANS GR_20 GR_50 GR_70 GR_70 GR_50 GR_20 TRANS WHITE,
145 (uint8_t*)WHITE TRANS TRANS TRANS GR_20 GR_50 GR_70 GR_70 GR_50 GR_20 TRANS WHITE,
146 (uint8_t*)WHITE TRANS TRANS TRANS TRANS GR_50 GR_50 GR_50 GR_50 TRANS TRANS WHITE,
147 (uint8_t*)WHITE TRANS TRANS TRANS TRANS GR_20 GR_20 GR_20 GR_20 TRANS TRANS WHITE,
148 (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE,
149 (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE,
150 };
151
152 static uint8_t* kOutlineRadius5x5[] = {
153 (uint8_t*)WHITE BLACK BLACK BLACK WHITE, (uint8_t*)BLACK TRANS GREEN TRANS WHITE,
154 (uint8_t*)BLACK GREEN GREEN GREEN WHITE, (uint8_t*)BLACK TRANS GREEN TRANS WHITE,
155 (uint8_t*)WHITE WHITE WHITE WHITE WHITE,
156 };
157
158 static uint8_t* kStretchAndPadding5x5[] = {
159 (uint8_t*)WHITE WHITE BLACK WHITE WHITE, (uint8_t*)WHITE RED RED RED WHITE,
160 (uint8_t*)BLACK RED RED RED BLACK, (uint8_t*)WHITE RED RED RED WHITE,
161 (uint8_t*)WHITE WHITE BLACK WHITE WHITE,
162 };
163
TEST(NinePatchTest,Minimum3x3)164 TEST(NinePatchTest, Minimum3x3) {
165 std::string err;
166 EXPECT_EQ(nullptr, NinePatch::Create(k2x2, 2, 2, &err));
167 EXPECT_FALSE(err.empty());
168 }
169
TEST(NinePatchTest,MixedNeutralColors)170 TEST(NinePatchTest, MixedNeutralColors) {
171 std::string err;
172 EXPECT_EQ(nullptr, NinePatch::Create(kMixedNeutralColor3x3, 3, 3, &err));
173 EXPECT_FALSE(err.empty());
174 }
175
TEST(NinePatchTest,TransparentNeutralColor)176 TEST(NinePatchTest, TransparentNeutralColor) {
177 std::string err;
178 EXPECT_NE(nullptr, NinePatch::Create(kTransparentNeutralColor3x3, 3, 3, &err));
179 }
180
TEST(NinePatchTest,SingleStretchRegion)181 TEST(NinePatchTest, SingleStretchRegion) {
182 std::string err;
183 std::unique_ptr<NinePatch> nine_patch = NinePatch::Create(kSingleStretch7x6, 7, 6, &err);
184 ASSERT_NE(nullptr, nine_patch);
185
186 ASSERT_EQ(1u, nine_patch->horizontal_stretch_regions.size());
187 ASSERT_EQ(1u, nine_patch->vertical_stretch_regions.size());
188
189 EXPECT_EQ(Range(1, 4), nine_patch->horizontal_stretch_regions.front());
190 EXPECT_EQ(Range(1, 3), nine_patch->vertical_stretch_regions.front());
191 }
192
TEST(NinePatchTest,MultipleStretchRegions)193 TEST(NinePatchTest, MultipleStretchRegions) {
194 std::string err;
195 std::unique_ptr<NinePatch> nine_patch = NinePatch::Create(kMultipleStretch10x7, 10, 7, &err);
196 ASSERT_NE(nullptr, nine_patch);
197
198 ASSERT_EQ(3u, nine_patch->horizontal_stretch_regions.size());
199 ASSERT_EQ(2u, nine_patch->vertical_stretch_regions.size());
200
201 EXPECT_EQ(Range(1, 2), nine_patch->horizontal_stretch_regions[0]);
202 EXPECT_EQ(Range(3, 5), nine_patch->horizontal_stretch_regions[1]);
203 EXPECT_EQ(Range(6, 7), nine_patch->horizontal_stretch_regions[2]);
204
205 EXPECT_EQ(Range(0, 2), nine_patch->vertical_stretch_regions[0]);
206 EXPECT_EQ(Range(3, 5), nine_patch->vertical_stretch_regions[1]);
207 }
208
TEST(NinePatchTest,InferPaddingFromStretchRegions)209 TEST(NinePatchTest, InferPaddingFromStretchRegions) {
210 std::string err;
211 std::unique_ptr<NinePatch> nine_patch = NinePatch::Create(kMultipleStretch10x7, 10, 7, &err);
212 ASSERT_NE(nullptr, nine_patch);
213 EXPECT_EQ(Bounds(1, 0, 1, 0), nine_patch->padding);
214 }
215
TEST(NinePatchTest,Padding)216 TEST(NinePatchTest, Padding) {
217 std::string err;
218 std::unique_ptr<NinePatch> nine_patch = NinePatch::Create(kPadding6x5, 6, 5, &err);
219 ASSERT_NE(nullptr, nine_patch);
220 EXPECT_EQ(Bounds(1, 1, 1, 1), nine_patch->padding);
221 }
222
TEST(NinePatchTest,LayoutBoundsAreOnWrongEdge)223 TEST(NinePatchTest, LayoutBoundsAreOnWrongEdge) {
224 std::string err;
225 EXPECT_EQ(nullptr, NinePatch::Create(kLayoutBoundsWrongEdge3x3, 3, 3, &err));
226 EXPECT_FALSE(err.empty());
227 }
228
TEST(NinePatchTest,LayoutBoundsMustTouchEdges)229 TEST(NinePatchTest, LayoutBoundsMustTouchEdges) {
230 std::string err;
231 EXPECT_EQ(nullptr, NinePatch::Create(kLayoutBoundsNotEdgeAligned5x5, 5, 5, &err));
232 EXPECT_FALSE(err.empty());
233 }
234
TEST(NinePatchTest,LayoutBounds)235 TEST(NinePatchTest, LayoutBounds) {
236 std::string err;
237 std::unique_ptr<NinePatch> nine_patch = NinePatch::Create(kLayoutBounds5x5, 5, 5, &err);
238 ASSERT_NE(nullptr, nine_patch);
239 EXPECT_EQ(Bounds(1, 1, 1, 1), nine_patch->layout_bounds);
240
241 nine_patch = NinePatch::Create(kAsymmetricLayoutBounds5x5, 5, 5, &err);
242 ASSERT_NE(nullptr, nine_patch);
243 EXPECT_EQ(Bounds(1, 1, 0, 0), nine_patch->layout_bounds);
244 }
245
TEST(NinePatchTest,PaddingAndLayoutBounds)246 TEST(NinePatchTest, PaddingAndLayoutBounds) {
247 std::string err;
248 std::unique_ptr<NinePatch> nine_patch = NinePatch::Create(kPaddingAndLayoutBounds5x5, 5, 5, &err);
249 ASSERT_NE(nullptr, nine_patch);
250 EXPECT_EQ(Bounds(1, 1, 1, 1), nine_patch->padding);
251 EXPECT_EQ(Bounds(1, 1, 1, 1), nine_patch->layout_bounds);
252 }
253
TEST(NinePatchTest,RegionColorsAreCorrect)254 TEST(NinePatchTest, RegionColorsAreCorrect) {
255 std::string err;
256 std::unique_ptr<NinePatch> nine_patch = NinePatch::Create(kColorfulImage5x5, 5, 5, &err);
257 ASSERT_NE(nullptr, nine_patch);
258
259 std::vector<uint32_t> expected_colors = {
260 NinePatch::PackRGBA((uint8_t*)RED), (uint32_t)android::Res_png_9patch::NO_COLOR,
261 NinePatch::PackRGBA((uint8_t*)GREEN), (uint32_t)android::Res_png_9patch::TRANSPARENT_COLOR,
262 NinePatch::PackRGBA((uint8_t*)BLUE), NinePatch::PackRGBA((uint8_t*)GREEN),
263 };
264 EXPECT_EQ(expected_colors, nine_patch->region_colors);
265 }
266
TEST(NinePatchTest,OutlineFromOpaqueImage)267 TEST(NinePatchTest, OutlineFromOpaqueImage) {
268 std::string err;
269 std::unique_ptr<NinePatch> nine_patch = NinePatch::Create(kOutlineOpaque10x10, 10, 10, &err);
270 ASSERT_NE(nullptr, nine_patch);
271 EXPECT_EQ(Bounds(2, 2, 2, 2), nine_patch->outline);
272 EXPECT_EQ(0x000000ffu, nine_patch->outline_alpha);
273 EXPECT_EQ(0.0f, nine_patch->outline_radius);
274 }
275
TEST(NinePatchTest,OutlineFromTranslucentImage)276 TEST(NinePatchTest, OutlineFromTranslucentImage) {
277 std::string err;
278 std::unique_ptr<NinePatch> nine_patch = NinePatch::Create(kOutlineTranslucent10x10, 10, 10, &err);
279 ASSERT_NE(nullptr, nine_patch);
280 EXPECT_EQ(Bounds(3, 3, 3, 3), nine_patch->outline);
281 EXPECT_EQ(0x000000b3u, nine_patch->outline_alpha);
282 EXPECT_EQ(0.0f, nine_patch->outline_radius);
283 }
284
TEST(NinePatchTest,OutlineFromOffCenterImage)285 TEST(NinePatchTest, OutlineFromOffCenterImage) {
286 std::string err;
287 std::unique_ptr<NinePatch> nine_patch =
288 NinePatch::Create(kOutlineOffsetTranslucent12x10, 12, 10, &err);
289 ASSERT_NE(nullptr, nine_patch);
290
291 // TODO(adamlesinski): The old AAPT algorithm searches from the outside to the
292 // middle for each inset. If the outline is shifted, the search may not find a
293 // closer bounds.
294 // This check should be:
295 // EXPECT_EQ(Bounds(5, 3, 3, 3), ninePatch->outline);
296 // but until I know what behavior I'm breaking, I will leave it at the
297 // incorrect:
298 EXPECT_EQ(Bounds(4, 3, 3, 3), nine_patch->outline);
299
300 EXPECT_EQ(0x000000b3u, nine_patch->outline_alpha);
301 EXPECT_EQ(0.0f, nine_patch->outline_radius);
302 }
303
TEST(NinePatchTest,OutlineRadius)304 TEST(NinePatchTest, OutlineRadius) {
305 std::string err;
306 std::unique_ptr<NinePatch> nine_patch = NinePatch::Create(kOutlineRadius5x5, 5, 5, &err);
307 ASSERT_NE(nullptr, nine_patch);
308 EXPECT_EQ(Bounds(0, 0, 0, 0), nine_patch->outline);
309 EXPECT_EQ(3.4142f, nine_patch->outline_radius);
310 }
311
BigEndianOne(uint8_t * cursor)312 ::testing::AssertionResult BigEndianOne(uint8_t* cursor) {
313 if (cursor[0] == 0 && cursor[1] == 0 && cursor[2] == 0 && cursor[3] == 1) {
314 return ::testing::AssertionSuccess();
315 }
316 return ::testing::AssertionFailure() << "Not BigEndian 1";
317 }
318
TEST(NinePatchTest,SerializePngEndianness)319 TEST(NinePatchTest, SerializePngEndianness) {
320 std::string err;
321 std::unique_ptr<NinePatch> nine_patch = NinePatch::Create(kStretchAndPadding5x5, 5, 5, &err);
322 ASSERT_NE(nullptr, nine_patch);
323
324 size_t len;
325 std::unique_ptr<uint8_t[]> data = nine_patch->SerializeBase(&len);
326 ASSERT_NE(nullptr, data);
327 ASSERT_NE(0u, len);
328
329 // Skip past wasDeserialized + numXDivs + numYDivs + numColors + xDivsOffset +
330 // yDivsOffset
331 // (12 bytes)
332 uint8_t* cursor = data.get() + 12;
333
334 // Check that padding is big-endian. Expecting value 1.
335 EXPECT_TRUE(BigEndianOne(cursor));
336 EXPECT_TRUE(BigEndianOne(cursor + 4));
337 EXPECT_TRUE(BigEndianOne(cursor + 8));
338 EXPECT_TRUE(BigEndianOne(cursor + 12));
339 }
340
341 } // namespace android
342