diff options
author | Ryan Mitchell <rtmitchell@google.com> | 2019-03-08 17:26:28 -0800 |
---|---|---|
committer | Ryan Mitchell <rtmitchell@google.com> | 2019-04-05 12:44:51 -0700 |
commit | 93bca97e7ce1f3a7938d6ce5af5f6f325ab66f89 (patch) | |
tree | 1348e5279b48745a2d057c213c21b9655b6fb987 /libs/androidfw/tests | |
parent | dd07ae579c291a2b6ffe09bd576fd908eb9e5ddd (diff) |
Allow non-references to be copied between AssetManagers
Hard-coded values in styles can be copied between AssetManagers even if
the source package is not present in the destination AssetManager. Only
references and strings should be prevented from being copied over
because they would be invalid in the destination AssetManager.
Bug:126400561
Test: manual
Change-Id: I970a3e961763b2c003c15b950d864a9a0b615022
Diffstat (limited to 'libs/androidfw/tests')
-rw-r--r-- | libs/androidfw/tests/Theme_test.cpp | 73 | ||||
-rw-r--r-- | libs/androidfw/tests/data/styles/R.h | 1 | ||||
-rwxr-xr-x | libs/androidfw/tests/data/styles/build | 4 | ||||
-rw-r--r-- | libs/androidfw/tests/data/styles/res/values/styles.xml | 10 | ||||
-rw-r--r-- | libs/androidfw/tests/data/styles/styles.apk | bin | 2550 -> 2774 bytes |
5 files changed, 67 insertions, 21 deletions
diff --git a/libs/androidfw/tests/Theme_test.cpp b/libs/androidfw/tests/Theme_test.cpp index 2c39ceead123..be5ecd94a588 100644 --- a/libs/androidfw/tests/Theme_test.cpp +++ b/libs/androidfw/tests/Theme_test.cpp @@ -282,48 +282,81 @@ TEST_F(ThemeTest, CopyThemeSameAssetManager) { } TEST_F(ThemeTest, OnlyCopySameAssetsThemeWhenAssetManagersDiffer) { - AssetManager2 assetmanager_one; - assetmanager_one.SetApkAssets({system_assets_.get(), lib_one_assets_.get(), style_assets_.get(), + AssetManager2 assetmanager_dst; + assetmanager_dst.SetApkAssets({system_assets_.get(), lib_one_assets_.get(), style_assets_.get(), libclient_assets_.get()}); - AssetManager2 assetmanager_two; - assetmanager_two.SetApkAssets({system_assets_.get(), lib_two_assets_.get(), lib_one_assets_.get(), + AssetManager2 assetmanager_src; + assetmanager_src.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)); + auto theme_dst = assetmanager_dst.NewTheme(); + ASSERT_TRUE(theme_dst->ApplyStyle(app::R::style::StyleOne)); - 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), + auto theme_src = assetmanager_src.NewTheme(); + ASSERT_TRUE(theme_src->ApplyStyle(R::style::Theme_One)); + ASSERT_TRUE(theme_src->ApplyStyle(app::R::style::StyleTwo)); + ASSERT_TRUE(theme_src->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), + ASSERT_TRUE(theme_src->ApplyStyle(fix_package_id(lib_two::R::style::Theme, 0x02), false /*force*/)); - theme_one->SetTo(*theme_two); + theme_dst->SetTo(*theme_src); Res_value value; uint32_t flags; - // System resources (present in destination asset manager) - EXPECT_EQ(0, theme_one->GetAttribute(R::attr::foreground, &value, &flags)); + // System resources (present in destination asset manager). + EXPECT_EQ(0, theme_dst->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)); + // Check that the cookie has been rewritten to the destination values. + EXPECT_EQ(2, theme_dst->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, + // Check that the cookie and packages have been rewritten to the destination values. + EXPECT_EQ(1, theme_dst->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, + EXPECT_EQ(1, theme_dst->GetAttribute(fix_package_id(lib_one::R::attr::attr2, 0x02), &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 + // correct after the value of attr2 had its package id rewritten to the destination package id. EXPECT_EQ(700, value.data); } +TEST_F(ThemeTest, CopyNonReferencesWhenPackagesDiffer) { + AssetManager2 assetmanager_dst; + assetmanager_dst.SetApkAssets({system_assets_.get()}); + + AssetManager2 assetmanager_src; + assetmanager_src.SetApkAssets({system_assets_.get(), style_assets_.get()}); + + auto theme_dst = assetmanager_dst.NewTheme(); + auto theme_src = assetmanager_src.NewTheme(); + ASSERT_TRUE(theme_src->ApplyStyle(app::R::style::StyleSeven)); + theme_dst->SetTo(*theme_src); + + Res_value value; + uint32_t flags; + + // Allow inline resource values to be copied even if the source apk asset is not present in the + // destination. + EXPECT_EQ(0, theme_dst->GetAttribute(0x0101021b /* android:versionCode */, &value, &flags)); + + // Do not copy strings since the data is an index into the values string pool of the source apk + // asset. + EXPECT_EQ(-1, theme_dst->GetAttribute(0x01010001 /* android:label */, &value, &flags)); + + // Do not copy values that reference another resource if the resource is not present in the + // destination. + EXPECT_EQ(-1, theme_dst->GetAttribute(0x01010002 /* android:icon */, &value, &flags)); + EXPECT_EQ(-1, theme_dst->GetAttribute(0x010100d1 /* android:tag */, &value, &flags)); + + // Allow @empty to and @null to be copied. + EXPECT_EQ(0, theme_dst->GetAttribute(0x010100d0 /* android:id */, &value, &flags)); + EXPECT_EQ(0, theme_dst->GetAttribute(0x01010000 /* android:theme */, &value, &flags)); +} + } // namespace android diff --git a/libs/androidfw/tests/data/styles/R.h b/libs/androidfw/tests/data/styles/R.h index 538a84717176..f11486fe924a 100644 --- a/libs/androidfw/tests/data/styles/R.h +++ b/libs/androidfw/tests/data/styles/R.h @@ -51,6 +51,7 @@ struct R { StyleFour = 0x7f020003u, StyleFive = 0x7f020004u, StyleSix = 0x7f020005u, + StyleSeven = 0x7f020006u, }; }; }; diff --git a/libs/androidfw/tests/data/styles/build b/libs/androidfw/tests/data/styles/build index 1ef8e6e19208..7b7c1f7aa962 100755 --- a/libs/androidfw/tests/data/styles/build +++ b/libs/androidfw/tests/data/styles/build @@ -2,5 +2,7 @@ set -e +PATH_TO_FRAMEWORK_RES=${ANDROID_BUILD_TOP}/prebuilts/sdk/current/public/android.jar + aapt2 compile -o compiled.flata --dir res -aapt2 link -o styles.apk --manifest AndroidManifest.xml compiled.flata +aapt2 link -o styles.apk --manifest AndroidManifest.xml -I $PATH_TO_FRAMEWORK_RES compiled.flata diff --git a/libs/androidfw/tests/data/styles/res/values/styles.xml b/libs/androidfw/tests/data/styles/res/values/styles.xml index 1a231768dade..06774a8a6005 100644 --- a/libs/androidfw/tests/data/styles/res/values/styles.xml +++ b/libs/androidfw/tests/data/styles/res/values/styles.xml @@ -79,4 +79,14 @@ <item name="attr_three">3</item> </style> + <public type="style" name="StyleSeven" id="0x7f020006" /> + <style name="StyleSeven" > + <item name="android:versionCode">3</item> + <item name="android:label">"string"</item> + <item name="android:icon">?attr/attr_one</item> + <item name="android:tag">@string/string_one</item> + <item name="android:id">@null</item> + <item name="android:theme">@empty</item> + </style> + </resources> diff --git a/libs/androidfw/tests/data/styles/styles.apk b/libs/androidfw/tests/data/styles/styles.apk Binary files differindex cd5c7a1c6c12..92e9bf90101e 100644 --- a/libs/androidfw/tests/data/styles/styles.apk +++ b/libs/androidfw/tests/data/styles/styles.apk |