summaryrefslogtreecommitdiff
path: root/libs/androidfw/tests
diff options
context:
space:
mode:
authorRyan Mitchell <rtmitchell@google.com>2018-10-16 12:48:38 -0700
committerRyan Mitchell <rtmitchell@google.com>2018-11-01 17:01:28 -0700
commitb3ae42e9762a826b1591ab932a6a092ad3e6c3a7 (patch)
tree0ca9b27919cb30c1d07dfcb4c0365d92768b6eef /libs/androidfw/tests
parentcfc152af9cbfc47ed4c8ab8a8b6e9ee4214206ea (diff)
Copy common resources between styles
The docuemntation of setTo states that the resources two styles from different AssetManagers have in common will be set in the destination theme. This change adds this functionality. The package ids of the attributes, the package ids of reference values, and the cookie of attribute values have to be rewritten to match the destination AssetManager. This change can later be made more generic if rewriting references between packages is needed elsewhere. Bug: 115897657 Test: libandroidfw_tests and manual test of app specified in the bug Change-Id: Iee999ea2cc8473168cac11aaf3c34e14c958e5ae
Diffstat (limited to 'libs/androidfw/tests')
-rw-r--r--libs/androidfw/tests/Theme_test.cpp39
-rw-r--r--libs/androidfw/tests/data/lib_two/R.h12
-rw-r--r--libs/androidfw/tests/data/lib_two/lib_two.apkbin1106 -> 1426 bytes
-rw-r--r--libs/androidfw/tests/data/lib_two/res/values/values.xml8
4 files changed, 50 insertions, 9 deletions
diff --git a/libs/androidfw/tests/Theme_test.cpp b/libs/androidfw/tests/Theme_test.cpp
index 55d53edf6a2b..2c39ceead123 100644
--- a/libs/androidfw/tests/Theme_test.cpp
+++ b/libs/androidfw/tests/Theme_test.cpp
@@ -21,12 +21,14 @@
#include "TestHelpers.h"
#include "androidfw/ResourceUtils.h"
#include "data/lib_one/R.h"
+#include "data/lib_two/R.h"
#include "data/libclient/R.h"
#include "data/styles/R.h"
#include "data/system/R.h"
namespace app = com::android::app;
namespace lib_one = com::android::lib_one;
+namespace lib_two = com::android::lib_two;
namespace libclient = com::android::libclient;
namespace android {
@@ -263,7 +265,7 @@ TEST_F(ThemeTest, CopyThemeSameAssetManager) {
ASSERT_TRUE(theme_two->ApplyStyle(app::R::style::StyleThree));
// Copy the theme to theme_one.
- ASSERT_TRUE(theme_one->SetTo(*theme_two));
+ theme_one->SetTo(*theme_two);
// Clear theme_two to make sure we test that there WAS a copy.
theme_two->Clear();
@@ -279,12 +281,14 @@ TEST_F(ThemeTest, CopyThemeSameAssetManager) {
EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
}
-TEST_F(ThemeTest, OnlyCopySystemThemeWhenAssetManagersDiffer) {
+TEST_F(ThemeTest, OnlyCopySameAssetsThemeWhenAssetManagersDiffer) {
AssetManager2 assetmanager_one;
- assetmanager_one.SetApkAssets({system_assets_.get(), style_assets_.get()});
+ assetmanager_one.SetApkAssets({system_assets_.get(), lib_one_assets_.get(), style_assets_.get(),
+ libclient_assets_.get()});
AssetManager2 assetmanager_two;
- assetmanager_two.SetApkAssets({system_assets_.get(), style_assets_.get()});
+ assetmanager_two.SetApkAssets({system_assets_.get(), lib_two_assets_.get(), lib_one_assets_.get(),
+ style_assets_.get()});
auto theme_one = assetmanager_one.NewTheme();
ASSERT_TRUE(theme_one->ApplyStyle(app::R::style::StyleOne));
@@ -292,17 +296,34 @@ TEST_F(ThemeTest, OnlyCopySystemThemeWhenAssetManagersDiffer) {
auto theme_two = assetmanager_two.NewTheme();
ASSERT_TRUE(theme_two->ApplyStyle(R::style::Theme_One));
ASSERT_TRUE(theme_two->ApplyStyle(app::R::style::StyleTwo));
+ ASSERT_TRUE(theme_two->ApplyStyle(fix_package_id(lib_one::R::style::Theme, 0x03),
+ false /*force*/));
+ ASSERT_TRUE(theme_two->ApplyStyle(fix_package_id(lib_two::R::style::Theme, 0x02),
+ false /*force*/));
- EXPECT_TRUE(theme_one->SetTo(*theme_two));
+ theme_one->SetTo(*theme_two);
Res_value value;
uint32_t flags;
- // No app resources.
- EXPECT_EQ(kInvalidCookie, theme_one->GetAttribute(app::R::attr::attr_one, &value, &flags));
+ // System resources (present in destination asset manager)
+ EXPECT_EQ(0, theme_one->GetAttribute(R::attr::foreground, &value, &flags));
+
+ // The cookie of the style asset is 3 in the source and 2 in the destination.
+ // Check that the cookie has been rewritten to the destination values
+ EXPECT_EQ(2, theme_one->GetAttribute(app::R::attr::attr_one, &value, &flags));
+
+ // The cookie of the lib_one asset is 2 in the source and 1 in the destination.
+ // The package id of the lib_one package is 0x03 in the source and 0x02 in the destination
+ // Check that the cookie and packages have been rewritten to the destination values
+ EXPECT_EQ(1, theme_one->GetAttribute(fix_package_id(lib_one::R::attr::attr1, 0x02), &value,
+ &flags));
+ EXPECT_EQ(1, theme_one->GetAttribute(fix_package_id(lib_one::R::attr::attr2, 0x02), &value,
+ &flags));
- // Only system.
- EXPECT_NE(kInvalidCookie, theme_one->GetAttribute(R::attr::foreground, &value, &flags));
+ // attr2 references an attribute in lib_one. Check that the resolution of the attribute value is
+ // correct after the value of attr2 had its package id rewritten to the destination package id
+ EXPECT_EQ(700, value.data);
}
} // namespace android
diff --git a/libs/androidfw/tests/data/lib_two/R.h b/libs/androidfw/tests/data/lib_two/R.h
index c04a9d3b4de0..92b9cc10e7a8 100644
--- a/libs/androidfw/tests/data/lib_two/R.h
+++ b/libs/androidfw/tests/data/lib_two/R.h
@@ -24,12 +24,24 @@ namespace android {
namespace lib_two {
struct R {
+ struct attr {
+ enum : uint32_t {
+ attr3 = 0x02010000, // default
+ };
+ };
+
struct string {
enum : uint32_t {
LibraryString = 0x02020000, // default
foo = 0x02020001, // default
};
};
+
+ struct style {
+ enum : uint32_t {
+ Theme = 0x02030000, // default
+ };
+ };
};
} // namespace lib_two
diff --git a/libs/androidfw/tests/data/lib_two/lib_two.apk b/libs/androidfw/tests/data/lib_two/lib_two.apk
index ad44f9c21e31..486c23000276 100644
--- a/libs/androidfw/tests/data/lib_two/lib_two.apk
+++ b/libs/androidfw/tests/data/lib_two/lib_two.apk
Binary files differ
diff --git a/libs/androidfw/tests/data/lib_two/res/values/values.xml b/libs/androidfw/tests/data/lib_two/res/values/values.xml
index f4eea2610cab..340d14c34c5d 100644
--- a/libs/androidfw/tests/data/lib_two/res/values/values.xml
+++ b/libs/androidfw/tests/data/lib_two/res/values/values.xml
@@ -15,9 +15,17 @@
-->
<resources>
+ <public type="attr" name="attr3" id="0x00010000" />
+ <attr name="attr3" format="integer" />
+
<public type="string" name="LibraryString" id="0x00020000" />
<string name="LibraryString">Hi from library two</string>
<public type="string" name="foo" id="0x00020001" />
<string name="foo">Foo from lib_two</string>
+
+ <public type="style" name="Theme" id="0x02030000" />
+ <style name="Theme">
+ <item name="com.android.lib_two:attr3">800</item>
+ </style>
</resources>