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 | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 23 | #include "androidfw/ResourceUtils.h" |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 24 | #include "data/appaslib/R.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 25 | #include "data/basic/R.h" |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 26 | #include "data/lib_one/R.h" |
| 27 | #include "data/lib_two/R.h" |
| 28 | #include "data/libclient/R.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 29 | #include "data/styles/R.h" |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 30 | #include "data/system/R.h" |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 31 | #include "data/unverified/R.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 32 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 33 | namespace app = com::android::app; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 34 | namespace appaslib = com::android::appaslib::app; |
| 35 | namespace basic = com::android::basic; |
| 36 | namespace lib_one = com::android::lib_one; |
| 37 | namespace lib_two = com::android::lib_two; |
| 38 | namespace libclient = com::android::libclient; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 39 | namespace unverified = com::android::unverified; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 40 | |
| 41 | namespace android { |
| 42 | |
| 43 | class AssetManager2Test : public ::testing::Test { |
| 44 | public: |
| 45 | void SetUp() override { |
| 46 | basic_assets_ = ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk"); |
| 47 | ASSERT_NE(nullptr, basic_assets_); |
| 48 | |
| 49 | basic_de_fr_assets_ = ApkAssets::Load(GetTestDataPath() + "/basic/basic_de_fr.apk"); |
| 50 | ASSERT_NE(nullptr, basic_de_fr_assets_); |
| 51 | |
| 52 | style_assets_ = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk"); |
| 53 | ASSERT_NE(nullptr, style_assets_); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 54 | |
| 55 | lib_one_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_one/lib_one.apk"); |
| 56 | ASSERT_NE(nullptr, lib_one_assets_); |
| 57 | |
| 58 | lib_two_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_two/lib_two.apk"); |
| 59 | ASSERT_NE(nullptr, lib_two_assets_); |
| 60 | |
| 61 | libclient_assets_ = ApkAssets::Load(GetTestDataPath() + "/libclient/libclient.apk"); |
| 62 | ASSERT_NE(nullptr, libclient_assets_); |
| 63 | |
| 64 | appaslib_assets_ = ApkAssets::Load(GetTestDataPath() + "/appaslib/appaslib.apk"); |
| 65 | ASSERT_NE(nullptr, appaslib_assets_); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 66 | |
| 67 | system_assets_ = ApkAssets::Load(GetTestDataPath() + "/system/system.apk", true /*system*/); |
| 68 | ASSERT_NE(nullptr, system_assets_); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | protected: |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 72 | std::unique_ptr<const ApkAssets> basic_assets_; |
| 73 | std::unique_ptr<const ApkAssets> basic_de_fr_assets_; |
| 74 | std::unique_ptr<const ApkAssets> style_assets_; |
| 75 | std::unique_ptr<const ApkAssets> lib_one_assets_; |
| 76 | std::unique_ptr<const ApkAssets> lib_two_assets_; |
| 77 | std::unique_ptr<const ApkAssets> libclient_assets_; |
| 78 | std::unique_ptr<const ApkAssets> appaslib_assets_; |
| 79 | std::unique_ptr<const ApkAssets> system_assets_; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 82 | TEST_F(AssetManager2Test, FindsResourceFromSingleApkAssets) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 83 | ResTable_config desired_config; |
| 84 | memset(&desired_config, 0, sizeof(desired_config)); |
| 85 | desired_config.language[0] = 'd'; |
| 86 | desired_config.language[1] = 'e'; |
| 87 | |
| 88 | AssetManager2 assetmanager; |
| 89 | assetmanager.SetConfiguration(desired_config); |
| 90 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 91 | |
| 92 | Res_value value; |
| 93 | ResTable_config selected_config; |
| 94 | uint32_t flags; |
| 95 | |
| 96 | ApkAssetsCookie cookie = |
| 97 | assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/, |
| 98 | 0 /*density_override*/, &value, &selected_config, &flags); |
| 99 | ASSERT_NE(kInvalidCookie, cookie); |
| 100 | |
| 101 | // Came from our ApkAssets. |
| 102 | EXPECT_EQ(0, cookie); |
| 103 | |
| 104 | // It is the default config. |
| 105 | EXPECT_EQ(0, selected_config.language[0]); |
| 106 | EXPECT_EQ(0, selected_config.language[1]); |
| 107 | |
| 108 | // It is a string. |
| 109 | EXPECT_EQ(Res_value::TYPE_STRING, value.dataType); |
| 110 | } |
| 111 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 112 | TEST_F(AssetManager2Test, FindsResourceFromMultipleApkAssets) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 113 | ResTable_config desired_config; |
| 114 | memset(&desired_config, 0, sizeof(desired_config)); |
| 115 | desired_config.language[0] = 'd'; |
| 116 | desired_config.language[1] = 'e'; |
| 117 | |
| 118 | AssetManager2 assetmanager; |
| 119 | assetmanager.SetConfiguration(desired_config); |
| 120 | assetmanager.SetApkAssets({basic_assets_.get(), basic_de_fr_assets_.get()}); |
| 121 | |
| 122 | Res_value value; |
| 123 | ResTable_config selected_config; |
| 124 | uint32_t flags; |
| 125 | |
| 126 | ApkAssetsCookie cookie = |
| 127 | assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/, |
| 128 | 0 /*density_override*/, &value, &selected_config, &flags); |
| 129 | ASSERT_NE(kInvalidCookie, cookie); |
| 130 | |
| 131 | // Came from our de_fr ApkAssets. |
| 132 | EXPECT_EQ(1, cookie); |
| 133 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 134 | // The configuration is German. |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 135 | EXPECT_EQ('d', selected_config.language[0]); |
| 136 | EXPECT_EQ('e', selected_config.language[1]); |
| 137 | |
| 138 | // It is a string. |
| 139 | EXPECT_EQ(Res_value::TYPE_STRING, value.dataType); |
| 140 | } |
| 141 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 142 | TEST_F(AssetManager2Test, FindsResourceFromSharedLibrary) { |
| 143 | AssetManager2 assetmanager; |
| 144 | |
| 145 | // libclient is built with lib_one and then lib_two in order. |
| 146 | // Reverse the order to test that proper package ID re-assignment is happening. |
| 147 | assetmanager.SetApkAssets( |
| 148 | {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()}); |
| 149 | |
| 150 | Res_value value; |
| 151 | ResTable_config selected_config; |
| 152 | uint32_t flags; |
| 153 | |
| 154 | ApkAssetsCookie cookie = |
| 155 | assetmanager.GetResource(libclient::R::string::foo_one, false /*may_be_bag*/, |
| 156 | 0 /*density_override*/, &value, &selected_config, &flags); |
| 157 | ASSERT_NE(kInvalidCookie, cookie); |
| 158 | |
| 159 | // Reference comes from libclient. |
| 160 | EXPECT_EQ(2, cookie); |
| 161 | EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType); |
| 162 | |
| 163 | // Lookup the reference. |
| 164 | cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/, |
| 165 | &value, &selected_config, &flags); |
| 166 | ASSERT_NE(kInvalidCookie, cookie); |
| 167 | EXPECT_EQ(1, cookie); |
| 168 | EXPECT_EQ(Res_value::TYPE_STRING, value.dataType); |
| 169 | EXPECT_EQ(std::string("Foo from lib_one"), |
| 170 | GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data)); |
| 171 | |
| 172 | cookie = assetmanager.GetResource(libclient::R::string::foo_two, false /*may_be_bag*/, |
| 173 | 0 /*density_override*/, &value, &selected_config, &flags); |
| 174 | ASSERT_NE(kInvalidCookie, cookie); |
| 175 | |
| 176 | // Reference comes from libclient. |
| 177 | EXPECT_EQ(2, cookie); |
| 178 | EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType); |
| 179 | |
| 180 | // Lookup the reference. |
| 181 | cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/, |
| 182 | &value, &selected_config, &flags); |
| 183 | ASSERT_NE(kInvalidCookie, cookie); |
| 184 | EXPECT_EQ(0, cookie); |
| 185 | EXPECT_EQ(Res_value::TYPE_STRING, value.dataType); |
| 186 | EXPECT_EQ(std::string("Foo from lib_two"), |
| 187 | GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data)); |
| 188 | } |
| 189 | |
| 190 | TEST_F(AssetManager2Test, FindsResourceFromAppLoadedAsSharedLibrary) { |
| 191 | AssetManager2 assetmanager; |
| 192 | assetmanager.SetApkAssets({appaslib_assets_.get()}); |
| 193 | |
| 194 | // The appaslib package will have been assigned the package ID 0x02. |
| 195 | |
| 196 | Res_value value; |
| 197 | ResTable_config selected_config; |
| 198 | uint32_t flags; |
| 199 | ApkAssetsCookie cookie = assetmanager.GetResource( |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 200 | fix_package_id(appaslib::R::integer::number1, 0x02), false /*may_be_bag*/, |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 201 | 0u /*density_override*/, &value, &selected_config, &flags); |
| 202 | ASSERT_NE(kInvalidCookie, cookie); |
| 203 | EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 204 | EXPECT_EQ(fix_package_id(appaslib::R::array::integerArray1, 0x02), value.data); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | TEST_F(AssetManager2Test, FindsBagResourceFromSingleApkAssets) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 208 | AssetManager2 assetmanager; |
| 209 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 210 | |
| 211 | const ResolvedBag* bag = assetmanager.GetBag(basic::R::array::integerArray1); |
| 212 | ASSERT_NE(nullptr, bag); |
| 213 | ASSERT_EQ(3u, bag->entry_count); |
| 214 | |
| 215 | EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[0].value.dataType); |
| 216 | EXPECT_EQ(1u, bag->entries[0].value.data); |
| 217 | EXPECT_EQ(0, bag->entries[0].cookie); |
| 218 | |
| 219 | EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[1].value.dataType); |
| 220 | EXPECT_EQ(2u, bag->entries[1].value.data); |
| 221 | EXPECT_EQ(0, bag->entries[1].cookie); |
| 222 | |
| 223 | EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[2].value.dataType); |
| 224 | EXPECT_EQ(3u, bag->entries[2].value.data); |
| 225 | EXPECT_EQ(0, bag->entries[2].cookie); |
| 226 | } |
| 227 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 228 | TEST_F(AssetManager2Test, FindsBagResourceFromMultipleApkAssets) {} |
| 229 | |
| 230 | TEST_F(AssetManager2Test, FindsBagResourceFromSharedLibrary) { |
| 231 | AssetManager2 assetmanager; |
| 232 | |
| 233 | // libclient is built with lib_one and then lib_two in order. |
| 234 | // Reverse the order to test that proper package ID re-assignment is happening. |
| 235 | assetmanager.SetApkAssets( |
| 236 | {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()}); |
| 237 | |
| 238 | const ResolvedBag* bag = assetmanager.GetBag(libclient::R::style::Theme); |
| 239 | ASSERT_NE(nullptr, bag); |
| 240 | ASSERT_GE(bag->entry_count, 2u); |
| 241 | |
| 242 | // First two attributes come from lib_one. |
| 243 | EXPECT_EQ(1, bag->entries[0].cookie); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 244 | EXPECT_EQ(0x03, get_package_id(bag->entries[0].key)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 245 | EXPECT_EQ(1, bag->entries[1].cookie); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 246 | EXPECT_EQ(0x03, get_package_id(bag->entries[1].key)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 247 | } |
| 248 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 249 | TEST_F(AssetManager2Test, MergesStylesWithParentFromSingleApkAssets) { |
| 250 | AssetManager2 assetmanager; |
| 251 | assetmanager.SetApkAssets({style_assets_.get()}); |
| 252 | |
| 253 | const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleOne); |
| 254 | ASSERT_NE(nullptr, bag_one); |
| 255 | ASSERT_EQ(2u, bag_one->entry_count); |
| 256 | |
| 257 | EXPECT_EQ(app::R::attr::attr_one, bag_one->entries[0].key); |
| 258 | EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[0].value.dataType); |
| 259 | EXPECT_EQ(1u, bag_one->entries[0].value.data); |
| 260 | EXPECT_EQ(0, bag_one->entries[0].cookie); |
| 261 | |
| 262 | EXPECT_EQ(app::R::attr::attr_two, bag_one->entries[1].key); |
| 263 | EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[1].value.dataType); |
| 264 | EXPECT_EQ(2u, bag_one->entries[1].value.data); |
| 265 | EXPECT_EQ(0, bag_one->entries[1].cookie); |
| 266 | |
| 267 | const ResolvedBag* bag_two = assetmanager.GetBag(app::R::style::StyleTwo); |
| 268 | ASSERT_NE(nullptr, bag_two); |
Adam Lesinski | 32e7501 | 2017-05-09 15:25:37 -0700 | [diff] [blame] | 269 | ASSERT_EQ(6u, bag_two->entry_count); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 270 | |
| 271 | // attr_one is inherited from StyleOne. |
| 272 | EXPECT_EQ(app::R::attr::attr_one, bag_two->entries[0].key); |
| 273 | EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[0].value.dataType); |
| 274 | EXPECT_EQ(1u, bag_two->entries[0].value.data); |
| 275 | EXPECT_EQ(0, bag_two->entries[0].cookie); |
| 276 | |
| 277 | // attr_two should be overridden from StyleOne by StyleTwo. |
| 278 | EXPECT_EQ(app::R::attr::attr_two, bag_two->entries[1].key); |
| 279 | EXPECT_EQ(Res_value::TYPE_STRING, bag_two->entries[1].value.dataType); |
| 280 | EXPECT_EQ(0, bag_two->entries[1].cookie); |
| 281 | EXPECT_EQ(std::string("string"), GetStringFromPool(assetmanager.GetStringPoolForCookie(0), |
| 282 | bag_two->entries[1].value.data)); |
| 283 | |
| 284 | // The rest are new attributes. |
| 285 | |
| 286 | EXPECT_EQ(app::R::attr::attr_three, bag_two->entries[2].key); |
| 287 | EXPECT_EQ(Res_value::TYPE_ATTRIBUTE, bag_two->entries[2].value.dataType); |
| 288 | EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[2].value.data); |
| 289 | EXPECT_EQ(0, bag_two->entries[2].cookie); |
| 290 | |
| 291 | EXPECT_EQ(app::R::attr::attr_five, bag_two->entries[3].key); |
| 292 | EXPECT_EQ(Res_value::TYPE_REFERENCE, bag_two->entries[3].value.dataType); |
| 293 | EXPECT_EQ(app::R::string::string_one, bag_two->entries[3].value.data); |
| 294 | EXPECT_EQ(0, bag_two->entries[3].cookie); |
| 295 | |
| 296 | EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[4].key); |
| 297 | EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[4].value.dataType); |
| 298 | EXPECT_EQ(3u, bag_two->entries[4].value.data); |
| 299 | EXPECT_EQ(0, bag_two->entries[4].cookie); |
Adam Lesinski | 32e7501 | 2017-05-09 15:25:37 -0700 | [diff] [blame] | 300 | |
| 301 | EXPECT_EQ(app::R::attr::attr_empty, bag_two->entries[5].key); |
| 302 | EXPECT_EQ(Res_value::TYPE_NULL, bag_two->entries[5].value.dataType); |
| 303 | EXPECT_EQ(Res_value::DATA_NULL_EMPTY, bag_two->entries[5].value.data); |
| 304 | EXPECT_EQ(0, bag_two->entries[5].cookie); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 307 | TEST_F(AssetManager2Test, ResolveReferenceToResource) { |
| 308 | AssetManager2 assetmanager; |
| 309 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 310 | |
| 311 | Res_value value; |
| 312 | ResTable_config selected_config; |
| 313 | uint32_t flags; |
| 314 | ApkAssetsCookie cookie = |
| 315 | assetmanager.GetResource(basic::R::integer::ref1, false /*may_be_bag*/, |
| 316 | 0u /*density_override*/, &value, &selected_config, &flags); |
| 317 | ASSERT_NE(kInvalidCookie, cookie); |
| 318 | |
| 319 | EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType); |
| 320 | EXPECT_EQ(basic::R::integer::ref2, value.data); |
| 321 | |
Adam Lesinski | 1c855a0 | 2017-11-29 09:59:37 -0800 | [diff] [blame^] | 322 | uint32_t last_ref = 0u; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 323 | cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref); |
| 324 | ASSERT_NE(kInvalidCookie, cookie); |
| 325 | EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType); |
| 326 | EXPECT_EQ(12000u, value.data); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 327 | EXPECT_EQ(basic::R::integer::ref2, last_ref); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | TEST_F(AssetManager2Test, ResolveReferenceToBag) { |
| 331 | AssetManager2 assetmanager; |
| 332 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 333 | |
| 334 | Res_value value; |
| 335 | ResTable_config selected_config; |
| 336 | uint32_t flags; |
| 337 | ApkAssetsCookie cookie = |
| 338 | assetmanager.GetResource(basic::R::integer::number2, true /*may_be_bag*/, |
| 339 | 0u /*density_override*/, &value, &selected_config, &flags); |
| 340 | ASSERT_NE(kInvalidCookie, cookie); |
| 341 | |
| 342 | EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType); |
| 343 | EXPECT_EQ(basic::R::array::integerArray1, value.data); |
| 344 | |
Adam Lesinski | 1c855a0 | 2017-11-29 09:59:37 -0800 | [diff] [blame^] | 345 | uint32_t last_ref = 0u; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 346 | cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref); |
| 347 | ASSERT_NE(kInvalidCookie, cookie); |
| 348 | EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType); |
| 349 | EXPECT_EQ(basic::R::array::integerArray1, value.data); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 350 | EXPECT_EQ(basic::R::array::integerArray1, last_ref); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Adam Lesinski | 1c855a0 | 2017-11-29 09:59:37 -0800 | [diff] [blame^] | 353 | TEST_F(AssetManager2Test, KeepLastReferenceIdUnmodifiedIfNoReferenceIsResolved) { |
| 354 | AssetManager2 assetmanager; |
| 355 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 356 | |
| 357 | ResTable_config selected_config; |
| 358 | memset(&selected_config, 0, sizeof(selected_config)); |
| 359 | |
| 360 | uint32_t flags = 0u; |
| 361 | |
| 362 | // Create some kind of Res_value that is NOT a reference. |
| 363 | Res_value value; |
| 364 | value.dataType = Res_value::TYPE_STRING; |
| 365 | value.data = 0; |
| 366 | |
| 367 | uint32_t last_ref = basic::R::string::test1; |
| 368 | EXPECT_EQ(1, assetmanager.ResolveReference(1, &value, &selected_config, &flags, &last_ref)); |
| 369 | EXPECT_EQ(basic::R::string::test1, last_ref); |
| 370 | } |
| 371 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 372 | static bool IsConfigurationPresent(const std::set<ResTable_config>& configurations, |
| 373 | const ResTable_config& configuration) { |
| 374 | return configurations.count(configuration) > 0; |
| 375 | } |
| 376 | |
| 377 | TEST_F(AssetManager2Test, GetResourceConfigurations) { |
| 378 | AssetManager2 assetmanager; |
| 379 | assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()}); |
| 380 | |
| 381 | std::set<ResTable_config> configurations = assetmanager.GetResourceConfigurations(); |
| 382 | |
| 383 | // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets. |
| 384 | // And one extra for the default configuration. |
| 385 | EXPECT_EQ(4u, configurations.size()); |
| 386 | |
| 387 | ResTable_config expected_config; |
| 388 | memset(&expected_config, 0, sizeof(expected_config)); |
| 389 | expected_config.language[0] = 's'; |
| 390 | expected_config.language[1] = 'v'; |
| 391 | EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config)); |
| 392 | |
| 393 | expected_config.language[0] = 'd'; |
| 394 | expected_config.language[1] = 'e'; |
| 395 | EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config)); |
| 396 | |
| 397 | expected_config.language[0] = 'f'; |
| 398 | expected_config.language[1] = 'r'; |
| 399 | EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config)); |
| 400 | |
| 401 | // Take out the system assets. |
| 402 | configurations = assetmanager.GetResourceConfigurations(true /* exclude_system */); |
| 403 | |
| 404 | // We expect de and fr from basic_de_fr assets. |
| 405 | EXPECT_EQ(2u, configurations.size()); |
| 406 | |
| 407 | expected_config.language[0] = 's'; |
| 408 | expected_config.language[1] = 'v'; |
| 409 | EXPECT_FALSE(IsConfigurationPresent(configurations, expected_config)); |
| 410 | |
| 411 | expected_config.language[0] = 'd'; |
| 412 | expected_config.language[1] = 'e'; |
| 413 | EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config)); |
| 414 | |
| 415 | expected_config.language[0] = 'f'; |
| 416 | expected_config.language[1] = 'r'; |
| 417 | EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config)); |
| 418 | } |
| 419 | |
| 420 | TEST_F(AssetManager2Test, GetResourceLocales) { |
| 421 | AssetManager2 assetmanager; |
| 422 | assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()}); |
| 423 | |
| 424 | std::set<std::string> locales = assetmanager.GetResourceLocales(); |
| 425 | |
| 426 | // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets. |
| 427 | EXPECT_EQ(3u, locales.size()); |
| 428 | EXPECT_GT(locales.count("sv"), 0u); |
| 429 | EXPECT_GT(locales.count("de"), 0u); |
| 430 | EXPECT_GT(locales.count("fr"), 0u); |
| 431 | |
| 432 | locales = assetmanager.GetResourceLocales(true /*exclude_system*/); |
| 433 | // We expect the de and fr locales from basic_de_fr assets. |
| 434 | EXPECT_EQ(2u, locales.size()); |
| 435 | EXPECT_GT(locales.count("de"), 0u); |
| 436 | EXPECT_GT(locales.count("fr"), 0u); |
| 437 | } |
| 438 | |
| 439 | TEST_F(AssetManager2Test, GetResourceId) { |
| 440 | AssetManager2 assetmanager; |
| 441 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 442 | |
| 443 | EXPECT_EQ(basic::R::layout::main, |
| 444 | assetmanager.GetResourceId("com.android.basic:layout/main", "", "")); |
| 445 | EXPECT_EQ(basic::R::layout::main, |
| 446 | assetmanager.GetResourceId("layout/main", "", "com.android.basic")); |
| 447 | EXPECT_EQ(basic::R::layout::main, |
| 448 | assetmanager.GetResourceId("main", "layout", "com.android.basic")); |
| 449 | } |
| 450 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 451 | TEST_F(AssetManager2Test, OpensFileFromSingleApkAssets) {} |
| 452 | |
| 453 | TEST_F(AssetManager2Test, OpensFileFromMultipleApkAssets) {} |
| 454 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 455 | TEST_F(AssetManager2Test, OperateOnUnverifiedApkAssets) { |
| 456 | std::unique_ptr<const ApkAssets> unverified_assets = |
| 457 | ApkAssets::Load(GetTestDataPath() + "/unverified/unverified.apk"); |
| 458 | ASSERT_NE(nullptr, unverified_assets); |
| 459 | |
| 460 | AssetManager2 assetmanager; |
| 461 | assetmanager.SetApkAssets({unverified_assets.get()}); |
| 462 | |
| 463 | Res_value value; |
| 464 | ResTable_config config; |
| 465 | uint32_t flags; |
| 466 | |
| 467 | EXPECT_EQ(kInvalidCookie, |
| 468 | assetmanager.GetResource(unverified::R::string::test1, false /*may_be_bag*/, 0u, &value, |
| 469 | &config, &flags)); |
| 470 | EXPECT_EQ(kInvalidCookie, |
| 471 | assetmanager.GetResource(unverified::R::string::test2, false /*may_be_bag*/, 0u, &value, |
| 472 | &config, &flags)); |
| 473 | EXPECT_NE(kInvalidCookie, |
| 474 | assetmanager.GetResource(unverified::R::integer::number1, false /*may_be_bag*/, 0u, |
| 475 | &value, &config, &flags)); |
| 476 | |
| 477 | EXPECT_EQ(nullptr, assetmanager.GetBag(unverified::R::style::Theme1)); |
| 478 | EXPECT_NE(nullptr, assetmanager.GetBag(unverified::R::array::integerArray1)); |
| 479 | } |
| 480 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 481 | } // namespace android |