blob: 6a9314e5ffb835cd8201d1bc6ba5be573c0c2a88 [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
17#include <androidfw/ResourceTypes.h>
18
19#include <utils/String8.h>
20#include <utils/String16.h>
21#include "TestHelpers.h"
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -070022#include "data/basic/R.h"
23#include "data/lib/R.h"
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070024
25#include <gtest/gtest.h>
26
27using namespace android;
28
29namespace {
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 Lesinskiccf25c7b2014-08-08 15:32:40 -070038#include "data/lib/lib_arsc.h"
39
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070040TEST(ResTableTest, shouldLoadSuccessfully) {
41 ResTable table;
42 ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len));
43}
44
45TEST(ResTableTest, simpleTypeIsRetrievedCorrectly) {
46 ResTable table;
47 ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len));
48
Adam Lesinski60293192014-10-21 18:36:42 -070049 EXPECT_TRUE(IsStringEqual(table, base::R::string::test1, "test1"));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070050}
51
52TEST(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 Lesinskiccf25c7b2014-08-08 15:32:40 -070062 ASSERT_EQ(base::R::string::test1, resID);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070063}
64
65TEST(ResTableTest, noParentThemeIsAppliedCorrectly) {
66 ResTable table;
67 ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len));
68
69 ResTable::Theme theme(table);
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -070070 ASSERT_EQ(NO_ERROR, theme.applyStyle(base::R::style::Theme1));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070071
72 Res_value val;
73 uint32_t specFlags = 0;
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -070074 ssize_t index = theme.getAttribute(base::R::attr::attr1, &val, &specFlags);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070075 ASSERT_GE(index, 0);
76 ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType);
77 ASSERT_EQ(uint32_t(100), val.data);
78
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -070079 index = theme.getAttribute(base::R::attr::attr2, &val, &specFlags);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070080 ASSERT_GE(index, 0);
81 ASSERT_EQ(Res_value::TYPE_REFERENCE, val.dataType);
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -070082 ASSERT_EQ(base::R::integer::number1, val.data);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070083}
84
85TEST(ResTableTest, parentThemeIsAppliedCorrectly) {
86 ResTable table;
87 ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len));
88
89 ResTable::Theme theme(table);
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -070090 ASSERT_EQ(NO_ERROR, theme.applyStyle(base::R::style::Theme2));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070091
92 Res_value val;
93 uint32_t specFlags = 0;
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -070094 ssize_t index = theme.getAttribute(base::R::attr::attr1, &val, &specFlags);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070095 ASSERT_GE(index, 0);
96 ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType);
97 ASSERT_EQ(uint32_t(300), val.data);
98
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -070099 index = theme.getAttribute(base::R::attr::attr2, &val, &specFlags);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700100 ASSERT_GE(index, 0);
101 ASSERT_EQ(Res_value::TYPE_REFERENCE, val.dataType);
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -0700102 ASSERT_EQ(base::R::integer::number1, val.data);
103}
104
105TEST(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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700118}
119
120TEST(ResTableTest, referenceToBagIsNotResolved) {
121 ResTable table;
122 ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len));
123
124 Res_value val;
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -0700125 ssize_t block = table.getResource(base::R::integer::number2, &val, MAY_NOT_BE_BAG);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700126 ASSERT_GE(block, 0);
127 ASSERT_EQ(Res_value::TYPE_REFERENCE, val.dataType);
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -0700128 ASSERT_EQ(base::R::array::integerArray1, val.data);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700129
130 ssize_t newBlock = table.resolveReference(&val, block);
131 EXPECT_EQ(block, newBlock);
132 EXPECT_EQ(Res_value::TYPE_REFERENCE, val.dataType);
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -0700133 EXPECT_EQ(base::R::array::integerArray1, val.data);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700134}
135
136TEST(ResTableTest, resourcesStillAccessibleAfterParameterChange) {
137 ResTable table;
138 ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len));
139
140 Res_value val;
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -0700141 ssize_t block = table.getResource(base::R::integer::number1, &val, MAY_NOT_BE_BAG);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700142 ASSERT_GE(block, 0);
143 ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType);
144
145 const ResTable::bag_entry* entry;
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -0700146 ssize_t count = table.lockBag(base::R::array::integerArray1, &entry);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700147 ASSERT_GE(count, 0);
148 table.unlockBag(entry);
149
150 ResTable_config param;
151 memset(&param, 0, sizeof(param));
152 param.density = 320;
153 table.setParameters(&param);
154
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -0700155 block = table.getResource(base::R::integer::number1, &val, MAY_NOT_BE_BAG);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700156 ASSERT_GE(block, 0);
157 ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType);
158
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -0700159 count = table.lockBag(base::R::array::integerArray1, &entry);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700160 ASSERT_GE(count, 0);
161 table.unlockBag(entry);
162}
163
164TEST(ResTableTest, resourceIsOverridenWithBetterConfig) {
165 ResTable table;
166 ASSERT_EQ(NO_ERROR, table.add(basic_arsc, basic_arsc_len));
167
168 Res_value val;
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -0700169 ssize_t block = table.getResource(base::R::integer::number1, &val, MAY_NOT_BE_BAG);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700170 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(&param, 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(&param);
181
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -0700182 block = table.getResource(base::R::integer::number1, &val, MAY_NOT_BE_BAG);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700183 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 Lesinski2cb761e2014-08-15 13:59:02 -0700188TEST(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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700204}