Files
frameworks_base/tools/aapt2/optimize/MultiApkGenerator_test.cpp
Shane Farmer 0a5b201156 AAPT2: Add a APK filtering.
Allow resource files to be removed from the final artifact based on the
density and locale configuration in the config file. The APK is split
along the density, locale and ABI axis. Each split is generated from the
original APK without modifying the original. The new resource table is
written back to the file system with unneeded assets etc removed.

Test: Unit tests
Test: Manually run optimize command against an APK and inspect results
Test: Installed split searchlite APK (after resigning) and ran on N6

Change-Id: If73597dcfd88c02d2616518585d0e25a5c6a84d1
2017-08-16 19:19:54 +00:00

109 lines
3.9 KiB
C++

/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "optimize/MultiApkGenerator.h"
#include <string>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "LoadedApk.h"
#include "ResourceTable.h"
#include "configuration/ConfigurationParser.h"
#include "filter/Filter.h"
#include "flatten/Archive.h"
#include "flatten/TableFlattener.h"
#include "process/IResourceTableConsumer.h"
#include "test/Context.h"
#include "test/Test.h"
namespace aapt {
namespace {
using ::aapt::configuration::Abi;
using ::aapt::configuration::Artifact;
using ::aapt::configuration::PostProcessingConfiguration;
using ::testing::Eq;
using ::testing::Return;
using ::testing::_;
class MockApk : public LoadedApk {
public:
MockApk(std::unique_ptr<ResourceTable> table) : LoadedApk({"test.apk"}, {}, std::move(table)){};
MOCK_METHOD5(WriteToArchive, bool(IAaptContext*, ResourceTable*, const TableFlattenerOptions&,
FilterChain*, IArchiveWriter*));
};
TEST(MultiApkGeneratorTest, FromBaseApk) {
std::unique_ptr<ResourceTable> table =
test::ResourceTableBuilder()
.AddFileReference("android:drawable/icon", "res/drawable-mdpi/icon.png",
test::ParseConfigOrDie("mdpi"))
.AddFileReference("android:drawable/icon", "res/drawable-hdpi/icon.png",
test::ParseConfigOrDie("hdpi"))
.AddFileReference("android:drawable/icon", "res/drawable-xhdpi/icon.png",
test::ParseConfigOrDie("xhdpi"))
.AddFileReference("android:drawable/icon", "res/drawable-xxhdpi/icon.png",
test::ParseConfigOrDie("xxhdpi"))
.AddSimple("android:string/one")
.Build();
MockApk apk{std::move(table)};
EXPECT_CALL(apk, WriteToArchive(_, _, _, _, _)).Times(0);
test::Context ctx;
PostProcessingConfiguration empty_config;
TableFlattenerOptions table_flattener_options;
MultiApkGenerator generator{&apk, &ctx};
EXPECT_TRUE(generator.FromBaseApk("out", empty_config, table_flattener_options));
Artifact x64 = test::ArtifactBuilder()
.SetName("${basename}.x64.apk")
.SetAbiGroup("x64")
.SetLocaleGroup("en")
.SetDensityGroup("xhdpi")
.Build();
Artifact intel = test::ArtifactBuilder()
.SetName("${basename}.intel.apk")
.SetAbiGroup("intel")
.SetLocaleGroup("europe")
.SetDensityGroup("large")
.Build();
auto config = test::PostProcessingConfigurationBuilder()
.SetLocaleGroup("en", {"en"})
.SetLocaleGroup("europe", {"en", "fr", "de", "es"})
.SetAbiGroup("x64", {Abi::kX86_64})
.SetAbiGroup("intel", {Abi::kX86_64, Abi::kX86})
.SetDensityGroup("xhdpi", {"xhdpi"})
.SetDensityGroup("large", {"xhdpi", "xxhdpi", "xxxhdpi"})
.AddArtifact(x64)
.AddArtifact(intel)
.Build();
// Called once for each artifact.
EXPECT_CALL(apk, WriteToArchive(Eq(&ctx), _, _, _, _)).Times(2).WillRepeatedly(Return(true));
EXPECT_TRUE(generator.FromBaseApk("out", config, table_flattener_options));
}
} // namespace
} // namespace aapt