blob: b08bf9a1ff173019c2e6086df20bb1ec18578c4e [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
Clark DuValle9fedbe2020-01-09 11:52:52 -0800112TEST(ResourceUtilsTest, ParseBinaryDynamicReference) {
113 android::Res_value value = {};
114 value.data = util::HostToDevice32(0x01);
115 value.dataType = android::Res_value::TYPE_DYNAMIC_REFERENCE;
116 std::unique_ptr<Item> item = ResourceUtils::ParseBinaryResValue(ResourceType::kId,
117 android::ConfigDescription(),
118 android::ResStringPool(), value,
119 nullptr);
120
121 Reference* ref = ValueCast<Reference>(item.get());
122 EXPECT_TRUE(ref->is_dynamic);
123 EXPECT_EQ(ref->id.value().id, 0x01);
124}
125
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700126TEST(ResourceUtilsTest, FailToParseAutoCreateNonIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 bool private_ref = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 ResourceNameRef actual;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700130 EXPECT_FALSE(ResourceUtils::ParseReference("@+android:color/foo", &actual, &create, &private_ref));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131}
132
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800133TEST(ResourceUtilsTest, ParseAttributeReferences) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android"));
135 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:foo"));
136 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?attr/foo"));
137 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:attr/foo"));
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800138}
139
140TEST(ResourceUtilsTest, FailParseIncompleteReference) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?style/foo"));
142 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:style/foo"));
143 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:"));
144 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:attr/"));
145 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/"));
146 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/foo"));
147 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/"));
148 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/foo"));
149 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?attr/"));
150 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?/foo"));
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800151}
152
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153TEST(ResourceUtilsTest, ParseStyleParentReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700154 const ResourceName kAndroidStyleFooName("android", ResourceType::kStyle, "foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 const ResourceName kStyleFooName({}, ResourceType::kStyle, "foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700156
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 std::string err_str;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700158 Maybe<Reference> ref = ResourceUtils::ParseStyleParentReference("@android:style/foo", &err_str);
159 ASSERT_TRUE(ref);
160 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700161
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162 ref = ResourceUtils::ParseStyleParentReference("@style/foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700163 ASSERT_TRUE(ref);
164 EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700165
Adam Lesinskia45893a2017-05-30 15:19:02 -0700166 ref = ResourceUtils::ParseStyleParentReference("?android:style/foo", &err_str);
167 ASSERT_TRUE(ref);
168 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 ref = ResourceUtils::ParseStyleParentReference("?style/foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700171 ASSERT_TRUE(ref);
172 EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 ref = ResourceUtils::ParseStyleParentReference("android:style/foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700175 ASSERT_TRUE(ref);
176 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700177
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 ref = ResourceUtils::ParseStyleParentReference("android:foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700179 ASSERT_TRUE(ref);
180 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700181
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 ref = ResourceUtils::ParseStyleParentReference("@android:foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700183 ASSERT_TRUE(ref);
184 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski52364f72016-01-11 13:10:24 -0800185
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 ref = ResourceUtils::ParseStyleParentReference("foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700187 ASSERT_TRUE(ref);
188 EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName)));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800189
Adam Lesinskia45893a2017-05-30 15:19:02 -0700190 ref = ResourceUtils::ParseStyleParentReference("*android:style/foo", &err_str);
191 ASSERT_TRUE(ref);
192 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 EXPECT_TRUE(ref.value().private_reference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700194}
195
Adam Lesinski52364f72016-01-11 13:10:24 -0800196TEST(ResourceUtilsTest, ParseEmptyFlag) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800197 std::unique_ptr<Attribute> attr = test::AttributeBuilder()
198 .SetTypeMask(ResTable_map::TYPE_FLAGS)
199 .AddItem("one", 0x01)
200 .AddItem("two", 0x02)
201 .Build();
Adam Lesinski52364f72016-01-11 13:10:24 -0800202
Adam Lesinskia45893a2017-05-30 15:19:02 -0700203 std::unique_ptr<BinaryPrimitive> result = ResourceUtils::TryParseFlagSymbol(attr.get(), "");
204 ASSERT_THAT(result, NotNull());
205 EXPECT_THAT(result->value.data, Eq(0u));
Adam Lesinski52364f72016-01-11 13:10:24 -0800206}
207
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700208TEST(ResourceUtilsTest, NullIsEmptyReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700209 ASSERT_THAT(ResourceUtils::MakeNull(), Pointee(ValueEq(Reference())));
210 ASSERT_THAT(ResourceUtils::TryParseNullOrEmpty("@null"), Pointee(ValueEq(Reference())));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700211}
212
213TEST(ResourceUtilsTest, EmptyIsBinaryPrimitive) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700214 ASSERT_THAT(ResourceUtils::MakeEmpty(), Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_NULL, Res_value::DATA_NULL_EMPTY))));
215 ASSERT_THAT(ResourceUtils::TryParseNullOrEmpty("@empty"), Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_NULL, Res_value::DATA_NULL_EMPTY))));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700216}
217
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700218TEST(ResourceUtilsTest, ItemsWithWhitespaceAreParsedCorrectly) {
219 EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" 12\n ", ResTable_map::TYPE_INTEGER),
220 Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_DEC, 12u))));
221 EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" true\n ", ResTable_map::TYPE_BOOLEAN),
222 Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_BOOLEAN, 0xffffffffu))));
223
224 const float expected_float = 12.0f;
225 const uint32_t expected_float_flattened = *(uint32_t*)&expected_float;
226 EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" 12.0\n ", ResTable_map::TYPE_FLOAT),
227 Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, expected_float_flattened))));
228}
229
Nicholas Lativy79f03962019-01-16 16:19:09 +0000230TEST(ResourceUtilsTest, ParseSdkVersionWithCodename) {
Ryan Mitchell48002f62019-04-11 14:29:25 -0700231 EXPECT_THAT(ResourceUtils::ParseSdkVersion("Q"), Eq(Maybe<int>(10000)));
Nicholas Lativy79f03962019-01-16 16:19:09 +0000232 EXPECT_THAT(
Ryan Mitchell48002f62019-04-11 14:29:25 -0700233 ResourceUtils::ParseSdkVersion("Q.fingerprint"),
234 Eq(Maybe<int>(10000)));
235
236 EXPECT_THAT(ResourceUtils::ParseSdkVersion("R"), Eq(Maybe<int>(10000)));
237 EXPECT_THAT(
238 ResourceUtils::ParseSdkVersion("R.fingerprint"),
239 Eq(Maybe<int>(10000)));
Nicholas Lativy79f03962019-01-16 16:19:09 +0000240}
241
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800242TEST(ResourceUtilsTest, StringBuilderWhitespaceRemoval) {
243 EXPECT_THAT(ResourceUtils::StringBuilder()
244 .AppendText(" hey guys ")
245 .AppendText(" this is so cool ")
246 .to_string(),
247 Eq(" hey guys this is so cool "));
248 EXPECT_THAT(ResourceUtils::StringBuilder()
249 .AppendText(" \" wow, so many \t ")
250 .AppendText("spaces. \"what? ")
251 .to_string(),
252 Eq(" wow, so many \t spaces. what? "));
253 EXPECT_THAT(ResourceUtils::StringBuilder()
254 .AppendText(" where \t ")
255 .AppendText(" \nis the pie?")
256 .to_string(),
257 Eq(" where is the pie?"));
258}
259
260TEST(ResourceUtilsTest, StringBuilderEscaping) {
261 EXPECT_THAT(ResourceUtils::StringBuilder()
262 .AppendText("hey guys\\n ")
263 .AppendText(" this \\t is so\\\\ cool")
264 .to_string(),
265 Eq("hey guys\n this \t is so\\ cool"));
266 EXPECT_THAT(ResourceUtils::StringBuilder().AppendText("\\@\\?\\#\\\\\\'").to_string(),
267 Eq("@?#\\\'"));
268}
269
270TEST(ResourceUtilsTest, StringBuilderMisplacedQuote) {
271 ResourceUtils::StringBuilder builder;
272 EXPECT_FALSE(builder.AppendText("they're coming!"));
273}
274
275TEST(ResourceUtilsTest, StringBuilderUnicodeCodes) {
276 EXPECT_THAT(ResourceUtils::StringBuilder().AppendText("\\u00AF\\u0AF0 woah").to_string(),
277 Eq("\u00AF\u0AF0 woah"));
278 EXPECT_FALSE(ResourceUtils::StringBuilder().AppendText("\\u00 yo"));
279}
280
281TEST(ResourceUtilsTest, StringBuilderPreserveSpaces) {
282 EXPECT_THAT(ResourceUtils::StringBuilder(true /*preserve_spaces*/).AppendText("\"").to_string(),
283 Eq("\""));
284}
285
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286} // namespace aapt