blob: 7fa8ea2f7f9487569bd6e8b27e08c4d64b47996c [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 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 "ResourceTable.h"
Adam Lesinskicacb28f2016-10-19 12:18:14 -070018#include "Diagnostics.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019#include "ResourceValues.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070020#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "util/Util.h"
22
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include <ostream>
25#include <string>
26
Adam Lesinski71be7052017-12-12 16:48:07 -080027using ::android::StringPiece;
28using ::testing::Eq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070029using ::testing::NotNull;
Adam Lesinski71be7052017-12-12 16:48:07 -080030using ::testing::StrEq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070031
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032namespace aapt {
33
Adam Lesinskicc5609d2016-04-05 12:41:07 -070034TEST(ResourceTableTest, FailToAddResourceWithBadName) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 EXPECT_FALSE(table.AddResource(
38 test::ParseNameOrDie("android:id/hey,there"), ConfigDescription{}, "",
39 test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(),
40 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080041
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 EXPECT_FALSE(table.AddResource(
43 test::ParseNameOrDie("android:id/hey:there"), ConfigDescription{}, "",
44 test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(),
45 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046}
47
Adam Lesinskib1afa072017-03-29 13:52:38 -070048TEST(ResourceTableTest, AddResourceWithWeirdNameWhenAddingMangledResources) {
49 ResourceTable table;
50
Adam Lesinski71be7052017-12-12 16:48:07 -080051 EXPECT_TRUE(table.AddResourceMangled(
Adam Lesinskib1afa072017-03-29 13:52:38 -070052 test::ParseNameOrDie("android:id/heythere "), ConfigDescription{}, "",
53 test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(), test::GetDiagnostics()));
54}
55
Adam Lesinskicc5609d2016-04-05 12:41:07 -070056TEST(ResourceTableTest, AddOneResource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080058
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 EXPECT_TRUE(table.AddResource(
60 test::ParseNameOrDie("android:attr/id"), ConfigDescription{}, "",
61 test::ValueBuilder<Id>().SetSource("test/path/file.xml", 23u).Build(),
62 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080063
Adam Lesinskia45893a2017-05-30 15:19:02 -070064 EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/id"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065}
66
Adam Lesinskicc5609d2016-04-05 12:41:07 -070067TEST(ResourceTableTest, AddMultipleResources) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 ConfigDescription config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 ConfigDescription language_config;
72 memcpy(language_config.language, "pl", sizeof(language_config.language));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 EXPECT_TRUE(table.AddResource(
75 test::ParseNameOrDie("android:attr/layout_width"), config, "",
76 test::ValueBuilder<Id>().SetSource("test/path/file.xml", 10u).Build(),
77 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 EXPECT_TRUE(table.AddResource(
80 test::ParseNameOrDie("android:attr/id"), config, "",
81 test::ValueBuilder<Id>().SetSource("test/path/file.xml", 12u).Build(),
82 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 EXPECT_TRUE(table.AddResource(
85 test::ParseNameOrDie("android:string/ok"), config, "",
86 test::ValueBuilder<Id>().SetSource("test/path/file.xml", 14u).Build(),
87 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080088
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 EXPECT_TRUE(table.AddResource(
90 test::ParseNameOrDie("android:string/ok"), language_config, "",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 test::ValueBuilder<BinaryPrimitive>(android::Res_value{})
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 .SetSource("test/path/file.xml", 20u)
93 .Build(),
94 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095
Adam Lesinskia45893a2017-05-30 15:19:02 -070096 EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/layout_width"), NotNull());
97 EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/id"), NotNull());
98 EXPECT_THAT(test::GetValue<Id>(&table, "android:string/ok"), NotNull());
99 EXPECT_THAT(test::GetValueForConfig<BinaryPrimitive>(&table, "android:string/ok", language_config), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800100}
101
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700102TEST(ResourceTableTest, OverrideWeakResourceValue) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800105 ASSERT_TRUE(table.AddResource(test::ParseNameOrDie("android:attr/foo"), ConfigDescription{}, "",
106 test::AttributeBuilder().SetWeak(true).Build(),
107 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 Attribute* attr = test::GetValue<Attribute>(&table, "android:attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700110 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800113 ASSERT_TRUE(table.AddResource(test::ParseNameOrDie("android:attr/foo"), ConfigDescription{}, "",
114 util::make_unique<Attribute>(), test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 attr = test::GetValue<Attribute>(&table, "android:attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700117 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 EXPECT_FALSE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119}
120
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800121TEST(ResourceTableTest, AllowCompatibleDuplicateAttributes) {
122 ResourceTable table;
123
124 const ResourceName name = test::ParseNameOrDie("android:attr/foo");
125 Attribute attr_one(android::ResTable_map::TYPE_STRING);
126 attr_one.SetWeak(true);
127 Attribute attr_two(android::ResTable_map::TYPE_STRING | android::ResTable_map::TYPE_REFERENCE);
128 attr_two.SetWeak(true);
129
130 ASSERT_TRUE(table.AddResource(name, ConfigDescription{}, "",
131 util::make_unique<Attribute>(attr_one), test::GetDiagnostics()));
132 ASSERT_TRUE(table.AddResource(name, ConfigDescription{}, "",
133 util::make_unique<Attribute>(attr_two), test::GetDiagnostics()));
134}
135
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700136TEST(ResourceTableTest, ProductVaryingValues) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 ResourceTable table;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800138
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 EXPECT_TRUE(table.AddResource(test::ParseNameOrDie("android:string/foo"),
140 test::ParseConfigOrDie("land"), "tablet",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 util::make_unique<Id>(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 test::GetDiagnostics()));
143 EXPECT_TRUE(table.AddResource(test::ParseNameOrDie("android:string/foo"),
144 test::ParseConfigOrDie("land"), "phone",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 util::make_unique<Id>(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 test::GetDiagnostics()));
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800147
Adam Lesinskia45893a2017-05-30 15:19:02 -0700148 EXPECT_THAT(test::GetValueForConfigAndProduct<Id>(&table, "android:string/foo",test::ParseConfigOrDie("land"), "tablet"), NotNull());
149 EXPECT_THAT(test::GetValueForConfigAndProduct<Id>(&table, "android:string/foo",test::ParseConfigOrDie("land"), "phone"), NotNull());
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800150
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 Maybe<ResourceTable::SearchResult> sr =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 table.FindResource(test::ParseNameOrDie("android:string/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700153 ASSERT_TRUE(sr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 std::vector<ResourceConfigValue*> values =
Adam Lesinskib1afa072017-03-29 13:52:38 -0700155 sr.value().entry->FindAllValues(test::ParseConfigOrDie("land"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 ASSERT_EQ(2u, values.size());
157 EXPECT_EQ(std::string("phone"), values[0]->product);
158 EXPECT_EQ(std::string("tablet"), values[1]->product);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800159}
160
Adam Lesinski71be7052017-12-12 16:48:07 -0800161static StringPiece LevelToString(Visibility::Level level) {
162 switch (level) {
163 case Visibility::Level::kPrivate:
164 return "private";
165 case Visibility::Level::kPublic:
166 return "private";
167 default:
168 return "undefined";
169 }
170}
171
172static ::testing::AssertionResult VisibilityOfResource(const ResourceTable& table,
173 const ResourceNameRef& name,
174 Visibility::Level level,
175 const StringPiece& comment) {
176 Maybe<ResourceTable::SearchResult> result = table.FindResource(name);
177 if (!result) {
178 return ::testing::AssertionFailure() << "no resource '" << name << "' found in table";
179 }
180
181 const Visibility& visibility = result.value().entry->visibility;
182 if (visibility.level != level) {
183 return ::testing::AssertionFailure() << "expected visibility " << LevelToString(level)
184 << " but got " << LevelToString(visibility.level);
185 }
186
187 if (visibility.comment != comment) {
188 return ::testing::AssertionFailure() << "expected visibility comment '" << comment
189 << "' but got '" << visibility.comment << "'";
190 }
191 return ::testing::AssertionSuccess();
192}
193
194TEST(ResourceTableTest, SetVisibility) {
195 using Level = Visibility::Level;
196
197 ResourceTable table;
198 const ResourceName name = test::ParseNameOrDie("android:string/foo");
199
200 Visibility visibility;
201 visibility.level = Visibility::Level::kPrivate;
202 visibility.comment = "private";
203 ASSERT_TRUE(table.SetVisibility(name, visibility, test::GetDiagnostics()));
204 ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPrivate, "private"));
205
206 visibility.level = Visibility::Level::kUndefined;
207 visibility.comment = "undefined";
208 ASSERT_TRUE(table.SetVisibility(name, visibility, test::GetDiagnostics()));
209 ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPrivate, "private"));
210
211 visibility.level = Visibility::Level::kPublic;
212 visibility.comment = "public";
213 ASSERT_TRUE(table.SetVisibility(name, visibility, test::GetDiagnostics()));
214 ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPublic, "public"));
215
216 visibility.level = Visibility::Level::kPrivate;
217 visibility.comment = "private";
218 ASSERT_TRUE(table.SetVisibility(name, visibility, test::GetDiagnostics()));
219 ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPublic, "public"));
220}
221
222TEST(ResourceTableTest, SetAllowNew) {
223 ResourceTable table;
224 const ResourceName name = test::ParseNameOrDie("android:string/foo");
225
226 AllowNew allow_new;
227 Maybe<ResourceTable::SearchResult> result;
228
229 allow_new.comment = "first";
230 ASSERT_TRUE(table.SetAllowNew(name, allow_new, test::GetDiagnostics()));
231 result = table.FindResource(name);
232 ASSERT_TRUE(result);
233 ASSERT_TRUE(result.value().entry->allow_new);
234 ASSERT_THAT(result.value().entry->allow_new.value().comment, StrEq("first"));
235
236 allow_new.comment = "second";
237 ASSERT_TRUE(table.SetAllowNew(name, allow_new, test::GetDiagnostics()));
238 result = table.FindResource(name);
239 ASSERT_TRUE(result);
240 ASSERT_TRUE(result.value().entry->allow_new);
241 ASSERT_THAT(result.value().entry->allow_new.value().comment, StrEq("second"));
242}
243
244TEST(ResourceTableTest, SetOverlayable) {
245 ResourceTable table;
246 const ResourceName name = test::ParseNameOrDie("android:string/foo");
247
248 Overlayable overlayable;
249
250 overlayable.comment = "first";
251 ASSERT_TRUE(table.SetOverlayable(name, overlayable, test::GetDiagnostics()));
252 Maybe<ResourceTable::SearchResult> result = table.FindResource(name);
253 ASSERT_TRUE(result);
254 ASSERT_TRUE(result.value().entry->overlayable);
255 ASSERT_THAT(result.value().entry->overlayable.value().comment, StrEq("first"));
256
257 overlayable.comment = "second";
258 ASSERT_FALSE(table.SetOverlayable(name, overlayable, test::GetDiagnostics()));
259}
260
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261} // namespace aapt