Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 17 | #include "androidfw/ResourceTypes.h" |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 18 | |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 19 | #include <codecvt> |
| 20 | #include <locale> |
| 21 | #include <string> |
| 22 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 23 | #include "utils/String16.h" |
| 24 | #include "utils/String8.h" |
| 25 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 26 | #include "TestHelpers.h" |
Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 27 | #include "data/basic/R.h" |
| 28 | #include "data/lib/R.h" |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 29 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 30 | namespace basic = com::android::basic; |
| 31 | namespace lib = com::android::lib; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 32 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 33 | namespace android { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 34 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 35 | TEST(ResTableTest, ShouldLoadSuccessfully) { |
| 36 | std::string contents; |
| 37 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 38 | "resources.arsc", &contents)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 39 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 40 | ResTable table; |
| 41 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 44 | TEST(ResTableTest, SimpleTypeIsRetrievedCorrectly) { |
| 45 | std::string contents; |
| 46 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 47 | "resources.arsc", &contents)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 48 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 49 | ResTable table; |
| 50 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
| 51 | |
| 52 | EXPECT_TRUE(IsStringEqual(table, basic::R::string::test1, "test1")); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 55 | TEST(ResTableTest, ResourceNameIsResolved) { |
| 56 | std::string contents; |
| 57 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 58 | "resources.arsc", &contents)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 59 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 60 | ResTable table; |
| 61 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
| 62 | |
| 63 | String16 defPackage("com.android.basic"); |
| 64 | String16 testName("@string/test1"); |
| 65 | uint32_t resID = |
| 66 | table.identifierForName(testName.string(), testName.size(), 0, 0, |
| 67 | defPackage.string(), defPackage.size()); |
| 68 | ASSERT_NE(uint32_t(0x00000000), resID); |
| 69 | ASSERT_EQ(basic::R::string::test1, resID); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 72 | TEST(ResTableTest, NoParentThemeIsAppliedCorrectly) { |
| 73 | std::string contents; |
| 74 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 75 | "resources.arsc", &contents)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 76 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 77 | ResTable table; |
| 78 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 79 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 80 | ResTable::Theme theme(table); |
| 81 | ASSERT_EQ(NO_ERROR, theme.applyStyle(basic::R::style::Theme1)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 82 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 83 | Res_value val; |
| 84 | uint32_t specFlags = 0; |
| 85 | ssize_t index = theme.getAttribute(basic::R::attr::attr1, &val, &specFlags); |
| 86 | ASSERT_GE(index, 0); |
| 87 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 88 | ASSERT_EQ(uint32_t(100), val.data); |
| 89 | |
| 90 | index = theme.getAttribute(basic::R::attr::attr2, &val, &specFlags); |
| 91 | ASSERT_GE(index, 0); |
| 92 | ASSERT_EQ(Res_value::TYPE_REFERENCE, val.dataType); |
| 93 | ASSERT_EQ(basic::R::integer::number1, val.data); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 96 | TEST(ResTableTest, ParentThemeIsAppliedCorrectly) { |
| 97 | std::string contents; |
| 98 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 99 | "resources.arsc", &contents)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 100 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 101 | ResTable table; |
| 102 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 103 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 104 | ResTable::Theme theme(table); |
| 105 | ASSERT_EQ(NO_ERROR, theme.applyStyle(basic::R::style::Theme2)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 106 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 107 | Res_value val; |
| 108 | uint32_t specFlags = 0; |
| 109 | ssize_t index = theme.getAttribute(basic::R::attr::attr1, &val, &specFlags); |
| 110 | ASSERT_GE(index, 0); |
| 111 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 112 | ASSERT_EQ(uint32_t(300), val.data); |
| 113 | |
| 114 | index = theme.getAttribute(basic::R::attr::attr2, &val, &specFlags); |
| 115 | ASSERT_GE(index, 0); |
| 116 | ASSERT_EQ(Res_value::TYPE_REFERENCE, val.dataType); |
| 117 | ASSERT_EQ(basic::R::integer::number1, val.data); |
Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 120 | TEST(ResTableTest, LibraryThemeIsAppliedCorrectly) { |
| 121 | std::string contents; |
| 122 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/lib/lib.apk", |
| 123 | "resources.arsc", &contents)); |
Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 124 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 125 | ResTable table; |
| 126 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 127 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 128 | ResTable::Theme theme(table); |
| 129 | ASSERT_EQ(NO_ERROR, theme.applyStyle(lib::R::style::Theme)); |
Adam Lesinski | 8ac51d1 | 2016-05-10 10:01:12 -0700 | [diff] [blame] | 130 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 131 | Res_value val; |
| 132 | uint32_t specFlags = 0; |
| 133 | ssize_t index = theme.getAttribute(lib::R::attr::attr1, &val, &specFlags); |
| 134 | ASSERT_GE(index, 0); |
| 135 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 136 | ASSERT_EQ(uint32_t(700), val.data); |
| 137 | |
| 138 | index = theme.getAttribute(lib::R::attr::attr2, &val, &specFlags); |
| 139 | ASSERT_GE(index, 0); |
| 140 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 141 | ASSERT_EQ(uint32_t(700), val.data); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 144 | TEST(ResTableTest, ReferenceToBagIsNotResolved) { |
| 145 | std::string contents; |
| 146 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 147 | "resources.arsc", &contents)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 148 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 149 | ResTable table; |
| 150 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 151 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 152 | Res_value val; |
| 153 | ssize_t block = |
| 154 | table.getResource(basic::R::integer::number2, &val, MAY_NOT_BE_BAG); |
| 155 | ASSERT_GE(block, 0); |
| 156 | ASSERT_EQ(Res_value::TYPE_REFERENCE, val.dataType); |
| 157 | ASSERT_EQ(basic::R::array::integerArray1, val.data); |
| 158 | |
| 159 | ssize_t newBlock = table.resolveReference(&val, block); |
| 160 | EXPECT_EQ(block, newBlock); |
| 161 | EXPECT_EQ(Res_value::TYPE_REFERENCE, val.dataType); |
| 162 | EXPECT_EQ(basic::R::array::integerArray1, val.data); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 165 | TEST(ResTableTest, ResourcesStillAccessibleAfterParameterChange) { |
| 166 | std::string contents; |
| 167 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 168 | "resources.arsc", &contents)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 169 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 170 | ResTable table; |
| 171 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 172 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 173 | Res_value val; |
| 174 | ssize_t block = |
| 175 | table.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| 176 | ASSERT_GE(block, 0); |
| 177 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 178 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 179 | const ResTable::bag_entry* entry; |
| 180 | ssize_t count = table.lockBag(basic::R::array::integerArray1, &entry); |
| 181 | ASSERT_GE(count, 0); |
| 182 | table.unlockBag(entry); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 183 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 184 | ResTable_config param; |
| 185 | memset(¶m, 0, sizeof(param)); |
| 186 | param.density = 320; |
| 187 | table.setParameters(¶m); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 188 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 189 | block = table.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| 190 | ASSERT_GE(block, 0); |
| 191 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 192 | |
| 193 | count = table.lockBag(basic::R::array::integerArray1, &entry); |
| 194 | ASSERT_GE(count, 0); |
| 195 | table.unlockBag(entry); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 198 | TEST(ResTableTest, ResourceIsOverridenWithBetterConfig) { |
| 199 | std::string contents; |
| 200 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 201 | "resources.arsc", &contents)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 202 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 203 | ResTable table; |
| 204 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 205 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 206 | Res_value val; |
| 207 | ssize_t block = |
| 208 | table.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| 209 | ASSERT_GE(block, 0); |
| 210 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 211 | ASSERT_EQ(uint32_t(200), val.data); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 212 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 213 | ResTable_config param; |
| 214 | memset(¶m, 0, sizeof(param)); |
| 215 | param.language[0] = 's'; |
| 216 | param.language[1] = 'v'; |
| 217 | param.country[0] = 'S'; |
| 218 | param.country[1] = 'E'; |
| 219 | table.setParameters(¶m); |
| 220 | |
| 221 | block = table.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| 222 | ASSERT_GE(block, 0); |
| 223 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 224 | ASSERT_EQ(uint32_t(400), val.data); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 227 | TEST(ResTableTest, emptyTableHasSensibleDefaults) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 228 | const int32_t assetCookie = 1; |
Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 229 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 230 | ResTable table; |
| 231 | ASSERT_EQ(NO_ERROR, table.addEmpty(assetCookie)); |
Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 232 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 233 | // Adding an empty table gives us one table! |
| 234 | ASSERT_EQ(uint32_t(1), table.getTableCount()); |
Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 235 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 236 | // Adding an empty table doesn't mean we get packages. |
| 237 | ASSERT_EQ(uint32_t(0), table.getBasePackageCount()); |
Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 238 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 239 | Res_value val; |
| 240 | ASSERT_LT(table.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG), |
| 241 | 0); |
Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 244 | void testU16StringToInt(const char16_t* str, uint32_t expectedValue, |
| 245 | bool expectSuccess, bool expectHex) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 246 | size_t len = std::char_traits<char16_t>::length(str); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 247 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 248 | // Gtest can't print UTF-16 strings, so we have to convert to UTF-8 :( |
| 249 | std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert; |
| 250 | std::string s = convert.to_bytes(std::u16string(str, len)); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 251 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 252 | Res_value out = {}; |
| 253 | ASSERT_EQ(expectSuccess, U16StringToInt(str, len, &out)) << "Failed with " |
| 254 | << s; |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 255 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 256 | if (!expectSuccess) { |
| 257 | ASSERT_EQ(out.TYPE_NULL, out.dataType) << "Failed with " << s; |
| 258 | return; |
| 259 | } |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 260 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 261 | if (expectHex) { |
| 262 | ASSERT_EQ(out.TYPE_INT_HEX, out.dataType) << "Failed with " << s; |
| 263 | } else { |
| 264 | ASSERT_EQ(out.TYPE_INT_DEC, out.dataType) << "Failed with " << s; |
| 265 | } |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 266 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 267 | ASSERT_EQ(expectedValue, out.data) << "Failed with " << s; |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | TEST(ResTableTest, U16StringToInt) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 271 | testU16StringToInt(u"", 0U, false, false); |
| 272 | testU16StringToInt(u" ", 0U, false, false); |
| 273 | testU16StringToInt(u"\t\n", 0U, false, false); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 274 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 275 | testU16StringToInt(u"abcd", 0U, false, false); |
| 276 | testU16StringToInt(u"10abcd", 0U, false, false); |
| 277 | testU16StringToInt(u"42 42", 0U, false, false); |
| 278 | testU16StringToInt(u"- 42", 0U, false, false); |
| 279 | testU16StringToInt(u"-", 0U, false, false); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 280 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 281 | testU16StringToInt(u"0x", 0U, false, true); |
| 282 | testU16StringToInt(u"0xnope", 0U, false, true); |
| 283 | testU16StringToInt(u"0X42", 0U, false, true); |
| 284 | testU16StringToInt(u"0x42 0x42", 0U, false, true); |
| 285 | testU16StringToInt(u"-0x0", 0U, false, true); |
| 286 | testU16StringToInt(u"-0x42", 0U, false, true); |
| 287 | testU16StringToInt(u"- 0x42", 0U, false, true); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 288 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 289 | // Note that u" 42" would pass. This preserves the old behavior, but it may |
| 290 | // not be desired. |
| 291 | testU16StringToInt(u"42 ", 0U, false, false); |
| 292 | testU16StringToInt(u"0x42 ", 0U, false, true); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 293 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 294 | // Decimal cases. |
| 295 | testU16StringToInt(u"0", 0U, true, false); |
| 296 | testU16StringToInt(u"-0", 0U, true, false); |
| 297 | testU16StringToInt(u"42", 42U, true, false); |
| 298 | testU16StringToInt(u" 42", 42U, true, false); |
| 299 | testU16StringToInt(u"-42", static_cast<uint32_t>(-42), true, false); |
| 300 | testU16StringToInt(u" -42", static_cast<uint32_t>(-42), true, false); |
| 301 | testU16StringToInt(u"042", 42U, true, false); |
| 302 | testU16StringToInt(u"-042", static_cast<uint32_t>(-42), true, false); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 303 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 304 | // Hex cases. |
| 305 | testU16StringToInt(u"0x0", 0x0, true, true); |
| 306 | testU16StringToInt(u"0x42", 0x42, true, true); |
| 307 | testU16StringToInt(u" 0x42", 0x42, true, true); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 308 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 309 | // Just before overflow cases: |
| 310 | testU16StringToInt(u"2147483647", INT_MAX, true, false); |
| 311 | testU16StringToInt(u"-2147483648", static_cast<uint32_t>(INT_MIN), true, |
| 312 | false); |
| 313 | testU16StringToInt(u"0xffffffff", UINT_MAX, true, true); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 314 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 315 | // Overflow cases: |
| 316 | testU16StringToInt(u"2147483648", 0U, false, false); |
| 317 | testU16StringToInt(u"-2147483649", 0U, false, false); |
| 318 | testU16StringToInt(u"0x1ffffffff", 0U, false, true); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 321 | TEST(ResTableTest, ShareButDontModifyResTable) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 322 | std::string contents; |
| 323 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 324 | "resources.arsc", &contents)); |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 325 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 326 | ResTable sharedTable; |
| 327 | ASSERT_EQ(NO_ERROR, sharedTable.add(contents.data(), contents.size())); |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 328 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 329 | ResTable_config param; |
| 330 | memset(¶m, 0, sizeof(param)); |
| 331 | param.language[0] = 'v'; |
| 332 | param.language[1] = 's'; |
| 333 | sharedTable.setParameters(¶m); |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 334 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 335 | // Check that we get the default value for @integer:number1 |
| 336 | Res_value val; |
| 337 | ssize_t block = |
| 338 | sharedTable.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| 339 | ASSERT_GE(block, 0); |
| 340 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 341 | ASSERT_EQ(uint32_t(600), val.data); |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 342 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 343 | // Create a new table that shares the entries of the shared table. |
| 344 | ResTable table; |
| 345 | ASSERT_EQ(NO_ERROR, table.add(&sharedTable, false)); |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 346 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 347 | // Set a new configuration on the new table. |
| 348 | memset(¶m, 0, sizeof(param)); |
| 349 | param.language[0] = 's'; |
| 350 | param.language[1] = 'v'; |
| 351 | param.country[0] = 'S'; |
| 352 | param.country[1] = 'E'; |
| 353 | table.setParameters(¶m); |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 354 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 355 | // Check that we get a new value in the new table. |
| 356 | block = table.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| 357 | ASSERT_GE(block, 0); |
| 358 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 359 | ASSERT_EQ(uint32_t(400), val.data); |
| 360 | |
| 361 | // Check that we still get the old value in the shared table. |
| 362 | block = |
| 363 | sharedTable.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| 364 | ASSERT_GE(block, 0); |
| 365 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 366 | ASSERT_EQ(uint32_t(600), val.data); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 367 | } |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 368 | |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 369 | TEST(ResTableTest, GetConfigurationsReturnsUniqueList) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 370 | std::string contents; |
| 371 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 372 | "resources.arsc", &contents)); |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 373 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 374 | std::string system_contents; |
| 375 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/system/system.apk", |
| 376 | "resources.arsc", &system_contents)); |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 377 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 378 | ResTable table; |
| 379 | ASSERT_EQ(NO_ERROR, |
| 380 | table.add(system_contents.data(), system_contents.size())); |
| 381 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 382 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 383 | ResTable_config configSv; |
| 384 | memset(&configSv, 0, sizeof(configSv)); |
| 385 | configSv.language[0] = 's'; |
| 386 | configSv.language[1] = 'v'; |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 387 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 388 | Vector<ResTable_config> configs; |
| 389 | table.getConfigurations(&configs); |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 390 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 391 | EXPECT_EQ(1, std::count(configs.begin(), configs.end(), configSv)); |
| 392 | |
| 393 | Vector<String8> locales; |
| 394 | table.getLocales(&locales); |
| 395 | |
| 396 | EXPECT_EQ(1, std::count(locales.begin(), locales.end(), String8("sv"))); |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 399 | } // namespace android |