Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "androidfw/AssetManager2.h" |
| 18 | #include "androidfw/AssetManager.h" |
| 19 | |
| 20 | #include "android-base/logging.h" |
| 21 | |
| 22 | #include "TestHelpers.h" |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame^] | 23 | #include "data/appaslib/R.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 24 | #include "data/basic/R.h" |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame^] | 25 | #include "data/lib_one/R.h" |
| 26 | #include "data/lib_two/R.h" |
| 27 | #include "data/libclient/R.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 28 | #include "data/styles/R.h" |
| 29 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 30 | namespace app = com::android::app; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame^] | 31 | namespace appaslib = com::android::appaslib::app; |
| 32 | namespace basic = com::android::basic; |
| 33 | namespace lib_one = com::android::lib_one; |
| 34 | namespace lib_two = com::android::lib_two; |
| 35 | namespace libclient = com::android::libclient; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 36 | |
| 37 | namespace android { |
| 38 | |
| 39 | class AssetManager2Test : public ::testing::Test { |
| 40 | public: |
| 41 | void SetUp() override { |
| 42 | basic_assets_ = ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk"); |
| 43 | ASSERT_NE(nullptr, basic_assets_); |
| 44 | |
| 45 | basic_de_fr_assets_ = ApkAssets::Load(GetTestDataPath() + "/basic/basic_de_fr.apk"); |
| 46 | ASSERT_NE(nullptr, basic_de_fr_assets_); |
| 47 | |
| 48 | style_assets_ = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk"); |
| 49 | ASSERT_NE(nullptr, style_assets_); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame^] | 50 | |
| 51 | lib_one_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_one/lib_one.apk"); |
| 52 | ASSERT_NE(nullptr, lib_one_assets_); |
| 53 | |
| 54 | lib_two_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_two/lib_two.apk"); |
| 55 | ASSERT_NE(nullptr, lib_two_assets_); |
| 56 | |
| 57 | libclient_assets_ = ApkAssets::Load(GetTestDataPath() + "/libclient/libclient.apk"); |
| 58 | ASSERT_NE(nullptr, libclient_assets_); |
| 59 | |
| 60 | appaslib_assets_ = ApkAssets::Load(GetTestDataPath() + "/appaslib/appaslib.apk"); |
| 61 | ASSERT_NE(nullptr, appaslib_assets_); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | protected: |
| 65 | std::unique_ptr<ApkAssets> basic_assets_; |
| 66 | std::unique_ptr<ApkAssets> basic_de_fr_assets_; |
| 67 | std::unique_ptr<ApkAssets> style_assets_; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame^] | 68 | std::unique_ptr<ApkAssets> lib_one_assets_; |
| 69 | std::unique_ptr<ApkAssets> lib_two_assets_; |
| 70 | std::unique_ptr<ApkAssets> libclient_assets_; |
| 71 | std::unique_ptr<ApkAssets> appaslib_assets_; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame^] | 74 | TEST_F(AssetManager2Test, FindsResourceFromSingleApkAssets) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 75 | ResTable_config desired_config; |
| 76 | memset(&desired_config, 0, sizeof(desired_config)); |
| 77 | desired_config.language[0] = 'd'; |
| 78 | desired_config.language[1] = 'e'; |
| 79 | |
| 80 | AssetManager2 assetmanager; |
| 81 | assetmanager.SetConfiguration(desired_config); |
| 82 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 83 | |
| 84 | Res_value value; |
| 85 | ResTable_config selected_config; |
| 86 | uint32_t flags; |
| 87 | |
| 88 | ApkAssetsCookie cookie = |
| 89 | assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/, |
| 90 | 0 /*density_override*/, &value, &selected_config, &flags); |
| 91 | ASSERT_NE(kInvalidCookie, cookie); |
| 92 | |
| 93 | // Came from our ApkAssets. |
| 94 | EXPECT_EQ(0, cookie); |
| 95 | |
| 96 | // It is the default config. |
| 97 | EXPECT_EQ(0, selected_config.language[0]); |
| 98 | EXPECT_EQ(0, selected_config.language[1]); |
| 99 | |
| 100 | // It is a string. |
| 101 | EXPECT_EQ(Res_value::TYPE_STRING, value.dataType); |
| 102 | } |
| 103 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame^] | 104 | TEST_F(AssetManager2Test, FindsResourceFromMultipleApkAssets) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 105 | ResTable_config desired_config; |
| 106 | memset(&desired_config, 0, sizeof(desired_config)); |
| 107 | desired_config.language[0] = 'd'; |
| 108 | desired_config.language[1] = 'e'; |
| 109 | |
| 110 | AssetManager2 assetmanager; |
| 111 | assetmanager.SetConfiguration(desired_config); |
| 112 | assetmanager.SetApkAssets({basic_assets_.get(), basic_de_fr_assets_.get()}); |
| 113 | |
| 114 | Res_value value; |
| 115 | ResTable_config selected_config; |
| 116 | uint32_t flags; |
| 117 | |
| 118 | ApkAssetsCookie cookie = |
| 119 | assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/, |
| 120 | 0 /*density_override*/, &value, &selected_config, &flags); |
| 121 | ASSERT_NE(kInvalidCookie, cookie); |
| 122 | |
| 123 | // Came from our de_fr ApkAssets. |
| 124 | EXPECT_EQ(1, cookie); |
| 125 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame^] | 126 | // The configuration is German. |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 127 | EXPECT_EQ('d', selected_config.language[0]); |
| 128 | EXPECT_EQ('e', selected_config.language[1]); |
| 129 | |
| 130 | // It is a string. |
| 131 | EXPECT_EQ(Res_value::TYPE_STRING, value.dataType); |
| 132 | } |
| 133 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame^] | 134 | TEST_F(AssetManager2Test, FindsResourceFromSharedLibrary) { |
| 135 | AssetManager2 assetmanager; |
| 136 | |
| 137 | // libclient is built with lib_one and then lib_two in order. |
| 138 | // Reverse the order to test that proper package ID re-assignment is happening. |
| 139 | assetmanager.SetApkAssets( |
| 140 | {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()}); |
| 141 | |
| 142 | Res_value value; |
| 143 | ResTable_config selected_config; |
| 144 | uint32_t flags; |
| 145 | |
| 146 | ApkAssetsCookie cookie = |
| 147 | assetmanager.GetResource(libclient::R::string::foo_one, false /*may_be_bag*/, |
| 148 | 0 /*density_override*/, &value, &selected_config, &flags); |
| 149 | ASSERT_NE(kInvalidCookie, cookie); |
| 150 | |
| 151 | // Reference comes from libclient. |
| 152 | EXPECT_EQ(2, cookie); |
| 153 | EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType); |
| 154 | |
| 155 | // Lookup the reference. |
| 156 | cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/, |
| 157 | &value, &selected_config, &flags); |
| 158 | ASSERT_NE(kInvalidCookie, cookie); |
| 159 | EXPECT_EQ(1, cookie); |
| 160 | EXPECT_EQ(Res_value::TYPE_STRING, value.dataType); |
| 161 | EXPECT_EQ(std::string("Foo from lib_one"), |
| 162 | GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data)); |
| 163 | |
| 164 | cookie = assetmanager.GetResource(libclient::R::string::foo_two, false /*may_be_bag*/, |
| 165 | 0 /*density_override*/, &value, &selected_config, &flags); |
| 166 | ASSERT_NE(kInvalidCookie, cookie); |
| 167 | |
| 168 | // Reference comes from libclient. |
| 169 | EXPECT_EQ(2, cookie); |
| 170 | EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType); |
| 171 | |
| 172 | // Lookup the reference. |
| 173 | cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/, |
| 174 | &value, &selected_config, &flags); |
| 175 | ASSERT_NE(kInvalidCookie, cookie); |
| 176 | EXPECT_EQ(0, cookie); |
| 177 | EXPECT_EQ(Res_value::TYPE_STRING, value.dataType); |
| 178 | EXPECT_EQ(std::string("Foo from lib_two"), |
| 179 | GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data)); |
| 180 | } |
| 181 | |
| 182 | TEST_F(AssetManager2Test, FindsResourceFromAppLoadedAsSharedLibrary) { |
| 183 | AssetManager2 assetmanager; |
| 184 | assetmanager.SetApkAssets({appaslib_assets_.get()}); |
| 185 | |
| 186 | // The appaslib package will have been assigned the package ID 0x02. |
| 187 | |
| 188 | Res_value value; |
| 189 | ResTable_config selected_config; |
| 190 | uint32_t flags; |
| 191 | ApkAssetsCookie cookie = assetmanager.GetResource( |
| 192 | util::fix_package_id(appaslib::R::integer::number1, 0x02), false /*may_be_bag*/, |
| 193 | 0u /*density_override*/, &value, &selected_config, &flags); |
| 194 | ASSERT_NE(kInvalidCookie, cookie); |
| 195 | EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType); |
| 196 | EXPECT_EQ(util::fix_package_id(appaslib::R::array::integerArray1, 0x02), value.data); |
| 197 | } |
| 198 | |
| 199 | TEST_F(AssetManager2Test, FindsBagResourceFromSingleApkAssets) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 200 | AssetManager2 assetmanager; |
| 201 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 202 | |
| 203 | const ResolvedBag* bag = assetmanager.GetBag(basic::R::array::integerArray1); |
| 204 | ASSERT_NE(nullptr, bag); |
| 205 | ASSERT_EQ(3u, bag->entry_count); |
| 206 | |
| 207 | EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[0].value.dataType); |
| 208 | EXPECT_EQ(1u, bag->entries[0].value.data); |
| 209 | EXPECT_EQ(0, bag->entries[0].cookie); |
| 210 | |
| 211 | EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[1].value.dataType); |
| 212 | EXPECT_EQ(2u, bag->entries[1].value.data); |
| 213 | EXPECT_EQ(0, bag->entries[1].cookie); |
| 214 | |
| 215 | EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[2].value.dataType); |
| 216 | EXPECT_EQ(3u, bag->entries[2].value.data); |
| 217 | EXPECT_EQ(0, bag->entries[2].cookie); |
| 218 | } |
| 219 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame^] | 220 | TEST_F(AssetManager2Test, FindsBagResourceFromMultipleApkAssets) {} |
| 221 | |
| 222 | TEST_F(AssetManager2Test, FindsBagResourceFromSharedLibrary) { |
| 223 | AssetManager2 assetmanager; |
| 224 | |
| 225 | // libclient is built with lib_one and then lib_two in order. |
| 226 | // Reverse the order to test that proper package ID re-assignment is happening. |
| 227 | assetmanager.SetApkAssets( |
| 228 | {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()}); |
| 229 | |
| 230 | const ResolvedBag* bag = assetmanager.GetBag(libclient::R::style::Theme); |
| 231 | ASSERT_NE(nullptr, bag); |
| 232 | ASSERT_GE(bag->entry_count, 2u); |
| 233 | |
| 234 | // First two attributes come from lib_one. |
| 235 | EXPECT_EQ(1, bag->entries[0].cookie); |
| 236 | EXPECT_EQ(0x03, util::get_package_id(bag->entries[0].key)); |
| 237 | EXPECT_EQ(1, bag->entries[1].cookie); |
| 238 | EXPECT_EQ(0x03, util::get_package_id(bag->entries[1].key)); |
| 239 | } |
| 240 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 241 | TEST_F(AssetManager2Test, MergesStylesWithParentFromSingleApkAssets) { |
| 242 | AssetManager2 assetmanager; |
| 243 | assetmanager.SetApkAssets({style_assets_.get()}); |
| 244 | |
| 245 | const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleOne); |
| 246 | ASSERT_NE(nullptr, bag_one); |
| 247 | ASSERT_EQ(2u, bag_one->entry_count); |
| 248 | |
| 249 | EXPECT_EQ(app::R::attr::attr_one, bag_one->entries[0].key); |
| 250 | EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[0].value.dataType); |
| 251 | EXPECT_EQ(1u, bag_one->entries[0].value.data); |
| 252 | EXPECT_EQ(0, bag_one->entries[0].cookie); |
| 253 | |
| 254 | EXPECT_EQ(app::R::attr::attr_two, bag_one->entries[1].key); |
| 255 | EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[1].value.dataType); |
| 256 | EXPECT_EQ(2u, bag_one->entries[1].value.data); |
| 257 | EXPECT_EQ(0, bag_one->entries[1].cookie); |
| 258 | |
| 259 | const ResolvedBag* bag_two = assetmanager.GetBag(app::R::style::StyleTwo); |
| 260 | ASSERT_NE(nullptr, bag_two); |
| 261 | ASSERT_EQ(5u, bag_two->entry_count); |
| 262 | |
| 263 | // attr_one is inherited from StyleOne. |
| 264 | EXPECT_EQ(app::R::attr::attr_one, bag_two->entries[0].key); |
| 265 | EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[0].value.dataType); |
| 266 | EXPECT_EQ(1u, bag_two->entries[0].value.data); |
| 267 | EXPECT_EQ(0, bag_two->entries[0].cookie); |
| 268 | |
| 269 | // attr_two should be overridden from StyleOne by StyleTwo. |
| 270 | EXPECT_EQ(app::R::attr::attr_two, bag_two->entries[1].key); |
| 271 | EXPECT_EQ(Res_value::TYPE_STRING, bag_two->entries[1].value.dataType); |
| 272 | EXPECT_EQ(0, bag_two->entries[1].cookie); |
| 273 | EXPECT_EQ(std::string("string"), GetStringFromPool(assetmanager.GetStringPoolForCookie(0), |
| 274 | bag_two->entries[1].value.data)); |
| 275 | |
| 276 | // The rest are new attributes. |
| 277 | |
| 278 | EXPECT_EQ(app::R::attr::attr_three, bag_two->entries[2].key); |
| 279 | EXPECT_EQ(Res_value::TYPE_ATTRIBUTE, bag_two->entries[2].value.dataType); |
| 280 | EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[2].value.data); |
| 281 | EXPECT_EQ(0, bag_two->entries[2].cookie); |
| 282 | |
| 283 | EXPECT_EQ(app::R::attr::attr_five, bag_two->entries[3].key); |
| 284 | EXPECT_EQ(Res_value::TYPE_REFERENCE, bag_two->entries[3].value.dataType); |
| 285 | EXPECT_EQ(app::R::string::string_one, bag_two->entries[3].value.data); |
| 286 | EXPECT_EQ(0, bag_two->entries[3].cookie); |
| 287 | |
| 288 | EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[4].key); |
| 289 | EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[4].value.dataType); |
| 290 | EXPECT_EQ(3u, bag_two->entries[4].value.data); |
| 291 | EXPECT_EQ(0, bag_two->entries[4].cookie); |
| 292 | } |
| 293 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 294 | TEST_F(AssetManager2Test, OpensFileFromSingleApkAssets) {} |
| 295 | |
| 296 | TEST_F(AssetManager2Test, OpensFileFromMultipleApkAssets) {} |
| 297 | |
| 298 | } // namespace android |