blob: 9018b0fc372a287f57aa50035621bd17c21a1bec [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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
Adam Lesinski1ab598f2015-08-14 14:26:04 -070017#include "ResourceUtils.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Nicholas Lativy79f03962019-01-16 16:19:09 +000019#include "SdkConstants.h"
Adam Lesinskicacb28f2016-10-19 12:18:14 -070020#include "Resource.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070021#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070022
Adam Lesinskibab4ef52017-06-01 15:22:57 -070023using ::aapt::test::ValueEq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070024using ::android::Res_value;
25using ::android::ResTable_map;
26using ::testing::Eq;
27using ::testing::NotNull;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070028using ::testing::Pointee;
29
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030namespace aapt {
31
Adam Lesinski52364f72016-01-11 13:10:24 -080032TEST(ResourceUtilsTest, ParseBool) {
Adam Lesinskia45893a2017-05-30 15:19:02 -070033 EXPECT_THAT(ResourceUtils::ParseBool("true"), Eq(Maybe<bool>(true)));
34 EXPECT_THAT(ResourceUtils::ParseBool("TRUE"), Eq(Maybe<bool>(true)));
35 EXPECT_THAT(ResourceUtils::ParseBool("True"), Eq(Maybe<bool>(true)));
36
37 EXPECT_THAT(ResourceUtils::ParseBool("false"), Eq(Maybe<bool>(false)));
38 EXPECT_THAT(ResourceUtils::ParseBool("FALSE"), Eq(Maybe<bool>(false)));
39 EXPECT_THAT(ResourceUtils::ParseBool("False"), Eq(Maybe<bool>(false)));
Adam Lesinski8a3bffe2017-06-27 12:27:43 -070040
41 EXPECT_THAT(ResourceUtils::ParseBool(" False\n "), Eq(Maybe<bool>(false)));
Adam Lesinski52364f72016-01-11 13:10:24 -080042}
43
Adam Lesinski467f1712015-11-16 17:35:44 -080044TEST(ResourceUtilsTest, ParseResourceName) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 ResourceNameRef actual;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 bool actual_priv = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -070047 EXPECT_TRUE(ResourceUtils::ParseResourceName("android:color/foo", &actual, &actual_priv));
48 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 EXPECT_FALSE(actual_priv);
Adam Lesinski467f1712015-11-16 17:35:44 -080050
Adam Lesinskia45893a2017-05-30 15:19:02 -070051 EXPECT_TRUE(ResourceUtils::ParseResourceName("color/foo", &actual, &actual_priv));
52 EXPECT_THAT(actual, Eq(ResourceNameRef({}, ResourceType::kColor, "foo")));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 EXPECT_FALSE(actual_priv);
Adam Lesinski467f1712015-11-16 17:35:44 -080054
Adam Lesinskia45893a2017-05-30 15:19:02 -070055 EXPECT_TRUE(ResourceUtils::ParseResourceName("*android:color/foo", &actual, &actual_priv));
56 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 EXPECT_TRUE(actual_priv);
Adam Lesinski59e04c62016-02-04 15:59:23 -080058
Adam Lesinskid5083f62017-01-16 15:07:21 -080059 EXPECT_FALSE(ResourceUtils::ParseResourceName(android::StringPiece(), &actual, &actual_priv));
Adam Lesinski467f1712015-11-16 17:35:44 -080060}
61
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062TEST(ResourceUtilsTest, ParseReferenceWithNoPackage) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 ResourceNameRef actual;
64 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 bool private_ref = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -070066 EXPECT_TRUE(ResourceUtils::ParseReference("@color/foo", &actual, &create, &private_ref));
67 EXPECT_THAT(actual, Eq(ResourceNameRef({}, ResourceType::kColor, "foo")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 EXPECT_FALSE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070070}
71
72TEST(ResourceUtilsTest, ParseReferenceWithPackage) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 ResourceNameRef actual;
74 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 bool private_ref = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -070076 EXPECT_TRUE(ResourceUtils::ParseReference("@android:color/foo", &actual, &create, &private_ref));
77 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 EXPECT_FALSE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080}
81
82TEST(ResourceUtilsTest, ParseReferenceWithSurroundingWhitespace) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 ResourceNameRef actual;
84 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 bool private_ref = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -070086 EXPECT_TRUE(ResourceUtils::ParseReference("\t @android:color/foo\n \n\t", &actual, &create, &private_ref));
87 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 EXPECT_FALSE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070090}
91
92TEST(ResourceUtilsTest, ParseAutoCreateIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 ResourceNameRef actual;
94 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 bool private_ref = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -070096 EXPECT_TRUE(ResourceUtils::ParseReference("@+android:id/foo", &actual, &create, &private_ref));
97 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kId, "foo")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 EXPECT_TRUE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 EXPECT_FALSE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100}
101
102TEST(ResourceUtilsTest, ParsePrivateReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 ResourceNameRef actual;
104 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 bool private_ref = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700106 EXPECT_TRUE(ResourceUtils::ParseReference("@*android:id/foo", &actual, &create, &private_ref));
107 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kId, "foo")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 EXPECT_TRUE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700110}
111
112TEST(ResourceUtilsTest, FailToParseAutoCreateNonIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 bool private_ref = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 ResourceNameRef actual;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700116 EXPECT_FALSE(ResourceUtils::ParseReference("@+android:color/foo", &actual, &create, &private_ref));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700117}
118
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800119TEST(ResourceUtilsTest, ParseAttributeReferences) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android"));
121 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:foo"));
122 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?attr/foo"));
123 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:attr/foo"));
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800124}
125
126TEST(ResourceUtilsTest, FailParseIncompleteReference) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?style/foo"));
128 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:style/foo"));
129 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:"));
130 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:attr/"));
131 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/"));
132 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/foo"));
133 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/"));
134 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/foo"));
135 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?attr/"));
136 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?/foo"));
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800137}
138
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139TEST(ResourceUtilsTest, ParseStyleParentReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700140 const ResourceName kAndroidStyleFooName("android", ResourceType::kStyle, "foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 const ResourceName kStyleFooName({}, ResourceType::kStyle, "foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700142
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 std::string err_str;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700144 Maybe<Reference> ref = ResourceUtils::ParseStyleParentReference("@android:style/foo", &err_str);
145 ASSERT_TRUE(ref);
146 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 ref = ResourceUtils::ParseStyleParentReference("@style/foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700149 ASSERT_TRUE(ref);
150 EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700151
Adam Lesinskia45893a2017-05-30 15:19:02 -0700152 ref = ResourceUtils::ParseStyleParentReference("?android:style/foo", &err_str);
153 ASSERT_TRUE(ref);
154 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700155
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 ref = ResourceUtils::ParseStyleParentReference("?style/foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700157 ASSERT_TRUE(ref);
158 EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 ref = ResourceUtils::ParseStyleParentReference("android:style/foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700161 ASSERT_TRUE(ref);
162 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700163
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 ref = ResourceUtils::ParseStyleParentReference("android:foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700165 ASSERT_TRUE(ref);
166 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700167
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 ref = ResourceUtils::ParseStyleParentReference("@android:foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700169 ASSERT_TRUE(ref);
170 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski52364f72016-01-11 13:10:24 -0800171
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 ref = ResourceUtils::ParseStyleParentReference("foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700173 ASSERT_TRUE(ref);
174 EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName)));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800175
Adam Lesinskia45893a2017-05-30 15:19:02 -0700176 ref = ResourceUtils::ParseStyleParentReference("*android:style/foo", &err_str);
177 ASSERT_TRUE(ref);
178 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 EXPECT_TRUE(ref.value().private_reference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700180}
181
Adam Lesinski52364f72016-01-11 13:10:24 -0800182TEST(ResourceUtilsTest, ParseEmptyFlag) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800183 std::unique_ptr<Attribute> attr = test::AttributeBuilder()
184 .SetTypeMask(ResTable_map::TYPE_FLAGS)
185 .AddItem("one", 0x01)
186 .AddItem("two", 0x02)
187 .Build();
Adam Lesinski52364f72016-01-11 13:10:24 -0800188
Adam Lesinskia45893a2017-05-30 15:19:02 -0700189 std::unique_ptr<BinaryPrimitive> result = ResourceUtils::TryParseFlagSymbol(attr.get(), "");
190 ASSERT_THAT(result, NotNull());
191 EXPECT_THAT(result->value.data, Eq(0u));
Adam Lesinski52364f72016-01-11 13:10:24 -0800192}
193
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700194TEST(ResourceUtilsTest, NullIsEmptyReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700195 ASSERT_THAT(ResourceUtils::MakeNull(), Pointee(ValueEq(Reference())));
196 ASSERT_THAT(ResourceUtils::TryParseNullOrEmpty("@null"), Pointee(ValueEq(Reference())));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700197}
198
199TEST(ResourceUtilsTest, EmptyIsBinaryPrimitive) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700200 ASSERT_THAT(ResourceUtils::MakeEmpty(), Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_NULL, Res_value::DATA_NULL_EMPTY))));
201 ASSERT_THAT(ResourceUtils::TryParseNullOrEmpty("@empty"), Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_NULL, Res_value::DATA_NULL_EMPTY))));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700202}
203
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700204TEST(ResourceUtilsTest, ItemsWithWhitespaceAreParsedCorrectly) {
205 EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" 12\n ", ResTable_map::TYPE_INTEGER),
206 Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_DEC, 12u))));
207 EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" true\n ", ResTable_map::TYPE_BOOLEAN),
208 Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_BOOLEAN, 0xffffffffu))));
209
210 const float expected_float = 12.0f;
211 const uint32_t expected_float_flattened = *(uint32_t*)&expected_float;
212 EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" 12.0\n ", ResTable_map::TYPE_FLOAT),
213 Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, expected_float_flattened))));
214}
215
Nicholas Lativy79f03962019-01-16 16:19:09 +0000216TEST(ResourceUtilsTest, ParseSdkVersionWithCodename) {
217 const android::StringPiece codename =
218 GetDevelopmentSdkCodeNameAndVersion().first;
219 const int version = GetDevelopmentSdkCodeNameAndVersion().second;
220
221 EXPECT_THAT(ResourceUtils::ParseSdkVersion(codename), Eq(Maybe<int>(version)));
222 EXPECT_THAT(
223 ResourceUtils::ParseSdkVersion(codename.to_string() + ".fingerprint"),
224 Eq(Maybe<int>(version)));
225}
226
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800227TEST(ResourceUtilsTest, StringBuilderWhitespaceRemoval) {
228 EXPECT_THAT(ResourceUtils::StringBuilder()
229 .AppendText(" hey guys ")
230 .AppendText(" this is so cool ")
231 .to_string(),
232 Eq(" hey guys this is so cool "));
233 EXPECT_THAT(ResourceUtils::StringBuilder()
234 .AppendText(" \" wow, so many \t ")
235 .AppendText("spaces. \"what? ")
236 .to_string(),
237 Eq(" wow, so many \t spaces. what? "));
238 EXPECT_THAT(ResourceUtils::StringBuilder()
239 .AppendText(" where \t ")
240 .AppendText(" \nis the pie?")
241 .to_string(),
242 Eq(" where is the pie?"));
243}
244
245TEST(ResourceUtilsTest, StringBuilderEscaping) {
246 EXPECT_THAT(ResourceUtils::StringBuilder()
247 .AppendText("hey guys\\n ")
248 .AppendText(" this \\t is so\\\\ cool")
249 .to_string(),
250 Eq("hey guys\n this \t is so\\ cool"));
251 EXPECT_THAT(ResourceUtils::StringBuilder().AppendText("\\@\\?\\#\\\\\\'").to_string(),
252 Eq("@?#\\\'"));
253}
254
255TEST(ResourceUtilsTest, StringBuilderMisplacedQuote) {
256 ResourceUtils::StringBuilder builder;
257 EXPECT_FALSE(builder.AppendText("they're coming!"));
258}
259
260TEST(ResourceUtilsTest, StringBuilderUnicodeCodes) {
261 EXPECT_THAT(ResourceUtils::StringBuilder().AppendText("\\u00AF\\u0AF0 woah").to_string(),
262 Eq("\u00AF\u0AF0 woah"));
263 EXPECT_FALSE(ResourceUtils::StringBuilder().AppendText("\\u00 yo"));
264}
265
266TEST(ResourceUtilsTest, StringBuilderPreserveSpaces) {
267 EXPECT_THAT(ResourceUtils::StringBuilder(true /*preserve_spaces*/).AppendText("\"").to_string(),
268 Eq("\""));
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700269
270 // Single quotes should be able to be used without escaping them when preserving spaces and the
271 // spaces should not be trimmed
272 EXPECT_THAT(ResourceUtils::StringBuilder()
273 .AppendText(" hey guys ")
274 .AppendText(" 'this is so cool' ", /* preserve_spaces */ true)
275 .AppendText(" wow ")
276 .to_string(),
277 Eq(" hey guys 'this is so cool' wow "));
278
279 // Reading a double quote while preserving spaces should not change the quote state
280 EXPECT_THAT(ResourceUtils::StringBuilder()
281 .AppendText(" hey guys ")
282 .AppendText(" \"this is so cool' ", /* preserve_spaces */ true)
283 .AppendText(" wow ")
284 .to_string(),
285 Eq(" hey guys \"this is so cool' wow "));
286 EXPECT_THAT(ResourceUtils::StringBuilder()
287 .AppendText(" hey guys\" ")
288 .AppendText(" \"this is so cool' ", /* preserve_spaces */ true)
289 .AppendText(" wow \" ")
290 .to_string(),
291 Eq(" hey guys \"this is so cool' wow "));
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800292}
293
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294} // namespace aapt