blob: 139bfbcd0f41a2abf653b9f1e76058d58dde39d9 [file] [log] [blame]
Donald Chaib8f078c2017-10-18 23:51:18 -07001/*
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
Ryan Mitchell0ce89732018-10-03 09:20:57 -070019#include "android-base/stringprintf.h"
20
Donald Chaib8f078c2017-10-18 23:51:18 -070021#include "AppInfo.h"
22#include "split/TableSplitter.h"
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -070023#include "test/Builders.h"
Donald Chaib8f078c2017-10-18 23:51:18 -070024#include "test/Test.h"
Ryan Mitchell0ce89732018-10-03 09:20:57 -070025#include "util/Files.h"
Donald Chaib8f078c2017-10-18 23:51:18 -070026
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020027using ::android::ConfigDescription;
Iurii Makhno054e4332022-10-12 16:03:03 +000028using testing::UnorderedElementsAre;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020029
Donald Chaib8f078c2017-10-18 23:51:18 -070030namespace aapt {
Ryan Mitchell0ce89732018-10-03 09:20:57 -070031
32#ifdef _WIN32
33#define CREATE_PATH(path) android::base::StringPrintf(";%s", path)
34#else
35#define CREATE_PATH(path) android::base::StringPrintf(":%s", path)
36#endif
37
Todd Kennedy9fbdf892018-08-28 16:31:15 -070038#define EXPECT_CONFIG_EQ(constraints, config) \
39 EXPECT_EQ(constraints.configs.size(), 1); \
40 EXPECT_EQ(*constraints.configs.begin(), config); \
41 constraints.configs.clear();
Donald Chaib8f078c2017-10-18 23:51:18 -070042
43TEST(UtilTest, SplitNamesAreSanitized) {
44 AppInfo app_info{"com.pkg"};
Donald Chai414e48a2017-11-09 21:06:52 -080045 SplitConstraints split_constraints{
46 {test::ParseConfigOrDie("en-rUS-land"), test::ParseConfigOrDie("b+sr+Latn")}};
Donald Chaib8f078c2017-10-18 23:51:18 -070047
48 const auto doc = GenerateSplitManifest(app_info, split_constraints);
49 const auto &root = doc->root;
50 EXPECT_EQ(root->name, "manifest");
Donald Chai414e48a2017-11-09 21:06:52 -080051 // split names cannot contain hyphens or plus signs.
52 EXPECT_EQ(root->FindAttribute("", "split")->value, "config.b_sr_Latn_en_rUS_land");
Donald Chaib8f078c2017-10-18 23:51:18 -070053 // but we should use resource qualifiers verbatim in 'targetConfig'.
Donald Chai414e48a2017-11-09 21:06:52 -080054 EXPECT_EQ(root->FindAttribute("", "targetConfig")->value, "b+sr+Latn,en-rUS-land");
Donald Chaib8f078c2017-10-18 23:51:18 -070055}
56
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -070057TEST (UtilTest, LongVersionCodeDefined) {
58 auto doc = test::BuildXmlDom(R"(
59 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
60 package="com.android.aapt.test" android:versionCode="0x1" android:versionCodeMajor="0x1">
61 </manifest>)");
62 SetLongVersionCode(doc->root.get(), 42);
63
64 auto version_code = doc->root->FindAttribute(xml::kSchemaAndroid, "versionCode");
65 ASSERT_NE(version_code, nullptr);
66 EXPECT_EQ(version_code->value, "0x0000002a");
67
68 ASSERT_NE(version_code->compiled_value, nullptr);
69 auto compiled_version_code = ValueCast<BinaryPrimitive>(version_code->compiled_value.get());
70 ASSERT_NE(compiled_version_code, nullptr);
71 EXPECT_EQ(compiled_version_code->value.data, 42U);
72
73 auto version_code_major = doc->root->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
74 EXPECT_EQ(version_code_major, nullptr);
75}
76
77TEST (UtilTest, LongVersionCodeUndefined) {
78 auto doc = test::BuildXmlDom(R"(
79 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
80 package="com.android.aapt.test">
81 </manifest>)");
82 SetLongVersionCode(doc->root.get(), 420000000000);
83
84 auto version_code = doc->root->FindAttribute(xml::kSchemaAndroid, "versionCode");
85 ASSERT_NE(version_code, nullptr);
86 EXPECT_EQ(version_code->value, "0xc9f36800");
87
88 ASSERT_NE(version_code->compiled_value, nullptr);
89 auto compiled_version_code = ValueCast<BinaryPrimitive>(version_code->compiled_value.get());
90 ASSERT_NE(compiled_version_code, nullptr);
91 EXPECT_EQ(compiled_version_code->value.data, 0xc9f36800);
92
93 auto version_code_major = doc->root->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
94 ASSERT_NE(version_code_major, nullptr);
95 EXPECT_EQ(version_code_major->value, "0x00000061");
96
97 ASSERT_NE(version_code_major->compiled_value, nullptr);
98 auto compiled_version_code_major = ValueCast<BinaryPrimitive>(
99 version_code_major->compiled_value.get());
100 ASSERT_NE(compiled_version_code_major, nullptr);
101 EXPECT_EQ(compiled_version_code_major->value.data, 0x61);
102}
103
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700104
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700105TEST (UtilTest, ParseSplitParameters) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000106 android::IDiagnostics* diagnostics = test::ContextBuilder().Build().get()->GetDiagnostics();
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700107 std::string path;
108 SplitConstraints constraints;
109 ConfigDescription expected_configuration;
110
111 // ========== Test IMSI ==========
112 // mcc: 'mcc[0-9]{3}'
113 // mnc: 'mnc[0-9]{1,3}'
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700114 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("mcc310"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700115 diagnostics, &path, &constraints));
116 expected_configuration = test::ConfigDescriptionBuilder()
117 .setMcc(0x0136)
118 .Build();
119 EXPECT_CONFIG_EQ(constraints, expected_configuration);
120
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700121 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("mcc310-mnc004"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700122 diagnostics, &path, &constraints));
123 expected_configuration = test::ConfigDescriptionBuilder()
124 .setMcc(0x0136)
125 .setMnc(0x0004)
126 .Build();
127 EXPECT_CONFIG_EQ(constraints, expected_configuration);
128
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700129 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("mcc310-mnc000"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700130 diagnostics, &path, &constraints));
131 expected_configuration = test::ConfigDescriptionBuilder()
132 .setMcc(0x0136)
133 .setMnc(0xFFFF)
134 .Build();
135 EXPECT_CONFIG_EQ(constraints, expected_configuration);
136
137 // ========== Test LOCALE ==========
138 // locale: '[a-z]{2,3}(-r[a-z]{2})?'
139 // locale: 'b+[a-z]{2,3}(+[a-z[0-9]]{2})?'
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700140 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("es"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700141 diagnostics, &path, &constraints));
142 expected_configuration = test::ConfigDescriptionBuilder()
143 .setLanguage(0x6573)
144 .Build();
145 EXPECT_CONFIG_EQ(constraints, expected_configuration);
146
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700147 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("fr-rCA"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700148 diagnostics, &path, &constraints));
149 expected_configuration = test::ConfigDescriptionBuilder()
150 .setLanguage(0x6672)
151 .setCountry(0x4341)
152 .Build();
153 EXPECT_CONFIG_EQ(constraints, expected_configuration);
154
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700155 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("b+es+419"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700156 diagnostics, &path, &constraints));
157 expected_configuration = test::ConfigDescriptionBuilder()
158 .setLanguage(0x6573)
159 .setCountry(0xA424)
160 .Build();
161 EXPECT_CONFIG_EQ(constraints, expected_configuration);
162
163 // ========== Test SCREEN_TYPE ==========
164 // orientation: '(port|land|square)'
165 // touchscreen: '(notouch|stylus|finger)'
166 // density" '(anydpi|nodpi|ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|[0-9]*dpi)'
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700167 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("square"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700168 diagnostics, &path, &constraints));
169 expected_configuration = test::ConfigDescriptionBuilder()
170 .setOrientation(0x03)
171 .Build();
172 EXPECT_CONFIG_EQ(constraints, expected_configuration);
173
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700174 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("stylus"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700175 diagnostics, &path, &constraints));
176 expected_configuration = test::ConfigDescriptionBuilder()
177 .setTouchscreen(0x02)
178 .Build();
179 EXPECT_CONFIG_EQ(constraints, expected_configuration);
180
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700181 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("xxxhdpi"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700182 diagnostics, &path, &constraints));
183 expected_configuration = test::ConfigDescriptionBuilder()
184 .setDensity(0x0280)
185 .setSdkVersion(0x0004) // version [any density requires donut]
186 .Build();
187 EXPECT_CONFIG_EQ(constraints, expected_configuration);
188
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700189 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("land-xhdpi-finger"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700190 diagnostics, &path, &constraints));
191 expected_configuration = test::ConfigDescriptionBuilder()
192 .setOrientation(0x02)
193 .setTouchscreen(0x03)
194 .setDensity(0x0140)
195 .setSdkVersion(0x0004) // version [any density requires donut]
196 .Build();
197 EXPECT_CONFIG_EQ(constraints, expected_configuration);
198
199 // ========== Test INPUT ==========
200 // keyboard: '(nokeys|qwerty|12key)'
201 // navigation: '(nonav|dpad|trackball|wheel)'
202 // inputFlags: '(keysexposed|keyshidden|keyssoft)'
203 // inputFlags: '(navexposed|navhidden)'
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700204 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("qwerty"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700205 diagnostics, &path, &constraints));
206 expected_configuration = test::ConfigDescriptionBuilder()
207 .setKeyboard(0x02)
208 .Build();
209 EXPECT_CONFIG_EQ(constraints, expected_configuration);
210
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700211 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("dpad"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700212 diagnostics, &path, &constraints));
213 expected_configuration = test::ConfigDescriptionBuilder()
214 .setNavigation(0x02)
215 .Build();
216 EXPECT_CONFIG_EQ(constraints, expected_configuration);
217
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700218 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("keyssoft-navhidden"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700219 diagnostics, &path, &constraints));
220 expected_configuration = test::ConfigDescriptionBuilder()
221 .setInputFlags(0x0B)
222 .Build();
223 EXPECT_CONFIG_EQ(constraints, expected_configuration);
224
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700225 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("keyshidden-nokeys-navexposed-trackball"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700226 diagnostics, &path, &constraints));
227 expected_configuration = test::ConfigDescriptionBuilder()
228 .setKeyboard(0x01)
229 .setNavigation(0x03)
230 .setInputFlags(0x06)
231 .Build();
232 EXPECT_CONFIG_EQ(constraints, expected_configuration);
233
234 // ========== Test SCREEN_SIZE ==========
235 // screenWidth/screenHeight: '[0-9]+x[0-9]+'
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700236 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("1920x1080"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700237 diagnostics, &path, &constraints));
238 expected_configuration = test::ConfigDescriptionBuilder()
239 .setScreenWidth(0x0780)
240 .setScreenHeight(0x0438)
241 .Build();
242 EXPECT_CONFIG_EQ(constraints, expected_configuration);
243
244 // ========== Test VERSION ==========
245 // version 'v[0-9]+'
246
247 // ========== Test SCREEN_CONFIG ==========
248 // screenLayout [direction]: '(ldltr|ldrtl)'
249 // screenLayout [size]: '(small|normal|large|xlarge)'
250 // screenLayout [long]: '(long|notlong)'
251 // uiMode [type]: '(desk|car|television|appliance|watch|vrheadset)'
252 // uiMode [night]: '(night|notnight)'
253 // smallestScreenWidthDp: 'sw[0-9]dp'
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700254 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("ldrtl"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700255 diagnostics, &path, &constraints));
256 expected_configuration = test::ConfigDescriptionBuilder()
257 .setScreenLayout(0x80)
258 .Build();
259 EXPECT_CONFIG_EQ(constraints, expected_configuration);
260
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700261 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("small"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700262 diagnostics, &path, &constraints));
263 expected_configuration = test::ConfigDescriptionBuilder()
264 .setScreenLayout(0x01)
265 .setSdkVersion(0x0004) // screenLayout (size) requires donut
266 .Build();
267 EXPECT_CONFIG_EQ(constraints, expected_configuration);
268
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700269 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("notlong"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700270 diagnostics, &path, &constraints));
271 expected_configuration = test::ConfigDescriptionBuilder()
272 .setScreenLayout(0x10)
273 .setSdkVersion(0x0004) // screenLayout (long) requires donut
274 .Build();
275 EXPECT_CONFIG_EQ(constraints, expected_configuration);
276
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700277 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("ldltr-normal-long"),
278 diagnostics, &path, &constraints));
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700279 expected_configuration = test::ConfigDescriptionBuilder()
280 .setScreenLayout(0x62)
281 .setSdkVersion(0x0004) // screenLayout (size|long) requires donut
282 .Build();
283 EXPECT_CONFIG_EQ(constraints, expected_configuration);
284
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700285 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("car"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700286 diagnostics, &path, &constraints));
287 expected_configuration = test::ConfigDescriptionBuilder()
288 .setUiMode(0x03)
289 .setSdkVersion(0x0008) // uiMode requires froyo
290 .Build();
291 EXPECT_CONFIG_EQ(constraints, expected_configuration);
292
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700293 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("vrheadset"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700294 diagnostics, &path, &constraints));
295 expected_configuration = test::ConfigDescriptionBuilder()
296 .setUiMode(0x07)
297 .setSdkVersion(0x001A) // uiMode 'vrheadset' requires oreo
298 .Build();
299 EXPECT_CONFIG_EQ(constraints, expected_configuration);
300
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700301 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("television-night"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700302 diagnostics, &path, &constraints));
303 expected_configuration = test::ConfigDescriptionBuilder()
304 .setUiMode(0x24)
305 .setSdkVersion(0x0008) // uiMode requires froyo
306 .Build();
307 EXPECT_CONFIG_EQ(constraints, expected_configuration);
308
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700309 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("sw1920dp"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700310 diagnostics, &path, &constraints));
311 expected_configuration = test::ConfigDescriptionBuilder()
312 .setSmallestScreenWidthDp(0x0780)
313 .setSdkVersion(0x000D) // smallestScreenWidthDp requires honeycomb mr2
314 .Build();
315 EXPECT_CONFIG_EQ(constraints, expected_configuration);
316
317 // ========== Test SCREEN_SIZE_DP ==========
318 // screenWidthDp: 'w[0-9]dp'
319 // screenHeightDp: 'h[0-9]dp'
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700320 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("w1920dp"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700321 diagnostics, &path, &constraints));
322 expected_configuration = test::ConfigDescriptionBuilder()
323 .setScreenWidthDp(0x0780)
324 .setSdkVersion(0x000D) // screenWidthDp requires honeycomb mr2
325 .Build();
326 EXPECT_CONFIG_EQ(constraints, expected_configuration);
327
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700328 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("h1080dp"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700329 diagnostics, &path, &constraints));
330 expected_configuration = test::ConfigDescriptionBuilder()
331 .setScreenHeightDp(0x0438)
332 .setSdkVersion(0x000D) // screenHeightDp requires honeycomb mr2
333 .Build();
334 EXPECT_CONFIG_EQ(constraints, expected_configuration);
335
336 // ========== Test SCREEN_CONFIG_2 ==========
337 // screenLayout2: '(round|notround)'
338 // colorMode: '(widecg|nowidecg)'
339 // colorMode: '(highhdr|lowdr)'
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700340 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("round"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700341 diagnostics, &path, &constraints));
342 expected_configuration = test::ConfigDescriptionBuilder()
343 .setScreenLayout2(0x02)
344 .setSdkVersion(0x0017) // screenLayout2 (round) requires marshmallow
345 .Build();
346 EXPECT_CONFIG_EQ(constraints, expected_configuration);
347
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700348 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("widecg-highdr"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700349 diagnostics, &path, &constraints));
350 expected_configuration = test::ConfigDescriptionBuilder()
351 .setColorMode(0x0A)
352 .setSdkVersion(0x001A) // colorMode (hdr|colour gamut) requires oreo
353 .Build();
354 EXPECT_CONFIG_EQ(constraints, expected_configuration);
355}
356
357TEST (UtilTest, AdjustSplitConstraintsForMinSdk) {
358 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
359
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000360 android::IDiagnostics* diagnostics = context.get()->GetDiagnostics();
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700361 std::vector<SplitConstraints> test_constraints;
362 std::string path;
363
364 test_constraints.push_back({});
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700365 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("v7"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700366 diagnostics, &path, &test_constraints.back()));
367 test_constraints.push_back({});
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700368 ASSERT_TRUE(ParseSplitParameter(CREATE_PATH("xhdpi"),
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700369 diagnostics, &path, &test_constraints.back()));
370 EXPECT_EQ(test_constraints.size(), 2);
371 EXPECT_EQ(test_constraints[0].name, "v7");
372 EXPECT_EQ(test_constraints[0].configs.size(), 1);
373 EXPECT_NE(*test_constraints[0].configs.begin(), ConfigDescription::DefaultConfig());
374 EXPECT_EQ(test_constraints[1].name, "xhdpi");
375 EXPECT_EQ(test_constraints[1].configs.size(), 1);
376 EXPECT_NE(*test_constraints[0].configs.begin(), ConfigDescription::DefaultConfig());
377
378 auto adjusted_contraints = AdjustSplitConstraintsForMinSdk(26, test_constraints);
379 EXPECT_EQ(adjusted_contraints.size(), 2);
380 EXPECT_EQ(adjusted_contraints[0].name, "v7");
381 EXPECT_EQ(adjusted_contraints[0].configs.size(), 0);
382 EXPECT_EQ(adjusted_contraints[1].name, "xhdpi");
383 EXPECT_EQ(adjusted_contraints[1].configs.size(), 1);
384 EXPECT_NE(*adjusted_contraints[1].configs.begin(), ConfigDescription::DefaultConfig());
385}
386
Izabela Orlowska5b89b2d2019-11-27 18:37:09 +0000387TEST (UtilTest, RegularExperssionsSimple) {
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100388 std::string valid(".bc$");
389 std::regex expression = GetRegularExpression(valid);
390 EXPECT_TRUE(std::regex_search("file.abc", expression));
391 EXPECT_TRUE(std::regex_search("file.123bc", expression));
392 EXPECT_FALSE(std::regex_search("abc.zip", expression));
Izabela Orlowska5b89b2d2019-11-27 18:37:09 +0000393}
394
395TEST (UtilTest, RegularExpressionComplex) {
396 std::string valid("\\.(d|D)(e|E)(x|X)$");
397 std::regex expression = GetRegularExpression(valid);
398 EXPECT_TRUE(std::regex_search("file.dex", expression));
399 EXPECT_TRUE(std::regex_search("file.DEX", expression));
400 EXPECT_TRUE(std::regex_search("file.dEx", expression));
401 EXPECT_FALSE(std::regex_search("file.dexx", expression));
402 EXPECT_FALSE(std::regex_search("dex.file", expression));
403 EXPECT_FALSE(std::regex_search("file.adex", expression));
404}
405
406TEST (UtilTest, RegularExpressionNonEnglish) {
407 std::string valid("\\.(k|K)(o|O)(ń|Ń)(c|C)(ó|Ó)(w|W)(k|K)(a|A)$");
408 std::regex expression = GetRegularExpression(valid);
409 EXPECT_TRUE(std::regex_search("file.końcówka", expression));
410 EXPECT_TRUE(std::regex_search("file.KOŃCÓWKA", expression));
411 EXPECT_TRUE(std::regex_search("file.kOńcÓwkA", expression));
412 EXPECT_FALSE(std::regex_search("file.koncowka", expression));
413}
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100414
Iurii Makhno054e4332022-10-12 16:03:03 +0000415TEST(UtilTest, ParseConfigWithDirectives) {
416 const std::string& content = R"(
417bool/remove_me#remove
418bool/keep_name#no_collapse
branliuf1ed5232022-12-16 19:02:29 +0800419layout/keep_path#no_path_shorten
Iurii Makhno054e4332022-10-12 16:03:03 +0000420string/foo#no_obfuscate
421dimen/bar#no_obfuscate
branliuf1ed5232022-12-16 19:02:29 +0800422layout/keep_name_and_path#no_collapse,no_path_shorten
Iurii Makhno054e4332022-10-12 16:03:03 +0000423)";
424 aapt::test::Context context;
425 std::unordered_set<ResourceName> resource_exclusion;
426 std::set<ResourceName> name_collapse_exemptions;
branliuf1ed5232022-12-16 19:02:29 +0800427 std::set<ResourceName> path_shorten_exemptions;
Iurii Makhno054e4332022-10-12 16:03:03 +0000428
branliuf1ed5232022-12-16 19:02:29 +0800429 EXPECT_TRUE(ParseResourceConfig(content, &context, resource_exclusion, name_collapse_exemptions,
430 path_shorten_exemptions));
Iurii Makhno054e4332022-10-12 16:03:03 +0000431
432 EXPECT_THAT(name_collapse_exemptions,
433 UnorderedElementsAre(ResourceName({}, ResourceType::kString, "foo"),
434 ResourceName({}, ResourceType::kDimen, "bar"),
branliuf1ed5232022-12-16 19:02:29 +0800435 ResourceName({}, ResourceType::kBool, "keep_name"),
436 ResourceName({}, ResourceType::kLayout, "keep_name_and_path")));
437 EXPECT_THAT(path_shorten_exemptions,
438 UnorderedElementsAre(ResourceName({}, ResourceType::kLayout, "keep_path"),
439 ResourceName({}, ResourceType::kLayout, "keep_name_and_path")));
Iurii Makhno054e4332022-10-12 16:03:03 +0000440 EXPECT_THAT(resource_exclusion,
441 UnorderedElementsAre(ResourceName({}, ResourceType::kBool, "remove_me")));
442}
443
444TEST(UtilTest, ParseConfigResourceWithPackage) {
445 const std::string& content = R"(
446package:bool/remove_me#remove
447)";
448 aapt::test::Context context;
449 std::unordered_set<ResourceName> resource_exclusion;
450 std::set<ResourceName> name_collapse_exemptions;
branliuf1ed5232022-12-16 19:02:29 +0800451 std::set<ResourceName> path_shorten_exemptions;
Iurii Makhno054e4332022-10-12 16:03:03 +0000452
branliuf1ed5232022-12-16 19:02:29 +0800453 EXPECT_FALSE(ParseResourceConfig(content, &context, resource_exclusion, name_collapse_exemptions,
454 path_shorten_exemptions));
Iurii Makhno054e4332022-10-12 16:03:03 +0000455}
456
457TEST(UtilTest, ParseConfigInvalidName) {
458 const std::string& content = R"(
459package:bool/1231#remove
460)";
461 aapt::test::Context context;
462 std::unordered_set<ResourceName> resource_exclusion;
463 std::set<ResourceName> name_collapse_exemptions;
branliuf1ed5232022-12-16 19:02:29 +0800464 std::set<ResourceName> path_shorten_exemptions;
Iurii Makhno054e4332022-10-12 16:03:03 +0000465
branliuf1ed5232022-12-16 19:02:29 +0800466 EXPECT_FALSE(ParseResourceConfig(content, &context, resource_exclusion, name_collapse_exemptions,
467 path_shorten_exemptions));
Iurii Makhno054e4332022-10-12 16:03:03 +0000468}
469
470TEST(UtilTest, ParseConfigNoHash) {
471 const std::string& content = R"(
472package:bool/my_bool
473)";
474 aapt::test::Context context;
475 std::unordered_set<ResourceName> resource_exclusion;
476 std::set<ResourceName> name_collapse_exemptions;
branliuf1ed5232022-12-16 19:02:29 +0800477 std::set<ResourceName> path_shorten_exemptions;
Iurii Makhno054e4332022-10-12 16:03:03 +0000478
branliuf1ed5232022-12-16 19:02:29 +0800479 EXPECT_FALSE(ParseResourceConfig(content, &context, resource_exclusion, name_collapse_exemptions,
480 path_shorten_exemptions));
Iurii Makhno054e4332022-10-12 16:03:03 +0000481}
482
Donald Chaib8f078c2017-10-18 23:51:18 -0700483} // namespace aapt