blob: 894cfcf72144437b2acba30a37216fa7e73f7f62 [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
17#include "Resource.h"
18#include "ResourceUtils.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 Lesinski36c73a52016-08-11 13:39:24 -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) {
33 ResourceNameRef actual;
34 bool actualPriv = false;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070035 EXPECT_TRUE(ResourceUtils::parseResourceName("android:color/foo", &actual, &actualPriv));
36 EXPECT_EQ(ResourceNameRef("android", ResourceType::kColor, "foo"), actual);
Adam Lesinski467f1712015-11-16 17:35:44 -080037 EXPECT_FALSE(actualPriv);
38
Adam Lesinskid0f116b2016-07-08 15:00:32 -070039 EXPECT_TRUE(ResourceUtils::parseResourceName("color/foo", &actual, &actualPriv));
40 EXPECT_EQ(ResourceNameRef({}, ResourceType::kColor, "foo"), actual);
Adam Lesinski467f1712015-11-16 17:35:44 -080041 EXPECT_FALSE(actualPriv);
42
Adam Lesinskid0f116b2016-07-08 15:00:32 -070043 EXPECT_TRUE(ResourceUtils::parseResourceName("*android:color/foo", &actual, &actualPriv));
44 EXPECT_EQ(ResourceNameRef("android", ResourceType::kColor, "foo"), actual);
Adam Lesinski467f1712015-11-16 17:35:44 -080045 EXPECT_TRUE(actualPriv);
Adam Lesinski59e04c62016-02-04 15:59:23 -080046
Adam Lesinskid0f116b2016-07-08 15:00:32 -070047 EXPECT_FALSE(ResourceUtils::parseResourceName(StringPiece(), &actual, &actualPriv));
Adam Lesinski467f1712015-11-16 17:35:44 -080048}
49
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050TEST(ResourceUtilsTest, ParseReferenceWithNoPackage) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -070051 ResourceNameRef expected({}, ResourceType::kColor, "foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052 ResourceNameRef actual;
53 bool create = false;
54 bool privateRef = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -070055 EXPECT_TRUE(ResourceUtils::parseReference("@color/foo", &actual, &create, &privateRef));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056 EXPECT_EQ(expected, actual);
57 EXPECT_FALSE(create);
58 EXPECT_FALSE(privateRef);
59}
60
61TEST(ResourceUtilsTest, ParseReferenceWithPackage) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -070062 ResourceNameRef expected("android", ResourceType::kColor, "foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063 ResourceNameRef actual;
64 bool create = false;
65 bool privateRef = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -070066 EXPECT_TRUE(ResourceUtils::parseReference("@android:color/foo", &actual, &create,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067 &privateRef));
68 EXPECT_EQ(expected, actual);
69 EXPECT_FALSE(create);
70 EXPECT_FALSE(privateRef);
71}
72
73TEST(ResourceUtilsTest, ParseReferenceWithSurroundingWhitespace) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -070074 ResourceNameRef expected("android", ResourceType::kColor, "foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075 ResourceNameRef actual;
76 bool create = false;
77 bool privateRef = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -070078 EXPECT_TRUE(ResourceUtils::parseReference("\t @android:color/foo\n \n\t", &actual,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070079 &create, &privateRef));
80 EXPECT_EQ(expected, actual);
81 EXPECT_FALSE(create);
82 EXPECT_FALSE(privateRef);
83}
84
85TEST(ResourceUtilsTest, ParseAutoCreateIdReference) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -070086 ResourceNameRef expected("android", ResourceType::kId, "foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070087 ResourceNameRef actual;
88 bool create = false;
89 bool privateRef = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -070090 EXPECT_TRUE(ResourceUtils::parseReference("@+android:id/foo", &actual, &create,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070091 &privateRef));
92 EXPECT_EQ(expected, actual);
93 EXPECT_TRUE(create);
94 EXPECT_FALSE(privateRef);
95}
96
97TEST(ResourceUtilsTest, ParsePrivateReference) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -070098 ResourceNameRef expected("android", ResourceType::kId, "foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070099 ResourceNameRef actual;
100 bool create = false;
101 bool privateRef = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700102 EXPECT_TRUE(ResourceUtils::parseReference("@*android:id/foo", &actual, &create,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700103 &privateRef));
104 EXPECT_EQ(expected, actual);
105 EXPECT_FALSE(create);
106 EXPECT_TRUE(privateRef);
107}
108
109TEST(ResourceUtilsTest, FailToParseAutoCreateNonIdReference) {
110 bool create = false;
111 bool privateRef = false;
112 ResourceNameRef actual;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700113 EXPECT_FALSE(ResourceUtils::parseReference("@+android:color/foo", &actual, &create,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700114 &privateRef));
115}
116
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800117TEST(ResourceUtilsTest, ParseAttributeReferences) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700118 EXPECT_TRUE(ResourceUtils::isAttributeReference("?android"));
119 EXPECT_TRUE(ResourceUtils::isAttributeReference("?android:foo"));
120 EXPECT_TRUE(ResourceUtils::isAttributeReference("?attr/foo"));
121 EXPECT_TRUE(ResourceUtils::isAttributeReference("?android:attr/foo"));
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800122}
123
124TEST(ResourceUtilsTest, FailParseIncompleteReference) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700125 EXPECT_FALSE(ResourceUtils::isAttributeReference("?style/foo"));
126 EXPECT_FALSE(ResourceUtils::isAttributeReference("?android:style/foo"));
127 EXPECT_FALSE(ResourceUtils::isAttributeReference("?android:"));
128 EXPECT_FALSE(ResourceUtils::isAttributeReference("?android:attr/"));
129 EXPECT_FALSE(ResourceUtils::isAttributeReference("?:attr/"));
130 EXPECT_FALSE(ResourceUtils::isAttributeReference("?:attr/foo"));
131 EXPECT_FALSE(ResourceUtils::isAttributeReference("?:/"));
132 EXPECT_FALSE(ResourceUtils::isAttributeReference("?:/foo"));
133 EXPECT_FALSE(ResourceUtils::isAttributeReference("?attr/"));
134 EXPECT_FALSE(ResourceUtils::isAttributeReference("?/foo"));
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800135}
136
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700137TEST(ResourceUtilsTest, ParseStyleParentReference) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700138 const ResourceName kAndroidStyleFooName("android", ResourceType::kStyle, "foo");
139 const ResourceName kStyleFooName({}, ResourceType::kStyle, "foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700140
141 std::string errStr;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700142 Maybe<Reference> ref = ResourceUtils::parseStyleParentReference("@android:style/foo", &errStr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143 AAPT_ASSERT_TRUE(ref);
144 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
145
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700146 ref = ResourceUtils::parseStyleParentReference("@style/foo", &errStr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147 AAPT_ASSERT_TRUE(ref);
148 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
149
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700150 ref = ResourceUtils::parseStyleParentReference("?android:style/foo", &errStr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700151 AAPT_ASSERT_TRUE(ref);
152 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
153
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700154 ref = ResourceUtils::parseStyleParentReference("?style/foo", &errStr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700155 AAPT_ASSERT_TRUE(ref);
156 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
157
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700158 ref = ResourceUtils::parseStyleParentReference("android:style/foo", &errStr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159 AAPT_ASSERT_TRUE(ref);
160 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
161
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700162 ref = ResourceUtils::parseStyleParentReference("android:foo", &errStr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700163 AAPT_ASSERT_TRUE(ref);
164 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
165
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700166 ref = ResourceUtils::parseStyleParentReference("@android:foo", &errStr);
Adam Lesinski52364f72016-01-11 13:10:24 -0800167 AAPT_ASSERT_TRUE(ref);
168 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
169
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700170 ref = ResourceUtils::parseStyleParentReference("foo", &errStr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700171 AAPT_ASSERT_TRUE(ref);
172 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800173
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700174 ref = ResourceUtils::parseStyleParentReference("*android:style/foo", &errStr);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800175 AAPT_ASSERT_TRUE(ref);
176 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
177 EXPECT_TRUE(ref.value().privateReference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700178}
179
Adam Lesinski52364f72016-01-11 13:10:24 -0800180TEST(ResourceUtilsTest, ParseEmptyFlag) {
181 std::unique_ptr<Attribute> attr = test::AttributeBuilder(false)
182 .setTypeMask(android::ResTable_map::TYPE_FLAGS)
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700183 .addItem("one", 0x01)
184 .addItem("two", 0x02)
Adam Lesinski52364f72016-01-11 13:10:24 -0800185 .build();
186
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700187 std::unique_ptr<BinaryPrimitive> result = ResourceUtils::tryParseFlagSymbol(attr.get(), "");
Adam Lesinski52364f72016-01-11 13:10:24 -0800188 ASSERT_NE(nullptr, result);
189 EXPECT_EQ(0u, result->value.data);
190}
191
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700192} // namespace aapt