summaryrefslogtreecommitdiff
path: root/libs/androidfw/tests
diff options
context:
space:
mode:
authorWinson <chiuwinson@google.com>2019-08-16 10:20:39 -0700
committerWinson <chiuwinson@google.com>2019-10-10 15:41:03 -0700
commit9947f1e4fa6b1e58ca38bfe0ed440a8ab4f66465 (patch)
tree79b14d3da22669b9c574252a5b74503d17a07bb7 /libs/androidfw/tests
parent98bfc275494411c7888d806f307ae193f09c4e9e (diff)
Add ResourceLoader API with .apk and .arsc support
ResourceLoaders allow inserting another .apk/.arsc into AssetManager's resource resolution search. The effect is similar to overlays, where a entry of >= config later in the path list will return that ApkAsset's resource value instead. Because loading from an .arsc is supported, which doesn't contain any actual files, ResourceLoader exposes loadDrawable and loadXmlResourceParser to allow an application load those files from anywhere or create them in code. The data being loaded is either pushed into an .apk or .arsc that mocks itself as the package being "overlaid" and is passed in through ResourcesProvider, an interface with static methods that supports loading from a readable path on disk or a FileDescriptor. The APIs are accessed through a Context's getResources(), which has been changed to be unique per "Context-scope", which is usually the lifetime of the Java object. The exception is that Activities who get their Resources object persisted across recreations maintain that logic for persisting ResourceLoaders. Bug: 135270223 Test: atest FrameworksResourceLoaderTests Change-Id: I6929f0828629ad39a21fa155e7fec73bd75eec7d
Diffstat (limited to 'libs/androidfw/tests')
-rw-r--r--libs/androidfw/tests/LoadedArsc_test.cpp34
-rw-r--r--libs/androidfw/tests/data/loader/resources.arscbin0 -> 620 bytes
-rw-r--r--libs/androidfw/tests/data/system/R.h6
3 files changed, 40 insertions, 0 deletions
diff --git a/libs/androidfw/tests/LoadedArsc_test.cpp b/libs/androidfw/tests/LoadedArsc_test.cpp
index d58e8d20c8aa..fd57a92c216b 100644
--- a/libs/androidfw/tests/LoadedArsc_test.cpp
+++ b/libs/androidfw/tests/LoadedArsc_test.cpp
@@ -25,6 +25,7 @@
#include "data/overlayable/R.h"
#include "data/sparse/R.h"
#include "data/styles/R.h"
+#include "data/system/R.h"
namespace app = com::android::app;
namespace basic = com::android::basic;
@@ -387,6 +388,39 @@ TEST(LoadedArscTest, GetOverlayableMap) {
ASSERT_EQ(map.at("OverlayableResources2"), "overlay://com.android.overlayable");
}
+TEST(LoadedArscTest, LoadCustomLoader) {
+ std::string contents;
+
+ std::unique_ptr<Asset>
+ asset = ApkAssets::CreateAssetFromFile(GetTestDataPath() + "/loader/resources.arsc");
+
+ MockLoadedIdmap loaded_idmap;
+ const StringPiece data(
+ reinterpret_cast<const char*>(asset->getBuffer(true /*wordAligned*/)),
+ asset->getLength());
+
+ std::unique_ptr<const LoadedArsc> loaded_arsc =
+ LoadedArsc::Load(data, nullptr, false, false, true);
+ ASSERT_THAT(loaded_arsc, NotNull());
+
+ const LoadedPackage* package =
+ loaded_arsc->GetPackageById(get_package_id(android::R::string::cancel));
+ ASSERT_THAT(package, NotNull());
+ EXPECT_THAT(package->GetPackageName(), StrEq("android"));
+ EXPECT_THAT(package->GetPackageId(), Eq(0x01));
+
+ const uint8_t type_index = get_type_id(android::R::string::cancel) - 1;
+ const uint16_t entry_index = get_entry_id(android::R::string::cancel);
+
+ const TypeSpec* type_spec = package->GetTypeSpecByTypeIndex(type_index);
+ ASSERT_THAT(type_spec, NotNull());
+ ASSERT_THAT(type_spec->type_count, Ge(1u));
+
+ const ResTable_type* type = type_spec->types[0];
+ ASSERT_THAT(type, NotNull());
+ ASSERT_THAT(LoadedPackage::GetEntry(type, entry_index), NotNull());
+}
+
// structs with size fields (like Res_value, ResTable_entry) should be
// backwards and forwards compatible (aka checking the size field against
// sizeof(Res_value) might not be backwards compatible.
diff --git a/libs/androidfw/tests/data/loader/resources.arsc b/libs/androidfw/tests/data/loader/resources.arsc
new file mode 100644
index 000000000000..2c881f2cdfe5
--- /dev/null
+++ b/libs/androidfw/tests/data/loader/resources.arsc
Binary files differ
diff --git a/libs/androidfw/tests/data/system/R.h b/libs/androidfw/tests/data/system/R.h
index becb38830fb3..374107484784 100644
--- a/libs/androidfw/tests/data/system/R.h
+++ b/libs/androidfw/tests/data/system/R.h
@@ -40,6 +40,12 @@ struct R {
number = 0x01030000, // sv
};
};
+
+ struct string {
+ enum : uint32_t {
+ cancel = 0x01040000,
+ };
+ };
};
} // namespace android