blob: f9c500b42c136c937b66a06ef6fa9857c7e283f1 [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
22namespace aapt {
23
Adam Lesinski52364f72016-01-11 13:10:24 -080024TEST(ResourceUtilsTest, ParseBool) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025 EXPECT_EQ(Maybe<bool>(true), ResourceUtils::ParseBool("true"));
26 EXPECT_EQ(Maybe<bool>(true), ResourceUtils::ParseBool("TRUE"));
27 EXPECT_EQ(Maybe<bool>(true), ResourceUtils::ParseBool("True"));
28 EXPECT_EQ(Maybe<bool>(false), ResourceUtils::ParseBool("false"));
29 EXPECT_EQ(Maybe<bool>(false), ResourceUtils::ParseBool("FALSE"));
30 EXPECT_EQ(Maybe<bool>(false), ResourceUtils::ParseBool("False"));
Adam Lesinski52364f72016-01-11 13:10:24 -080031}
32
Adam Lesinski467f1712015-11-16 17:35:44 -080033TEST(ResourceUtilsTest, ParseResourceName) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 ResourceNameRef actual;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 bool actual_priv = false;
36 EXPECT_TRUE(ResourceUtils::ParseResourceName("android:color/foo", &actual,
37 &actual_priv));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 EXPECT_EQ(ResourceNameRef("android", ResourceType::kColor, "foo"), actual);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 EXPECT_FALSE(actual_priv);
Adam Lesinski467f1712015-11-16 17:35:44 -080040
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 EXPECT_TRUE(
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 ResourceUtils::ParseResourceName("color/foo", &actual, &actual_priv));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 EXPECT_EQ(ResourceNameRef({}, ResourceType::kColor, "foo"), actual);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 EXPECT_FALSE(actual_priv);
Adam Lesinski467f1712015-11-16 17:35:44 -080045
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 EXPECT_TRUE(ResourceUtils::ParseResourceName("*android:color/foo", &actual,
47 &actual_priv));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 EXPECT_EQ(ResourceNameRef("android", ResourceType::kColor, "foo"), actual);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 EXPECT_TRUE(actual_priv);
Adam Lesinski59e04c62016-02-04 15:59:23 -080050
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 EXPECT_FALSE(
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 ResourceUtils::ParseResourceName(StringPiece(), &actual, &actual_priv));
Adam Lesinski467f1712015-11-16 17:35:44 -080053}
54
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055TEST(ResourceUtilsTest, ParseReferenceWithNoPackage) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 ResourceNameRef expected({}, ResourceType::kColor, "foo");
57 ResourceNameRef actual;
58 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 bool private_ref = false;
60 EXPECT_TRUE(ResourceUtils::ParseReference("@color/foo", &actual, &create,
61 &private_ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 EXPECT_EQ(expected, actual);
63 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 EXPECT_FALSE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065}
66
67TEST(ResourceUtilsTest, ParseReferenceWithPackage) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 ResourceNameRef expected("android", ResourceType::kColor, "foo");
69 ResourceNameRef actual;
70 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 bool private_ref = false;
72 EXPECT_TRUE(ResourceUtils::ParseReference("@android:color/foo", &actual,
73 &create, &private_ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 EXPECT_EQ(expected, actual);
75 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 expected("android", ResourceType::kColor, "foo");
81 ResourceNameRef actual;
82 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 bool private_ref = false;
84 EXPECT_TRUE(ResourceUtils::ParseReference("\t @android:color/foo\n \n\t",
85 &actual, &create, &private_ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 EXPECT_EQ(expected, actual);
87 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 expected("android", ResourceType::kId, "foo");
93 ResourceNameRef actual;
94 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 bool private_ref = false;
96 EXPECT_TRUE(ResourceUtils::ParseReference("@+android:id/foo", &actual,
97 &create, &private_ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 EXPECT_EQ(expected, actual);
99 EXPECT_TRUE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 EXPECT_FALSE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700101}
102
103TEST(ResourceUtilsTest, ParsePrivateReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 ResourceNameRef expected("android", ResourceType::kId, "foo");
105 ResourceNameRef actual;
106 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 bool private_ref = false;
108 EXPECT_TRUE(ResourceUtils::ParseReference("@*android:id/foo", &actual,
109 &create, &private_ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 EXPECT_EQ(expected, actual);
111 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 EXPECT_TRUE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700113}
114
115TEST(ResourceUtilsTest, FailToParseAutoCreateNonIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 bool private_ref = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 ResourceNameRef actual;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 EXPECT_FALSE(ResourceUtils::ParseReference("@+android:color/foo", &actual,
120 &create, &private_ref));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700121}
122
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800123TEST(ResourceUtilsTest, ParseAttributeReferences) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android"));
125 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:foo"));
126 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?attr/foo"));
127 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:attr/foo"));
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800128}
129
130TEST(ResourceUtilsTest, FailParseIncompleteReference) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?style/foo"));
132 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:style/foo"));
133 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:"));
134 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:attr/"));
135 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/"));
136 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/foo"));
137 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/"));
138 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/foo"));
139 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?attr/"));
140 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?/foo"));
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800141}
142
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143TEST(ResourceUtilsTest, ParseStyleParentReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 const ResourceName kAndroidStyleFooName("android", ResourceType::kStyle,
145 "foo");
146 const ResourceName kStyleFooName({}, ResourceType::kStyle, "foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 std::string err_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 Maybe<Reference> ref =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 ResourceUtils::ParseStyleParentReference("@android:style/foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 AAPT_ASSERT_TRUE(ref);
152 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 ref = ResourceUtils::ParseStyleParentReference("@style/foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 AAPT_ASSERT_TRUE(ref);
156 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700157
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 ref =
159 ResourceUtils::ParseStyleParentReference("?android:style/foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 AAPT_ASSERT_TRUE(ref);
161 EXPECT_EQ(ref.value().name.value(), 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 Lesinskicacb28f2016-10-19 12:18:14 -0700164 AAPT_ASSERT_TRUE(ref);
165 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 ref = ResourceUtils::ParseStyleParentReference("android:style/foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 AAPT_ASSERT_TRUE(ref);
169 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 ref = ResourceUtils::ParseStyleParentReference("android:foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 AAPT_ASSERT_TRUE(ref);
173 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700174
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 ref = ResourceUtils::ParseStyleParentReference("@android:foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 AAPT_ASSERT_TRUE(ref);
177 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinski52364f72016-01-11 13:10:24 -0800178
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 ref = ResourceUtils::ParseStyleParentReference("foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 AAPT_ASSERT_TRUE(ref);
181 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800182
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 ref =
184 ResourceUtils::ParseStyleParentReference("*android:style/foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 AAPT_ASSERT_TRUE(ref);
186 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 EXPECT_TRUE(ref.value().private_reference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700188}
189
Adam Lesinski52364f72016-01-11 13:10:24 -0800190TEST(ResourceUtilsTest, ParseEmptyFlag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 std::unique_ptr<Attribute> attr =
192 test::AttributeBuilder(false)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 .SetTypeMask(android::ResTable_map::TYPE_FLAGS)
194 .AddItem("one", 0x01)
195 .AddItem("two", 0x02)
196 .Build();
Adam Lesinski52364f72016-01-11 13:10:24 -0800197
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 std::unique_ptr<BinaryPrimitive> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 ResourceUtils::TryParseFlagSymbol(attr.get(), "");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 ASSERT_NE(nullptr, result);
201 EXPECT_EQ(0u, result->value.data);
Adam Lesinski52364f72016-01-11 13:10:24 -0800202}
203
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204} // namespace aapt