blob: 048c69252e472591068d9e1e62df84d28b459ba5 [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 Lesinskid5083f62017-01-16 15:07:21 -080051 EXPECT_FALSE(ResourceUtils::ParseResourceName(android::StringPiece(), &actual, &actual_priv));
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;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 bool private_ref = false;
59 EXPECT_TRUE(ResourceUtils::ParseReference("@color/foo", &actual, &create,
60 &private_ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 EXPECT_EQ(expected, actual);
62 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 EXPECT_FALSE(private_ref);
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;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 bool private_ref = false;
71 EXPECT_TRUE(ResourceUtils::ParseReference("@android:color/foo", &actual,
72 &create, &private_ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 EXPECT_EQ(expected, actual);
74 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 EXPECT_FALSE(private_ref);
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;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 bool private_ref = false;
83 EXPECT_TRUE(ResourceUtils::ParseReference("\t @android:color/foo\n \n\t",
84 &actual, &create, &private_ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 EXPECT_EQ(expected, actual);
86 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 EXPECT_FALSE(private_ref);
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;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 bool private_ref = false;
95 EXPECT_TRUE(ResourceUtils::ParseReference("@+android:id/foo", &actual,
96 &create, &private_ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 EXPECT_EQ(expected, actual);
98 EXPECT_TRUE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 EXPECT_FALSE(private_ref);
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;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 bool private_ref = false;
107 EXPECT_TRUE(ResourceUtils::ParseReference("@*android:id/foo", &actual,
108 &create, &private_ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 EXPECT_EQ(expected, actual);
110 EXPECT_FALSE(create);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 EXPECT_TRUE(private_ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700112}
113
114TEST(ResourceUtilsTest, FailToParseAutoCreateNonIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 bool private_ref = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 ResourceNameRef actual;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 EXPECT_FALSE(ResourceUtils::ParseReference("@+android:color/foo", &actual,
119 &create, &private_ref));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700120}
121
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800122TEST(ResourceUtilsTest, ParseAttributeReferences) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -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 Lesinskice5e56e2016-10-21 17:56:45 -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 Lesinskice5e56e2016-10-21 17:56:45 -0700147 std::string err_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 Maybe<Reference> ref =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 ResourceUtils::ParseStyleParentReference("@android:style/foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 AAPT_ASSERT_TRUE(ref);
151 EXPECT_EQ(ref.value().name.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 Lesinskicacb28f2016-10-19 12:18:14 -0700154 AAPT_ASSERT_TRUE(ref);
155 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700156
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 ref =
158 ResourceUtils::ParseStyleParentReference("?android:style/foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 AAPT_ASSERT_TRUE(ref);
160 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700161
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162 ref = ResourceUtils::ParseStyleParentReference("?style/foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 AAPT_ASSERT_TRUE(ref);
164 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700165
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 ref = ResourceUtils::ParseStyleParentReference("android:style/foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 AAPT_ASSERT_TRUE(ref);
168 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 ref = ResourceUtils::ParseStyleParentReference("android:foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 AAPT_ASSERT_TRUE(ref);
172 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 ref = ResourceUtils::ParseStyleParentReference("@android:foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 AAPT_ASSERT_TRUE(ref);
176 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinski52364f72016-01-11 13:10:24 -0800177
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 ref = ResourceUtils::ParseStyleParentReference("foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 AAPT_ASSERT_TRUE(ref);
180 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800181
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 ref =
183 ResourceUtils::ParseStyleParentReference("*android:style/foo", &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 AAPT_ASSERT_TRUE(ref);
185 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 EXPECT_TRUE(ref.value().private_reference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700187}
188
Adam Lesinski52364f72016-01-11 13:10:24 -0800189TEST(ResourceUtilsTest, ParseEmptyFlag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 std::unique_ptr<Attribute> attr =
191 test::AttributeBuilder(false)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 .SetTypeMask(android::ResTable_map::TYPE_FLAGS)
193 .AddItem("one", 0x01)
194 .AddItem("two", 0x02)
195 .Build();
Adam Lesinski52364f72016-01-11 13:10:24 -0800196
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700197 std::unique_ptr<BinaryPrimitive> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198 ResourceUtils::TryParseFlagSymbol(attr.get(), "");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 ASSERT_NE(nullptr, result);
200 EXPECT_EQ(0u, result->value.data);
Adam Lesinski52364f72016-01-11 13:10:24 -0800201}
202
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203} // namespace aapt