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 | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 31 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 32 | namespace app = com::android::app; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 33 | namespace appaslib = com::android::appaslib::app; |
| 34 | namespace basic = com::android::basic; |
| 35 | namespace lib_one = com::android::lib_one; |
| 36 | namespace lib_two = com::android::lib_two; |
| 37 | namespace libclient = com::android::libclient; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 38 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 39 | using ::testing::Eq; |
| 40 | using ::testing::NotNull; |
| 41 | using ::testing::StrEq; |
| 42 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 43 | namespace android { |
| 44 | |
| 45 | class AssetManager2Test : public ::testing::Test { |
| 46 | public: |
| 47 | void SetUp() override { |
| 48 | basic_assets_ = ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk"); |
| 49 | ASSERT_NE(nullptr, basic_assets_); |
| 50 | |
| 51 | basic_de_fr_assets_ = ApkAssets::Load(GetTestDataPath() + "/basic/basic_de_fr.apk"); |
| 52 | ASSERT_NE(nullptr, basic_de_fr_assets_); |
| 53 | |
| 54 | style_assets_ = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk"); |
| 55 | ASSERT_NE(nullptr, style_assets_); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 56 | |
| 57 | lib_one_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_one/lib_one.apk"); |
| 58 | ASSERT_NE(nullptr, lib_one_assets_); |
| 59 | |
| 60 | lib_two_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_two/lib_two.apk"); |
| 61 | ASSERT_NE(nullptr, lib_two_assets_); |
| 62 | |
| 63 | libclient_assets_ = ApkAssets::Load(GetTestDataPath() + "/libclient/libclient.apk"); |
| 64 | ASSERT_NE(nullptr, libclient_assets_); |
| 65 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 66 | appaslib_assets_ = ApkAssets::LoadAsSharedLibrary(GetTestDataPath() + "/appaslib/appaslib.apk"); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 67 | ASSERT_NE(nullptr, appaslib_assets_); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 68 | |
| 69 | system_assets_ = ApkAssets::Load(GetTestDataPath() + "/system/system.apk", true /*system*/); |
| 70 | ASSERT_NE(nullptr, system_assets_); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 71 | |
| 72 | app_assets_ = ApkAssets::Load(GetTestDataPath() + "/app/app.apk"); |
| 73 | ASSERT_THAT(app_assets_, NotNull()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | protected: |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 77 | std::unique_ptr<const ApkAssets> basic_assets_; |
| 78 | std::unique_ptr<const ApkAssets> basic_de_fr_assets_; |
| 79 | std::unique_ptr<const ApkAssets> style_assets_; |
| 80 | std::unique_ptr<const ApkAssets> lib_one_assets_; |
| 81 | std::unique_ptr<const ApkAssets> lib_two_assets_; |
| 82 | std::unique_ptr<const ApkAssets> libclient_assets_; |
| 83 | std::unique_ptr<const ApkAssets> appaslib_assets_; |
| 84 | std::unique_ptr<const ApkAssets> system_assets_; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 85 | std::unique_ptr<const ApkAssets> app_assets_; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 88 | TEST_F(AssetManager2Test, FindsResourceFromSingleApkAssets) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 89 | ResTable_config desired_config; |
| 90 | memset(&desired_config, 0, sizeof(desired_config)); |
| 91 | desired_config.language[0] = 'd'; |
| 92 | desired_config.language[1] = 'e'; |
| 93 | |
| 94 | AssetManager2 assetmanager; |
| 95 | assetmanager.SetConfiguration(desired_config); |
| 96 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 97 | |
| 98 | Res_value value; |
| 99 | ResTable_config selected_config; |
| 100 | uint32_t flags; |
| 101 | |
| 102 | ApkAssetsCookie cookie = |
| 103 | assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/, |
| 104 | 0 /*density_override*/, &value, &selected_config, &flags); |
| 105 | ASSERT_NE(kInvalidCookie, cookie); |
| 106 | |
| 107 | // Came from our ApkAssets. |
| 108 | EXPECT_EQ(0, cookie); |
| 109 | |
| 110 | // It is the default config. |
| 111 | EXPECT_EQ(0, selected_config.language[0]); |
| 112 | EXPECT_EQ(0, selected_config.language[1]); |
| 113 | |
| 114 | // It is a string. |
| 115 | EXPECT_EQ(Res_value::TYPE_STRING, value.dataType); |
| 116 | } |
| 117 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 118 | TEST_F(AssetManager2Test, FindsResourceFromMultipleApkAssets) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 119 | ResTable_config desired_config; |
| 120 | memset(&desired_config, 0, sizeof(desired_config)); |
| 121 | desired_config.language[0] = 'd'; |
| 122 | desired_config.language[1] = 'e'; |
| 123 | |
| 124 | AssetManager2 assetmanager; |
| 125 | assetmanager.SetConfiguration(desired_config); |
| 126 | assetmanager.SetApkAssets({basic_assets_.get(), basic_de_fr_assets_.get()}); |
| 127 | |
| 128 | Res_value value; |
| 129 | ResTable_config selected_config; |
| 130 | uint32_t flags; |
| 131 | |
| 132 | ApkAssetsCookie cookie = |
| 133 | assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/, |
| 134 | 0 /*density_override*/, &value, &selected_config, &flags); |
| 135 | ASSERT_NE(kInvalidCookie, cookie); |
| 136 | |
| 137 | // Came from our de_fr ApkAssets. |
| 138 | EXPECT_EQ(1, cookie); |
| 139 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 140 | // The configuration is German. |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 141 | EXPECT_EQ('d', selected_config.language[0]); |
| 142 | EXPECT_EQ('e', selected_config.language[1]); |
| 143 | |
| 144 | // It is a string. |
| 145 | EXPECT_EQ(Res_value::TYPE_STRING, value.dataType); |
| 146 | } |
| 147 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 148 | TEST_F(AssetManager2Test, FindsResourceFromSharedLibrary) { |
| 149 | AssetManager2 assetmanager; |
| 150 | |
| 151 | // libclient is built with lib_one and then lib_two in order. |
| 152 | // Reverse the order to test that proper package ID re-assignment is happening. |
| 153 | assetmanager.SetApkAssets( |
| 154 | {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()}); |
| 155 | |
| 156 | Res_value value; |
| 157 | ResTable_config selected_config; |
| 158 | uint32_t flags; |
| 159 | |
| 160 | ApkAssetsCookie cookie = |
| 161 | assetmanager.GetResource(libclient::R::string::foo_one, false /*may_be_bag*/, |
| 162 | 0 /*density_override*/, &value, &selected_config, &flags); |
| 163 | ASSERT_NE(kInvalidCookie, cookie); |
| 164 | |
| 165 | // Reference comes from libclient. |
| 166 | EXPECT_EQ(2, cookie); |
| 167 | EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType); |
| 168 | |
| 169 | // Lookup the reference. |
| 170 | cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/, |
| 171 | &value, &selected_config, &flags); |
| 172 | ASSERT_NE(kInvalidCookie, cookie); |
| 173 | EXPECT_EQ(1, cookie); |
| 174 | EXPECT_EQ(Res_value::TYPE_STRING, value.dataType); |
| 175 | EXPECT_EQ(std::string("Foo from lib_one"), |
| 176 | GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data)); |
| 177 | |
| 178 | cookie = assetmanager.GetResource(libclient::R::string::foo_two, false /*may_be_bag*/, |
| 179 | 0 /*density_override*/, &value, &selected_config, &flags); |
| 180 | ASSERT_NE(kInvalidCookie, cookie); |
| 181 | |
| 182 | // Reference comes from libclient. |
| 183 | EXPECT_EQ(2, cookie); |
| 184 | EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType); |
| 185 | |
| 186 | // Lookup the reference. |
| 187 | cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/, |
| 188 | &value, &selected_config, &flags); |
| 189 | ASSERT_NE(kInvalidCookie, cookie); |
| 190 | EXPECT_EQ(0, cookie); |
| 191 | EXPECT_EQ(Res_value::TYPE_STRING, value.dataType); |
| 192 | EXPECT_EQ(std::string("Foo from lib_two"), |
| 193 | GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data)); |
| 194 | } |
| 195 | |
| 196 | TEST_F(AssetManager2Test, FindsResourceFromAppLoadedAsSharedLibrary) { |
| 197 | AssetManager2 assetmanager; |
| 198 | assetmanager.SetApkAssets({appaslib_assets_.get()}); |
| 199 | |
| 200 | // The appaslib package will have been assigned the package ID 0x02. |
| 201 | |
| 202 | Res_value value; |
| 203 | ResTable_config selected_config; |
| 204 | uint32_t flags; |
| 205 | ApkAssetsCookie cookie = assetmanager.GetResource( |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 206 | fix_package_id(appaslib::R::integer::number1, 0x02), false /*may_be_bag*/, |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 207 | 0u /*density_override*/, &value, &selected_config, &flags); |
| 208 | ASSERT_NE(kInvalidCookie, cookie); |
| 209 | EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 210 | EXPECT_EQ(fix_package_id(appaslib::R::array::integerArray1, 0x02), value.data); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | TEST_F(AssetManager2Test, FindsBagResourceFromSingleApkAssets) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 214 | AssetManager2 assetmanager; |
| 215 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 216 | |
| 217 | const ResolvedBag* bag = assetmanager.GetBag(basic::R::array::integerArray1); |
| 218 | ASSERT_NE(nullptr, bag); |
| 219 | ASSERT_EQ(3u, bag->entry_count); |
| 220 | |
| 221 | EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[0].value.dataType); |
| 222 | EXPECT_EQ(1u, bag->entries[0].value.data); |
| 223 | EXPECT_EQ(0, bag->entries[0].cookie); |
| 224 | |
| 225 | EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[1].value.dataType); |
| 226 | EXPECT_EQ(2u, bag->entries[1].value.data); |
| 227 | EXPECT_EQ(0, bag->entries[1].cookie); |
| 228 | |
| 229 | EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[2].value.dataType); |
| 230 | EXPECT_EQ(3u, bag->entries[2].value.data); |
| 231 | EXPECT_EQ(0, bag->entries[2].cookie); |
| 232 | } |
| 233 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 234 | TEST_F(AssetManager2Test, FindsBagResourceFromMultipleApkAssets) {} |
| 235 | |
| 236 | TEST_F(AssetManager2Test, FindsBagResourceFromSharedLibrary) { |
| 237 | AssetManager2 assetmanager; |
| 238 | |
| 239 | // libclient is built with lib_one and then lib_two in order. |
| 240 | // Reverse the order to test that proper package ID re-assignment is happening. |
| 241 | assetmanager.SetApkAssets( |
| 242 | {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()}); |
| 243 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 244 | const ResolvedBag* bag = assetmanager.GetBag(fix_package_id(lib_one::R::style::Theme, 0x03)); |
| 245 | ASSERT_NE(nullptr, bag); |
| 246 | ASSERT_GE(bag->entry_count, 2u); |
| 247 | |
| 248 | // First two attributes come from lib_one. |
| 249 | EXPECT_EQ(1, bag->entries[0].cookie); |
| 250 | EXPECT_EQ(0x03, get_package_id(bag->entries[0].key)); |
| 251 | EXPECT_EQ(1, bag->entries[1].cookie); |
| 252 | EXPECT_EQ(0x03, get_package_id(bag->entries[1].key)); |
| 253 | } |
| 254 | |
| 255 | TEST_F(AssetManager2Test, FindsStyleResourceWithParentFromSharedLibrary) { |
| 256 | AssetManager2 assetmanager; |
| 257 | |
| 258 | // libclient is built with lib_one and then lib_two in order. |
| 259 | // Reverse the order to test that proper package ID re-assignment is happening. |
| 260 | assetmanager.SetApkAssets( |
| 261 | {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()}); |
| 262 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 263 | const ResolvedBag* bag = assetmanager.GetBag(libclient::R::style::Theme); |
| 264 | ASSERT_NE(nullptr, bag); |
| 265 | ASSERT_GE(bag->entry_count, 2u); |
| 266 | |
| 267 | // First two attributes come from lib_one. |
| 268 | EXPECT_EQ(1, bag->entries[0].cookie); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 269 | EXPECT_EQ(0x03, get_package_id(bag->entries[0].key)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 270 | EXPECT_EQ(1, bag->entries[1].cookie); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 271 | EXPECT_EQ(0x03, get_package_id(bag->entries[1].key)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 272 | } |
| 273 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 274 | TEST_F(AssetManager2Test, MergesStylesWithParentFromSingleApkAssets) { |
| 275 | AssetManager2 assetmanager; |
| 276 | assetmanager.SetApkAssets({style_assets_.get()}); |
| 277 | |
| 278 | const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleOne); |
| 279 | ASSERT_NE(nullptr, bag_one); |
| 280 | ASSERT_EQ(2u, bag_one->entry_count); |
| 281 | |
| 282 | EXPECT_EQ(app::R::attr::attr_one, bag_one->entries[0].key); |
| 283 | EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[0].value.dataType); |
| 284 | EXPECT_EQ(1u, bag_one->entries[0].value.data); |
| 285 | EXPECT_EQ(0, bag_one->entries[0].cookie); |
| 286 | |
| 287 | EXPECT_EQ(app::R::attr::attr_two, bag_one->entries[1].key); |
| 288 | EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[1].value.dataType); |
| 289 | EXPECT_EQ(2u, bag_one->entries[1].value.data); |
| 290 | EXPECT_EQ(0, bag_one->entries[1].cookie); |
| 291 | |
| 292 | const ResolvedBag* bag_two = assetmanager.GetBag(app::R::style::StyleTwo); |
| 293 | ASSERT_NE(nullptr, bag_two); |
Adam Lesinski | 32e7501 | 2017-05-09 15:25:37 -0700 | [diff] [blame] | 294 | ASSERT_EQ(6u, bag_two->entry_count); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 295 | |
| 296 | // attr_one is inherited from StyleOne. |
| 297 | EXPECT_EQ(app::R::attr::attr_one, bag_two->entries[0].key); |
| 298 | EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[0].value.dataType); |
| 299 | EXPECT_EQ(1u, bag_two->entries[0].value.data); |
| 300 | EXPECT_EQ(0, bag_two->entries[0].cookie); |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 301 | EXPECT_EQ(app::R::style::StyleOne, bag_two->entries[0].style); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 302 | |
| 303 | // attr_two should be overridden from StyleOne by StyleTwo. |
| 304 | EXPECT_EQ(app::R::attr::attr_two, bag_two->entries[1].key); |
| 305 | EXPECT_EQ(Res_value::TYPE_STRING, bag_two->entries[1].value.dataType); |
| 306 | EXPECT_EQ(0, bag_two->entries[1].cookie); |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 307 | EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[1].style); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 308 | EXPECT_EQ(std::string("string"), GetStringFromPool(assetmanager.GetStringPoolForCookie(0), |
| 309 | bag_two->entries[1].value.data)); |
| 310 | |
| 311 | // The rest are new attributes. |
| 312 | |
| 313 | EXPECT_EQ(app::R::attr::attr_three, bag_two->entries[2].key); |
| 314 | EXPECT_EQ(Res_value::TYPE_ATTRIBUTE, bag_two->entries[2].value.dataType); |
| 315 | EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[2].value.data); |
| 316 | EXPECT_EQ(0, bag_two->entries[2].cookie); |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 317 | EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[2].style); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 318 | |
| 319 | EXPECT_EQ(app::R::attr::attr_five, bag_two->entries[3].key); |
| 320 | EXPECT_EQ(Res_value::TYPE_REFERENCE, bag_two->entries[3].value.dataType); |
| 321 | EXPECT_EQ(app::R::string::string_one, bag_two->entries[3].value.data); |
| 322 | EXPECT_EQ(0, bag_two->entries[3].cookie); |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 323 | EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[3].style); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 324 | |
| 325 | EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[4].key); |
| 326 | EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[4].value.dataType); |
| 327 | EXPECT_EQ(3u, bag_two->entries[4].value.data); |
| 328 | EXPECT_EQ(0, bag_two->entries[4].cookie); |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 329 | EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[4].style); |
Adam Lesinski | 32e7501 | 2017-05-09 15:25:37 -0700 | [diff] [blame] | 330 | |
| 331 | EXPECT_EQ(app::R::attr::attr_empty, bag_two->entries[5].key); |
| 332 | EXPECT_EQ(Res_value::TYPE_NULL, bag_two->entries[5].value.dataType); |
| 333 | EXPECT_EQ(Res_value::DATA_NULL_EMPTY, bag_two->entries[5].value.data); |
| 334 | EXPECT_EQ(0, bag_two->entries[5].cookie); |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 335 | EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[5].style); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 336 | } |
| 337 | |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 338 | TEST_F(AssetManager2Test, MergeStylesCircularDependency) { |
| 339 | AssetManager2 assetmanager; |
| 340 | assetmanager.SetApkAssets({style_assets_.get()}); |
| 341 | |
| 342 | // GetBag should stop traversing the parents of styles when a circular |
| 343 | // dependency is detected |
| 344 | const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleFour); |
| 345 | ASSERT_NE(nullptr, bag_one); |
| 346 | ASSERT_EQ(3u, bag_one->entry_count); |
| 347 | } |
| 348 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 349 | TEST_F(AssetManager2Test, ResolveReferenceToResource) { |
| 350 | AssetManager2 assetmanager; |
| 351 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 352 | |
| 353 | Res_value value; |
| 354 | ResTable_config selected_config; |
| 355 | uint32_t flags; |
| 356 | ApkAssetsCookie cookie = |
| 357 | assetmanager.GetResource(basic::R::integer::ref1, false /*may_be_bag*/, |
| 358 | 0u /*density_override*/, &value, &selected_config, &flags); |
| 359 | ASSERT_NE(kInvalidCookie, cookie); |
| 360 | |
| 361 | EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType); |
| 362 | EXPECT_EQ(basic::R::integer::ref2, value.data); |
| 363 | |
Adam Lesinski | 1c855a0 | 2017-11-29 09:59:37 -0800 | [diff] [blame] | 364 | uint32_t last_ref = 0u; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 365 | cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref); |
| 366 | ASSERT_NE(kInvalidCookie, cookie); |
| 367 | EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType); |
| 368 | EXPECT_EQ(12000u, value.data); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 369 | EXPECT_EQ(basic::R::integer::ref2, last_ref); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | TEST_F(AssetManager2Test, ResolveReferenceToBag) { |
| 373 | AssetManager2 assetmanager; |
| 374 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 375 | |
| 376 | Res_value value; |
| 377 | ResTable_config selected_config; |
| 378 | uint32_t flags; |
| 379 | ApkAssetsCookie cookie = |
| 380 | assetmanager.GetResource(basic::R::integer::number2, true /*may_be_bag*/, |
| 381 | 0u /*density_override*/, &value, &selected_config, &flags); |
| 382 | ASSERT_NE(kInvalidCookie, cookie); |
| 383 | |
| 384 | EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType); |
| 385 | EXPECT_EQ(basic::R::array::integerArray1, value.data); |
| 386 | |
Adam Lesinski | 1c855a0 | 2017-11-29 09:59:37 -0800 | [diff] [blame] | 387 | uint32_t last_ref = 0u; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 388 | cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref); |
| 389 | ASSERT_NE(kInvalidCookie, cookie); |
| 390 | EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType); |
| 391 | EXPECT_EQ(basic::R::array::integerArray1, value.data); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 392 | EXPECT_EQ(basic::R::array::integerArray1, last_ref); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 393 | } |
| 394 | |
y | 9efbbef | 2018-04-18 11:29:09 -0700 | [diff] [blame] | 395 | TEST_F(AssetManager2Test, ResolveDeepIdReference) { |
| 396 | AssetManager2 assetmanager; |
| 397 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 398 | |
| 399 | // Set up the resource ids |
| 400 | const uint32_t high_ref = assetmanager |
| 401 | .GetResourceId("@id/high_ref", "values", "com.android.basic"); |
| 402 | ASSERT_NE(high_ref, 0u); |
| 403 | const uint32_t middle_ref = assetmanager |
| 404 | .GetResourceId("@id/middle_ref", "values", "com.android.basic"); |
| 405 | ASSERT_NE(middle_ref, 0u); |
| 406 | const uint32_t low_ref = assetmanager |
| 407 | .GetResourceId("@id/low_ref", "values", "com.android.basic"); |
| 408 | ASSERT_NE(low_ref, 0u); |
| 409 | |
| 410 | // Retrieve the most shallow resource |
| 411 | Res_value value; |
| 412 | ResTable_config config; |
| 413 | uint32_t flags; |
| 414 | ApkAssetsCookie cookie = assetmanager.GetResource(high_ref, false /*may_be_bag*/, |
| 415 | 0 /*density_override*/, |
| 416 | &value, &config, &flags); |
| 417 | ASSERT_NE(kInvalidCookie, cookie); |
| 418 | EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType); |
| 419 | EXPECT_EQ(middle_ref, value.data); |
| 420 | |
| 421 | // Check that resolving the reference resolves to the deepest id |
| 422 | uint32_t last_ref = high_ref; |
| 423 | assetmanager.ResolveReference(cookie, &value, &config, &flags, &last_ref); |
| 424 | EXPECT_EQ(last_ref, low_ref); |
| 425 | } |
| 426 | |
Adam Lesinski | 1c855a0 | 2017-11-29 09:59:37 -0800 | [diff] [blame] | 427 | TEST_F(AssetManager2Test, KeepLastReferenceIdUnmodifiedIfNoReferenceIsResolved) { |
| 428 | AssetManager2 assetmanager; |
| 429 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 430 | |
| 431 | ResTable_config selected_config; |
| 432 | memset(&selected_config, 0, sizeof(selected_config)); |
| 433 | |
| 434 | uint32_t flags = 0u; |
| 435 | |
| 436 | // Create some kind of Res_value that is NOT a reference. |
| 437 | Res_value value; |
| 438 | value.dataType = Res_value::TYPE_STRING; |
| 439 | value.data = 0; |
| 440 | |
| 441 | uint32_t last_ref = basic::R::string::test1; |
| 442 | EXPECT_EQ(1, assetmanager.ResolveReference(1, &value, &selected_config, &flags, &last_ref)); |
| 443 | EXPECT_EQ(basic::R::string::test1, last_ref); |
| 444 | } |
| 445 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 446 | static bool IsConfigurationPresent(const std::set<ResTable_config>& configurations, |
| 447 | const ResTable_config& configuration) { |
| 448 | return configurations.count(configuration) > 0; |
| 449 | } |
| 450 | |
| 451 | TEST_F(AssetManager2Test, GetResourceConfigurations) { |
| 452 | AssetManager2 assetmanager; |
| 453 | assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()}); |
| 454 | |
| 455 | std::set<ResTable_config> configurations = assetmanager.GetResourceConfigurations(); |
| 456 | |
| 457 | // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets. |
| 458 | // And one extra for the default configuration. |
| 459 | EXPECT_EQ(4u, configurations.size()); |
| 460 | |
| 461 | ResTable_config expected_config; |
| 462 | memset(&expected_config, 0, sizeof(expected_config)); |
| 463 | expected_config.language[0] = 's'; |
| 464 | expected_config.language[1] = 'v'; |
| 465 | EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config)); |
| 466 | |
| 467 | expected_config.language[0] = 'd'; |
| 468 | expected_config.language[1] = 'e'; |
| 469 | EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config)); |
| 470 | |
| 471 | expected_config.language[0] = 'f'; |
| 472 | expected_config.language[1] = 'r'; |
| 473 | EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config)); |
| 474 | |
| 475 | // Take out the system assets. |
| 476 | configurations = assetmanager.GetResourceConfigurations(true /* exclude_system */); |
| 477 | |
| 478 | // We expect de and fr from basic_de_fr assets. |
| 479 | EXPECT_EQ(2u, configurations.size()); |
| 480 | |
| 481 | expected_config.language[0] = 's'; |
| 482 | expected_config.language[1] = 'v'; |
| 483 | EXPECT_FALSE(IsConfigurationPresent(configurations, expected_config)); |
| 484 | |
| 485 | expected_config.language[0] = 'd'; |
| 486 | expected_config.language[1] = 'e'; |
| 487 | EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config)); |
| 488 | |
| 489 | expected_config.language[0] = 'f'; |
| 490 | expected_config.language[1] = 'r'; |
| 491 | EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config)); |
| 492 | } |
| 493 | |
| 494 | TEST_F(AssetManager2Test, GetResourceLocales) { |
| 495 | AssetManager2 assetmanager; |
| 496 | assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()}); |
| 497 | |
| 498 | std::set<std::string> locales = assetmanager.GetResourceLocales(); |
| 499 | |
| 500 | // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets. |
| 501 | EXPECT_EQ(3u, locales.size()); |
| 502 | EXPECT_GT(locales.count("sv"), 0u); |
| 503 | EXPECT_GT(locales.count("de"), 0u); |
| 504 | EXPECT_GT(locales.count("fr"), 0u); |
| 505 | |
| 506 | locales = assetmanager.GetResourceLocales(true /*exclude_system*/); |
| 507 | // We expect the de and fr locales from basic_de_fr assets. |
| 508 | EXPECT_EQ(2u, locales.size()); |
| 509 | EXPECT_GT(locales.count("de"), 0u); |
| 510 | EXPECT_GT(locales.count("fr"), 0u); |
| 511 | } |
| 512 | |
| 513 | TEST_F(AssetManager2Test, GetResourceId) { |
| 514 | AssetManager2 assetmanager; |
| 515 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 516 | |
| 517 | EXPECT_EQ(basic::R::layout::main, |
| 518 | assetmanager.GetResourceId("com.android.basic:layout/main", "", "")); |
| 519 | EXPECT_EQ(basic::R::layout::main, |
| 520 | assetmanager.GetResourceId("layout/main", "", "com.android.basic")); |
| 521 | EXPECT_EQ(basic::R::layout::main, |
| 522 | assetmanager.GetResourceId("main", "layout", "com.android.basic")); |
| 523 | } |
| 524 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 525 | TEST_F(AssetManager2Test, OpensFileFromSingleApkAssets) { |
| 526 | AssetManager2 assetmanager; |
| 527 | assetmanager.SetApkAssets({system_assets_.get()}); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 528 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 529 | std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER); |
| 530 | ASSERT_THAT(asset, NotNull()); |
| 531 | |
| 532 | const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/)); |
| 533 | ASSERT_THAT(data, NotNull()); |
| 534 | EXPECT_THAT(std::string(data, asset->getLength()), StrEq("file\n")); |
| 535 | } |
| 536 | |
| 537 | TEST_F(AssetManager2Test, OpensFileFromMultipleApkAssets) { |
| 538 | AssetManager2 assetmanager; |
| 539 | assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()}); |
| 540 | |
| 541 | std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER); |
| 542 | ASSERT_THAT(asset, NotNull()); |
| 543 | |
| 544 | const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/)); |
| 545 | ASSERT_THAT(data, NotNull()); |
| 546 | EXPECT_THAT(std::string(data, asset->getLength()), StrEq("app override file\n")); |
| 547 | } |
| 548 | |
| 549 | TEST_F(AssetManager2Test, OpenDir) { |
| 550 | AssetManager2 assetmanager; |
| 551 | assetmanager.SetApkAssets({system_assets_.get()}); |
| 552 | |
| 553 | std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir(""); |
| 554 | ASSERT_THAT(asset_dir, NotNull()); |
| 555 | ASSERT_THAT(asset_dir->getFileCount(), Eq(2u)); |
| 556 | |
| 557 | EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("file.txt"))); |
| 558 | EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular)); |
| 559 | |
| 560 | EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("subdir"))); |
| 561 | EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeDirectory)); |
| 562 | |
| 563 | asset_dir = assetmanager.OpenDir("subdir"); |
| 564 | ASSERT_THAT(asset_dir, NotNull()); |
| 565 | ASSERT_THAT(asset_dir->getFileCount(), Eq(1u)); |
| 566 | |
| 567 | EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("subdir_file.txt"))); |
| 568 | EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular)); |
| 569 | } |
| 570 | |
| 571 | TEST_F(AssetManager2Test, OpenDirFromManyApks) { |
| 572 | AssetManager2 assetmanager; |
| 573 | assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()}); |
| 574 | |
| 575 | std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir(""); |
| 576 | ASSERT_THAT(asset_dir, NotNull()); |
| 577 | ASSERT_THAT(asset_dir->getFileCount(), Eq(3u)); |
| 578 | |
| 579 | EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("app_file.txt"))); |
| 580 | EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular)); |
| 581 | |
| 582 | EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("file.txt"))); |
| 583 | EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeRegular)); |
| 584 | |
| 585 | EXPECT_THAT(asset_dir->getFileName(2), Eq(String8("subdir"))); |
| 586 | EXPECT_THAT(asset_dir->getFileType(2), Eq(FileType::kFileTypeDirectory)); |
| 587 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 588 | |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame^] | 589 | TEST_F(AssetManager2Test, GetLastPathWithoutEnablingReturnsEmpty) { |
| 590 | ResTable_config desired_config; |
| 591 | |
| 592 | AssetManager2 assetmanager; |
| 593 | assetmanager.SetConfiguration(desired_config); |
| 594 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 595 | assetmanager.SetResourceResolutionLoggingEnabled(false); |
| 596 | |
| 597 | Res_value value; |
| 598 | ResTable_config selected_config; |
| 599 | uint32_t flags; |
| 600 | |
| 601 | ApkAssetsCookie cookie = |
| 602 | assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/, |
| 603 | 0 /*density_override*/, &value, &selected_config, &flags); |
| 604 | ASSERT_NE(kInvalidCookie, cookie); |
| 605 | |
| 606 | auto result = assetmanager.GetLastResourceResolution(); |
| 607 | EXPECT_EQ("", result); |
| 608 | } |
| 609 | |
| 610 | TEST_F(AssetManager2Test, GetLastPathWithoutResolutionReturnsEmpty) { |
| 611 | ResTable_config desired_config; |
| 612 | |
| 613 | AssetManager2 assetmanager; |
| 614 | assetmanager.SetConfiguration(desired_config); |
| 615 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 616 | |
| 617 | auto result = assetmanager.GetLastResourceResolution(); |
| 618 | EXPECT_EQ("", result); |
| 619 | } |
| 620 | |
| 621 | TEST_F(AssetManager2Test, GetLastPathWithSingleApkAssets) { |
| 622 | ResTable_config desired_config; |
| 623 | memset(&desired_config, 0, sizeof(desired_config)); |
| 624 | desired_config.language[0] = 'd'; |
| 625 | desired_config.language[1] = 'e'; |
| 626 | |
| 627 | AssetManager2 assetmanager; |
| 628 | assetmanager.SetResourceResolutionLoggingEnabled(true); |
| 629 | assetmanager.SetConfiguration(desired_config); |
| 630 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 631 | |
| 632 | Res_value value; |
| 633 | ResTable_config selected_config; |
| 634 | uint32_t flags; |
| 635 | |
| 636 | ApkAssetsCookie cookie = |
| 637 | assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/, |
| 638 | 0 /*density_override*/, &value, &selected_config, &flags); |
| 639 | ASSERT_NE(kInvalidCookie, cookie); |
| 640 | |
| 641 | auto result = assetmanager.GetLastResourceResolution(); |
| 642 | EXPECT_EQ("Resolution for 0x7f030000 com.android.basic:string/test1\n\tFor config -de\n\tFound initial: com.android.basic", result); |
| 643 | } |
| 644 | |
| 645 | TEST_F(AssetManager2Test, GetLastPathWithMultipleApkAssets) { |
| 646 | ResTable_config desired_config; |
| 647 | memset(&desired_config, 0, sizeof(desired_config)); |
| 648 | desired_config.language[0] = 'd'; |
| 649 | desired_config.language[1] = 'e'; |
| 650 | |
| 651 | AssetManager2 assetmanager; |
| 652 | assetmanager.SetResourceResolutionLoggingEnabled(true); |
| 653 | assetmanager.SetConfiguration(desired_config); |
| 654 | assetmanager.SetApkAssets({basic_assets_.get(), basic_de_fr_assets_.get()}); |
| 655 | |
| 656 | Res_value value = Res_value(); |
| 657 | ResTable_config selected_config; |
| 658 | uint32_t flags; |
| 659 | |
| 660 | ApkAssetsCookie cookie = |
| 661 | assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/, |
| 662 | 0 /*density_override*/, &value, &selected_config, &flags); |
| 663 | ASSERT_NE(kInvalidCookie, cookie); |
| 664 | |
| 665 | auto result = assetmanager.GetLastResourceResolution(); |
| 666 | EXPECT_EQ("Resolution for 0x7f030000 com.android.basic:string/test1\n\tFor config -de\n\tFound initial: com.android.basic\n\tFound better: com.android.basic -de", result); |
| 667 | } |
| 668 | |
| 669 | TEST_F(AssetManager2Test, GetLastPathAfterDisablingReturnsEmpty) { |
| 670 | ResTable_config desired_config; |
| 671 | memset(&desired_config, 0, sizeof(desired_config)); |
| 672 | |
| 673 | AssetManager2 assetmanager; |
| 674 | assetmanager.SetResourceResolutionLoggingEnabled(true); |
| 675 | assetmanager.SetConfiguration(desired_config); |
| 676 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 677 | |
| 678 | Res_value value = Res_value(); |
| 679 | ResTable_config selected_config; |
| 680 | uint32_t flags; |
| 681 | |
| 682 | ApkAssetsCookie cookie = |
| 683 | assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/, |
| 684 | 0 /*density_override*/, &value, &selected_config, &flags); |
| 685 | ASSERT_NE(kInvalidCookie, cookie); |
| 686 | |
| 687 | auto resultEnabled = assetmanager.GetLastResourceResolution(); |
| 688 | ASSERT_NE("", resultEnabled); |
| 689 | |
| 690 | assetmanager.SetResourceResolutionLoggingEnabled(false); |
| 691 | |
| 692 | auto resultDisabled = assetmanager.GetLastResourceResolution(); |
| 693 | EXPECT_EQ("", resultDisabled); |
| 694 | } |
| 695 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 696 | } // namespace android |