blob: 2c242dbd3e28314b17498e52441d5b77b914dd73 [file] [log] [blame]
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07001/*
2 * Copyright (C) 2014 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
Adam Lesinski4c67a472016-11-10 16:43:59 -080017#include "androidfw/ResourceTypes.h"
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070018
Adam Lesinski4c67a472016-11-10 16:43:59 -080019#include "utils/String16.h"
20#include "utils/String8.h"
21
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070022#include "TestHelpers.h"
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -070023#include "data/basic/R.h"
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070024
Adam Lesinski4c67a472016-11-10 16:43:59 -080025using com::android::basic::R;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070026
Adam Lesinski4c67a472016-11-10 16:43:59 -080027namespace android {
Adam Lesinski833f3cc2014-06-18 15:06:01 -070028
Adam Lesinski4c67a472016-11-10 16:43:59 -080029static void makeConfigFrench(ResTable_config* config) {
30 memset(config, 0, sizeof(*config));
31 config->language[0] = 'f';
32 config->language[1] = 'r';
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070033}
34
Adam Lesinski4c67a472016-11-10 16:43:59 -080035class SplitTest : public ::testing::Test {
36 public:
37 void SetUp() override {
38 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
39 "resources.arsc", &basic_contents_));
40 ASSERT_TRUE(
41 ReadFileFromZipToString(GetTestDataPath() + "/basic/basic_de_fr.apk",
42 "resources.arsc", &basic_de_fr_contents_));
43 ASSERT_TRUE(
44 ReadFileFromZipToString(GetTestDataPath() + "/basic/basic_hdpi-v4.apk",
45 "resources.arsc", &basic_hdpi_contents_));
46 ASSERT_TRUE(
47 ReadFileFromZipToString(GetTestDataPath() + "/basic/basic_xhdpi-v4.apk",
48 "resources.arsc", &basic_xhdpi_contents_));
49 ASSERT_TRUE(ReadFileFromZipToString(
50 GetTestDataPath() + "/basic/basic_xxhdpi-v4.apk", "resources.arsc",
51 &basic_xxhdpi_contents_));
52 ASSERT_TRUE(
53 ReadFileFromZipToString(GetTestDataPath() + "/feature/feature.apk",
54 "resources.arsc", &feature_contents_));
55 }
56
57 protected:
58 std::string basic_contents_;
59 std::string basic_de_fr_contents_;
60 std::string basic_hdpi_contents_;
61 std::string basic_xhdpi_contents_;
62 std::string basic_xxhdpi_contents_;
63 std::string feature_contents_;
64};
65
66TEST_F(SplitTest, TestLoadBase) {
67 ResTable table;
68 ASSERT_EQ(NO_ERROR,
69 table.add(basic_contents_.data(), basic_contents_.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070070}
71
Adam Lesinski4c67a472016-11-10 16:43:59 -080072TEST_F(SplitTest, TestGetResourceFromBase) {
73 ResTable_config frenchConfig;
74 makeConfigFrench(&frenchConfig);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070075
Adam Lesinski4c67a472016-11-10 16:43:59 -080076 ResTable table;
77 table.setParameters(&frenchConfig);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070078
Adam Lesinski4c67a472016-11-10 16:43:59 -080079 ASSERT_EQ(NO_ERROR,
80 table.add(basic_contents_.data(), basic_contents_.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070081
Adam Lesinski4c67a472016-11-10 16:43:59 -080082 ResTable_config expectedConfig;
83 memset(&expectedConfig, 0, sizeof(expectedConfig));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070084
Adam Lesinski4c67a472016-11-10 16:43:59 -080085 Res_value val;
86 ResTable_config config;
87 ssize_t block = table.getResource(R::string::test1, &val, MAY_NOT_BE_BAG, 0,
88 NULL, &config);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070089
Adam Lesinski4c67a472016-11-10 16:43:59 -080090 // The returned block should tell us which string pool to get the value, if it
91 // is a string.
92 EXPECT_GE(block, 0);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070093
Adam Lesinski4c67a472016-11-10 16:43:59 -080094 // We expect the default resource to be selected since it is the only resource
95 // configuration.
96 EXPECT_EQ(0, expectedConfig.compare(config));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070097
Adam Lesinski4c67a472016-11-10 16:43:59 -080098 EXPECT_EQ(Res_value::TYPE_STRING, val.dataType);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070099}
100
Adam Lesinski4c67a472016-11-10 16:43:59 -0800101TEST_F(SplitTest, TestGetResourceFromSplit) {
102 ResTable_config expectedConfig;
103 makeConfigFrench(&expectedConfig);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700104
Adam Lesinski4c67a472016-11-10 16:43:59 -0800105 ResTable table;
106 table.setParameters(&expectedConfig);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700107
Adam Lesinski4c67a472016-11-10 16:43:59 -0800108 ASSERT_EQ(NO_ERROR,
109 table.add(basic_contents_.data(), basic_contents_.size()));
110 ASSERT_EQ(NO_ERROR, table.add(basic_de_fr_contents_.data(),
111 basic_de_fr_contents_.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700112
Adam Lesinski4c67a472016-11-10 16:43:59 -0800113 Res_value val;
114 ResTable_config config;
115 ssize_t block = table.getResource(R::string::test1, &val, MAY_NOT_BE_BAG, 0,
116 NULL, &config);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700117
Adam Lesinski4c67a472016-11-10 16:43:59 -0800118 EXPECT_GE(block, 0);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700119
Adam Lesinski4c67a472016-11-10 16:43:59 -0800120 EXPECT_EQ(0, expectedConfig.compare(config));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700121
Adam Lesinski4c67a472016-11-10 16:43:59 -0800122 EXPECT_EQ(Res_value::TYPE_STRING, val.dataType);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700123}
124
Adam Lesinski4c67a472016-11-10 16:43:59 -0800125TEST_F(SplitTest, ResourcesFromBaseAndSplitHaveSameNames) {
126 ResTable_config expectedConfig;
127 makeConfigFrench(&expectedConfig);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700128
Adam Lesinski4c67a472016-11-10 16:43:59 -0800129 ResTable table;
130 table.setParameters(&expectedConfig);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700131
Adam Lesinski4c67a472016-11-10 16:43:59 -0800132 ASSERT_EQ(NO_ERROR,
133 table.add(basic_contents_.data(), basic_contents_.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700134
Adam Lesinski4c67a472016-11-10 16:43:59 -0800135 ResTable::resource_name baseName;
136 EXPECT_TRUE(table.getResourceName(R::string::test1, false, &baseName));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700137
Adam Lesinski4c67a472016-11-10 16:43:59 -0800138 ASSERT_EQ(NO_ERROR, table.add(basic_de_fr_contents_.data(),
139 basic_de_fr_contents_.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700140
Adam Lesinski4c67a472016-11-10 16:43:59 -0800141 ResTable::resource_name frName;
142 EXPECT_TRUE(table.getResourceName(R::string::test1, false, &frName));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700143
Adam Lesinski4c67a472016-11-10 16:43:59 -0800144 EXPECT_EQ(String16(baseName.package, baseName.packageLen),
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700145 String16(frName.package, frName.packageLen));
146
Adam Lesinski4c67a472016-11-10 16:43:59 -0800147 EXPECT_EQ(String16(baseName.type, baseName.typeLen),
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700148 String16(frName.type, frName.typeLen));
149
Adam Lesinski4c67a472016-11-10 16:43:59 -0800150 EXPECT_EQ(String16(baseName.name, baseName.nameLen),
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700151 String16(frName.name, frName.nameLen));
152}
153
Adam Lesinski4c67a472016-11-10 16:43:59 -0800154TEST_F(SplitTest, TypeEntrySpecFlagsAreUpdated) {
155 ResTable_config defaultConfig;
156 memset(&defaultConfig, 0, sizeof(defaultConfig));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700157
Adam Lesinski4c67a472016-11-10 16:43:59 -0800158 ResTable table;
159 ASSERT_EQ(NO_ERROR,
160 table.add(basic_contents_.data(), basic_contents_.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700161
Adam Lesinski4c67a472016-11-10 16:43:59 -0800162 Res_value val;
163 uint32_t specFlags = 0;
164 ssize_t block = table.getResource(R::string::test1, &val, MAY_NOT_BE_BAG, 0,
165 &specFlags, NULL);
166 EXPECT_GE(block, 0);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700167
Adam Lesinski351471f92016-12-12 14:08:13 -0800168 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), specFlags);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700169
Adam Lesinski4c67a472016-11-10 16:43:59 -0800170 ASSERT_EQ(NO_ERROR, table.add(basic_de_fr_contents_.data(),
171 basic_de_fr_contents_.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700172
Adam Lesinski4c67a472016-11-10 16:43:59 -0800173 uint32_t frSpecFlags = 0;
174 block = table.getResource(R::string::test1, &val, MAY_NOT_BE_BAG, 0,
175 &frSpecFlags, NULL);
Adam Lesinski351471f92016-12-12 14:08:13 -0800176 ASSERT_GE(block, 0);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700177
Adam Lesinski351471f92016-12-12 14:08:13 -0800178 EXPECT_EQ(static_cast<uint32_t>(ResTable_config::CONFIG_LOCALE | ResTable_typeSpec::SPEC_PUBLIC),
179 frSpecFlags);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700180}
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700181
Adam Lesinski4c67a472016-11-10 16:43:59 -0800182TEST_F(SplitTest, SelectBestDensity) {
183 ResTable_config baseConfig;
184 memset(&baseConfig, 0, sizeof(baseConfig));
185 baseConfig.density = ResTable_config::DENSITY_XHIGH;
186 baseConfig.sdkVersion = 21;
Adam Lesinski60293192014-10-21 18:36:42 -0700187
Adam Lesinski4c67a472016-11-10 16:43:59 -0800188 ResTable table;
189 table.setParameters(&baseConfig);
190 ASSERT_EQ(NO_ERROR,
191 table.add(basic_contents_.data(), basic_contents_.size()));
192 ASSERT_EQ(NO_ERROR, table.add(basic_hdpi_contents_.data(),
193 basic_hdpi_contents_.size()));
Adam Lesinski60293192014-10-21 18:36:42 -0700194
Adam Lesinski4c67a472016-11-10 16:43:59 -0800195 EXPECT_TRUE(IsStringEqual(table, R::string::density, "hdpi"));
Adam Lesinski60293192014-10-21 18:36:42 -0700196
Adam Lesinski4c67a472016-11-10 16:43:59 -0800197 ASSERT_EQ(NO_ERROR, table.add(basic_xhdpi_contents_.data(),
198 basic_xhdpi_contents_.size()));
Adam Lesinski60293192014-10-21 18:36:42 -0700199
Adam Lesinski4c67a472016-11-10 16:43:59 -0800200 EXPECT_TRUE(IsStringEqual(table, R::string::density, "xhdpi"));
Adam Lesinski60293192014-10-21 18:36:42 -0700201
Adam Lesinski4c67a472016-11-10 16:43:59 -0800202 ASSERT_EQ(NO_ERROR, table.add(basic_xxhdpi_contents_.data(),
203 basic_xxhdpi_contents_.size()));
Adam Lesinski60293192014-10-21 18:36:42 -0700204
Adam Lesinski4c67a472016-11-10 16:43:59 -0800205 EXPECT_TRUE(IsStringEqual(table, R::string::density, "xhdpi"));
Adam Lesinski60293192014-10-21 18:36:42 -0700206
Adam Lesinski4c67a472016-11-10 16:43:59 -0800207 baseConfig.density = ResTable_config::DENSITY_XXHIGH;
208 table.setParameters(&baseConfig);
Adam Lesinski60293192014-10-21 18:36:42 -0700209
Adam Lesinski4c67a472016-11-10 16:43:59 -0800210 EXPECT_TRUE(IsStringEqual(table, R::string::density, "xxhdpi"));
Adam Lesinski60293192014-10-21 18:36:42 -0700211}
212
Adam Lesinski4c67a472016-11-10 16:43:59 -0800213TEST_F(SplitTest, TestNewResourceIsAccessible) {
214 ResTable table;
215 ASSERT_EQ(NO_ERROR,
216 table.add(basic_contents_.data(), basic_contents_.size()));
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700217
Adam Lesinski4c67a472016-11-10 16:43:59 -0800218 Res_value val;
219 ssize_t block = table.getResource(R::string::test3, &val, MAY_NOT_BE_BAG);
220 EXPECT_LT(block, 0);
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700221
Adam Lesinski4c67a472016-11-10 16:43:59 -0800222 ASSERT_EQ(NO_ERROR,
223 table.add(feature_contents_.data(), feature_contents_.size()));
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700224
Adam Lesinski4c67a472016-11-10 16:43:59 -0800225 block = table.getResource(R::string::test3, &val, MAY_NOT_BE_BAG);
Adam Lesinski351471f92016-12-12 14:08:13 -0800226 ASSERT_GE(block, 0);
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700227
Adam Lesinski4c67a472016-11-10 16:43:59 -0800228 EXPECT_EQ(Res_value::TYPE_STRING, val.dataType);
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700229}
230
Adam Lesinski4c67a472016-11-10 16:43:59 -0800231TEST_F(SplitTest, TestNewResourceNameHasCorrectName) {
232 ResTable table;
233 ASSERT_EQ(NO_ERROR,
234 table.add(basic_contents_.data(), basic_contents_.size()));
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700235
Adam Lesinski4c67a472016-11-10 16:43:59 -0800236 ResTable::resource_name name;
237 EXPECT_FALSE(table.getResourceName(R::string::test3, false, &name));
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700238
Adam Lesinski4c67a472016-11-10 16:43:59 -0800239 ASSERT_EQ(NO_ERROR,
240 table.add(feature_contents_.data(), feature_contents_.size()));
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700241
Adam Lesinski4c67a472016-11-10 16:43:59 -0800242 ASSERT_TRUE(table.getResourceName(R::string::test3, false, &name));
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700243
Adam Lesinski4c67a472016-11-10 16:43:59 -0800244 EXPECT_EQ(String16("com.android.basic"),
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700245 String16(name.package, name.packageLen));
246
Adam Lesinski4c67a472016-11-10 16:43:59 -0800247 EXPECT_EQ(String16("string"), String16(name.type, name.typeLen));
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700248
Adam Lesinski4c67a472016-11-10 16:43:59 -0800249 EXPECT_EQ(String16("test3"), String16(name.name, name.nameLen));
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700250}
251
Adam Lesinski4c67a472016-11-10 16:43:59 -0800252TEST_F(SplitTest, TestNewResourceIsAccessibleByName) {
253 ResTable table;
254 ASSERT_EQ(NO_ERROR,
255 table.add(basic_contents_.data(), basic_contents_.size()));
256 ASSERT_EQ(NO_ERROR,
257 table.add(feature_contents_.data(), feature_contents_.size()));
Adam Lesinskie60a87f2014-10-09 11:08:04 -0700258
Adam Lesinski4c67a472016-11-10 16:43:59 -0800259 const String16 name("test3");
260 const String16 type("string");
261 const String16 package("com.android.basic");
262 ASSERT_EQ(
263 R::string::test3,
264 table.identifierForName(name.string(), name.size(), type.string(),
265 type.size(), package.string(), package.size()));
Adam Lesinskie60a87f2014-10-09 11:08:04 -0700266}
267
Adam Lesinski4c67a472016-11-10 16:43:59 -0800268} // namespace