blob: 1f207e2e63732693f630e809210630a4a438f3ad [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 Lesinski4c67a472016-11-10 16:43:59 -0800168 EXPECT_EQ(static_cast<uint32_t>(0), 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);
176 EXPECT_GE(block, 0);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700177
Adam Lesinski4c67a472016-11-10 16:43:59 -0800178 EXPECT_EQ(ResTable_config::CONFIG_LOCALE, frSpecFlags);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700179}
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700180
Adam Lesinski4c67a472016-11-10 16:43:59 -0800181TEST_F(SplitTest, SelectBestDensity) {
182 ResTable_config baseConfig;
183 memset(&baseConfig, 0, sizeof(baseConfig));
184 baseConfig.density = ResTable_config::DENSITY_XHIGH;
185 baseConfig.sdkVersion = 21;
Adam Lesinski60293192014-10-21 18:36:42 -0700186
Adam Lesinski4c67a472016-11-10 16:43:59 -0800187 ResTable table;
188 table.setParameters(&baseConfig);
189 ASSERT_EQ(NO_ERROR,
190 table.add(basic_contents_.data(), basic_contents_.size()));
191 ASSERT_EQ(NO_ERROR, table.add(basic_hdpi_contents_.data(),
192 basic_hdpi_contents_.size()));
Adam Lesinski60293192014-10-21 18:36:42 -0700193
Adam Lesinski4c67a472016-11-10 16:43:59 -0800194 EXPECT_TRUE(IsStringEqual(table, R::string::density, "hdpi"));
Adam Lesinski60293192014-10-21 18:36:42 -0700195
Adam Lesinski4c67a472016-11-10 16:43:59 -0800196 ASSERT_EQ(NO_ERROR, table.add(basic_xhdpi_contents_.data(),
197 basic_xhdpi_contents_.size()));
Adam Lesinski60293192014-10-21 18:36:42 -0700198
Adam Lesinski4c67a472016-11-10 16:43:59 -0800199 EXPECT_TRUE(IsStringEqual(table, R::string::density, "xhdpi"));
Adam Lesinski60293192014-10-21 18:36:42 -0700200
Adam Lesinski4c67a472016-11-10 16:43:59 -0800201 ASSERT_EQ(NO_ERROR, table.add(basic_xxhdpi_contents_.data(),
202 basic_xxhdpi_contents_.size()));
Adam Lesinski60293192014-10-21 18:36:42 -0700203
Adam Lesinski4c67a472016-11-10 16:43:59 -0800204 EXPECT_TRUE(IsStringEqual(table, R::string::density, "xhdpi"));
Adam Lesinski60293192014-10-21 18:36:42 -0700205
Adam Lesinski4c67a472016-11-10 16:43:59 -0800206 baseConfig.density = ResTable_config::DENSITY_XXHIGH;
207 table.setParameters(&baseConfig);
Adam Lesinski60293192014-10-21 18:36:42 -0700208
Adam Lesinski4c67a472016-11-10 16:43:59 -0800209 EXPECT_TRUE(IsStringEqual(table, R::string::density, "xxhdpi"));
Adam Lesinski60293192014-10-21 18:36:42 -0700210}
211
Adam Lesinski4c67a472016-11-10 16:43:59 -0800212TEST_F(SplitTest, TestNewResourceIsAccessible) {
213 ResTable table;
214 ASSERT_EQ(NO_ERROR,
215 table.add(basic_contents_.data(), basic_contents_.size()));
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700216
Adam Lesinski4c67a472016-11-10 16:43:59 -0800217 Res_value val;
218 ssize_t block = table.getResource(R::string::test3, &val, MAY_NOT_BE_BAG);
219 EXPECT_LT(block, 0);
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700220
Adam Lesinski4c67a472016-11-10 16:43:59 -0800221 ASSERT_EQ(NO_ERROR,
222 table.add(feature_contents_.data(), feature_contents_.size()));
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700223
Adam Lesinski4c67a472016-11-10 16:43:59 -0800224 block = table.getResource(R::string::test3, &val, MAY_NOT_BE_BAG);
225 EXPECT_GE(block, 0);
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700226
Adam Lesinski4c67a472016-11-10 16:43:59 -0800227 EXPECT_EQ(Res_value::TYPE_STRING, val.dataType);
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700228}
229
Adam Lesinski4c67a472016-11-10 16:43:59 -0800230TEST_F(SplitTest, TestNewResourceNameHasCorrectName) {
231 ResTable table;
232 ASSERT_EQ(NO_ERROR,
233 table.add(basic_contents_.data(), basic_contents_.size()));
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700234
Adam Lesinski4c67a472016-11-10 16:43:59 -0800235 ResTable::resource_name name;
236 EXPECT_FALSE(table.getResourceName(R::string::test3, false, &name));
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700237
Adam Lesinski4c67a472016-11-10 16:43:59 -0800238 ASSERT_EQ(NO_ERROR,
239 table.add(feature_contents_.data(), feature_contents_.size()));
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700240
Adam Lesinski4c67a472016-11-10 16:43:59 -0800241 ASSERT_TRUE(table.getResourceName(R::string::test3, false, &name));
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700242
Adam Lesinski4c67a472016-11-10 16:43:59 -0800243 EXPECT_EQ(String16("com.android.basic"),
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700244 String16(name.package, name.packageLen));
245
Adam Lesinski4c67a472016-11-10 16:43:59 -0800246 EXPECT_EQ(String16("string"), String16(name.type, name.typeLen));
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700247
Adam Lesinski4c67a472016-11-10 16:43:59 -0800248 EXPECT_EQ(String16("test3"), String16(name.name, name.nameLen));
Adam Lesinski833f3cc2014-06-18 15:06:01 -0700249}
250
Adam Lesinski4c67a472016-11-10 16:43:59 -0800251TEST_F(SplitTest, TestNewResourceIsAccessibleByName) {
252 ResTable table;
253 ASSERT_EQ(NO_ERROR,
254 table.add(basic_contents_.data(), basic_contents_.size()));
255 ASSERT_EQ(NO_ERROR,
256 table.add(feature_contents_.data(), feature_contents_.size()));
Adam Lesinskie60a87f2014-10-09 11:08:04 -0700257
Adam Lesinski4c67a472016-11-10 16:43:59 -0800258 const String16 name("test3");
259 const String16 type("string");
260 const String16 package("com.android.basic");
261 ASSERT_EQ(
262 R::string::test3,
263 table.identifierForName(name.string(), name.size(), type.string(),
264 type.size(), package.string(), package.size()));
Adam Lesinskie60a87f2014-10-09 11:08:04 -0700265}
266
Adam Lesinski4c67a472016-11-10 16:43:59 -0800267} // namespace