blob: cdc34f1ec752f877afc57ed7b48bd15ebb44b13a [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;
23using ::testing::Pointee;
24
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025namespace aapt {
26
Adam Lesinski52364f72016-01-11 13:10:24 -080027TEST(ResourceUtilsTest, ParseBool) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028 EXPECT_EQ(Maybe<bool>(true), ResourceUtils::ParseBool("true"));
29 EXPECT_EQ(Maybe<bool>(true), ResourceUtils::ParseBool("TRUE"));
30 EXPECT_EQ(Maybe<bool>(true), ResourceUtils::ParseBool("True"));
31 EXPECT_EQ(Maybe<bool>(false), ResourceUtils::ParseBool("false"));
32 EXPECT_EQ(Maybe<bool>(false), ResourceUtils::ParseBool("FALSE"));
33 EXPECT_EQ(Maybe<bool>(false), ResourceUtils::ParseBool("False"));
Adam Lesinski52364f72016-01-11 13:10:24 -080034}
35
Adam Lesinski467f1712015-11-16 17:35:44 -080036TEST(ResourceUtilsTest, ParseResourceName) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 ResourceNameRef actual;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 bool actual_priv = false;
39 EXPECT_TRUE(ResourceUtils::ParseResourceName("android:color/foo", &actual,
40 &actual_priv));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 EXPECT_EQ(ResourceNameRef("android", ResourceType::kColor, "foo"), actual);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 EXPECT_FALSE(actual_priv);
Adam Lesinski467f1712015-11-16 17:35:44 -080043
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 EXPECT_TRUE(
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 ResourceUtils::ParseResourceName("color/foo", &actual, &actual_priv));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 EXPECT_EQ(ResourceNameRef({}, ResourceType::kColor, "foo"), actual);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 EXPECT_FALSE(actual_priv);
Adam Lesinski467f1712015-11-16 17:35:44 -080048
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 EXPECT_TRUE(ResourceUtils::ParseResourceName("*android:color/foo", &actual,
50 &actual_priv));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 EXPECT_EQ(ResourceNameRef("android", ResourceType::kColor, "foo"), actual);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 EXPECT_TRUE(actual_priv);
Adam Lesinski59e04c62016-02-04 15:59:23 -080053
Adam Lesinskid5083f62017-01-16 15:07:21 -080054 EXPECT_FALSE(ResourceUtils::ParseResourceName(android::StringPiece(), &actual, &actual_priv));
Adam Lesinski467f1712015-11-16 17:35:44 -080055}
56
Adam Lesinski1ab598f2015-08-14 14:26:04 -070057TEST(ResourceUtilsTest, ParseReferenceWithNoPackage) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 ResourceNameRef expected({}, ResourceType::kColor, "foo");
59 ResourceNameRef actual;
60 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 bool private_ref = false;
62 EXPECT_TRUE(ResourceUtils::ParseReference("@color/foo", &actual, &create,
63 &private_ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 EXPECT_EQ(expected, actual);
65 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 expected("android", ResourceType::kColor, "foo");
71 ResourceNameRef actual;
72 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 bool private_ref = false;
74 EXPECT_TRUE(ResourceUtils::ParseReference("@android:color/foo", &actual,
75 &create, &private_ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 EXPECT_EQ(expected, actual);
77 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 expected("android", ResourceType::kColor, "foo");
83 ResourceNameRef actual;
84 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 bool private_ref = false;
86 EXPECT_TRUE(ResourceUtils::ParseReference("\t @android:color/foo\n \n\t",
87 &actual, &create, &private_ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 EXPECT_EQ(expected, actual);
89 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 EXPECT_FALSE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070091}
92
93TEST(ResourceUtilsTest, ParseAutoCreateIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 ResourceNameRef expected("android", ResourceType::kId, "foo");
95 ResourceNameRef actual;
96 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 bool private_ref = false;
98 EXPECT_TRUE(ResourceUtils::ParseReference("@+android:id/foo", &actual,
99 &create, &private_ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 EXPECT_EQ(expected, actual);
101 EXPECT_TRUE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 EXPECT_FALSE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700103}
104
105TEST(ResourceUtilsTest, ParsePrivateReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 ResourceNameRef expected("android", ResourceType::kId, "foo");
107 ResourceNameRef actual;
108 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 bool private_ref = false;
110 EXPECT_TRUE(ResourceUtils::ParseReference("@*android:id/foo", &actual,
111 &create, &private_ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 EXPECT_EQ(expected, actual);
113 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 EXPECT_TRUE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700115}
116
117TEST(ResourceUtilsTest, FailToParseAutoCreateNonIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 bool private_ref = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 ResourceNameRef actual;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 EXPECT_FALSE(ResourceUtils::ParseReference("@+android:color/foo", &actual,
122 &create, &private_ref));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700123}
124
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800125TEST(ResourceUtilsTest, ParseAttributeReferences) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android"));
127 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:foo"));
128 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?attr/foo"));
129 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:attr/foo"));
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800130}
131
132TEST(ResourceUtilsTest, FailParseIncompleteReference) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?style/foo"));
134 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:style/foo"));
135 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:"));
136 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:attr/"));
137 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/"));
138 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/foo"));
139 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/"));
140 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/foo"));
141 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?attr/"));
142 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?/foo"));
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800143}
144
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700145TEST(ResourceUtilsTest, ParseStyleParentReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 const ResourceName kAndroidStyleFooName("android", ResourceType::kStyle,
147 "foo");
148 const ResourceName kStyleFooName({}, ResourceType::kStyle, "foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700149
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 std::string err_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 Maybe<Reference> ref =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 ResourceUtils::ParseStyleParentReference("@android:style/foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 AAPT_ASSERT_TRUE(ref);
154 EXPECT_EQ(ref.value().name.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 Lesinskicacb28f2016-10-19 12:18:14 -0700157 AAPT_ASSERT_TRUE(ref);
158 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 ref =
161 ResourceUtils::ParseStyleParentReference("?android:style/foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 AAPT_ASSERT_TRUE(ref);
163 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700164
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 ref = ResourceUtils::ParseStyleParentReference("?style/foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 AAPT_ASSERT_TRUE(ref);
167 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700168
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 ref = ResourceUtils::ParseStyleParentReference("android:style/foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 AAPT_ASSERT_TRUE(ref);
171 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700172
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 ref = ResourceUtils::ParseStyleParentReference("android:foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 AAPT_ASSERT_TRUE(ref);
175 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700176
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 ref = ResourceUtils::ParseStyleParentReference("@android:foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 AAPT_ASSERT_TRUE(ref);
179 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinski52364f72016-01-11 13:10:24 -0800180
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 ref = ResourceUtils::ParseStyleParentReference("foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 AAPT_ASSERT_TRUE(ref);
183 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800184
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 ref =
186 ResourceUtils::ParseStyleParentReference("*android:style/foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 AAPT_ASSERT_TRUE(ref);
188 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 EXPECT_TRUE(ref.value().private_reference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700190}
191
Adam Lesinski52364f72016-01-11 13:10:24 -0800192TEST(ResourceUtilsTest, ParseEmptyFlag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 std::unique_ptr<Attribute> attr =
194 test::AttributeBuilder(false)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195 .SetTypeMask(android::ResTable_map::TYPE_FLAGS)
196 .AddItem("one", 0x01)
197 .AddItem("two", 0x02)
198 .Build();
Adam Lesinski52364f72016-01-11 13:10:24 -0800199
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 std::unique_ptr<BinaryPrimitive> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 ResourceUtils::TryParseFlagSymbol(attr.get(), "");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 ASSERT_NE(nullptr, result);
203 EXPECT_EQ(0u, result->value.data);
Adam Lesinski52364f72016-01-11 13:10:24 -0800204}
205
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700206TEST(ResourceUtilsTest, NullIsEmptyReference) {
207 auto null_value = ResourceUtils::MakeNull();
208 ASSERT_THAT(null_value, Pointee(ValueEq(Reference())));
209
210 auto value = ResourceUtils::TryParseNullOrEmpty("@null");
211 ASSERT_THAT(value, Pointee(ValueEq(Reference())));
212}
213
214TEST(ResourceUtilsTest, EmptyIsBinaryPrimitive) {
215 auto empty_value = ResourceUtils::MakeEmpty();
216 ASSERT_THAT(empty_value, Pointee(ValueEq(BinaryPrimitive(android::Res_value::TYPE_NULL,
217 android::Res_value::DATA_NULL_EMPTY))));
218
219 auto value = ResourceUtils::TryParseNullOrEmpty("@empty");
220 ASSERT_THAT(value, Pointee(ValueEq(BinaryPrimitive(android::Res_value::TYPE_NULL,
221 android::Res_value::DATA_NULL_EMPTY))));
222}
223
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224} // namespace aapt