blob: 4bbfc32b9b371c0e15b4ff336d9b8f82dea4a59b [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 Lesinski1ab598f2015-08-14 14:26:04 -070019#include "test/Common.h"
20
21#include <gtest/gtest.h>
22
23namespace aapt {
24
Adam Lesinski467f1712015-11-16 17:35:44 -080025TEST(ResourceUtilsTest, ParseResourceName) {
26 ResourceNameRef actual;
27 bool actualPriv = false;
28 EXPECT_TRUE(ResourceUtils::parseResourceName(u"android:color/foo", &actual, &actualPriv));
29 EXPECT_EQ(ResourceNameRef(u"android", ResourceType::kColor, u"foo"), actual);
30 EXPECT_FALSE(actualPriv);
31
32 EXPECT_TRUE(ResourceUtils::parseResourceName(u"color/foo", &actual, &actualPriv));
33 EXPECT_EQ(ResourceNameRef({}, ResourceType::kColor, u"foo"), actual);
34 EXPECT_FALSE(actualPriv);
35
36 EXPECT_TRUE(ResourceUtils::parseResourceName(u"*android:color/foo", &actual, &actualPriv));
37 EXPECT_EQ(ResourceNameRef(u"android", ResourceType::kColor, u"foo"), actual);
38 EXPECT_TRUE(actualPriv);
39}
40
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041TEST(ResourceUtilsTest, ParseReferenceWithNoPackage) {
Adam Lesinski467f1712015-11-16 17:35:44 -080042 ResourceNameRef expected({}, ResourceType::kColor, u"foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070043 ResourceNameRef actual;
44 bool create = false;
45 bool privateRef = false;
46 EXPECT_TRUE(ResourceUtils::tryParseReference(u"@color/foo", &actual, &create, &privateRef));
47 EXPECT_EQ(expected, actual);
48 EXPECT_FALSE(create);
49 EXPECT_FALSE(privateRef);
50}
51
52TEST(ResourceUtilsTest, ParseReferenceWithPackage) {
Adam Lesinski467f1712015-11-16 17:35:44 -080053 ResourceNameRef expected(u"android", ResourceType::kColor, u"foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054 ResourceNameRef actual;
55 bool create = false;
56 bool privateRef = false;
57 EXPECT_TRUE(ResourceUtils::tryParseReference(u"@android:color/foo", &actual, &create,
58 &privateRef));
59 EXPECT_EQ(expected, actual);
60 EXPECT_FALSE(create);
61 EXPECT_FALSE(privateRef);
62}
63
64TEST(ResourceUtilsTest, ParseReferenceWithSurroundingWhitespace) {
Adam Lesinski467f1712015-11-16 17:35:44 -080065 ResourceNameRef expected(u"android", ResourceType::kColor, u"foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070066 ResourceNameRef actual;
67 bool create = false;
68 bool privateRef = false;
69 EXPECT_TRUE(ResourceUtils::tryParseReference(u"\t @android:color/foo\n \n\t", &actual,
70 &create, &privateRef));
71 EXPECT_EQ(expected, actual);
72 EXPECT_FALSE(create);
73 EXPECT_FALSE(privateRef);
74}
75
76TEST(ResourceUtilsTest, ParseAutoCreateIdReference) {
Adam Lesinski467f1712015-11-16 17:35:44 -080077 ResourceNameRef expected(u"android", ResourceType::kId, u"foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070078 ResourceNameRef actual;
79 bool create = false;
80 bool privateRef = false;
81 EXPECT_TRUE(ResourceUtils::tryParseReference(u"@+android:id/foo", &actual, &create,
82 &privateRef));
83 EXPECT_EQ(expected, actual);
84 EXPECT_TRUE(create);
85 EXPECT_FALSE(privateRef);
86}
87
88TEST(ResourceUtilsTest, ParsePrivateReference) {
Adam Lesinski467f1712015-11-16 17:35:44 -080089 ResourceNameRef expected(u"android", ResourceType::kId, u"foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070090 ResourceNameRef actual;
91 bool create = false;
92 bool privateRef = false;
93 EXPECT_TRUE(ResourceUtils::tryParseReference(u"@*android:id/foo", &actual, &create,
94 &privateRef));
95 EXPECT_EQ(expected, actual);
96 EXPECT_FALSE(create);
97 EXPECT_TRUE(privateRef);
98}
99
100TEST(ResourceUtilsTest, FailToParseAutoCreateNonIdReference) {
101 bool create = false;
102 bool privateRef = false;
103 ResourceNameRef actual;
104 EXPECT_FALSE(ResourceUtils::tryParseReference(u"@+android:color/foo", &actual, &create,
105 &privateRef));
106}
107
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800108TEST(ResourceUtilsTest, ParseAttributeReferences) {
109 EXPECT_TRUE(ResourceUtils::isAttributeReference(u"?android"));
110 EXPECT_TRUE(ResourceUtils::isAttributeReference(u"?android:foo"));
111 EXPECT_TRUE(ResourceUtils::isAttributeReference(u"?attr/foo"));
112 EXPECT_TRUE(ResourceUtils::isAttributeReference(u"?android:attr/foo"));
113}
114
115TEST(ResourceUtilsTest, FailParseIncompleteReference) {
116 EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?style/foo"));
117 EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?android:style/foo"));
118 EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?android:"));
119 EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?android:attr/"));
120 EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?:attr/"));
121 EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?:attr/foo"));
122 EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?:/"));
123 EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?:/foo"));
124 EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?attr/"));
125 EXPECT_FALSE(ResourceUtils::isAttributeReference(u"?/foo"));
126}
127
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700128TEST(ResourceUtilsTest, ParseStyleParentReference) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800129 const ResourceName kAndroidStyleFooName(u"android", ResourceType::kStyle, u"foo");
130 const ResourceName kStyleFooName({}, ResourceType::kStyle, u"foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131
132 std::string errStr;
133 Maybe<Reference> ref = ResourceUtils::parseStyleParentReference(u"@android:style/foo", &errStr);
134 AAPT_ASSERT_TRUE(ref);
135 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
136
137 ref = ResourceUtils::parseStyleParentReference(u"@style/foo", &errStr);
138 AAPT_ASSERT_TRUE(ref);
139 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
140
141 ref = ResourceUtils::parseStyleParentReference(u"?android:style/foo", &errStr);
142 AAPT_ASSERT_TRUE(ref);
143 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
144
145 ref = ResourceUtils::parseStyleParentReference(u"?style/foo", &errStr);
146 AAPT_ASSERT_TRUE(ref);
147 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
148
149 ref = ResourceUtils::parseStyleParentReference(u"android:style/foo", &errStr);
150 AAPT_ASSERT_TRUE(ref);
151 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
152
153 ref = ResourceUtils::parseStyleParentReference(u"android:foo", &errStr);
154 AAPT_ASSERT_TRUE(ref);
155 EXPECT_EQ(ref.value().name.value(), kAndroidStyleFooName);
156
157 ref = ResourceUtils::parseStyleParentReference(u"foo", &errStr);
158 AAPT_ASSERT_TRUE(ref);
159 EXPECT_EQ(ref.value().name.value(), kStyleFooName);
160}
161
162} // namespace aapt