Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 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 | #include "test/Test.h" |
| 19 | |
| 20 | namespace aapt { |
| 21 | |
| 22 | // Pixels are in RGBA_8888 packing. |
| 23 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 24 | #define RED "\xff\x00\x00\xff" |
| 25 | #define BLUE "\x00\x00\xff\xff" |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 26 | #define GREEN "\xff\x00\x00\xff" |
| 27 | #define GR_70 "\xff\x00\x00\xb3" |
| 28 | #define GR_50 "\xff\x00\x00\x80" |
| 29 | #define GR_20 "\xff\x00\x00\x33" |
| 30 | #define BLACK "\x00\x00\x00\xff" |
| 31 | #define WHITE "\xff\xff\xff\xff" |
| 32 | #define TRANS "\x00\x00\x00\x00" |
| 33 | |
| 34 | static uint8_t* k2x2[] = { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 35 | (uint8_t*)WHITE WHITE, (uint8_t*)WHITE WHITE, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static uint8_t* kMixedNeutralColor3x3[] = { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 39 | (uint8_t*)WHITE BLACK TRANS, (uint8_t*)TRANS RED TRANS, |
| 40 | (uint8_t*)WHITE WHITE WHITE, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static uint8_t* kTransparentNeutralColor3x3[] = { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 44 | (uint8_t*)TRANS BLACK TRANS, (uint8_t*)BLACK RED BLACK, |
| 45 | (uint8_t*)TRANS BLACK TRANS, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | static uint8_t* kSingleStretch7x6[] = { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 49 | (uint8_t*)WHITE WHITE BLACK BLACK BLACK WHITE WHITE, |
| 50 | (uint8_t*)WHITE RED RED RED RED RED WHITE, |
| 51 | (uint8_t*)BLACK RED RED RED RED RED WHITE, |
| 52 | (uint8_t*)BLACK RED RED RED RED RED WHITE, |
| 53 | (uint8_t*)WHITE RED RED RED RED RED WHITE, |
| 54 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE WHITE, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | static uint8_t* kMultipleStretch10x7[] = { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 58 | (uint8_t*)WHITE WHITE BLACK WHITE BLACK BLACK WHITE BLACK WHITE WHITE, |
| 59 | (uint8_t*)BLACK RED BLUE RED BLUE BLUE RED BLUE RED WHITE, |
| 60 | (uint8_t*)BLACK RED BLUE RED BLUE BLUE RED BLUE RED WHITE, |
| 61 | (uint8_t*)WHITE RED BLUE RED BLUE BLUE RED BLUE RED WHITE, |
| 62 | (uint8_t*)BLACK 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*)WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | static uint8_t* kPadding6x5[] = { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 68 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE, |
| 69 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE, |
| 70 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE BLACK, |
| 71 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE, |
| 72 | (uint8_t*)WHITE WHITE BLACK BLACK WHITE WHITE, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | static uint8_t* kLayoutBoundsWrongEdge3x3[] = { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 76 | (uint8_t*)WHITE RED WHITE, (uint8_t*)RED WHITE WHITE, |
| 77 | (uint8_t*)WHITE WHITE WHITE, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | static uint8_t* kLayoutBoundsNotEdgeAligned5x5[] = { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 81 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE, |
| 82 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE, |
| 83 | (uint8_t*)WHITE WHITE WHITE WHITE RED, |
| 84 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE, |
| 85 | (uint8_t*)WHITE WHITE RED WHITE WHITE, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | static uint8_t* kLayoutBounds5x5[] = { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 89 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE, |
| 90 | (uint8_t*)WHITE WHITE WHITE WHITE RED, |
| 91 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE, |
| 92 | (uint8_t*)WHITE WHITE WHITE WHITE RED, |
| 93 | (uint8_t*)WHITE RED WHITE RED WHITE, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | static uint8_t* kAsymmetricLayoutBounds5x5[] = { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 97 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE, |
| 98 | (uint8_t*)WHITE WHITE WHITE WHITE RED, |
| 99 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE, |
| 100 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE, |
| 101 | (uint8_t*)WHITE RED WHITE WHITE WHITE, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | static uint8_t* kPaddingAndLayoutBounds5x5[] = { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 105 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE, |
| 106 | (uint8_t*)WHITE WHITE WHITE WHITE RED, |
| 107 | (uint8_t*)WHITE WHITE WHITE WHITE BLACK, |
| 108 | (uint8_t*)WHITE WHITE WHITE WHITE RED, |
| 109 | (uint8_t*)WHITE RED BLACK RED WHITE, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | static uint8_t* kColorfulImage5x5[] = { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 113 | (uint8_t*)WHITE BLACK WHITE BLACK WHITE, |
| 114 | (uint8_t*)BLACK RED BLUE GREEN WHITE, |
| 115 | (uint8_t*)BLACK RED GREEN GREEN WHITE, |
| 116 | (uint8_t*)WHITE TRANS BLUE GREEN WHITE, |
| 117 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | static uint8_t* kOutlineOpaque10x10[] = { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 121 | (uint8_t*)WHITE BLACK BLACK BLACK BLACK BLACK BLACK BLACK BLACK WHITE, |
| 122 | (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE, |
| 123 | (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE, |
| 124 | (uint8_t*)WHITE TRANS TRANS GREEN GREEN GREEN GREEN 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 TRANS TRANS TRANS TRANS TRANS TRANS WHITE, |
| 129 | (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE, |
| 130 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | static uint8_t* kOutlineTranslucent10x10[] = { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 134 | (uint8_t*)WHITE BLACK BLACK BLACK BLACK BLACK BLACK BLACK BLACK WHITE, |
| 135 | (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE, |
| 136 | (uint8_t*)WHITE TRANS TRANS GR_20 GR_20 GR_20 GR_20 TRANS TRANS WHITE, |
| 137 | (uint8_t*)WHITE TRANS TRANS GR_50 GR_50 GR_50 GR_50 TRANS TRANS WHITE, |
| 138 | (uint8_t*)WHITE TRANS GR_20 GR_50 GR_70 GR_70 GR_50 GR_20 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 TRANS GR_50 GR_50 GR_50 GR_50 TRANS TRANS WHITE, |
| 141 | (uint8_t*)WHITE TRANS TRANS GR_20 GR_20 GR_20 GR_20 TRANS TRANS WHITE, |
| 142 | (uint8_t*)WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE, |
| 143 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | static uint8_t* kOutlineOffsetTranslucent12x10[] = { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 147 | (uint8_t*) |
| 148 | WHITE WHITE WHITE BLACK BLACK BLACK BLACK BLACK BLACK BLACK BLACK WHITE, |
| 149 | (uint8_t*) |
| 150 | WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE, |
| 151 | (uint8_t*) |
| 152 | WHITE TRANS TRANS TRANS TRANS GR_20 GR_20 GR_20 GR_20 TRANS TRANS WHITE, |
| 153 | (uint8_t*) |
| 154 | WHITE TRANS TRANS TRANS TRANS GR_50 GR_50 GR_50 GR_50 TRANS TRANS WHITE, |
| 155 | (uint8_t*) |
| 156 | WHITE TRANS TRANS TRANS GR_20 GR_50 GR_70 GR_70 GR_50 GR_20 TRANS WHITE, |
| 157 | (uint8_t*) |
| 158 | WHITE TRANS TRANS TRANS GR_20 GR_50 GR_70 GR_70 GR_50 GR_20 TRANS WHITE, |
| 159 | (uint8_t*) |
| 160 | WHITE TRANS TRANS TRANS TRANS GR_50 GR_50 GR_50 GR_50 TRANS TRANS WHITE, |
| 161 | (uint8_t*) |
| 162 | WHITE TRANS TRANS TRANS TRANS GR_20 GR_20 GR_20 GR_20 TRANS TRANS WHITE, |
| 163 | (uint8_t*) |
| 164 | WHITE TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS TRANS WHITE, |
| 165 | (uint8_t*) |
| 166 | WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE WHITE, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | static uint8_t* kOutlineRadius5x5[] = { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 170 | (uint8_t*)WHITE BLACK BLACK BLACK WHITE, |
| 171 | (uint8_t*)BLACK TRANS GREEN TRANS WHITE, |
| 172 | (uint8_t*)BLACK GREEN GREEN GREEN WHITE, |
| 173 | (uint8_t*)BLACK TRANS GREEN TRANS WHITE, |
| 174 | (uint8_t*)WHITE WHITE WHITE WHITE WHITE, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
Adam Lesinski | edba941 | 2016-10-04 17:33:04 -0700 | [diff] [blame] | 177 | static uint8_t* kStretchAndPadding5x5[] = { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 178 | (uint8_t*)WHITE WHITE BLACK WHITE WHITE, (uint8_t*)WHITE RED RED RED WHITE, |
| 179 | (uint8_t*)BLACK RED RED RED BLACK, (uint8_t*)WHITE RED RED RED WHITE, |
| 180 | (uint8_t*)WHITE WHITE BLACK WHITE WHITE, |
Adam Lesinski | edba941 | 2016-10-04 17:33:04 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 183 | TEST(NinePatchTest, Minimum3x3) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 184 | std::string err; |
| 185 | EXPECT_EQ(nullptr, NinePatch::create(k2x2, 2, 2, &err)); |
| 186 | EXPECT_FALSE(err.empty()); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | TEST(NinePatchTest, MixedNeutralColors) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 190 | std::string err; |
| 191 | EXPECT_EQ(nullptr, NinePatch::create(kMixedNeutralColor3x3, 3, 3, &err)); |
| 192 | EXPECT_FALSE(err.empty()); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | TEST(NinePatchTest, TransparentNeutralColor) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 196 | std::string err; |
| 197 | EXPECT_NE(nullptr, |
| 198 | NinePatch::create(kTransparentNeutralColor3x3, 3, 3, &err)); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | TEST(NinePatchTest, SingleStretchRegion) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 202 | std::string err; |
| 203 | std::unique_ptr<NinePatch> ninePatch = |
| 204 | NinePatch::create(kSingleStretch7x6, 7, 6, &err); |
| 205 | ASSERT_NE(nullptr, ninePatch); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 206 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 207 | ASSERT_EQ(1u, ninePatch->horizontalStretchRegions.size()); |
| 208 | ASSERT_EQ(1u, ninePatch->verticalStretchRegions.size()); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 209 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 210 | EXPECT_EQ(Range(1, 4), ninePatch->horizontalStretchRegions.front()); |
| 211 | EXPECT_EQ(Range(1, 3), ninePatch->verticalStretchRegions.front()); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | TEST(NinePatchTest, MultipleStretchRegions) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 215 | std::string err; |
| 216 | std::unique_ptr<NinePatch> ninePatch = |
| 217 | NinePatch::create(kMultipleStretch10x7, 10, 7, &err); |
| 218 | ASSERT_NE(nullptr, ninePatch); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 219 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 220 | ASSERT_EQ(3u, ninePatch->horizontalStretchRegions.size()); |
| 221 | ASSERT_EQ(2u, ninePatch->verticalStretchRegions.size()); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 222 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 223 | EXPECT_EQ(Range(1, 2), ninePatch->horizontalStretchRegions[0]); |
| 224 | EXPECT_EQ(Range(3, 5), ninePatch->horizontalStretchRegions[1]); |
| 225 | EXPECT_EQ(Range(6, 7), ninePatch->horizontalStretchRegions[2]); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 226 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 227 | EXPECT_EQ(Range(0, 2), ninePatch->verticalStretchRegions[0]); |
| 228 | EXPECT_EQ(Range(3, 5), ninePatch->verticalStretchRegions[1]); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | TEST(NinePatchTest, InferPaddingFromStretchRegions) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 232 | std::string err; |
| 233 | std::unique_ptr<NinePatch> ninePatch = |
| 234 | NinePatch::create(kMultipleStretch10x7, 10, 7, &err); |
| 235 | ASSERT_NE(nullptr, ninePatch); |
| 236 | EXPECT_EQ(Bounds(1, 0, 1, 0), ninePatch->padding); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | TEST(NinePatchTest, Padding) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 240 | std::string err; |
| 241 | std::unique_ptr<NinePatch> ninePatch = |
| 242 | NinePatch::create(kPadding6x5, 6, 5, &err); |
| 243 | ASSERT_NE(nullptr, ninePatch); |
| 244 | EXPECT_EQ(Bounds(1, 1, 1, 1), ninePatch->padding); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | TEST(NinePatchTest, LayoutBoundsAreOnWrongEdge) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 248 | std::string err; |
| 249 | EXPECT_EQ(nullptr, NinePatch::create(kLayoutBoundsWrongEdge3x3, 3, 3, &err)); |
| 250 | EXPECT_FALSE(err.empty()); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | TEST(NinePatchTest, LayoutBoundsMustTouchEdges) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 254 | std::string err; |
| 255 | EXPECT_EQ(nullptr, |
| 256 | NinePatch::create(kLayoutBoundsNotEdgeAligned5x5, 5, 5, &err)); |
| 257 | EXPECT_FALSE(err.empty()); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | TEST(NinePatchTest, LayoutBounds) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 261 | std::string err; |
| 262 | std::unique_ptr<NinePatch> ninePatch = |
| 263 | NinePatch::create(kLayoutBounds5x5, 5, 5, &err); |
| 264 | ASSERT_NE(nullptr, ninePatch); |
| 265 | EXPECT_EQ(Bounds(1, 1, 1, 1), ninePatch->layoutBounds); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 266 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 267 | ninePatch = NinePatch::create(kAsymmetricLayoutBounds5x5, 5, 5, &err); |
| 268 | ASSERT_NE(nullptr, ninePatch); |
| 269 | EXPECT_EQ(Bounds(1, 1, 0, 0), ninePatch->layoutBounds); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | TEST(NinePatchTest, PaddingAndLayoutBounds) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 273 | std::string err; |
| 274 | std::unique_ptr<NinePatch> ninePatch = |
| 275 | NinePatch::create(kPaddingAndLayoutBounds5x5, 5, 5, &err); |
| 276 | ASSERT_NE(nullptr, ninePatch); |
| 277 | EXPECT_EQ(Bounds(1, 1, 1, 1), ninePatch->padding); |
| 278 | EXPECT_EQ(Bounds(1, 1, 1, 1), ninePatch->layoutBounds); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | TEST(NinePatchTest, RegionColorsAreCorrect) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 282 | std::string err; |
| 283 | std::unique_ptr<NinePatch> ninePatch = |
| 284 | NinePatch::create(kColorfulImage5x5, 5, 5, &err); |
| 285 | ASSERT_NE(nullptr, ninePatch); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 286 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 287 | std::vector<uint32_t> expectedColors = { |
| 288 | NinePatch::packRGBA((uint8_t*)RED), |
| 289 | (uint32_t)android::Res_png_9patch::NO_COLOR, |
| 290 | NinePatch::packRGBA((uint8_t*)GREEN), |
| 291 | (uint32_t)android::Res_png_9patch::TRANSPARENT_COLOR, |
| 292 | NinePatch::packRGBA((uint8_t*)BLUE), |
| 293 | NinePatch::packRGBA((uint8_t*)GREEN), |
| 294 | }; |
| 295 | EXPECT_EQ(expectedColors, ninePatch->regionColors); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | TEST(NinePatchTest, OutlineFromOpaqueImage) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 299 | std::string err; |
| 300 | std::unique_ptr<NinePatch> ninePatch = |
| 301 | NinePatch::create(kOutlineOpaque10x10, 10, 10, &err); |
| 302 | ASSERT_NE(nullptr, ninePatch); |
| 303 | EXPECT_EQ(Bounds(2, 2, 2, 2), ninePatch->outline); |
| 304 | EXPECT_EQ(0x000000ffu, ninePatch->outlineAlpha); |
| 305 | EXPECT_EQ(0.0f, ninePatch->outlineRadius); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | TEST(NinePatchTest, OutlineFromTranslucentImage) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 309 | std::string err; |
| 310 | std::unique_ptr<NinePatch> ninePatch = |
| 311 | NinePatch::create(kOutlineTranslucent10x10, 10, 10, &err); |
| 312 | ASSERT_NE(nullptr, ninePatch); |
| 313 | EXPECT_EQ(Bounds(3, 3, 3, 3), ninePatch->outline); |
| 314 | EXPECT_EQ(0x000000b3u, ninePatch->outlineAlpha); |
| 315 | EXPECT_EQ(0.0f, ninePatch->outlineRadius); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | TEST(NinePatchTest, OutlineFromOffCenterImage) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 319 | std::string err; |
| 320 | std::unique_ptr<NinePatch> ninePatch = |
| 321 | NinePatch::create(kOutlineOffsetTranslucent12x10, 12, 10, &err); |
| 322 | ASSERT_NE(nullptr, ninePatch); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 323 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 324 | // TODO(adamlesinski): The old AAPT algorithm searches from the outside to the |
| 325 | // middle |
| 326 | // for each inset. If the outline is shifted, the search may not find a closer |
| 327 | // bounds. |
| 328 | // This check should be: |
| 329 | // EXPECT_EQ(Bounds(5, 3, 3, 3), ninePatch->outline); |
| 330 | // but until I know what behaviour I'm breaking, I will leave it at the |
| 331 | // incorrect: |
| 332 | EXPECT_EQ(Bounds(4, 3, 3, 3), ninePatch->outline); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 333 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 334 | EXPECT_EQ(0x000000b3u, ninePatch->outlineAlpha); |
| 335 | EXPECT_EQ(0.0f, ninePatch->outlineRadius); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | TEST(NinePatchTest, OutlineRadius) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 339 | std::string err; |
| 340 | std::unique_ptr<NinePatch> ninePatch = |
| 341 | NinePatch::create(kOutlineRadius5x5, 5, 5, &err); |
| 342 | ASSERT_NE(nullptr, ninePatch); |
| 343 | EXPECT_EQ(Bounds(0, 0, 0, 0), ninePatch->outline); |
| 344 | EXPECT_EQ(3.4142f, ninePatch->outlineRadius); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Adam Lesinski | edba941 | 2016-10-04 17:33:04 -0700 | [diff] [blame] | 347 | ::testing::AssertionResult bigEndianOne(uint8_t* cursor) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 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"; |
Adam Lesinski | edba941 | 2016-10-04 17:33:04 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | TEST(NinePatchTest, SerializePngEndianness) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 355 | std::string err; |
| 356 | std::unique_ptr<NinePatch> ninePatch = |
| 357 | NinePatch::create(kStretchAndPadding5x5, 5, 5, &err); |
| 358 | ASSERT_NE(nullptr, ninePatch); |
Adam Lesinski | edba941 | 2016-10-04 17:33:04 -0700 | [diff] [blame] | 359 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 360 | size_t len; |
| 361 | std::unique_ptr<uint8_t[]> data = ninePatch->serializeBase(&len); |
| 362 | ASSERT_NE(nullptr, data); |
| 363 | ASSERT_NE(0u, len); |
Adam Lesinski | edba941 | 2016-10-04 17:33:04 -0700 | [diff] [blame] | 364 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 365 | // Skip past wasDeserialized + numXDivs + numYDivs + numColors + xDivsOffset + |
| 366 | // yDivsOffset |
| 367 | // (12 bytes) |
| 368 | uint8_t* cursor = data.get() + 12; |
Adam Lesinski | edba941 | 2016-10-04 17:33:04 -0700 | [diff] [blame] | 369 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 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)); |
Adam Lesinski | edba941 | 2016-10-04 17:33:04 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 377 | } // namespace aapt |