Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -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 "configuration/ConfigurationParser.h" |
| 18 | |
| 19 | #include <string> |
| 20 | |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 21 | #include "androidfw/ResourceTypes.h" |
| 22 | |
| 23 | #include "test/Test.h" |
| 24 | #include "xml/XmlDom.h" |
| 25 | |
| 26 | namespace aapt { |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 27 | |
| 28 | namespace configuration { |
| 29 | void 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 Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 36 | namespace { |
| 37 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 38 | using ::android::ResTable_config; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 39 | using configuration::Abi; |
| 40 | using configuration::AndroidSdk; |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 41 | using configuration::Artifact; |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 42 | using configuration::PostProcessingConfiguration; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 43 | using configuration::DeviceFeature; |
| 44 | using configuration::GlTexture; |
| 45 | using configuration::Locale; |
| 46 | using configuration::AndroidManifest; |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 47 | using ::testing::ElementsAre; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 48 | using xml::Element; |
| 49 | using xml::NodeCast; |
| 50 | |
| 51 | constexpr 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 Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 76 | <locale>en</locale> |
| 77 | <locale>es</locale> |
| 78 | <locale>fr</locale> |
| 79 | <locale>de</locale> |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 80 | </locale-group> |
| 81 | <locale-group label="north-america"> |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 82 | <locale>en</locale> |
| 83 | <locale>es-rMX</locale> |
| 84 | <locale>fr-rCA</locale> |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 85 | </locale-group> |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 86 | <android-sdk-group label="v19"> |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 87 | <android-sdk |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 88 | minSdkVersion="19" |
| 89 | targetSdkVersion="24" |
| 90 | maxSdkVersion="25"> |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 91 | <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 Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 114 | android-sdk-group="v19" |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 115 | 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 Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 122 | android-sdk-group="v19" |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 123 | gl-texture-group="dxt1" |
| 124 | device-feature-group="low-latency"/> |
| 125 | </artifacts> |
| 126 | </post-process> |
| 127 | )"; |
| 128 | |
| 129 | class ConfigurationParserTest : public ConfigurationParser, public ::testing::Test { |
| 130 | public: |
| 131 | ConfigurationParserTest() : ConfigurationParser("") {} |
| 132 | |
| 133 | protected: |
| 134 | StdErrDiagnostics diag_; |
| 135 | }; |
| 136 | |
Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 137 | TEST_F(ConfigurationParserTest, ForPath_NoFile) { |
| 138 | auto result = ConfigurationParser::ForPath("./does_not_exist.xml"); |
| 139 | EXPECT_FALSE(result); |
| 140 | } |
| 141 | |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 142 | TEST_F(ConfigurationParserTest, ValidateFile) { |
| 143 | auto parser = ConfigurationParser::ForContents(kValidConfig).WithDiagnostics(&diag_); |
| 144 | auto result = parser.Parse(); |
| 145 | ASSERT_TRUE(result); |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 146 | PostProcessingConfiguration& value = result.value(); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 147 | 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 Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 162 | EXPECT_EQ(2ul, value.locale_groups.size()); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 163 | EXPECT_EQ(4ul, value.locale_groups["europe"].size()); |
| 164 | EXPECT_EQ(3ul, value.locale_groups["north-america"].size()); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 165 | |
| 166 | EXPECT_EQ(1ul, value.android_sdk_groups.size()); |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 167 | EXPECT_TRUE(value.android_sdk_groups["v19"].min_sdk_version); |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 168 | EXPECT_EQ(19, value.android_sdk_groups["v19"].min_sdk_version.value()); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 169 | |
| 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 | |
| 177 | TEST_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 | |
| 185 | TEST_F(ConfigurationParserTest, ArtifactAction) { |
Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 186 | 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 Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 231 | <artifact |
Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 232 | version="5" |
| 233 | abi-group="other" |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 234 | screen-density-group="large" |
| 235 | locale-group="europe" |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 236 | android-sdk-group="v19" |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 237 | gl-texture-group="dxt1" |
Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 238 | device-feature-group="low-latency"/>)xml"); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 239 | |
Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 240 | 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 Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 244 | |
Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 245 | { |
| 246 | // Perform a fourth action to ensure the version code still increments. |
| 247 | const auto doc = test::BuildXmlDom(R"xml( |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 248 | <artifact |
| 249 | abi-group="other" |
| 250 | screen-density-group="large" |
| 251 | locale-group="europe" |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 252 | android-sdk-group="v19" |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 253 | gl-texture-group="dxt1" |
Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 254 | device-feature-group="low-latency"/>)xml"); |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 255 | |
Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 256 | 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 | |
| 262 | TEST_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 Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | TEST_F(ConfigurationParserTest, ArtifactFormatAction) { |
Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 293 | const auto doc = test::BuildXmlDom(R"xml( |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 294 | <artifact-format> |
| 295 | ${base}.${abi}.${screen-density}.${locale}.${sdk}.${gl}.${feature}.release |
Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 296 | </artifact-format>)xml"); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 297 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 298 | PostProcessingConfiguration config; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 299 | 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 | |
| 308 | TEST_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 Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 321 | PostProcessingConfiguration config; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 322 | 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 | |
| 332 | TEST_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 Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 344 | PostProcessingConfiguration config; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 345 | 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 | |
| 363 | TEST_F(ConfigurationParserTest, LocaleGroupAction) { |
| 364 | static constexpr const char* xml = R"xml( |
| 365 | <locale-group label="europe"> |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 366 | <locale>en</locale> |
| 367 | <locale>es</locale> |
| 368 | <locale>fr</locale> |
| 369 | <locale>de</locale> |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 370 | </locale-group>)xml"; |
| 371 | |
| 372 | auto doc = test::BuildXmlDom(xml); |
| 373 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 374 | PostProcessingConfiguration config; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 375 | 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 Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 381 | const auto& out = config.locale_groups["europe"]; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 382 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 383 | ConfigDescription en = test::ParseConfigOrDie("en"); |
| 384 | ConfigDescription es = test::ParseConfigOrDie("es"); |
| 385 | ConfigDescription fr = test::ParseConfigOrDie("fr"); |
| 386 | ConfigDescription de = test::ParseConfigOrDie("de"); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 387 | |
| 388 | ASSERT_THAT(out, ElementsAre(en, es, fr, de)); |
| 389 | } |
| 390 | |
| 391 | TEST_F(ConfigurationParserTest, AndroidSdkGroupAction) { |
| 392 | static constexpr const char* xml = R"xml( |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 393 | <android-sdk-group label="v19"> |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 394 | <android-sdk |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 395 | minSdkVersion="19" |
| 396 | targetSdkVersion="24" |
| 397 | maxSdkVersion="25"> |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 398 | <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 Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 406 | PostProcessingConfiguration config; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 407 | 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 Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 411 | ASSERT_EQ(1u, config.android_sdk_groups.count("v19")); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 412 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 413 | auto& out = config.android_sdk_groups["v19"]; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 414 | |
| 415 | AndroidSdk sdk; |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 416 | sdk.min_sdk_version = 19; |
| 417 | sdk.target_sdk_version = 24; |
| 418 | sdk.max_sdk_version = 25; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 419 | sdk.manifest = AndroidManifest(); |
| 420 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 421 | ASSERT_EQ(sdk, out); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 424 | TEST_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 Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 453 | TEST_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 Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 466 | PostProcessingConfiguration config; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 467 | 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 | |
| 484 | TEST_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 Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 495 | PostProcessingConfiguration config; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 496 | 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 Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 510 | // Artifact name parser test cases. |
| 511 | |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 512 | TEST(ArtifactTest, Simple) { |
| 513 | StdErrDiagnostics diag; |
| 514 | Artifact x86; |
| 515 | x86.abi_group = {"x86"}; |
| 516 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 517 | auto x86_result = x86.ToArtifactName("something.${abi}.apk", "", &diag); |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 518 | 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 Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 524 | { |
| 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 Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | TEST(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 Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 557 | artifact.android_sdk_group = {"v26"}; |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 558 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 559 | { |
| 560 | auto result = artifact.ToArtifactName( |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 561 | "app.${density}_${locale}_${feature}_${gl}.${sdk}.${abi}.apk", "", &diag); |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 562 | ASSERT_TRUE(result); |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 563 | EXPECT_EQ(result.value(), "app.ldpi_en-AU_df1_glx1.v26.mips64.apk"); |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | { |
| 567 | auto result = artifact.ToArtifactName( |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 568 | "app.${density}_${locale}_${feature}_${gl}.${sdk}.${abi}.apk", "app.apk", &diag); |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 569 | ASSERT_TRUE(result); |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 570 | EXPECT_EQ(result.value(), "app.ldpi_en-AU_df1_glx1.v26.mips64.apk"); |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | { |
| 574 | auto result = artifact.ToArtifactName( |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 575 | "${basename}.${density}_${locale}_${feature}_${gl}.${sdk}.${abi}.apk", "app.apk", &diag); |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 576 | ASSERT_TRUE(result); |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 577 | EXPECT_EQ(result.value(), "app.ldpi_en-AU_df1_glx1.v26.mips64.apk"); |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | { |
| 581 | auto result = artifact.ToArtifactName( |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 582 | "app.${density}_${locale}_${feature}_${gl}.${sdk}.${abi}.${ext}", "app.apk", &diag); |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 583 | ASSERT_TRUE(result); |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 584 | EXPECT_EQ(result.value(), "app.ldpi_en-AU_df1_glx1.v26.mips64.apk"); |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | { |
| 588 | auto result = artifact.ToArtifactName( |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 589 | "${basename}.${density}_${locale}_${feature}_${gl}.${sdk}.${abi}", "app.apk", &diag); |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 590 | ASSERT_TRUE(result); |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 591 | EXPECT_EQ(result.value(), "app.ldpi_en-AU_df1_glx1.v26.mips64.apk"); |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 592 | } |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | TEST(ArtifactTest, Missing) { |
| 596 | StdErrDiagnostics diag; |
| 597 | Artifact x86; |
| 598 | x86.abi_group = {"x86"}; |
| 599 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 600 | 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 Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | TEST(ArtifactTest, Empty) { |
| 607 | StdErrDiagnostics diag; |
| 608 | Artifact artifact; |
| 609 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 610 | 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 Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 616 | TEST(ArtifactTest, Repeated) { |
| 617 | StdErrDiagnostics diag; |
| 618 | Artifact artifact; |
| 619 | artifact.screen_density_group = {"mdpi"}; |
| 620 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 621 | 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 Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | TEST(ArtifactTest, Nesting) { |
| 627 | StdErrDiagnostics diag; |
| 628 | Artifact x86; |
| 629 | x86.abi_group = {"x86"}; |
| 630 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 631 | EXPECT_FALSE(x86.ToArtifactName("something.${abi${density}}.apk", "", &diag)); |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 632 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 633 | const Maybe<std::string>& name = x86.ToArtifactName("something.${abi${abi}}.apk", "", &diag); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 634 | ASSERT_TRUE(name); |
| 635 | EXPECT_EQ(name.value(), "something.${abix86}.apk"); |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | TEST(ArtifactTest, Recursive) { |
| 639 | StdErrDiagnostics diag; |
| 640 | Artifact artifact; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 641 | artifact.device_feature_group = {"${gl}"}; |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 642 | artifact.gl_texture_group = {"glx1"}; |
| 643 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 644 | EXPECT_FALSE(artifact.ToArtifactName("app.${feature}.${gl}.apk", "", &diag)); |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 645 | |
| 646 | artifact.device_feature_group = {"df1"}; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 647 | artifact.gl_texture_group = {"${feature}"}; |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 648 | { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 649 | const auto& result = artifact.ToArtifactName("app.${feature}.${gl}.apk", "", &diag); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 650 | ASSERT_TRUE(result); |
| 651 | EXPECT_EQ(result.value(), "app.df1.${feature}.apk"); |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | // This is an invalid case, but should be the only possible case due to the ordering of |
| 655 | // replacement. |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 656 | artifact.device_feature_group = {"${gl}"}; |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 657 | artifact.gl_texture_group = {"glx1"}; |
| 658 | { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 659 | const auto& result = artifact.ToArtifactName("app.${feature}.apk", "", &diag); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 660 | ASSERT_TRUE(result); |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 661 | EXPECT_EQ(result.value(), "app.glx1.apk"); |
| 662 | } |
| 663 | } |
| 664 | |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 665 | } // namespace |
| 666 | } // namespace aapt |