blob: 3a5e685e6b94861169a9e82fb494d19e7453c81a [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 Lesinski52364f72016-01-11 13:10:24 -080039}
40
Adam Lesinski467f1712015-11-16 17:35:44 -080041TEST(ResourceUtilsTest, ParseResourceName) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 ResourceNameRef actual;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 bool actual_priv = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -070044 EXPECT_TRUE(ResourceUtils::ParseResourceName("android:color/foo", &actual, &actual_priv));
45 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 EXPECT_FALSE(actual_priv);
Adam Lesinski467f1712015-11-16 17:35:44 -080047
Adam Lesinskia45893a2017-05-30 15:19:02 -070048 EXPECT_TRUE(ResourceUtils::ParseResourceName("color/foo", &actual, &actual_priv));
49 EXPECT_THAT(actual, Eq(ResourceNameRef({}, ResourceType::kColor, "foo")));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 EXPECT_FALSE(actual_priv);
Adam Lesinski467f1712015-11-16 17:35:44 -080051
Adam Lesinskia45893a2017-05-30 15:19:02 -070052 EXPECT_TRUE(ResourceUtils::ParseResourceName("*android:color/foo", &actual, &actual_priv));
53 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 EXPECT_TRUE(actual_priv);
Adam Lesinski59e04c62016-02-04 15:59:23 -080055
Adam Lesinskid5083f62017-01-16 15:07:21 -080056 EXPECT_FALSE(ResourceUtils::ParseResourceName(android::StringPiece(), &actual, &actual_priv));
Adam Lesinski467f1712015-11-16 17:35:44 -080057}
58
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059TEST(ResourceUtilsTest, ParseReferenceWithNoPackage) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 ResourceNameRef actual;
61 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 bool private_ref = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -070063 EXPECT_TRUE(ResourceUtils::ParseReference("@color/foo", &actual, &create, &private_ref));
64 EXPECT_THAT(actual, Eq(ResourceNameRef({}, ResourceType::kColor, "foo")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 EXPECT_FALSE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067}
68
69TEST(ResourceUtilsTest, ParseReferenceWithPackage) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 ResourceNameRef actual;
71 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 bool private_ref = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -070073 EXPECT_TRUE(ResourceUtils::ParseReference("@android:color/foo", &actual, &create, &private_ref));
74 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 EXPECT_FALSE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077}
78
79TEST(ResourceUtilsTest, ParseReferenceWithSurroundingWhitespace) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 ResourceNameRef actual;
81 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 bool private_ref = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -070083 EXPECT_TRUE(ResourceUtils::ParseReference("\t @android:color/foo\n \n\t", &actual, &create, &private_ref));
84 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 EXPECT_FALSE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070087}
88
89TEST(ResourceUtilsTest, ParseAutoCreateIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 ResourceNameRef actual;
91 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 bool private_ref = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -070093 EXPECT_TRUE(ResourceUtils::ParseReference("@+android:id/foo", &actual, &create, &private_ref));
94 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kId, "foo")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 EXPECT_TRUE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 EXPECT_FALSE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070097}
98
99TEST(ResourceUtilsTest, ParsePrivateReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 ResourceNameRef actual;
101 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 bool private_ref = false;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700103 EXPECT_TRUE(ResourceUtils::ParseReference("@*android:id/foo", &actual, &create, &private_ref));
104 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kId, "foo")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 EXPECT_TRUE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700107}
108
109TEST(ResourceUtilsTest, FailToParseAutoCreateNonIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 bool private_ref = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 ResourceNameRef actual;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700113 EXPECT_FALSE(ResourceUtils::ParseReference("@+android:color/foo", &actual, &create, &private_ref));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700114}
115
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800116TEST(ResourceUtilsTest, ParseAttributeReferences) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android"));
118 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:foo"));
119 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?attr/foo"));
120 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:attr/foo"));
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800121}
122
123TEST(ResourceUtilsTest, FailParseIncompleteReference) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?style/foo"));
125 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:style/foo"));
126 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:"));
127 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:attr/"));
128 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/"));
129 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/foo"));
130 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/"));
131 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/foo"));
132 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?attr/"));
133 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?/foo"));
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800134}
135
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700136TEST(ResourceUtilsTest, ParseStyleParentReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700137 const ResourceName kAndroidStyleFooName("android", ResourceType::kStyle, "foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 const ResourceName kStyleFooName({}, ResourceType::kStyle, "foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 std::string err_str;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700141 Maybe<Reference> ref = ResourceUtils::ParseStyleParentReference("@android:style/foo", &err_str);
142 ASSERT_TRUE(ref);
143 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700144
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 ref = ResourceUtils::ParseStyleParentReference("@style/foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700146 ASSERT_TRUE(ref);
147 EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700148
Adam Lesinskia45893a2017-05-30 15:19:02 -0700149 ref = ResourceUtils::ParseStyleParentReference("?android:style/foo", &err_str);
150 ASSERT_TRUE(ref);
151 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700152
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 ref = ResourceUtils::ParseStyleParentReference("?style/foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700154 ASSERT_TRUE(ref);
155 EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700156
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 ref = ResourceUtils::ParseStyleParentReference("android:style/foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700158 ASSERT_TRUE(ref);
159 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700160
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 ref = ResourceUtils::ParseStyleParentReference("android:foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700162 ASSERT_TRUE(ref);
163 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700164
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 ref = ResourceUtils::ParseStyleParentReference("@android:foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700166 ASSERT_TRUE(ref);
167 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinski52364f72016-01-11 13:10:24 -0800168
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 ref = ResourceUtils::ParseStyleParentReference("foo", &err_str);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700170 ASSERT_TRUE(ref);
171 EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName)));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800172
Adam Lesinskia45893a2017-05-30 15:19:02 -0700173 ref = ResourceUtils::ParseStyleParentReference("*android:style/foo", &err_str);
174 ASSERT_TRUE(ref);
175 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 EXPECT_TRUE(ref.value().private_reference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700177}
178
Adam Lesinski52364f72016-01-11 13:10:24 -0800179TEST(ResourceUtilsTest, ParseEmptyFlag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 std::unique_ptr<Attribute> attr =
181 test::AttributeBuilder(false)
Adam Lesinskia45893a2017-05-30 15:19:02 -0700182 .SetTypeMask(ResTable_map::TYPE_FLAGS)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 .AddItem("one", 0x01)
184 .AddItem("two", 0x02)
185 .Build();
Adam Lesinski52364f72016-01-11 13:10:24 -0800186
Adam Lesinskia45893a2017-05-30 15:19:02 -0700187 std::unique_ptr<BinaryPrimitive> result = ResourceUtils::TryParseFlagSymbol(attr.get(), "");
188 ASSERT_THAT(result, NotNull());
189 EXPECT_THAT(result->value.data, Eq(0u));
Adam Lesinski52364f72016-01-11 13:10:24 -0800190}
191
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700192TEST(ResourceUtilsTest, NullIsEmptyReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700193 ASSERT_THAT(ResourceUtils::MakeNull(), Pointee(ValueEq(Reference())));
194 ASSERT_THAT(ResourceUtils::TryParseNullOrEmpty("@null"), Pointee(ValueEq(Reference())));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700195}
196
197TEST(ResourceUtilsTest, EmptyIsBinaryPrimitive) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700198 ASSERT_THAT(ResourceUtils::MakeEmpty(), Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_NULL, Res_value::DATA_NULL_EMPTY))));
199 ASSERT_THAT(ResourceUtils::TryParseNullOrEmpty("@empty"), Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_NULL, Res_value::DATA_NULL_EMPTY))));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700200}
201
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202} // namespace aapt