Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -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 "optimize/MultiApkGenerator.h" |
| 18 | |
| 19 | #include <string> |
| 20 | |
| 21 | #include "gmock/gmock.h" |
| 22 | #include "gtest/gtest.h" |
| 23 | |
| 24 | #include "LoadedApk.h" |
| 25 | #include "ResourceTable.h" |
| 26 | #include "configuration/ConfigurationParser.h" |
| 27 | #include "filter/Filter.h" |
| 28 | #include "flatten/Archive.h" |
| 29 | #include "flatten/TableFlattener.h" |
| 30 | #include "process/IResourceTableConsumer.h" |
| 31 | #include "test/Context.h" |
| 32 | #include "test/Test.h" |
| 33 | |
| 34 | namespace aapt { |
| 35 | namespace { |
| 36 | |
| 37 | using ::aapt::configuration::Abi; |
| 38 | using ::aapt::configuration::Artifact; |
| 39 | using ::aapt::configuration::PostProcessingConfiguration; |
| 40 | |
| 41 | using ::testing::Eq; |
| 42 | using ::testing::Return; |
| 43 | using ::testing::_; |
| 44 | |
| 45 | class MockApk : public LoadedApk { |
| 46 | public: |
| 47 | MockApk(std::unique_ptr<ResourceTable> table) : LoadedApk({"test.apk"}, {}, std::move(table)){}; |
| 48 | MOCK_METHOD5(WriteToArchive, bool(IAaptContext*, ResourceTable*, const TableFlattenerOptions&, |
| 49 | FilterChain*, IArchiveWriter*)); |
| 50 | }; |
| 51 | |
| 52 | TEST(MultiApkGeneratorTest, FromBaseApk) { |
| 53 | std::unique_ptr<ResourceTable> table = |
| 54 | test::ResourceTableBuilder() |
| 55 | .AddFileReference("android:drawable/icon", "res/drawable-mdpi/icon.png", |
| 56 | test::ParseConfigOrDie("mdpi")) |
| 57 | .AddFileReference("android:drawable/icon", "res/drawable-hdpi/icon.png", |
| 58 | test::ParseConfigOrDie("hdpi")) |
| 59 | .AddFileReference("android:drawable/icon", "res/drawable-xhdpi/icon.png", |
| 60 | test::ParseConfigOrDie("xhdpi")) |
| 61 | .AddFileReference("android:drawable/icon", "res/drawable-xxhdpi/icon.png", |
| 62 | test::ParseConfigOrDie("xxhdpi")) |
| 63 | .AddSimple("android:string/one") |
| 64 | .Build(); |
| 65 | |
| 66 | MockApk apk{std::move(table)}; |
| 67 | |
| 68 | EXPECT_CALL(apk, WriteToArchive(_, _, _, _, _)).Times(0); |
| 69 | |
| 70 | test::Context ctx; |
| 71 | PostProcessingConfiguration empty_config; |
| 72 | TableFlattenerOptions table_flattener_options; |
| 73 | |
| 74 | MultiApkGenerator generator{&apk, &ctx}; |
| 75 | EXPECT_TRUE(generator.FromBaseApk("out", empty_config, table_flattener_options)); |
| 76 | |
| 77 | Artifact x64 = test::ArtifactBuilder() |
| 78 | .SetName("${basename}.x64.apk") |
| 79 | .SetAbiGroup("x64") |
| 80 | .SetLocaleGroup("en") |
| 81 | .SetDensityGroup("xhdpi") |
| 82 | .Build(); |
| 83 | |
| 84 | Artifact intel = test::ArtifactBuilder() |
| 85 | .SetName("${basename}.intel.apk") |
| 86 | .SetAbiGroup("intel") |
| 87 | .SetLocaleGroup("europe") |
| 88 | .SetDensityGroup("large") |
| 89 | .Build(); |
| 90 | |
| 91 | auto config = test::PostProcessingConfigurationBuilder() |
| 92 | .SetLocaleGroup("en", {"en"}) |
| 93 | .SetLocaleGroup("europe", {"en", "fr", "de", "es"}) |
| 94 | .SetAbiGroup("x64", {Abi::kX86_64}) |
| 95 | .SetAbiGroup("intel", {Abi::kX86_64, Abi::kX86}) |
| 96 | .SetDensityGroup("xhdpi", {"xhdpi"}) |
| 97 | .SetDensityGroup("large", {"xhdpi", "xxhdpi", "xxxhdpi"}) |
| 98 | .AddArtifact(x64) |
| 99 | .AddArtifact(intel) |
| 100 | .Build(); |
| 101 | |
| 102 | // Called once for each artifact. |
| 103 | EXPECT_CALL(apk, WriteToArchive(Eq(&ctx), _, _, _, _)).Times(2).WillRepeatedly(Return(true)); |
| 104 | EXPECT_TRUE(generator.FromBaseApk("out", config, table_flattener_options)); |
| 105 | } |
| 106 | |
| 107 | } // namespace |
| 108 | } // namespace aapt |