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" |
| 23 | #include "data/basic/R.h" |
| 24 | #include "data/styles/R.h" |
| 25 | |
| 26 | namespace basic = com::android::basic; |
| 27 | namespace app = com::android::app; |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | class AssetManager2Test : public ::testing::Test { |
| 32 | public: |
| 33 | void SetUp() override { |
| 34 | basic_assets_ = ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk"); |
| 35 | ASSERT_NE(nullptr, basic_assets_); |
| 36 | |
| 37 | basic_de_fr_assets_ = ApkAssets::Load(GetTestDataPath() + "/basic/basic_de_fr.apk"); |
| 38 | ASSERT_NE(nullptr, basic_de_fr_assets_); |
| 39 | |
| 40 | style_assets_ = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk"); |
| 41 | ASSERT_NE(nullptr, style_assets_); |
| 42 | } |
| 43 | |
| 44 | protected: |
| 45 | std::unique_ptr<ApkAssets> basic_assets_; |
| 46 | std::unique_ptr<ApkAssets> basic_de_fr_assets_; |
| 47 | std::unique_ptr<ApkAssets> style_assets_; |
| 48 | }; |
| 49 | |
| 50 | TEST_F(AssetManager2Test, FindsResourcesFromSingleApkAssets) { |
| 51 | ResTable_config desired_config; |
| 52 | memset(&desired_config, 0, sizeof(desired_config)); |
| 53 | desired_config.language[0] = 'd'; |
| 54 | desired_config.language[1] = 'e'; |
| 55 | |
| 56 | AssetManager2 assetmanager; |
| 57 | assetmanager.SetConfiguration(desired_config); |
| 58 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 59 | |
| 60 | Res_value value; |
| 61 | ResTable_config selected_config; |
| 62 | uint32_t flags; |
| 63 | |
| 64 | ApkAssetsCookie cookie = |
| 65 | assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/, |
| 66 | 0 /*density_override*/, &value, &selected_config, &flags); |
| 67 | ASSERT_NE(kInvalidCookie, cookie); |
| 68 | |
| 69 | // Came from our ApkAssets. |
| 70 | EXPECT_EQ(0, cookie); |
| 71 | |
| 72 | // It is the default config. |
| 73 | EXPECT_EQ(0, selected_config.language[0]); |
| 74 | EXPECT_EQ(0, selected_config.language[1]); |
| 75 | |
| 76 | // It is a string. |
| 77 | EXPECT_EQ(Res_value::TYPE_STRING, value.dataType); |
| 78 | } |
| 79 | |
| 80 | TEST_F(AssetManager2Test, FindsResourcesFromMultipleApkAssets) { |
| 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(), basic_de_fr_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 de_fr ApkAssets. |
| 100 | EXPECT_EQ(1, cookie); |
| 101 | |
| 102 | // The configuration is german. |
| 103 | EXPECT_EQ('d', selected_config.language[0]); |
| 104 | EXPECT_EQ('e', selected_config.language[1]); |
| 105 | |
| 106 | // It is a string. |
| 107 | EXPECT_EQ(Res_value::TYPE_STRING, value.dataType); |
| 108 | } |
| 109 | |
| 110 | TEST_F(AssetManager2Test, FindsBagResourcesFromSingleApkAssets) { |
| 111 | AssetManager2 assetmanager; |
| 112 | assetmanager.SetApkAssets({basic_assets_.get()}); |
| 113 | |
| 114 | const ResolvedBag* bag = assetmanager.GetBag(basic::R::array::integerArray1); |
| 115 | ASSERT_NE(nullptr, bag); |
| 116 | ASSERT_EQ(3u, bag->entry_count); |
| 117 | |
| 118 | EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[0].value.dataType); |
| 119 | EXPECT_EQ(1u, bag->entries[0].value.data); |
| 120 | EXPECT_EQ(0, bag->entries[0].cookie); |
| 121 | |
| 122 | EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[1].value.dataType); |
| 123 | EXPECT_EQ(2u, bag->entries[1].value.data); |
| 124 | EXPECT_EQ(0, bag->entries[1].cookie); |
| 125 | |
| 126 | EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[2].value.dataType); |
| 127 | EXPECT_EQ(3u, bag->entries[2].value.data); |
| 128 | EXPECT_EQ(0, bag->entries[2].cookie); |
| 129 | } |
| 130 | |
| 131 | TEST_F(AssetManager2Test, MergesStylesWithParentFromSingleApkAssets) { |
| 132 | AssetManager2 assetmanager; |
| 133 | assetmanager.SetApkAssets({style_assets_.get()}); |
| 134 | |
| 135 | const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleOne); |
| 136 | ASSERT_NE(nullptr, bag_one); |
| 137 | ASSERT_EQ(2u, bag_one->entry_count); |
| 138 | |
| 139 | EXPECT_EQ(app::R::attr::attr_one, bag_one->entries[0].key); |
| 140 | EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[0].value.dataType); |
| 141 | EXPECT_EQ(1u, bag_one->entries[0].value.data); |
| 142 | EXPECT_EQ(0, bag_one->entries[0].cookie); |
| 143 | |
| 144 | EXPECT_EQ(app::R::attr::attr_two, bag_one->entries[1].key); |
| 145 | EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[1].value.dataType); |
| 146 | EXPECT_EQ(2u, bag_one->entries[1].value.data); |
| 147 | EXPECT_EQ(0, bag_one->entries[1].cookie); |
| 148 | |
| 149 | const ResolvedBag* bag_two = assetmanager.GetBag(app::R::style::StyleTwo); |
| 150 | ASSERT_NE(nullptr, bag_two); |
| 151 | ASSERT_EQ(5u, bag_two->entry_count); |
| 152 | |
| 153 | // attr_one is inherited from StyleOne. |
| 154 | EXPECT_EQ(app::R::attr::attr_one, bag_two->entries[0].key); |
| 155 | EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[0].value.dataType); |
| 156 | EXPECT_EQ(1u, bag_two->entries[0].value.data); |
| 157 | EXPECT_EQ(0, bag_two->entries[0].cookie); |
| 158 | |
| 159 | // attr_two should be overridden from StyleOne by StyleTwo. |
| 160 | EXPECT_EQ(app::R::attr::attr_two, bag_two->entries[1].key); |
| 161 | EXPECT_EQ(Res_value::TYPE_STRING, bag_two->entries[1].value.dataType); |
| 162 | EXPECT_EQ(0, bag_two->entries[1].cookie); |
| 163 | EXPECT_EQ(std::string("string"), GetStringFromPool(assetmanager.GetStringPoolForCookie(0), |
| 164 | bag_two->entries[1].value.data)); |
| 165 | |
| 166 | // The rest are new attributes. |
| 167 | |
| 168 | EXPECT_EQ(app::R::attr::attr_three, bag_two->entries[2].key); |
| 169 | EXPECT_EQ(Res_value::TYPE_ATTRIBUTE, bag_two->entries[2].value.dataType); |
| 170 | EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[2].value.data); |
| 171 | EXPECT_EQ(0, bag_two->entries[2].cookie); |
| 172 | |
| 173 | EXPECT_EQ(app::R::attr::attr_five, bag_two->entries[3].key); |
| 174 | EXPECT_EQ(Res_value::TYPE_REFERENCE, bag_two->entries[3].value.dataType); |
| 175 | EXPECT_EQ(app::R::string::string_one, bag_two->entries[3].value.data); |
| 176 | EXPECT_EQ(0, bag_two->entries[3].cookie); |
| 177 | |
| 178 | EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[4].key); |
| 179 | EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[4].value.dataType); |
| 180 | EXPECT_EQ(3u, bag_two->entries[4].value.data); |
| 181 | EXPECT_EQ(0, bag_two->entries[4].cookie); |
| 182 | } |
| 183 | |
| 184 | TEST_F(AssetManager2Test, FindsBagResourcesFromMultipleApkAssets) {} |
| 185 | |
| 186 | TEST_F(AssetManager2Test, OpensFileFromSingleApkAssets) {} |
| 187 | |
| 188 | TEST_F(AssetManager2Test, OpensFileFromMultipleApkAssets) {} |
| 189 | |
| 190 | } // namespace android |