| 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 | |
| 17 | #include <androidfw/ResourceTypes.h> |
| 18 | |
| 19 | #include <utils/String8.h> |
| 20 | #include <utils/String16.h> |
| 21 | #include "TestHelpers.h" |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 22 | #include "data/basic/R.h" |
| 23 | #include "data/lib/R.h" |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 24 | |
| 25 | #include <gtest/gtest.h> |
| 26 | |
| 27 | using namespace android; |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | /** |
| 32 | * Include a binary resource table. |
| 33 | * |
| 34 | * Package: com.android.test.basic |
| 35 | */ |
| 36 | #include "data/basic/basic_arsc.h" |
| 37 | |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 38 | #include "data/lib/lib_arsc.h" |
| 39 | |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 40 | TEST(ResTableTest, shouldLoadSuccessfully) { |
| 41 | ResTable table; |
| 42 | ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len)); |
| 43 | } |
| 44 | |
| 45 | TEST(ResTableTest, simpleTypeIsRetrievedCorrectly) { |
| 46 | ResTable table; |
| 47 | ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len)); |
| 48 | |
| Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 49 | EXPECT_TRUE(IsStringEqual(table, base::R::string::test1, "test1")); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | TEST(ResTableTest, resourceNameIsResolved) { |
| 53 | ResTable table; |
| 54 | ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len)); |
| 55 | |
| 56 | String16 defPackage("com.android.test.basic"); |
| 57 | String16 testName("@string/test1"); |
| 58 | uint32_t resID = table.identifierForName(testName.string(), testName.size(), |
| 59 | 0, 0, |
| 60 | defPackage.string(), defPackage.size()); |
| 61 | ASSERT_NE(uint32_t(0x00000000), resID); |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 62 | ASSERT_EQ(base::R::string::test1, resID); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | TEST(ResTableTest, noParentThemeIsAppliedCorrectly) { |
| 66 | ResTable table; |
| 67 | ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len)); |
| 68 | |
| 69 | ResTable::Theme theme(table); |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 70 | ASSERT_EQ(NO_ERROR, theme.applyStyle(base::R::style::Theme1)); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 71 | |
| 72 | Res_value val; |
| 73 | uint32_t specFlags = 0; |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 74 | ssize_t index = theme.getAttribute(base::R::attr::attr1, &val, &specFlags); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 75 | ASSERT_GE(index, 0); |
| 76 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 77 | ASSERT_EQ(uint32_t(100), val.data); |
| 78 | |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 79 | index = theme.getAttribute(base::R::attr::attr2, &val, &specFlags); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 80 | ASSERT_GE(index, 0); |
| 81 | ASSERT_EQ(Res_value::TYPE_REFERENCE, val.dataType); |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 82 | ASSERT_EQ(base::R::integer::number1, val.data); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | TEST(ResTableTest, parentThemeIsAppliedCorrectly) { |
| 86 | ResTable table; |
| 87 | ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len)); |
| 88 | |
| 89 | ResTable::Theme theme(table); |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 90 | ASSERT_EQ(NO_ERROR, theme.applyStyle(base::R::style::Theme2)); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 91 | |
| 92 | Res_value val; |
| 93 | uint32_t specFlags = 0; |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 94 | ssize_t index = theme.getAttribute(base::R::attr::attr1, &val, &specFlags); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 95 | ASSERT_GE(index, 0); |
| 96 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 97 | ASSERT_EQ(uint32_t(300), val.data); |
| 98 | |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 99 | index = theme.getAttribute(base::R::attr::attr2, &val, &specFlags); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 100 | ASSERT_GE(index, 0); |
| 101 | ASSERT_EQ(Res_value::TYPE_REFERENCE, val.dataType); |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 102 | ASSERT_EQ(base::R::integer::number1, val.data); |
| 103 | } |
| 104 | |
| 105 | TEST(ResTableTest, libraryThemeIsAppliedCorrectly) { |
| 106 | ResTable table; |
| 107 | ASSERT_EQ(NO_ERROR, table.add(lib_arsc, lib_arsc_len)); |
| 108 | |
| 109 | ResTable::Theme theme(table); |
| 110 | ASSERT_EQ(NO_ERROR, theme.applyStyle(lib::R::style::Theme)); |
| 111 | |
| 112 | Res_value val; |
| 113 | uint32_t specFlags = 0; |
| 114 | ssize_t index = theme.getAttribute(lib::R::attr::attr1, &val, &specFlags); |
| 115 | ASSERT_GE(index, 0); |
| 116 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 117 | ASSERT_EQ(uint32_t(700), val.data); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | TEST(ResTableTest, referenceToBagIsNotResolved) { |
| 121 | ResTable table; |
| 122 | ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len)); |
| 123 | |
| 124 | Res_value val; |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 125 | ssize_t block = table.getResource(base::R::integer::number2, &val, MAY_NOT_BE_BAG); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 126 | ASSERT_GE(block, 0); |
| 127 | ASSERT_EQ(Res_value::TYPE_REFERENCE, val.dataType); |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 128 | ASSERT_EQ(base::R::array::integerArray1, val.data); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 129 | |
| 130 | ssize_t newBlock = table.resolveReference(&val, block); |
| 131 | EXPECT_EQ(block, newBlock); |
| 132 | EXPECT_EQ(Res_value::TYPE_REFERENCE, val.dataType); |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 133 | EXPECT_EQ(base::R::array::integerArray1, val.data); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | TEST(ResTableTest, resourcesStillAccessibleAfterParameterChange) { |
| 137 | ResTable table; |
| 138 | ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len)); |
| 139 | |
| 140 | Res_value val; |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 141 | ssize_t block = table.getResource(base::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 142 | ASSERT_GE(block, 0); |
| 143 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 144 | |
| 145 | const ResTable::bag_entry* entry; |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 146 | ssize_t count = table.lockBag(base::R::array::integerArray1, &entry); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 147 | ASSERT_GE(count, 0); |
| 148 | table.unlockBag(entry); |
| 149 | |
| 150 | ResTable_config param; |
| 151 | memset(¶m, 0, sizeof(param)); |
| 152 | param.density = 320; |
| 153 | table.setParameters(¶m); |
| 154 | |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 155 | block = table.getResource(base::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 156 | ASSERT_GE(block, 0); |
| 157 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 158 | |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 159 | count = table.lockBag(base::R::array::integerArray1, &entry); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 160 | ASSERT_GE(count, 0); |
| 161 | table.unlockBag(entry); |
| 162 | } |
| 163 | |
| 164 | TEST(ResTableTest, resourceIsOverridenWithBetterConfig) { |
| 165 | ResTable table; |
| 166 | ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len)); |
| 167 | |
| 168 | Res_value val; |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 169 | ssize_t block = table.getResource(base::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 170 | ASSERT_GE(block, 0); |
| 171 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 172 | ASSERT_EQ(uint32_t(200), val.data); |
| 173 | |
| 174 | ResTable_config param; |
| 175 | memset(¶m, 0, sizeof(param)); |
| 176 | param.language[0] = 's'; |
| 177 | param.language[1] = 'v'; |
| 178 | param.country[0] = 'S'; |
| 179 | param.country[1] = 'E'; |
| 180 | table.setParameters(¶m); |
| 181 | |
| Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 182 | block = table.getResource(base::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 183 | ASSERT_GE(block, 0); |
| 184 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 185 | ASSERT_EQ(uint32_t(400), val.data); |
| 186 | } |
| 187 | |
| Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 188 | TEST(ResTableTest, emptyTableHasSensibleDefaults) { |
| 189 | const int32_t assetCookie = 1; |
| 190 | |
| 191 | ResTable table; |
| 192 | ASSERT_EQ(NO_ERROR, table.addEmpty(assetCookie)); |
| 193 | |
| 194 | // Adding an empty table gives us one table! |
| 195 | ASSERT_EQ(uint32_t(1), table.getTableCount()); |
| 196 | |
| 197 | // Adding an empty table doesn't mean we get packages. |
| 198 | ASSERT_EQ(uint32_t(0), table.getBasePackageCount()); |
| 199 | |
| 200 | Res_value val; |
| 201 | ASSERT_LT(table.getResource(base::R::integer::number1, &val, MAY_NOT_BE_BAG), 0); |
| 202 | } |
| 203 | |
| Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 204 | } |