blob: 3654901e3e02164d2c28fb50ad1f8263ae9ef15f [file] [log] [blame]
Shane Farmer74cdea32017-05-12 16:22:36 -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 "configuration/ConfigurationParser.h"
18
19#include <string>
20
Shane Farmer74cdea32017-05-12 16:22:36 -070021#include "androidfw/ResourceTypes.h"
22
23#include "test/Test.h"
24#include "xml/XmlDom.h"
25
26namespace aapt {
Shane Farmer3edd4722017-09-01 14:34:22 -070027
28namespace configuration {
29void PrintTo(const AndroidSdk& sdk, std::ostream* os) {
30 *os << "SDK: min=" << sdk.min_sdk_version.value_or_default(-1)
31 << ", target=" << sdk.target_sdk_version.value_or_default(-1)
32 << ", max=" << sdk.max_sdk_version.value_or_default(-1);
33}
34} // namespace configuration
35
Shane Farmer74cdea32017-05-12 16:22:36 -070036namespace {
37
Adam Lesinski6b372992017-08-09 10:54:23 -070038using ::android::ResTable_config;
Shane Farmer74cdea32017-05-12 16:22:36 -070039using configuration::Abi;
40using configuration::AndroidSdk;
Shane Farmer9f0e7f12017-06-22 12:26:44 -070041using configuration::Artifact;
Shane Farmer280be342017-06-21 15:20:15 -070042using configuration::PostProcessingConfiguration;
Shane Farmer74cdea32017-05-12 16:22:36 -070043using configuration::DeviceFeature;
44using configuration::GlTexture;
45using configuration::Locale;
46using configuration::AndroidManifest;
Adam Lesinski6b372992017-08-09 10:54:23 -070047using ::testing::ElementsAre;
Shane Farmer74cdea32017-05-12 16:22:36 -070048using xml::Element;
49using xml::NodeCast;
50
51constexpr const char* kValidConfig = R"(<?xml version="1.0" encoding="utf-8" ?>
52<post-process xmlns="http://schemas.android.com/tools/aapt">
53 <groups>
54 <abi-group label="arm">
55 <abi>armeabi-v7a</abi>
56 <abi>arm64-v8a</abi>
57 </abi-group>
58 <abi-group label="other">
59 <abi>x86</abi>
60 <abi>mips</abi>
61 </abi-group>
62 <screen-density-group label="large">
63 <screen-density>xhdpi</screen-density>
64 <screen-density>xxhdpi</screen-density>
65 <screen-density>xxxhdpi</screen-density>
66 </screen-density-group>
67 <screen-density-group label="alldpi">
68 <screen-density>ldpi</screen-density>
69 <screen-density>mdpi</screen-density>
70 <screen-density>hdpi</screen-density>
71 <screen-density>xhdpi</screen-density>
72 <screen-density>xxhdpi</screen-density>
73 <screen-density>xxxhdpi</screen-density>
74 </screen-density-group>
75 <locale-group label="europe">
Shane Farmer0a5b2012017-06-22 12:24:12 -070076 <locale>en</locale>
77 <locale>es</locale>
78 <locale>fr</locale>
79 <locale>de</locale>
Shane Farmer74cdea32017-05-12 16:22:36 -070080 </locale-group>
81 <locale-group label="north-america">
Shane Farmer0a5b2012017-06-22 12:24:12 -070082 <locale>en</locale>
83 <locale>es-rMX</locale>
84 <locale>fr-rCA</locale>
Shane Farmer74cdea32017-05-12 16:22:36 -070085 </locale-group>
Shane Farmerefe45392017-08-21 14:39:28 -070086 <android-sdk-group label="v19">
Shane Farmer74cdea32017-05-12 16:22:36 -070087 <android-sdk
Shane Farmer3edd4722017-09-01 14:34:22 -070088 minSdkVersion="19"
89 targetSdkVersion="24"
90 maxSdkVersion="25">
Shane Farmer74cdea32017-05-12 16:22:36 -070091 <manifest>
92 <!--- manifest additions here XSLT? TODO -->
93 </manifest>
94 </android-sdk>
95 </android-sdk-group>
96 <gl-texture-group label="dxt1">
97 <gl-texture name="GL_EXT_texture_compression_dxt1">
98 <texture-path>assets/dxt1/*</texture-path>
99 </gl-texture>
100 </gl-texture-group>
101 <device-feature-group label="low-latency">
102 <supports-feature>android.hardware.audio.low_latency</supports-feature>
103 </device-feature-group>
104 </groups>
105 <artifacts>
106 <artifact-format>
107 ${base}.${abi}.${screen-density}.${locale}.${sdk}.${gl}.${feature}.release
108 </artifact-format>
109 <artifact
110 name="art1"
111 abi-group="arm"
112 screen-density-group="large"
113 locale-group="europe"
Shane Farmerefe45392017-08-21 14:39:28 -0700114 android-sdk-group="v19"
Shane Farmer74cdea32017-05-12 16:22:36 -0700115 gl-texture-group="dxt1"
116 device-feature-group="low-latency"/>
117 <artifact
118 name="art2"
119 abi-group="other"
120 screen-density-group="alldpi"
121 locale-group="north-america"
Shane Farmerefe45392017-08-21 14:39:28 -0700122 android-sdk-group="v19"
Shane Farmer74cdea32017-05-12 16:22:36 -0700123 gl-texture-group="dxt1"
124 device-feature-group="low-latency"/>
125 </artifacts>
126</post-process>
127)";
128
129class ConfigurationParserTest : public ConfigurationParser, public ::testing::Test {
130 public:
131 ConfigurationParserTest() : ConfigurationParser("") {}
132
133 protected:
134 StdErrDiagnostics diag_;
135};
136
Shane Farmerb1027272017-06-14 09:10:28 -0700137TEST_F(ConfigurationParserTest, ForPath_NoFile) {
138 auto result = ConfigurationParser::ForPath("./does_not_exist.xml");
139 EXPECT_FALSE(result);
140}
141
Shane Farmer74cdea32017-05-12 16:22:36 -0700142TEST_F(ConfigurationParserTest, ValidateFile) {
143 auto parser = ConfigurationParser::ForContents(kValidConfig).WithDiagnostics(&diag_);
144 auto result = parser.Parse();
145 ASSERT_TRUE(result);
Shane Farmer280be342017-06-21 15:20:15 -0700146 PostProcessingConfiguration& value = result.value();
Shane Farmer74cdea32017-05-12 16:22:36 -0700147 EXPECT_EQ(2ul, value.artifacts.size());
148 ASSERT_TRUE(value.artifact_format);
149 EXPECT_EQ(
150 "${base}.${abi}.${screen-density}.${locale}.${sdk}.${gl}.${feature}.release",
151 value.artifact_format.value()
152 );
153
154 EXPECT_EQ(2ul, value.abi_groups.size());
155 EXPECT_EQ(2ul, value.abi_groups["arm"].size());
156 EXPECT_EQ(2ul, value.abi_groups["other"].size());
157
158 EXPECT_EQ(2ul, value.screen_density_groups.size());
159 EXPECT_EQ(3ul, value.screen_density_groups["large"].size());
160 EXPECT_EQ(6ul, value.screen_density_groups["alldpi"].size());
161
Shane Farmer0a5b2012017-06-22 12:24:12 -0700162 EXPECT_EQ(2ul, value.locale_groups.size());
Shane Farmer74cdea32017-05-12 16:22:36 -0700163 EXPECT_EQ(4ul, value.locale_groups["europe"].size());
164 EXPECT_EQ(3ul, value.locale_groups["north-america"].size());
Shane Farmer74cdea32017-05-12 16:22:36 -0700165
166 EXPECT_EQ(1ul, value.android_sdk_groups.size());
Shane Farmerefe45392017-08-21 14:39:28 -0700167 EXPECT_TRUE(value.android_sdk_groups["v19"].min_sdk_version);
Shane Farmer3edd4722017-09-01 14:34:22 -0700168 EXPECT_EQ(19, value.android_sdk_groups["v19"].min_sdk_version.value());
Shane Farmer74cdea32017-05-12 16:22:36 -0700169
170 EXPECT_EQ(1ul, value.gl_texture_groups.size());
171 EXPECT_EQ(1ul, value.gl_texture_groups["dxt1"].size());
172
173 EXPECT_EQ(1ul, value.device_feature_groups.size());
174 EXPECT_EQ(1ul, value.device_feature_groups["low-latency"].size());
175}
176
177TEST_F(ConfigurationParserTest, InvalidNamespace) {
178 constexpr const char* invalid_ns = R"(<?xml version="1.0" encoding="utf-8" ?>
179 <post-process xmlns="http://schemas.android.com/tools/another-unknown-tool" />)";
180
181 auto result = ConfigurationParser::ForContents(invalid_ns).Parse();
182 ASSERT_FALSE(result);
183}
184
185TEST_F(ConfigurationParserTest, ArtifactAction) {
Shane Farmer810fd182017-09-21 14:37:44 -0700186 PostProcessingConfiguration config;
187 {
188 const auto doc = test::BuildXmlDom(R"xml(
189 <artifact
190 abi-group="arm"
191 screen-density-group="large"
192 locale-group="europe"
193 android-sdk-group="v19"
194 gl-texture-group="dxt1"
195 device-feature-group="low-latency"/>)xml");
196
197 ASSERT_TRUE(artifact_handler_(&config, NodeCast<Element>(doc->root.get()), &diag_));
198
199 EXPECT_EQ(1ul, config.artifacts.size());
200
201 auto& artifact = config.artifacts.back();
202 EXPECT_FALSE(artifact.name); // TODO: make this fail.
203 EXPECT_EQ(1, artifact.version);
204 EXPECT_EQ("arm", artifact.abi_group.value());
205 EXPECT_EQ("large", artifact.screen_density_group.value());
206 EXPECT_EQ("europe", artifact.locale_group.value());
207 EXPECT_EQ("v19", artifact.android_sdk_group.value());
208 EXPECT_EQ("dxt1", artifact.gl_texture_group.value());
209 EXPECT_EQ("low-latency", artifact.device_feature_group.value());
210 }
211
212 {
213 // Perform a second action to ensure we get 2 artifacts.
214 const auto doc = test::BuildXmlDom(R"xml(
215 <artifact
216 abi-group="other"
217 screen-density-group="large"
218 locale-group="europe"
219 android-sdk-group="v19"
220 gl-texture-group="dxt1"
221 device-feature-group="low-latency"/>)xml");
222
223 ASSERT_TRUE(artifact_handler_(&config, NodeCast<Element>(doc.get()->root.get()), &diag_));
224 EXPECT_EQ(2ul, config.artifacts.size());
225 EXPECT_EQ(2, config.artifacts.back().version);
226 }
227
228 {
229 // Perform a third action with a set version code.
230 const auto doc = test::BuildXmlDom(R"xml(
Shane Farmer74cdea32017-05-12 16:22:36 -0700231 <artifact
Shane Farmer810fd182017-09-21 14:37:44 -0700232 version="5"
233 abi-group="other"
Shane Farmer74cdea32017-05-12 16:22:36 -0700234 screen-density-group="large"
235 locale-group="europe"
Shane Farmerefe45392017-08-21 14:39:28 -0700236 android-sdk-group="v19"
Shane Farmer74cdea32017-05-12 16:22:36 -0700237 gl-texture-group="dxt1"
Shane Farmer810fd182017-09-21 14:37:44 -0700238 device-feature-group="low-latency"/>)xml");
Shane Farmer74cdea32017-05-12 16:22:36 -0700239
Shane Farmer810fd182017-09-21 14:37:44 -0700240 ASSERT_TRUE(artifact_handler_(&config, NodeCast<Element>(doc.get()->root.get()), &diag_));
241 EXPECT_EQ(3ul, config.artifacts.size());
242 EXPECT_EQ(5, config.artifacts.back().version);
243 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700244
Shane Farmer810fd182017-09-21 14:37:44 -0700245 {
246 // Perform a fourth action to ensure the version code still increments.
247 const auto doc = test::BuildXmlDom(R"xml(
Shane Farmer57669432017-06-19 12:52:04 -0700248 <artifact
249 abi-group="other"
250 screen-density-group="large"
251 locale-group="europe"
Shane Farmerefe45392017-08-21 14:39:28 -0700252 android-sdk-group="v19"
Shane Farmer57669432017-06-19 12:52:04 -0700253 gl-texture-group="dxt1"
Shane Farmer810fd182017-09-21 14:37:44 -0700254 device-feature-group="low-latency"/>)xml");
Shane Farmer57669432017-06-19 12:52:04 -0700255
Shane Farmer810fd182017-09-21 14:37:44 -0700256 ASSERT_TRUE(artifact_handler_(&config, NodeCast<Element>(doc.get()->root.get()), &diag_));
257 EXPECT_EQ(4ul, config.artifacts.size());
258 EXPECT_EQ(6, config.artifacts.back().version);
259 }
260}
261
262TEST_F(ConfigurationParserTest, DuplicateArtifactVersion) {
263 static constexpr const char* configuration = R"xml(<?xml version="1.0" encoding="utf-8" ?>
264 <pst-process xmlns="http://schemas.android.com/tools/aapt">>
265 <artifacts>
266 <artifact-format>
267 ${base}.${abi}.${screen-density}.${locale}.${sdk}.${gl}.${feature}.release
268 </artifact-format>
269 <artifact
270 name="art1"
271 abi-group="arm"
272 screen-density-group="large"
273 locale-group="europe"
274 android-sdk-group="v19"
275 gl-texture-group="dxt1"
276 device-feature-group="low-latency"/>
277 <artifact
278 name="art2"
279 version = "1"
280 abi-group="other"
281 screen-density-group="alldpi"
282 locale-group="north-america"
283 android-sdk-group="v19"
284 gl-texture-group="dxt1"
285 device-feature-group="low-latency"/>
286 </artifacts>
287 </post-process>)xml";
288 auto result = ConfigurationParser::ForContents(configuration).Parse();
289 ASSERT_FALSE(result);
Shane Farmer74cdea32017-05-12 16:22:36 -0700290}
291
292TEST_F(ConfigurationParserTest, ArtifactFormatAction) {
Shane Farmer810fd182017-09-21 14:37:44 -0700293 const auto doc = test::BuildXmlDom(R"xml(
Shane Farmer74cdea32017-05-12 16:22:36 -0700294 <artifact-format>
295 ${base}.${abi}.${screen-density}.${locale}.${sdk}.${gl}.${feature}.release
Shane Farmer810fd182017-09-21 14:37:44 -0700296 </artifact-format>)xml");
Shane Farmer74cdea32017-05-12 16:22:36 -0700297
Shane Farmer280be342017-06-21 15:20:15 -0700298 PostProcessingConfiguration config;
Shane Farmer74cdea32017-05-12 16:22:36 -0700299 bool ok = artifact_format_handler_(&config, NodeCast<Element>(doc.get()->root.get()), &diag_);
300 ASSERT_TRUE(ok);
301 ASSERT_TRUE(config.artifact_format);
302 EXPECT_EQ(
303 "${base}.${abi}.${screen-density}.${locale}.${sdk}.${gl}.${feature}.release",
304 static_cast<std::string>(config.artifact_format.value())
305 );
306}
307
308TEST_F(ConfigurationParserTest, AbiGroupAction) {
309 static constexpr const char* xml = R"xml(
310 <abi-group label="arm">
311 <!-- First comment. -->
312 <abi>
313 armeabi-v7a
314 </abi>
315 <!-- Another comment. -->
316 <abi>arm64-v8a</abi>
317 </abi-group>)xml";
318
319 auto doc = test::BuildXmlDom(xml);
320
Shane Farmer280be342017-06-21 15:20:15 -0700321 PostProcessingConfiguration config;
Shane Farmer74cdea32017-05-12 16:22:36 -0700322 bool ok = abi_group_handler_(&config, NodeCast<Element>(doc.get()->root.get()), &diag_);
323 ASSERT_TRUE(ok);
324
325 EXPECT_EQ(1ul, config.abi_groups.size());
326 ASSERT_EQ(1u, config.abi_groups.count("arm"));
327
328 auto& out = config.abi_groups["arm"];
329 ASSERT_THAT(out, ElementsAre(Abi::kArmV7a, Abi::kArm64V8a));
330}
331
332TEST_F(ConfigurationParserTest, ScreenDensityGroupAction) {
333 static constexpr const char* xml = R"xml(
334 <screen-density-group label="large">
335 <screen-density>xhdpi</screen-density>
336 <screen-density>
337 xxhdpi
338 </screen-density>
339 <screen-density>xxxhdpi</screen-density>
340 </screen-density-group>)xml";
341
342 auto doc = test::BuildXmlDom(xml);
343
Shane Farmer280be342017-06-21 15:20:15 -0700344 PostProcessingConfiguration config;
Shane Farmer74cdea32017-05-12 16:22:36 -0700345 bool ok =
346 screen_density_group_handler_(&config, NodeCast<Element>(doc.get()->root.get()), &diag_);
347 ASSERT_TRUE(ok);
348
349 EXPECT_EQ(1ul, config.screen_density_groups.size());
350 ASSERT_EQ(1u, config.screen_density_groups.count("large"));
351
352 ConfigDescription xhdpi;
353 xhdpi.density = ResTable_config::DENSITY_XHIGH;
354 ConfigDescription xxhdpi;
355 xxhdpi.density = ResTable_config::DENSITY_XXHIGH;
356 ConfigDescription xxxhdpi;
357 xxxhdpi.density = ResTable_config::DENSITY_XXXHIGH;
358
359 auto& out = config.screen_density_groups["large"];
360 ASSERT_THAT(out, ElementsAre(xhdpi, xxhdpi, xxxhdpi));
361}
362
363TEST_F(ConfigurationParserTest, LocaleGroupAction) {
364 static constexpr const char* xml = R"xml(
365 <locale-group label="europe">
Shane Farmer0a5b2012017-06-22 12:24:12 -0700366 <locale>en</locale>
367 <locale>es</locale>
368 <locale>fr</locale>
369 <locale>de</locale>
Shane Farmer74cdea32017-05-12 16:22:36 -0700370 </locale-group>)xml";
371
372 auto doc = test::BuildXmlDom(xml);
373
Shane Farmer280be342017-06-21 15:20:15 -0700374 PostProcessingConfiguration config;
Shane Farmer74cdea32017-05-12 16:22:36 -0700375 bool ok = locale_group_handler_(&config, NodeCast<Element>(doc.get()->root.get()), &diag_);
376 ASSERT_TRUE(ok);
377
378 ASSERT_EQ(1ul, config.locale_groups.size());
379 ASSERT_EQ(1u, config.locale_groups.count("europe"));
380
Shane Farmer0a5b2012017-06-22 12:24:12 -0700381 const auto& out = config.locale_groups["europe"];
Shane Farmer74cdea32017-05-12 16:22:36 -0700382
Shane Farmer0a5b2012017-06-22 12:24:12 -0700383 ConfigDescription en = test::ParseConfigOrDie("en");
384 ConfigDescription es = test::ParseConfigOrDie("es");
385 ConfigDescription fr = test::ParseConfigOrDie("fr");
386 ConfigDescription de = test::ParseConfigOrDie("de");
Shane Farmer74cdea32017-05-12 16:22:36 -0700387
388 ASSERT_THAT(out, ElementsAre(en, es, fr, de));
389}
390
391TEST_F(ConfigurationParserTest, AndroidSdkGroupAction) {
392 static constexpr const char* xml = R"xml(
Shane Farmerefe45392017-08-21 14:39:28 -0700393 <android-sdk-group label="v19">
Shane Farmer74cdea32017-05-12 16:22:36 -0700394 <android-sdk
Shane Farmer3edd4722017-09-01 14:34:22 -0700395 minSdkVersion="19"
396 targetSdkVersion="24"
397 maxSdkVersion="25">
Shane Farmer74cdea32017-05-12 16:22:36 -0700398 <manifest>
399 <!--- manifest additions here XSLT? TODO -->
400 </manifest>
401 </android-sdk>
402 </android-sdk-group>)xml";
403
404 auto doc = test::BuildXmlDom(xml);
405
Shane Farmer280be342017-06-21 15:20:15 -0700406 PostProcessingConfiguration config;
Shane Farmer74cdea32017-05-12 16:22:36 -0700407 bool ok = android_sdk_group_handler_(&config, NodeCast<Element>(doc.get()->root.get()), &diag_);
408 ASSERT_TRUE(ok);
409
410 ASSERT_EQ(1ul, config.android_sdk_groups.size());
Shane Farmerefe45392017-08-21 14:39:28 -0700411 ASSERT_EQ(1u, config.android_sdk_groups.count("v19"));
Shane Farmer74cdea32017-05-12 16:22:36 -0700412
Shane Farmerefe45392017-08-21 14:39:28 -0700413 auto& out = config.android_sdk_groups["v19"];
Shane Farmer74cdea32017-05-12 16:22:36 -0700414
415 AndroidSdk sdk;
Shane Farmer3edd4722017-09-01 14:34:22 -0700416 sdk.min_sdk_version = 19;
417 sdk.target_sdk_version = 24;
418 sdk.max_sdk_version = 25;
Shane Farmer74cdea32017-05-12 16:22:36 -0700419 sdk.manifest = AndroidManifest();
420
Shane Farmerefe45392017-08-21 14:39:28 -0700421 ASSERT_EQ(sdk, out);
Shane Farmer74cdea32017-05-12 16:22:36 -0700422}
423
Shane Farmer3edd4722017-09-01 14:34:22 -0700424TEST_F(ConfigurationParserTest, AndroidSdkGroupAction_NonNumeric) {
425 static constexpr const char* xml = R"xml(
426 <android-sdk-group label="O">
427 <android-sdk
428 minSdkVersion="M"
429 targetSdkVersion="O"
430 maxSdkVersion="O">
431 </android-sdk>
432 </android-sdk-group>)xml";
433
434 auto doc = test::BuildXmlDom(xml);
435
436 PostProcessingConfiguration config;
437 bool ok = android_sdk_group_handler_(&config, NodeCast<Element>(doc.get()->root.get()), &diag_);
438 ASSERT_TRUE(ok);
439
440 ASSERT_EQ(1ul, config.android_sdk_groups.size());
441 ASSERT_EQ(1u, config.android_sdk_groups.count("O"));
442
443 auto& out = config.android_sdk_groups["O"];
444
445 AndroidSdk sdk;
446 sdk.min_sdk_version = {}; // Only the latest development version is supported.
447 sdk.target_sdk_version = 26;
448 sdk.max_sdk_version = 26;
449
450 ASSERT_EQ(sdk, out);
451}
452
Shane Farmer74cdea32017-05-12 16:22:36 -0700453TEST_F(ConfigurationParserTest, GlTextureGroupAction) {
454 static constexpr const char* xml = R"xml(
455 <gl-texture-group label="dxt1">
456 <gl-texture name="GL_EXT_texture_compression_dxt1">
457 <texture-path>assets/dxt1/main/*</texture-path>
458 <texture-path>
459 assets/dxt1/test/*
460 </texture-path>
461 </gl-texture>
462 </gl-texture-group>)xml";
463
464 auto doc = test::BuildXmlDom(xml);
465
Shane Farmer280be342017-06-21 15:20:15 -0700466 PostProcessingConfiguration config;
Shane Farmer74cdea32017-05-12 16:22:36 -0700467 bool ok = gl_texture_group_handler_(&config, NodeCast<Element>(doc.get()->root.get()), &diag_);
468 ASSERT_TRUE(ok);
469
470 EXPECT_EQ(1ul, config.gl_texture_groups.size());
471 ASSERT_EQ(1u, config.gl_texture_groups.count("dxt1"));
472
473 auto& out = config.gl_texture_groups["dxt1"];
474
475 GlTexture texture{
476 std::string("GL_EXT_texture_compression_dxt1"),
477 {"assets/dxt1/main/*", "assets/dxt1/test/*"}
478 };
479
480 ASSERT_EQ(1ul, out.size());
481 ASSERT_EQ(texture, out[0]);
482}
483
484TEST_F(ConfigurationParserTest, DeviceFeatureGroupAction) {
485 static constexpr const char* xml = R"xml(
486 <device-feature-group label="low-latency">
487 <supports-feature>android.hardware.audio.low_latency</supports-feature>
488 <supports-feature>
489 android.hardware.audio.pro
490 </supports-feature>
491 </device-feature-group>)xml";
492
493 auto doc = test::BuildXmlDom(xml);
494
Shane Farmer280be342017-06-21 15:20:15 -0700495 PostProcessingConfiguration config;
Shane Farmer74cdea32017-05-12 16:22:36 -0700496 bool ok
497 = device_feature_group_handler_(&config, NodeCast<Element>(doc.get()->root.get()), &diag_);
498 ASSERT_TRUE(ok);
499
500 EXPECT_EQ(1ul, config.device_feature_groups.size());
501 ASSERT_EQ(1u, config.device_feature_groups.count("low-latency"));
502
503 auto& out = config.device_feature_groups["low-latency"];
504
505 DeviceFeature low_latency = "android.hardware.audio.low_latency";
506 DeviceFeature pro = "android.hardware.audio.pro";
507 ASSERT_THAT(out, ElementsAre(low_latency, pro));
508}
509
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700510// Artifact name parser test cases.
511
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700512TEST(ArtifactTest, Simple) {
513 StdErrDiagnostics diag;
514 Artifact x86;
515 x86.abi_group = {"x86"};
516
Shane Farmer9ecc0752017-08-24 15:55:36 -0700517 auto x86_result = x86.ToArtifactName("something.${abi}.apk", "", &diag);
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700518 ASSERT_TRUE(x86_result);
519 EXPECT_EQ(x86_result.value(), "something.x86.apk");
520
521 Artifact arm;
522 arm.abi_group = {"armeabi-v7a"};
523
Shane Farmer9ecc0752017-08-24 15:55:36 -0700524 {
525 auto arm_result = arm.ToArtifactName("app.${abi}.apk", "", &diag);
526 ASSERT_TRUE(arm_result);
527 EXPECT_EQ(arm_result.value(), "app.armeabi-v7a.apk");
528 }
529
530 {
531 auto arm_result = arm.ToArtifactName("app.${abi}.apk", "different_name.apk", &diag);
532 ASSERT_TRUE(arm_result);
533 EXPECT_EQ(arm_result.value(), "app.armeabi-v7a.apk");
534 }
535
536 {
537 auto arm_result = arm.ToArtifactName("${basename}.${abi}.apk", "app.apk", &diag);
538 ASSERT_TRUE(arm_result);
539 EXPECT_EQ(arm_result.value(), "app.armeabi-v7a.apk");
540 }
541
542 {
543 auto arm_result = arm.ToArtifactName("app.${abi}.${ext}", "app.apk", &diag);
544 ASSERT_TRUE(arm_result);
545 EXPECT_EQ(arm_result.value(), "app.armeabi-v7a.apk");
546 }
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700547}
548
549TEST(ArtifactTest, Complex) {
550 StdErrDiagnostics diag;
551 Artifact artifact;
552 artifact.abi_group = {"mips64"};
553 artifact.screen_density_group = {"ldpi"};
554 artifact.device_feature_group = {"df1"};
555 artifact.gl_texture_group = {"glx1"};
556 artifact.locale_group = {"en-AU"};
Shane Farmerefe45392017-08-21 14:39:28 -0700557 artifact.android_sdk_group = {"v26"};
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700558
Shane Farmer9ecc0752017-08-24 15:55:36 -0700559 {
560 auto result = artifact.ToArtifactName(
Shane Farmerefe45392017-08-21 14:39:28 -0700561 "app.${density}_${locale}_${feature}_${gl}.${sdk}.${abi}.apk", "", &diag);
Shane Farmer9ecc0752017-08-24 15:55:36 -0700562 ASSERT_TRUE(result);
Shane Farmerefe45392017-08-21 14:39:28 -0700563 EXPECT_EQ(result.value(), "app.ldpi_en-AU_df1_glx1.v26.mips64.apk");
Shane Farmer9ecc0752017-08-24 15:55:36 -0700564 }
565
566 {
567 auto result = artifact.ToArtifactName(
Shane Farmerefe45392017-08-21 14:39:28 -0700568 "app.${density}_${locale}_${feature}_${gl}.${sdk}.${abi}.apk", "app.apk", &diag);
Shane Farmer9ecc0752017-08-24 15:55:36 -0700569 ASSERT_TRUE(result);
Shane Farmerefe45392017-08-21 14:39:28 -0700570 EXPECT_EQ(result.value(), "app.ldpi_en-AU_df1_glx1.v26.mips64.apk");
Shane Farmer9ecc0752017-08-24 15:55:36 -0700571 }
572
573 {
574 auto result = artifact.ToArtifactName(
Shane Farmerefe45392017-08-21 14:39:28 -0700575 "${basename}.${density}_${locale}_${feature}_${gl}.${sdk}.${abi}.apk", "app.apk", &diag);
Shane Farmer9ecc0752017-08-24 15:55:36 -0700576 ASSERT_TRUE(result);
Shane Farmerefe45392017-08-21 14:39:28 -0700577 EXPECT_EQ(result.value(), "app.ldpi_en-AU_df1_glx1.v26.mips64.apk");
Shane Farmer9ecc0752017-08-24 15:55:36 -0700578 }
579
580 {
581 auto result = artifact.ToArtifactName(
Shane Farmerefe45392017-08-21 14:39:28 -0700582 "app.${density}_${locale}_${feature}_${gl}.${sdk}.${abi}.${ext}", "app.apk", &diag);
Shane Farmer9ecc0752017-08-24 15:55:36 -0700583 ASSERT_TRUE(result);
Shane Farmerefe45392017-08-21 14:39:28 -0700584 EXPECT_EQ(result.value(), "app.ldpi_en-AU_df1_glx1.v26.mips64.apk");
Shane Farmer9ecc0752017-08-24 15:55:36 -0700585 }
586
587 {
588 auto result = artifact.ToArtifactName(
Shane Farmerefe45392017-08-21 14:39:28 -0700589 "${basename}.${density}_${locale}_${feature}_${gl}.${sdk}.${abi}", "app.apk", &diag);
Shane Farmer9ecc0752017-08-24 15:55:36 -0700590 ASSERT_TRUE(result);
Shane Farmerefe45392017-08-21 14:39:28 -0700591 EXPECT_EQ(result.value(), "app.ldpi_en-AU_df1_glx1.v26.mips64.apk");
Shane Farmer9ecc0752017-08-24 15:55:36 -0700592 }
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700593}
594
595TEST(ArtifactTest, Missing) {
596 StdErrDiagnostics diag;
597 Artifact x86;
598 x86.abi_group = {"x86"};
599
Shane Farmer9ecc0752017-08-24 15:55:36 -0700600 EXPECT_FALSE(x86.ToArtifactName("something.${density}.apk", "", &diag));
601 EXPECT_FALSE(x86.ToArtifactName("something.apk", "", &diag));
602 EXPECT_FALSE(x86.ToArtifactName("something.${density}.apk", "something.apk", &diag));
603 EXPECT_FALSE(x86.ToArtifactName("something.apk", "something.apk", &diag));
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700604}
605
606TEST(ArtifactTest, Empty) {
607 StdErrDiagnostics diag;
608 Artifact artifact;
609
Shane Farmer9ecc0752017-08-24 15:55:36 -0700610 EXPECT_FALSE(artifact.ToArtifactName("something.${density}.apk", "", &diag));
611 EXPECT_TRUE(artifact.ToArtifactName("something.apk", "", &diag));
612 EXPECT_FALSE(artifact.ToArtifactName("something.${density}.apk", "something.apk", &diag));
613 EXPECT_TRUE(artifact.ToArtifactName("something.apk", "something.apk", &diag));
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700614}
615
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700616TEST(ArtifactTest, Repeated) {
617 StdErrDiagnostics diag;
618 Artifact artifact;
619 artifact.screen_density_group = {"mdpi"};
620
Shane Farmer9ecc0752017-08-24 15:55:36 -0700621 ASSERT_TRUE(artifact.ToArtifactName("something.${density}.apk", "", &diag));
622 EXPECT_FALSE(artifact.ToArtifactName("something.${density}.${density}.apk", "", &diag));
623 ASSERT_TRUE(artifact.ToArtifactName("something.${density}.apk", "something.apk", &diag));
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700624}
625
626TEST(ArtifactTest, Nesting) {
627 StdErrDiagnostics diag;
628 Artifact x86;
629 x86.abi_group = {"x86"};
630
Shane Farmer9ecc0752017-08-24 15:55:36 -0700631 EXPECT_FALSE(x86.ToArtifactName("something.${abi${density}}.apk", "", &diag));
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700632
Shane Farmer9ecc0752017-08-24 15:55:36 -0700633 const Maybe<std::string>& name = x86.ToArtifactName("something.${abi${abi}}.apk", "", &diag);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700634 ASSERT_TRUE(name);
635 EXPECT_EQ(name.value(), "something.${abix86}.apk");
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700636}
637
638TEST(ArtifactTest, Recursive) {
639 StdErrDiagnostics diag;
640 Artifact artifact;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700641 artifact.device_feature_group = {"${gl}"};
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700642 artifact.gl_texture_group = {"glx1"};
643
Shane Farmer9ecc0752017-08-24 15:55:36 -0700644 EXPECT_FALSE(artifact.ToArtifactName("app.${feature}.${gl}.apk", "", &diag));
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700645
646 artifact.device_feature_group = {"df1"};
Shane Farmer0a5b2012017-06-22 12:24:12 -0700647 artifact.gl_texture_group = {"${feature}"};
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700648 {
Shane Farmer9ecc0752017-08-24 15:55:36 -0700649 const auto& result = artifact.ToArtifactName("app.${feature}.${gl}.apk", "", &diag);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700650 ASSERT_TRUE(result);
651 EXPECT_EQ(result.value(), "app.df1.${feature}.apk");
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700652 }
653
654 // This is an invalid case, but should be the only possible case due to the ordering of
655 // replacement.
Shane Farmer0a5b2012017-06-22 12:24:12 -0700656 artifact.device_feature_group = {"${gl}"};
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700657 artifact.gl_texture_group = {"glx1"};
658 {
Shane Farmer9ecc0752017-08-24 15:55:36 -0700659 const auto& result = artifact.ToArtifactName("app.${feature}.apk", "", &diag);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700660 ASSERT_TRUE(result);
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700661 EXPECT_EQ(result.value(), "app.glx1.apk");
662 }
663}
664
Shane Farmer74cdea32017-05-12 16:22:36 -0700665} // namespace
666} // namespace aapt