blob: cb786d3794c24e025a49cc0ec5451eaa4d7cb5a5 [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
Adam Lesinskicacb28f2016-10-19 12:18:14 -070019#include "Resource.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070020#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021
Adam Lesinskibab4ef52017-06-01 15:22:57 -070022using ::aapt::test::ValueEq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070023using ::android::Res_value;
24using ::android::ResTable_map;
25using ::testing::Eq;
26using ::testing::NotNull;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070027using ::testing::Pointee;
28
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029namespace aapt {
30
Adam Lesinski52364f72016-01-11 13:10:24 -080031TEST(ResourceUtilsTest, ParseBool) {
Adam Lesinskia45893a2017-05-30 15:19:02 -070032 EXPECT_THAT(ResourceUtils::ParseBool("true"), Eq(Maybe<bool>(true)));
33 EXPECT_THAT(ResourceUtils::ParseBool("TRUE"), Eq(Maybe<bool>(true)));
34 EXPECT_THAT(ResourceUtils::ParseBool("True"), Eq(Maybe<bool>(true)));
35
36 EXPECT_THAT(ResourceUtils::ParseBool("false"), Eq(Maybe<bool>(false)));
37 EXPECT_THAT(ResourceUtils::ParseBool("FALSE"), Eq(Maybe<bool>(false)));
38 EXPECT_THAT(ResourceUtils::ParseBool("False"), Eq(Maybe<bool>(false)));
Adam Lesinski8a3bffe2017-06-27 12:27:43 -070039
40 EXPECT_THAT(ResourceUtils::ParseBool(" False\n "), Eq(Maybe<bool>(false)));
Adam Lesinski52364f72016-01-11 13:10:24 -080041}
42
Adam Lesinski467f1712015-11-16 17:35:44 -080043TEST(ResourceUtilsTest, ParseResourceName) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 ResourceNameRef actual;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 bool actual_priv = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -070046 EXPECT_TRUE(ResourceUtils::ParseResourceName("android:color/foo", &actual, &actual_priv));
47 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 EXPECT_FALSE(actual_priv);
Adam Lesinski467f1712015-11-16 17:35:44 -080049
Adam Lesinskia45893a2017-05-30 15:19:02 -070050 EXPECT_TRUE(ResourceUtils::ParseResourceName("color/foo", &actual, &actual_priv));
51 EXPECT_THAT(actual, Eq(ResourceNameRef({}, ResourceType::kColor, "foo")));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 EXPECT_FALSE(actual_priv);
Adam Lesinski467f1712015-11-16 17:35:44 -080053
Adam Lesinskia45893a2017-05-30 15:19:02 -070054 EXPECT_TRUE(ResourceUtils::ParseResourceName("*android:color/foo", &actual, &actual_priv));
55 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 EXPECT_TRUE(actual_priv);
Adam Lesinski59e04c62016-02-04 15:59:23 -080057
Adam Lesinskid5083f62017-01-16 15:07:21 -080058 EXPECT_FALSE(ResourceUtils::ParseResourceName(android::StringPiece(), &actual, &actual_priv));
Adam Lesinski467f1712015-11-16 17:35:44 -080059}
60
Adam Lesinski1ab598f2015-08-14 14:26:04 -070061TEST(ResourceUtilsTest, ParseReferenceWithNoPackage) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 ResourceNameRef actual;
63 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 bool private_ref = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -070065 EXPECT_TRUE(ResourceUtils::ParseReference("@color/foo", &actual, &create, &private_ref));
66 EXPECT_THAT(actual, Eq(ResourceNameRef({}, ResourceType::kColor, "foo")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 EXPECT_FALSE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069}
70
71TEST(ResourceUtilsTest, ParseReferenceWithPackage) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 ResourceNameRef actual;
73 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 bool private_ref = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -070075 EXPECT_TRUE(ResourceUtils::ParseReference("@android:color/foo", &actual, &create, &private_ref));
76 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 EXPECT_FALSE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070079}
80
81TEST(ResourceUtilsTest, ParseReferenceWithSurroundingWhitespace) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 ResourceNameRef actual;
83 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 bool private_ref = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -070085 EXPECT_TRUE(ResourceUtils::ParseReference("\t @android:color/foo\n \n\t", &actual, &create, &private_ref));
86 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 EXPECT_FALSE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089}
90
91TEST(ResourceUtilsTest, ParseAutoCreateIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 ResourceNameRef actual;
93 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 bool private_ref = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -070095 EXPECT_TRUE(ResourceUtils::ParseReference("@+android:id/foo", &actual, &create, &private_ref));
96 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kId, "foo")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 EXPECT_TRUE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 EXPECT_FALSE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070099}
100
101TEST(ResourceUtilsTest, ParsePrivateReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 ResourceNameRef actual;
103 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 bool private_ref = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700105 EXPECT_TRUE(ResourceUtils::ParseReference("@*android:id/foo", &actual, &create, &private_ref));
106 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kId, "foo")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 EXPECT_TRUE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700109}
110
111TEST(ResourceUtilsTest, FailToParseAutoCreateNonIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 bool private_ref = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 ResourceNameRef actual;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700115 EXPECT_FALSE(ResourceUtils::ParseReference("@+android:color/foo", &actual, &create, &private_ref));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700116}
117
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800118TEST(ResourceUtilsTest, ParseAttributeReferences) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android"));
120 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:foo"));
121 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?attr/foo"));
122 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:attr/foo"));
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800123}
124
125TEST(ResourceUtilsTest, FailParseIncompleteReference) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?style/foo"));
127 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:style/foo"));
128 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:"));
129 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:attr/"));
130 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/"));
131 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/foo"));
132 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/"));
133 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/foo"));
134 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?attr/"));
135 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?/foo"));
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800136}
137
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700138TEST(ResourceUtilsTest, ParseStyleParentReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700139 const ResourceName kAndroidStyleFooName("android", ResourceType::kStyle, "foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 const ResourceName kStyleFooName({}, ResourceType::kStyle, "foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700141
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 std::string err_str;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700143 Maybe<Reference> ref = ResourceUtils::ParseStyleParentReference("@android:style/foo", &err_str);
144 ASSERT_TRUE(ref);
145 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 ref = ResourceUtils::ParseStyleParentReference("@style/foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700148 ASSERT_TRUE(ref);
149 EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700150
Adam Lesinskia45893a2017-05-30 15:19:02 -0700151 ref = ResourceUtils::ParseStyleParentReference("?android:style/foo", &err_str);
152 ASSERT_TRUE(ref);
153 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 ref = ResourceUtils::ParseStyleParentReference("?style/foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700156 ASSERT_TRUE(ref);
157 EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700158
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 ref = ResourceUtils::ParseStyleParentReference("android:style/foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700160 ASSERT_TRUE(ref);
161 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 ref = ResourceUtils::ParseStyleParentReference("android:foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700164 ASSERT_TRUE(ref);
165 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 ref = ResourceUtils::ParseStyleParentReference("@android:foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700168 ASSERT_TRUE(ref);
169 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski52364f72016-01-11 13:10:24 -0800170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 ref = ResourceUtils::ParseStyleParentReference("foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700172 ASSERT_TRUE(ref);
173 EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName)));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800174
Adam Lesinskia45893a2017-05-30 15:19:02 -0700175 ref = ResourceUtils::ParseStyleParentReference("*android:style/foo", &err_str);
176 ASSERT_TRUE(ref);
177 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 EXPECT_TRUE(ref.value().private_reference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700179}
180
Adam Lesinski52364f72016-01-11 13:10:24 -0800181TEST(ResourceUtilsTest, ParseEmptyFlag) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800182 std::unique_ptr<Attribute> attr = test::AttributeBuilder()
183 .SetTypeMask(ResTable_map::TYPE_FLAGS)
184 .AddItem("one", 0x01)
185 .AddItem("two", 0x02)
186 .Build();
Adam Lesinski52364f72016-01-11 13:10:24 -0800187
Adam Lesinskia45893a2017-05-30 15:19:02 -0700188 std::unique_ptr<BinaryPrimitive> result = ResourceUtils::TryParseFlagSymbol(attr.get(), "");
189 ASSERT_THAT(result, NotNull());
190 EXPECT_THAT(result->value.data, Eq(0u));
Adam Lesinski52364f72016-01-11 13:10:24 -0800191}
192
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700193TEST(ResourceUtilsTest, NullIsEmptyReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700194 ASSERT_THAT(ResourceUtils::MakeNull(), Pointee(ValueEq(Reference())));
195 ASSERT_THAT(ResourceUtils::TryParseNullOrEmpty("@null"), Pointee(ValueEq(Reference())));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700196}
197
198TEST(ResourceUtilsTest, EmptyIsBinaryPrimitive) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700199 ASSERT_THAT(ResourceUtils::MakeEmpty(), Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_NULL, Res_value::DATA_NULL_EMPTY))));
200 ASSERT_THAT(ResourceUtils::TryParseNullOrEmpty("@empty"), Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_NULL, Res_value::DATA_NULL_EMPTY))));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700201}
202
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700203TEST(ResourceUtilsTest, ItemsWithWhitespaceAreParsedCorrectly) {
204 EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" 12\n ", ResTable_map::TYPE_INTEGER),
205 Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_DEC, 12u))));
206 EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" true\n ", ResTable_map::TYPE_BOOLEAN),
207 Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_BOOLEAN, 0xffffffffu))));
208
209 const float expected_float = 12.0f;
210 const uint32_t expected_float_flattened = *(uint32_t*)&expected_float;
211 EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" 12.0\n ", ResTable_map::TYPE_FLOAT),
212 Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, expected_float_flattened))));
213}
214
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215} // namespace aapt