Donald Chai | b8f078c | 2017-10-18 23:51:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "Util.h" |
| 18 | |
| 19 | #include "AppInfo.h" |
| 20 | #include "split/TableSplitter.h" |
Ryan Mitchell | 5fa2bb1 | 2018-07-12 11:24:51 -0700 | [diff] [blame] | 21 | #include "test/Builders.h" |
Donald Chai | b8f078c | 2017-10-18 23:51:18 -0700 | [diff] [blame] | 22 | #include "test/Test.h" |
| 23 | |
| 24 | namespace aapt { |
Todd Kennedy | 9fbdf89 | 2018-08-28 16:31:15 -0700 | [diff] [blame^] | 25 | #define EXPECT_CONFIG_EQ(constraints, config) \ |
| 26 | EXPECT_EQ(constraints.configs.size(), 1); \ |
| 27 | EXPECT_EQ(*constraints.configs.begin(), config); \ |
| 28 | constraints.configs.clear(); |
Donald Chai | b8f078c | 2017-10-18 23:51:18 -0700 | [diff] [blame] | 29 | |
| 30 | TEST(UtilTest, SplitNamesAreSanitized) { |
| 31 | AppInfo app_info{"com.pkg"}; |
Donald Chai | 414e48a | 2017-11-09 21:06:52 -0800 | [diff] [blame] | 32 | SplitConstraints split_constraints{ |
| 33 | {test::ParseConfigOrDie("en-rUS-land"), test::ParseConfigOrDie("b+sr+Latn")}}; |
Donald Chai | b8f078c | 2017-10-18 23:51:18 -0700 | [diff] [blame] | 34 | |
| 35 | const auto doc = GenerateSplitManifest(app_info, split_constraints); |
| 36 | const auto &root = doc->root; |
| 37 | EXPECT_EQ(root->name, "manifest"); |
Donald Chai | 414e48a | 2017-11-09 21:06:52 -0800 | [diff] [blame] | 38 | // split names cannot contain hyphens or plus signs. |
| 39 | EXPECT_EQ(root->FindAttribute("", "split")->value, "config.b_sr_Latn_en_rUS_land"); |
Donald Chai | b8f078c | 2017-10-18 23:51:18 -0700 | [diff] [blame] | 40 | // but we should use resource qualifiers verbatim in 'targetConfig'. |
Donald Chai | 414e48a | 2017-11-09 21:06:52 -0800 | [diff] [blame] | 41 | EXPECT_EQ(root->FindAttribute("", "targetConfig")->value, "b+sr+Latn,en-rUS-land"); |
Donald Chai | b8f078c | 2017-10-18 23:51:18 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Ryan Mitchell | 5fa2bb1 | 2018-07-12 11:24:51 -0700 | [diff] [blame] | 44 | TEST (UtilTest, LongVersionCodeDefined) { |
| 45 | auto doc = test::BuildXmlDom(R"( |
| 46 | <manifest xmlns:android="http://schemas.android.com/apk/res/android" |
| 47 | package="com.android.aapt.test" android:versionCode="0x1" android:versionCodeMajor="0x1"> |
| 48 | </manifest>)"); |
| 49 | SetLongVersionCode(doc->root.get(), 42); |
| 50 | |
| 51 | auto version_code = doc->root->FindAttribute(xml::kSchemaAndroid, "versionCode"); |
| 52 | ASSERT_NE(version_code, nullptr); |
| 53 | EXPECT_EQ(version_code->value, "0x0000002a"); |
| 54 | |
| 55 | ASSERT_NE(version_code->compiled_value, nullptr); |
| 56 | auto compiled_version_code = ValueCast<BinaryPrimitive>(version_code->compiled_value.get()); |
| 57 | ASSERT_NE(compiled_version_code, nullptr); |
| 58 | EXPECT_EQ(compiled_version_code->value.data, 42U); |
| 59 | |
| 60 | auto version_code_major = doc->root->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor"); |
| 61 | EXPECT_EQ(version_code_major, nullptr); |
| 62 | } |
| 63 | |
| 64 | TEST (UtilTest, LongVersionCodeUndefined) { |
| 65 | auto doc = test::BuildXmlDom(R"( |
| 66 | <manifest xmlns:android="http://schemas.android.com/apk/res/android" |
| 67 | package="com.android.aapt.test"> |
| 68 | </manifest>)"); |
| 69 | SetLongVersionCode(doc->root.get(), 420000000000); |
| 70 | |
| 71 | auto version_code = doc->root->FindAttribute(xml::kSchemaAndroid, "versionCode"); |
| 72 | ASSERT_NE(version_code, nullptr); |
| 73 | EXPECT_EQ(version_code->value, "0xc9f36800"); |
| 74 | |
| 75 | ASSERT_NE(version_code->compiled_value, nullptr); |
| 76 | auto compiled_version_code = ValueCast<BinaryPrimitive>(version_code->compiled_value.get()); |
| 77 | ASSERT_NE(compiled_version_code, nullptr); |
| 78 | EXPECT_EQ(compiled_version_code->value.data, 0xc9f36800); |
| 79 | |
| 80 | auto version_code_major = doc->root->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor"); |
| 81 | ASSERT_NE(version_code_major, nullptr); |
| 82 | EXPECT_EQ(version_code_major->value, "0x00000061"); |
| 83 | |
| 84 | ASSERT_NE(version_code_major->compiled_value, nullptr); |
| 85 | auto compiled_version_code_major = ValueCast<BinaryPrimitive>( |
| 86 | version_code_major->compiled_value.get()); |
| 87 | ASSERT_NE(compiled_version_code_major, nullptr); |
| 88 | EXPECT_EQ(compiled_version_code_major->value.data, 0x61); |
| 89 | } |
| 90 | |
Todd Kennedy | 9fbdf89 | 2018-08-28 16:31:15 -0700 | [diff] [blame^] | 91 | |
| 92 | TEST (UtilTest, ParseSplitParameter) { |
| 93 | IDiagnostics* diagnostics = test::ContextBuilder().Build().get()->GetDiagnostics(); |
| 94 | std::string path; |
| 95 | SplitConstraints constraints; |
| 96 | ConfigDescription expected_configuration; |
| 97 | |
| 98 | // ========== Test IMSI ========== |
| 99 | // mcc: 'mcc[0-9]{3}' |
| 100 | // mnc: 'mnc[0-9]{1,3}' |
| 101 | ASSERT_TRUE(ParseSplitParameter(":mcc310", |
| 102 | diagnostics, &path, &constraints)); |
| 103 | expected_configuration = test::ConfigDescriptionBuilder() |
| 104 | .setMcc(0x0136) |
| 105 | .Build(); |
| 106 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 107 | |
| 108 | ASSERT_TRUE(ParseSplitParameter(":mcc310-mnc004", |
| 109 | diagnostics, &path, &constraints)); |
| 110 | expected_configuration = test::ConfigDescriptionBuilder() |
| 111 | .setMcc(0x0136) |
| 112 | .setMnc(0x0004) |
| 113 | .Build(); |
| 114 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 115 | |
| 116 | ASSERT_TRUE(ParseSplitParameter(":mcc310-mnc000", |
| 117 | diagnostics, &path, &constraints)); |
| 118 | expected_configuration = test::ConfigDescriptionBuilder() |
| 119 | .setMcc(0x0136) |
| 120 | .setMnc(0xFFFF) |
| 121 | .Build(); |
| 122 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 123 | |
| 124 | // ========== Test LOCALE ========== |
| 125 | // locale: '[a-z]{2,3}(-r[a-z]{2})?' |
| 126 | // locale: 'b+[a-z]{2,3}(+[a-z[0-9]]{2})?' |
| 127 | ASSERT_TRUE(ParseSplitParameter(":es", |
| 128 | diagnostics, &path, &constraints)); |
| 129 | expected_configuration = test::ConfigDescriptionBuilder() |
| 130 | .setLanguage(0x6573) |
| 131 | .Build(); |
| 132 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 133 | |
| 134 | ASSERT_TRUE(ParseSplitParameter(":fr-rCA", |
| 135 | diagnostics, &path, &constraints)); |
| 136 | expected_configuration = test::ConfigDescriptionBuilder() |
| 137 | .setLanguage(0x6672) |
| 138 | .setCountry(0x4341) |
| 139 | .Build(); |
| 140 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 141 | |
| 142 | ASSERT_TRUE(ParseSplitParameter(":b+es+419", |
| 143 | diagnostics, &path, &constraints)); |
| 144 | expected_configuration = test::ConfigDescriptionBuilder() |
| 145 | .setLanguage(0x6573) |
| 146 | .setCountry(0xA424) |
| 147 | .Build(); |
| 148 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 149 | |
| 150 | // ========== Test SCREEN_TYPE ========== |
| 151 | // orientation: '(port|land|square)' |
| 152 | // touchscreen: '(notouch|stylus|finger)' |
| 153 | // density" '(anydpi|nodpi|ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|[0-9]*dpi)' |
| 154 | ASSERT_TRUE(ParseSplitParameter(":square", |
| 155 | diagnostics, &path, &constraints)); |
| 156 | expected_configuration = test::ConfigDescriptionBuilder() |
| 157 | .setOrientation(0x03) |
| 158 | .Build(); |
| 159 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 160 | |
| 161 | ASSERT_TRUE(ParseSplitParameter(":stylus", |
| 162 | diagnostics, &path, &constraints)); |
| 163 | expected_configuration = test::ConfigDescriptionBuilder() |
| 164 | .setTouchscreen(0x02) |
| 165 | .Build(); |
| 166 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 167 | |
| 168 | ASSERT_TRUE(ParseSplitParameter(":xxxhdpi", |
| 169 | diagnostics, &path, &constraints)); |
| 170 | expected_configuration = test::ConfigDescriptionBuilder() |
| 171 | .setDensity(0x0280) |
| 172 | .setSdkVersion(0x0004) // version [any density requires donut] |
| 173 | .Build(); |
| 174 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 175 | |
| 176 | ASSERT_TRUE(ParseSplitParameter(":land-xhdpi-finger", |
| 177 | diagnostics, &path, &constraints)); |
| 178 | expected_configuration = test::ConfigDescriptionBuilder() |
| 179 | .setOrientation(0x02) |
| 180 | .setTouchscreen(0x03) |
| 181 | .setDensity(0x0140) |
| 182 | .setSdkVersion(0x0004) // version [any density requires donut] |
| 183 | .Build(); |
| 184 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 185 | |
| 186 | // ========== Test INPUT ========== |
| 187 | // keyboard: '(nokeys|qwerty|12key)' |
| 188 | // navigation: '(nonav|dpad|trackball|wheel)' |
| 189 | // inputFlags: '(keysexposed|keyshidden|keyssoft)' |
| 190 | // inputFlags: '(navexposed|navhidden)' |
| 191 | ASSERT_TRUE(ParseSplitParameter(":qwerty", |
| 192 | diagnostics, &path, &constraints)); |
| 193 | expected_configuration = test::ConfigDescriptionBuilder() |
| 194 | .setKeyboard(0x02) |
| 195 | .Build(); |
| 196 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 197 | |
| 198 | ASSERT_TRUE(ParseSplitParameter(":dpad", |
| 199 | diagnostics, &path, &constraints)); |
| 200 | expected_configuration = test::ConfigDescriptionBuilder() |
| 201 | .setNavigation(0x02) |
| 202 | .Build(); |
| 203 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 204 | |
| 205 | ASSERT_TRUE(ParseSplitParameter(":keyssoft-navhidden", |
| 206 | diagnostics, &path, &constraints)); |
| 207 | expected_configuration = test::ConfigDescriptionBuilder() |
| 208 | .setInputFlags(0x0B) |
| 209 | .Build(); |
| 210 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 211 | |
| 212 | ASSERT_TRUE(ParseSplitParameter(":keyshidden-nokeys-navexposed-trackball", |
| 213 | diagnostics, &path, &constraints)); |
| 214 | expected_configuration = test::ConfigDescriptionBuilder() |
| 215 | .setKeyboard(0x01) |
| 216 | .setNavigation(0x03) |
| 217 | .setInputFlags(0x06) |
| 218 | .Build(); |
| 219 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 220 | |
| 221 | // ========== Test SCREEN_SIZE ========== |
| 222 | // screenWidth/screenHeight: '[0-9]+x[0-9]+' |
| 223 | ASSERT_TRUE(ParseSplitParameter(":1920x1080", |
| 224 | diagnostics, &path, &constraints)); |
| 225 | expected_configuration = test::ConfigDescriptionBuilder() |
| 226 | .setScreenWidth(0x0780) |
| 227 | .setScreenHeight(0x0438) |
| 228 | .Build(); |
| 229 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 230 | |
| 231 | // ========== Test VERSION ========== |
| 232 | // version 'v[0-9]+' |
| 233 | |
| 234 | // ========== Test SCREEN_CONFIG ========== |
| 235 | // screenLayout [direction]: '(ldltr|ldrtl)' |
| 236 | // screenLayout [size]: '(small|normal|large|xlarge)' |
| 237 | // screenLayout [long]: '(long|notlong)' |
| 238 | // uiMode [type]: '(desk|car|television|appliance|watch|vrheadset)' |
| 239 | // uiMode [night]: '(night|notnight)' |
| 240 | // smallestScreenWidthDp: 'sw[0-9]dp' |
| 241 | ASSERT_TRUE(ParseSplitParameter(":ldrtl", |
| 242 | diagnostics, &path, &constraints)); |
| 243 | expected_configuration = test::ConfigDescriptionBuilder() |
| 244 | .setScreenLayout(0x80) |
| 245 | .Build(); |
| 246 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 247 | |
| 248 | ASSERT_TRUE(ParseSplitParameter(":small", |
| 249 | diagnostics, &path, &constraints)); |
| 250 | expected_configuration = test::ConfigDescriptionBuilder() |
| 251 | .setScreenLayout(0x01) |
| 252 | .setSdkVersion(0x0004) // screenLayout (size) requires donut |
| 253 | .Build(); |
| 254 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 255 | |
| 256 | ASSERT_TRUE(ParseSplitParameter(":notlong", |
| 257 | diagnostics, &path, &constraints)); |
| 258 | expected_configuration = test::ConfigDescriptionBuilder() |
| 259 | .setScreenLayout(0x10) |
| 260 | .setSdkVersion(0x0004) // screenLayout (long) requires donut |
| 261 | .Build(); |
| 262 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 263 | |
| 264 | ASSERT_TRUE(ParseSplitParameter(":ldltr-normal-long", |
| 265 | diagnostics, &path, &constraints)); |
| 266 | expected_configuration = test::ConfigDescriptionBuilder() |
| 267 | .setScreenLayout(0x62) |
| 268 | .setSdkVersion(0x0004) // screenLayout (size|long) requires donut |
| 269 | .Build(); |
| 270 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 271 | |
| 272 | ASSERT_TRUE(ParseSplitParameter(":car", |
| 273 | diagnostics, &path, &constraints)); |
| 274 | expected_configuration = test::ConfigDescriptionBuilder() |
| 275 | .setUiMode(0x03) |
| 276 | .setSdkVersion(0x0008) // uiMode requires froyo |
| 277 | .Build(); |
| 278 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 279 | |
| 280 | ASSERT_TRUE(ParseSplitParameter(":vrheadset", |
| 281 | diagnostics, &path, &constraints)); |
| 282 | expected_configuration = test::ConfigDescriptionBuilder() |
| 283 | .setUiMode(0x07) |
| 284 | .setSdkVersion(0x001A) // uiMode 'vrheadset' requires oreo |
| 285 | .Build(); |
| 286 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 287 | |
| 288 | ASSERT_TRUE(ParseSplitParameter(":television-night", |
| 289 | diagnostics, &path, &constraints)); |
| 290 | expected_configuration = test::ConfigDescriptionBuilder() |
| 291 | .setUiMode(0x24) |
| 292 | .setSdkVersion(0x0008) // uiMode requires froyo |
| 293 | .Build(); |
| 294 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 295 | |
| 296 | ASSERT_TRUE(ParseSplitParameter(":sw1920dp", |
| 297 | diagnostics, &path, &constraints)); |
| 298 | expected_configuration = test::ConfigDescriptionBuilder() |
| 299 | .setSmallestScreenWidthDp(0x0780) |
| 300 | .setSdkVersion(0x000D) // smallestScreenWidthDp requires honeycomb mr2 |
| 301 | .Build(); |
| 302 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 303 | |
| 304 | // ========== Test SCREEN_SIZE_DP ========== |
| 305 | // screenWidthDp: 'w[0-9]dp' |
| 306 | // screenHeightDp: 'h[0-9]dp' |
| 307 | ASSERT_TRUE(ParseSplitParameter(":w1920dp", |
| 308 | diagnostics, &path, &constraints)); |
| 309 | expected_configuration = test::ConfigDescriptionBuilder() |
| 310 | .setScreenWidthDp(0x0780) |
| 311 | .setSdkVersion(0x000D) // screenWidthDp requires honeycomb mr2 |
| 312 | .Build(); |
| 313 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 314 | |
| 315 | ASSERT_TRUE(ParseSplitParameter(":h1080dp", |
| 316 | diagnostics, &path, &constraints)); |
| 317 | expected_configuration = test::ConfigDescriptionBuilder() |
| 318 | .setScreenHeightDp(0x0438) |
| 319 | .setSdkVersion(0x000D) // screenHeightDp requires honeycomb mr2 |
| 320 | .Build(); |
| 321 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 322 | |
| 323 | // ========== Test SCREEN_CONFIG_2 ========== |
| 324 | // screenLayout2: '(round|notround)' |
| 325 | // colorMode: '(widecg|nowidecg)' |
| 326 | // colorMode: '(highhdr|lowdr)' |
| 327 | ASSERT_TRUE(ParseSplitParameter(":round", |
| 328 | diagnostics, &path, &constraints)); |
| 329 | expected_configuration = test::ConfigDescriptionBuilder() |
| 330 | .setScreenLayout2(0x02) |
| 331 | .setSdkVersion(0x0017) // screenLayout2 (round) requires marshmallow |
| 332 | .Build(); |
| 333 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 334 | |
| 335 | ASSERT_TRUE(ParseSplitParameter(":widecg-highdr", |
| 336 | diagnostics, &path, &constraints)); |
| 337 | expected_configuration = test::ConfigDescriptionBuilder() |
| 338 | .setColorMode(0x0A) |
| 339 | .setSdkVersion(0x001A) // colorMode (hdr|colour gamut) requires oreo |
| 340 | .Build(); |
| 341 | EXPECT_CONFIG_EQ(constraints, expected_configuration); |
| 342 | } |
| 343 | |
| 344 | TEST (UtilTest, AdjustSplitConstraintsForMinSdk) { |
| 345 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 346 | |
| 347 | IDiagnostics* diagnostics = context.get()->GetDiagnostics(); |
| 348 | std::vector<SplitConstraints> test_constraints; |
| 349 | std::string path; |
| 350 | |
| 351 | test_constraints.push_back({}); |
| 352 | ASSERT_TRUE(ParseSplitParameter(":v7", |
| 353 | diagnostics, &path, &test_constraints.back())); |
| 354 | test_constraints.push_back({}); |
| 355 | ASSERT_TRUE(ParseSplitParameter(":xhdpi", |
| 356 | diagnostics, &path, &test_constraints.back())); |
| 357 | EXPECT_EQ(test_constraints.size(), 2); |
| 358 | EXPECT_EQ(test_constraints[0].name, "v7"); |
| 359 | EXPECT_EQ(test_constraints[0].configs.size(), 1); |
| 360 | EXPECT_NE(*test_constraints[0].configs.begin(), ConfigDescription::DefaultConfig()); |
| 361 | EXPECT_EQ(test_constraints[1].name, "xhdpi"); |
| 362 | EXPECT_EQ(test_constraints[1].configs.size(), 1); |
| 363 | EXPECT_NE(*test_constraints[0].configs.begin(), ConfigDescription::DefaultConfig()); |
| 364 | |
| 365 | auto adjusted_contraints = AdjustSplitConstraintsForMinSdk(26, test_constraints); |
| 366 | EXPECT_EQ(adjusted_contraints.size(), 2); |
| 367 | EXPECT_EQ(adjusted_contraints[0].name, "v7"); |
| 368 | EXPECT_EQ(adjusted_contraints[0].configs.size(), 0); |
| 369 | EXPECT_EQ(adjusted_contraints[1].name, "xhdpi"); |
| 370 | EXPECT_EQ(adjusted_contraints[1].configs.size(), 1); |
| 371 | EXPECT_NE(*adjusted_contraints[1].configs.begin(), ConfigDescription::DefaultConfig()); |
| 372 | } |
| 373 | |
Donald Chai | b8f078c | 2017-10-18 23:51:18 -0700 | [diff] [blame] | 374 | } // namespace aapt |