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