blob: 568871a4d66e5c244501e464127848fe7bded220 [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) {
Ryan Mitchell4382e442021-07-14 12:53:01 -070033 EXPECT_THAT(ResourceUtils::ParseBool("true"), Eq(std::optional<bool>(true)));
34 EXPECT_THAT(ResourceUtils::ParseBool("TRUE"), Eq(std::optional<bool>(true)));
35 EXPECT_THAT(ResourceUtils::ParseBool("True"), Eq(std::optional<bool>(true)));
Adam Lesinskia45893a2017-05-30 15:19:02 -070036
Ryan Mitchell4382e442021-07-14 12:53:01 -070037 EXPECT_THAT(ResourceUtils::ParseBool("false"), Eq(std::optional<bool>(false)));
38 EXPECT_THAT(ResourceUtils::ParseBool("FALSE"), Eq(std::optional<bool>(false)));
39 EXPECT_THAT(ResourceUtils::ParseBool("False"), Eq(std::optional<bool>(false)));
Adam Lesinski8a3bffe2017-06-27 12:27:43 -070040
Ryan Mitchell4382e442021-07-14 12:53:01 -070041 EXPECT_THAT(ResourceUtils::ParseBool(" False\n "), Eq(std::optional<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 = {};
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000114 value.data = android::util::HostToDevice32(0x01);
Clark DuValle9fedbe2020-01-09 11:52:52 -0800115 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;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700158 std::optional<Reference> ref =
159 ResourceUtils::ParseStyleParentReference("@android:style/foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700160 ASSERT_TRUE(ref);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700161 EXPECT_THAT(ref.value().name, Eq(kAndroidStyleFooName));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 ref = ResourceUtils::ParseStyleParentReference("@style/foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700164 ASSERT_TRUE(ref);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700165 EXPECT_THAT(ref.value().name, Eq(kStyleFooName));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166
Adam Lesinskia45893a2017-05-30 15:19:02 -0700167 ref = ResourceUtils::ParseStyleParentReference("?android:style/foo", &err_str);
168 ASSERT_TRUE(ref);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700169 EXPECT_THAT(ref.value().name, Eq(kAndroidStyleFooName));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 ref = ResourceUtils::ParseStyleParentReference("?style/foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700172 ASSERT_TRUE(ref);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700173 EXPECT_THAT(ref.value().name, Eq(kStyleFooName));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700174
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 ref = ResourceUtils::ParseStyleParentReference("android:style/foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700176 ASSERT_TRUE(ref);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700177 EXPECT_THAT(ref.value().name, Eq(kAndroidStyleFooName));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700178
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 ref = ResourceUtils::ParseStyleParentReference("android:foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700180 ASSERT_TRUE(ref);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700181 EXPECT_THAT(ref.value().name, Eq(kAndroidStyleFooName));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700182
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 ref = ResourceUtils::ParseStyleParentReference("@android:foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700184 ASSERT_TRUE(ref);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700185 EXPECT_THAT(ref.value().name, Eq(kAndroidStyleFooName));
Adam Lesinski52364f72016-01-11 13:10:24 -0800186
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 ref = ResourceUtils::ParseStyleParentReference("foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700188 ASSERT_TRUE(ref);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700189 EXPECT_THAT(ref.value().name, Eq(kStyleFooName));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800190
Adam Lesinskia45893a2017-05-30 15:19:02 -0700191 ref = ResourceUtils::ParseStyleParentReference("*android:style/foo", &err_str);
192 ASSERT_TRUE(ref);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700193 EXPECT_THAT(ref.value().name, Eq(kAndroidStyleFooName));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 EXPECT_TRUE(ref.value().private_reference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700195}
196
Adam Lesinski52364f72016-01-11 13:10:24 -0800197TEST(ResourceUtilsTest, ParseEmptyFlag) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800198 std::unique_ptr<Attribute> attr = test::AttributeBuilder()
199 .SetTypeMask(ResTable_map::TYPE_FLAGS)
200 .AddItem("one", 0x01)
201 .AddItem("two", 0x02)
202 .Build();
Adam Lesinski52364f72016-01-11 13:10:24 -0800203
Adam Lesinskia45893a2017-05-30 15:19:02 -0700204 std::unique_ptr<BinaryPrimitive> result = ResourceUtils::TryParseFlagSymbol(attr.get(), "");
205 ASSERT_THAT(result, NotNull());
206 EXPECT_THAT(result->value.data, Eq(0u));
Adam Lesinski52364f72016-01-11 13:10:24 -0800207}
208
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700209TEST(ResourceUtilsTest, NullIsEmptyReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700210 ASSERT_THAT(ResourceUtils::MakeNull(), Pointee(ValueEq(Reference())));
211 ASSERT_THAT(ResourceUtils::TryParseNullOrEmpty("@null"), Pointee(ValueEq(Reference())));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700212}
213
214TEST(ResourceUtilsTest, EmptyIsBinaryPrimitive) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700215 ASSERT_THAT(ResourceUtils::MakeEmpty(), Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_NULL, Res_value::DATA_NULL_EMPTY))));
216 ASSERT_THAT(ResourceUtils::TryParseNullOrEmpty("@empty"), Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_NULL, Res_value::DATA_NULL_EMPTY))));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700217}
218
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700219TEST(ResourceUtilsTest, ItemsWithWhitespaceAreParsedCorrectly) {
220 EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" 12\n ", ResTable_map::TYPE_INTEGER),
221 Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_DEC, 12u))));
222 EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" true\n ", ResTable_map::TYPE_BOOLEAN),
223 Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_BOOLEAN, 0xffffffffu))));
224
225 const float expected_float = 12.0f;
226 const uint32_t expected_float_flattened = *(uint32_t*)&expected_float;
227 EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" 12.0\n ", ResTable_map::TYPE_FLOAT),
228 Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, expected_float_flattened))));
229}
230
Nicholas Lativy79f03962019-01-16 16:19:09 +0000231TEST(ResourceUtilsTest, ParseSdkVersionWithCodename) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700232 EXPECT_THAT(ResourceUtils::ParseSdkVersion("Q"), Eq(std::optional<int>(10000)));
233 EXPECT_THAT(ResourceUtils::ParseSdkVersion("Q.fingerprint"), Eq(std::optional<int>(10000)));
Ryan Mitchell48002f62019-04-11 14:29:25 -0700234
Ryan Mitchell4382e442021-07-14 12:53:01 -0700235 EXPECT_THAT(ResourceUtils::ParseSdkVersion("R"), Eq(std::optional<int>(10000)));
236 EXPECT_THAT(ResourceUtils::ParseSdkVersion("R.fingerprint"), Eq(std::optional<int>(10000)));
Nicholas Lativy79f03962019-01-16 16:19:09 +0000237}
238
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800239TEST(ResourceUtilsTest, StringBuilderWhitespaceRemoval) {
240 EXPECT_THAT(ResourceUtils::StringBuilder()
241 .AppendText(" hey guys ")
242 .AppendText(" this is so cool ")
243 .to_string(),
244 Eq(" hey guys this is so cool "));
245 EXPECT_THAT(ResourceUtils::StringBuilder()
246 .AppendText(" \" wow, so many \t ")
247 .AppendText("spaces. \"what? ")
248 .to_string(),
249 Eq(" wow, so many \t spaces. what? "));
250 EXPECT_THAT(ResourceUtils::StringBuilder()
251 .AppendText(" where \t ")
252 .AppendText(" \nis the pie?")
253 .to_string(),
254 Eq(" where is the pie?"));
255}
256
257TEST(ResourceUtilsTest, StringBuilderEscaping) {
258 EXPECT_THAT(ResourceUtils::StringBuilder()
259 .AppendText("hey guys\\n ")
260 .AppendText(" this \\t is so\\\\ cool")
261 .to_string(),
262 Eq("hey guys\n this \t is so\\ cool"));
263 EXPECT_THAT(ResourceUtils::StringBuilder().AppendText("\\@\\?\\#\\\\\\'").to_string(),
264 Eq("@?#\\\'"));
265}
266
267TEST(ResourceUtilsTest, StringBuilderMisplacedQuote) {
268 ResourceUtils::StringBuilder builder;
269 EXPECT_FALSE(builder.AppendText("they're coming!"));
270}
271
272TEST(ResourceUtilsTest, StringBuilderUnicodeCodes) {
273 EXPECT_THAT(ResourceUtils::StringBuilder().AppendText("\\u00AF\\u0AF0 woah").to_string(),
274 Eq("\u00AF\u0AF0 woah"));
275 EXPECT_FALSE(ResourceUtils::StringBuilder().AppendText("\\u00 yo"));
276}
277
278TEST(ResourceUtilsTest, StringBuilderPreserveSpaces) {
279 EXPECT_THAT(ResourceUtils::StringBuilder(true /*preserve_spaces*/).AppendText("\"").to_string(),
280 Eq("\""));
281}
282
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283} // namespace aapt