summaryrefslogtreecommitdiff
path: root/libs/androidfw/tests/AssetManager2_test.cpp
diff options
context:
space:
mode:
authorAdam Lesinski <adamlesinski@google.com>2016-12-29 16:08:16 -0500
committerAdam Lesinski <adamlesinski@google.com>2017-01-11 17:20:36 -0800
commitda431a22da38f9c4085b5d71ed9a9c6122c6a5a6 (patch)
tree7fc684c1ec3c653ff98bdc8eff50addc081a02e1 /libs/androidfw/tests/AssetManager2_test.cpp
parent7ad1110ecd6a840fcd2895c62668828a1ca029c6 (diff)
libandroidfw: Add new support for shared libraries
This adds support for shared resource libraries in the new ResTable/AssetManager implementation. The dynamic package map encoded in resources.arsc is parsed and stored with LoadedArsc, and combined to form a resolved table in AssetManager2. Benchmarks show that this implementation is an order of magnitude faster on angler-userdebug (make libandroidfw_benchmarks). Test: libandroidfw_tests Change-Id: I57c80248728b63b162bf8269ac9495b53c3e7fa0
Diffstat (limited to 'libs/androidfw/tests/AssetManager2_test.cpp')
-rw-r--r--libs/androidfw/tests/AssetManager2_test.cpp122
1 files changed, 115 insertions, 7 deletions
diff --git a/libs/androidfw/tests/AssetManager2_test.cpp b/libs/androidfw/tests/AssetManager2_test.cpp
index 39c5381feb04..543456afa2f4 100644
--- a/libs/androidfw/tests/AssetManager2_test.cpp
+++ b/libs/androidfw/tests/AssetManager2_test.cpp
@@ -20,11 +20,19 @@
#include "android-base/logging.h"
#include "TestHelpers.h"
+#include "data/appaslib/R.h"
#include "data/basic/R.h"
+#include "data/lib_one/R.h"
+#include "data/lib_two/R.h"
+#include "data/libclient/R.h"
#include "data/styles/R.h"
-namespace basic = com::android::basic;
namespace app = com::android::app;
+namespace appaslib = com::android::appaslib::app;
+namespace basic = com::android::basic;
+namespace lib_one = com::android::lib_one;
+namespace lib_two = com::android::lib_two;
+namespace libclient = com::android::libclient;
namespace android {
@@ -39,15 +47,31 @@ class AssetManager2Test : public ::testing::Test {
style_assets_ = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk");
ASSERT_NE(nullptr, style_assets_);
+
+ lib_one_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_one/lib_one.apk");
+ ASSERT_NE(nullptr, lib_one_assets_);
+
+ lib_two_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_two/lib_two.apk");
+ ASSERT_NE(nullptr, lib_two_assets_);
+
+ libclient_assets_ = ApkAssets::Load(GetTestDataPath() + "/libclient/libclient.apk");
+ ASSERT_NE(nullptr, libclient_assets_);
+
+ appaslib_assets_ = ApkAssets::Load(GetTestDataPath() + "/appaslib/appaslib.apk");
+ ASSERT_NE(nullptr, appaslib_assets_);
}
protected:
std::unique_ptr<ApkAssets> basic_assets_;
std::unique_ptr<ApkAssets> basic_de_fr_assets_;
std::unique_ptr<ApkAssets> style_assets_;
+ std::unique_ptr<ApkAssets> lib_one_assets_;
+ std::unique_ptr<ApkAssets> lib_two_assets_;
+ std::unique_ptr<ApkAssets> libclient_assets_;
+ std::unique_ptr<ApkAssets> appaslib_assets_;
};
-TEST_F(AssetManager2Test, FindsResourcesFromSingleApkAssets) {
+TEST_F(AssetManager2Test, FindsResourceFromSingleApkAssets) {
ResTable_config desired_config;
memset(&desired_config, 0, sizeof(desired_config));
desired_config.language[0] = 'd';
@@ -77,7 +101,7 @@ TEST_F(AssetManager2Test, FindsResourcesFromSingleApkAssets) {
EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
}
-TEST_F(AssetManager2Test, FindsResourcesFromMultipleApkAssets) {
+TEST_F(AssetManager2Test, FindsResourceFromMultipleApkAssets) {
ResTable_config desired_config;
memset(&desired_config, 0, sizeof(desired_config));
desired_config.language[0] = 'd';
@@ -99,7 +123,7 @@ TEST_F(AssetManager2Test, FindsResourcesFromMultipleApkAssets) {
// Came from our de_fr ApkAssets.
EXPECT_EQ(1, cookie);
- // The configuration is german.
+ // The configuration is German.
EXPECT_EQ('d', selected_config.language[0]);
EXPECT_EQ('e', selected_config.language[1]);
@@ -107,7 +131,72 @@ TEST_F(AssetManager2Test, FindsResourcesFromMultipleApkAssets) {
EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
}
-TEST_F(AssetManager2Test, FindsBagResourcesFromSingleApkAssets) {
+TEST_F(AssetManager2Test, FindsResourceFromSharedLibrary) {
+ AssetManager2 assetmanager;
+
+ // libclient is built with lib_one and then lib_two in order.
+ // Reverse the order to test that proper package ID re-assignment is happening.
+ assetmanager.SetApkAssets(
+ {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
+
+ Res_value value;
+ ResTable_config selected_config;
+ uint32_t flags;
+
+ ApkAssetsCookie cookie =
+ assetmanager.GetResource(libclient::R::string::foo_one, false /*may_be_bag*/,
+ 0 /*density_override*/, &value, &selected_config, &flags);
+ ASSERT_NE(kInvalidCookie, cookie);
+
+ // Reference comes from libclient.
+ EXPECT_EQ(2, cookie);
+ EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
+
+ // Lookup the reference.
+ cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/,
+ &value, &selected_config, &flags);
+ ASSERT_NE(kInvalidCookie, cookie);
+ EXPECT_EQ(1, cookie);
+ EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
+ EXPECT_EQ(std::string("Foo from lib_one"),
+ GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data));
+
+ cookie = assetmanager.GetResource(libclient::R::string::foo_two, false /*may_be_bag*/,
+ 0 /*density_override*/, &value, &selected_config, &flags);
+ ASSERT_NE(kInvalidCookie, cookie);
+
+ // Reference comes from libclient.
+ EXPECT_EQ(2, cookie);
+ EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
+
+ // Lookup the reference.
+ cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/,
+ &value, &selected_config, &flags);
+ ASSERT_NE(kInvalidCookie, cookie);
+ EXPECT_EQ(0, cookie);
+ EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
+ EXPECT_EQ(std::string("Foo from lib_two"),
+ GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data));
+}
+
+TEST_F(AssetManager2Test, FindsResourceFromAppLoadedAsSharedLibrary) {
+ AssetManager2 assetmanager;
+ assetmanager.SetApkAssets({appaslib_assets_.get()});
+
+ // The appaslib package will have been assigned the package ID 0x02.
+
+ Res_value value;
+ ResTable_config selected_config;
+ uint32_t flags;
+ ApkAssetsCookie cookie = assetmanager.GetResource(
+ util::fix_package_id(appaslib::R::integer::number1, 0x02), false /*may_be_bag*/,
+ 0u /*density_override*/, &value, &selected_config, &flags);
+ ASSERT_NE(kInvalidCookie, cookie);
+ EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
+ EXPECT_EQ(util::fix_package_id(appaslib::R::array::integerArray1, 0x02), value.data);
+}
+
+TEST_F(AssetManager2Test, FindsBagResourceFromSingleApkAssets) {
AssetManager2 assetmanager;
assetmanager.SetApkAssets({basic_assets_.get()});
@@ -128,6 +217,27 @@ TEST_F(AssetManager2Test, FindsBagResourcesFromSingleApkAssets) {
EXPECT_EQ(0, bag->entries[2].cookie);
}
+TEST_F(AssetManager2Test, FindsBagResourceFromMultipleApkAssets) {}
+
+TEST_F(AssetManager2Test, FindsBagResourceFromSharedLibrary) {
+ AssetManager2 assetmanager;
+
+ // libclient is built with lib_one and then lib_two in order.
+ // Reverse the order to test that proper package ID re-assignment is happening.
+ assetmanager.SetApkAssets(
+ {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
+
+ const ResolvedBag* bag = assetmanager.GetBag(libclient::R::style::Theme);
+ ASSERT_NE(nullptr, bag);
+ ASSERT_GE(bag->entry_count, 2u);
+
+ // First two attributes come from lib_one.
+ EXPECT_EQ(1, bag->entries[0].cookie);
+ EXPECT_EQ(0x03, util::get_package_id(bag->entries[0].key));
+ EXPECT_EQ(1, bag->entries[1].cookie);
+ EXPECT_EQ(0x03, util::get_package_id(bag->entries[1].key));
+}
+
TEST_F(AssetManager2Test, MergesStylesWithParentFromSingleApkAssets) {
AssetManager2 assetmanager;
assetmanager.SetApkAssets({style_assets_.get()});
@@ -181,8 +291,6 @@ TEST_F(AssetManager2Test, MergesStylesWithParentFromSingleApkAssets) {
EXPECT_EQ(0, bag_two->entries[4].cookie);
}
-TEST_F(AssetManager2Test, FindsBagResourcesFromMultipleApkAssets) {}
-
TEST_F(AssetManager2Test, OpensFileFromSingleApkAssets) {}
TEST_F(AssetManager2Test, OpensFileFromMultipleApkAssets) {}