blob: eb62b1b0f4fa34bfaf4cfcdb1aab7d03c1728511 [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 Lesinskicacb28f2016-10-19 12:18:14 -070018#include "Resource.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070019#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020
21namespace aapt {
22
Adam Lesinski52364f72016-01-11 13:10:24 -080023TEST(ResourceUtilsTest, ParseBool) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070024 EXPECT_EQ(Maybe<bool>(true), ResourceUtils::parseBool("true"));
25 EXPECT_EQ(Maybe<bool>(true), ResourceUtils::parseBool("TRUE"));
26 EXPECT_EQ(Maybe<bool>(true), ResourceUtils::parseBool("True"));
27 EXPECT_EQ(Maybe<bool>(false), ResourceUtils::parseBool("false"));
28 EXPECT_EQ(Maybe<bool>(false), ResourceUtils::parseBool("FALSE"));
29 EXPECT_EQ(Maybe<bool>(false), ResourceUtils::parseBool("False"));
Adam Lesinski52364f72016-01-11 13:10:24 -080030}
31
Adam Lesinski467f1712015-11-16 17:35:44 -080032TEST(ResourceUtilsTest, ParseResourceName) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 ResourceNameRef actual;
34 bool actualPriv = false;
35 EXPECT_TRUE(ResourceUtils::parseResourceName("android:color/foo", &actual,
36 &actualPriv));
37 EXPECT_EQ(ResourceNameRef("android", ResourceType::kColor, "foo"), actual);
38 EXPECT_FALSE(actualPriv);
Adam Lesinski467f1712015-11-16 17:35:44 -080039
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 EXPECT_TRUE(
41 ResourceUtils::parseResourceName("color/foo", &actual, &actualPriv));
42 EXPECT_EQ(ResourceNameRef({}, ResourceType::kColor, "foo"), actual);
43 EXPECT_FALSE(actualPriv);
Adam Lesinski467f1712015-11-16 17:35:44 -080044
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 EXPECT_TRUE(ResourceUtils::parseResourceName("*android:color/foo", &actual,
46 &actualPriv));
47 EXPECT_EQ(ResourceNameRef("android", ResourceType::kColor, "foo"), actual);
48 EXPECT_TRUE(actualPriv);
Adam Lesinski59e04c62016-02-04 15:59:23 -080049
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 EXPECT_FALSE(
51 ResourceUtils::parseResourceName(StringPiece(), &actual, &actualPriv));
Adam Lesinski467f1712015-11-16 17:35:44 -080052}
53
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054TEST(ResourceUtilsTest, ParseReferenceWithNoPackage) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 ResourceNameRef expected({}, ResourceType::kColor, "foo");
56 ResourceNameRef actual;
57 bool create = false;
58 bool privateRef = false;
59 EXPECT_TRUE(ResourceUtils::parseReference("@color/foo", &actual, &create,
60 &privateRef));
61 EXPECT_EQ(expected, actual);
62 EXPECT_FALSE(create);
63 EXPECT_FALSE(privateRef);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064}
65
66TEST(ResourceUtilsTest, ParseReferenceWithPackage) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 ResourceNameRef expected("android", ResourceType::kColor, "foo");
68 ResourceNameRef actual;
69 bool create = false;
70 bool privateRef = false;
71 EXPECT_TRUE(ResourceUtils::parseReference("@android:color/foo", &actual,
72 &create, &privateRef));
73 EXPECT_EQ(expected, actual);
74 EXPECT_FALSE(create);
75 EXPECT_FALSE(privateRef);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076}
77
78TEST(ResourceUtilsTest, ParseReferenceWithSurroundingWhitespace) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 ResourceNameRef expected("android", ResourceType::kColor, "foo");
80 ResourceNameRef actual;
81 bool create = false;
82 bool privateRef = false;
83 EXPECT_TRUE(ResourceUtils::parseReference("\t @android:color/foo\n \n\t",
84 &actual, &create, &privateRef));
85 EXPECT_EQ(expected, actual);
86 EXPECT_FALSE(create);
87 EXPECT_FALSE(privateRef);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088}
89
90TEST(ResourceUtilsTest, ParseAutoCreateIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 ResourceNameRef expected("android", ResourceType::kId, "foo");
92 ResourceNameRef actual;
93 bool create = false;
94 bool privateRef = false;
95 EXPECT_TRUE(ResourceUtils::parseReference("@+android:id/foo", &actual,
96 &create, &privateRef));
97 EXPECT_EQ(expected, actual);
98 EXPECT_TRUE(create);
99 EXPECT_FALSE(privateRef);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100}
101
102TEST(ResourceUtilsTest, ParsePrivateReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 ResourceNameRef expected("android", ResourceType::kId, "foo");
104 ResourceNameRef actual;
105 bool create = false;
106 bool privateRef = false;
107 EXPECT_TRUE(ResourceUtils::parseReference("@*android:id/foo", &actual,
108 &create, &privateRef));
109 EXPECT_EQ(expected, actual);
110 EXPECT_FALSE(create);
111 EXPECT_TRUE(privateRef);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700112}
113
114TEST(ResourceUtilsTest, FailToParseAutoCreateNonIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 bool create = false;
116 bool privateRef = false;
117 ResourceNameRef actual;
118 EXPECT_FALSE(ResourceUtils::parseReference("@+android:color/foo", &actual,
119 &create, &privateRef));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700120}
121
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800122TEST(ResourceUtilsTest, ParseAttributeReferences) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 EXPECT_TRUE(ResourceUtils::isAttributeReference("?android"));
124 EXPECT_TRUE(ResourceUtils::isAttributeReference("?android:foo"));
125 EXPECT_TRUE(ResourceUtils::isAttributeReference("?attr/foo"));
126 EXPECT_TRUE(ResourceUtils::isAttributeReference("?android:attr/foo"));
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800127}
128
129TEST(ResourceUtilsTest, FailParseIncompleteReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 EXPECT_FALSE(ResourceUtils::isAttributeReference("?style/foo"));
131 EXPECT_FALSE(ResourceUtils::isAttributeReference("?android:style/foo"));
132 EXPECT_FALSE(ResourceUtils::isAttributeReference("?android:"));
133 EXPECT_FALSE(ResourceUtils::isAttributeReference("?android:attr/"));
134 EXPECT_FALSE(ResourceUtils::isAttributeReference("?:attr/"));
135 EXPECT_FALSE(ResourceUtils::isAttributeReference("?:attr/foo"));
136 EXPECT_FALSE(ResourceUtils::isAttributeReference("?:/"));
137 EXPECT_FALSE(ResourceUtils::isAttributeReference("?:/foo"));
138 EXPECT_FALSE(ResourceUtils::isAttributeReference("?attr/"));
139 EXPECT_FALSE(ResourceUtils::isAttributeReference("?/foo"));
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800140}
141
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700142TEST(ResourceUtilsTest, ParseStyleParentReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 const ResourceName kAndroidStyleFooName("android", ResourceType::kStyle,
144 "foo");
145 const ResourceName kStyleFooName({}, ResourceType::kStyle, "foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 std::string errStr;
148 Maybe<Reference> ref =
149 ResourceUtils::parseStyleParentReference("@android:style/foo", &errStr);
150 AAPT_ASSERT_TRUE(ref);
151 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700152
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 ref = ResourceUtils::parseStyleParentReference("@style/foo", &errStr);
154 AAPT_ASSERT_TRUE(ref);
155 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700156
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 ref = ResourceUtils::parseStyleParentReference("?android:style/foo", &errStr);
158 AAPT_ASSERT_TRUE(ref);
159 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700160
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 ref = ResourceUtils::parseStyleParentReference("?style/foo", &errStr);
162 AAPT_ASSERT_TRUE(ref);
163 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700164
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 ref = ResourceUtils::parseStyleParentReference("android:style/foo", &errStr);
166 AAPT_ASSERT_TRUE(ref);
167 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700168
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 ref = ResourceUtils::parseStyleParentReference("android:foo", &errStr);
170 AAPT_ASSERT_TRUE(ref);
171 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700172
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 ref = ResourceUtils::parseStyleParentReference("@android:foo", &errStr);
174 AAPT_ASSERT_TRUE(ref);
175 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinski52364f72016-01-11 13:10:24 -0800176
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 ref = ResourceUtils::parseStyleParentReference("foo", &errStr);
178 AAPT_ASSERT_TRUE(ref);
179 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800180
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 ref = ResourceUtils::parseStyleParentReference("*android:style/foo", &errStr);
182 AAPT_ASSERT_TRUE(ref);
183 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
184 EXPECT_TRUE(ref.value().privateReference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700185}
186
Adam Lesinski52364f72016-01-11 13:10:24 -0800187TEST(ResourceUtilsTest, ParseEmptyFlag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 std::unique_ptr<Attribute> attr =
189 test::AttributeBuilder(false)
190 .setTypeMask(android::ResTable_map::TYPE_FLAGS)
191 .addItem("one", 0x01)
192 .addItem("two", 0x02)
193 .build();
Adam Lesinski52364f72016-01-11 13:10:24 -0800194
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 std::unique_ptr<BinaryPrimitive> result =
196 ResourceUtils::tryParseFlagSymbol(attr.get(), "");
197 ASSERT_NE(nullptr, result);
198 EXPECT_EQ(0u, result->value.data);
Adam Lesinski52364f72016-01-11 13:10:24 -0800199}
200
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201} // namespace aapt