1 /*
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 
19 #include "test/Test.h"
20 
21 using ::testing::Eq;
22 using ::testing::Optional;
23 
24 namespace aapt {
25 
TEST(ResourceTypeTest,ParseResourceTypes)26 TEST(ResourceTypeTest, ParseResourceTypes) {
27   const ResourceType* type = ParseResourceType("anim");
28   ASSERT_NE(type, nullptr);
29   EXPECT_EQ(*type, ResourceType::kAnim);
30 
31   type = ParseResourceType("animator");
32   ASSERT_NE(type, nullptr);
33   EXPECT_EQ(*type, ResourceType::kAnimator);
34 
35   type = ParseResourceType("array");
36   ASSERT_NE(type, nullptr);
37   EXPECT_EQ(*type, ResourceType::kArray);
38 
39   type = ParseResourceType("attr");
40   ASSERT_NE(type, nullptr);
41   EXPECT_EQ(*type, ResourceType::kAttr);
42 
43   type = ParseResourceType("^attr-private");
44   ASSERT_NE(type, nullptr);
45   EXPECT_EQ(*type, ResourceType::kAttrPrivate);
46 
47   type = ParseResourceType("bool");
48   ASSERT_NE(type, nullptr);
49   EXPECT_EQ(*type, ResourceType::kBool);
50 
51   type = ParseResourceType("color");
52   ASSERT_NE(type, nullptr);
53   EXPECT_EQ(*type, ResourceType::kColor);
54 
55   type = ParseResourceType("configVarying");
56   ASSERT_NE(type, nullptr);
57   EXPECT_EQ(*type, ResourceType::kConfigVarying);
58 
59   type = ParseResourceType("dimen");
60   ASSERT_NE(type, nullptr);
61   EXPECT_EQ(*type, ResourceType::kDimen);
62 
63   type = ParseResourceType("drawable");
64   ASSERT_NE(type, nullptr);
65   EXPECT_EQ(*type, ResourceType::kDrawable);
66 
67   type = ParseResourceType("font");
68   ASSERT_NE(type, nullptr);
69   EXPECT_EQ(*type, ResourceType::kFont);
70 
71   type = ParseResourceType("fraction");
72   ASSERT_NE(type, nullptr);
73   EXPECT_EQ(*type, ResourceType::kFraction);
74 
75   type = ParseResourceType("id");
76   ASSERT_NE(type, nullptr);
77   EXPECT_EQ(*type, ResourceType::kId);
78 
79   type = ParseResourceType("integer");
80   ASSERT_NE(type, nullptr);
81   EXPECT_EQ(*type, ResourceType::kInteger);
82 
83   type = ParseResourceType("interpolator");
84   ASSERT_NE(type, nullptr);
85   EXPECT_EQ(*type, ResourceType::kInterpolator);
86 
87   type = ParseResourceType("layout");
88   ASSERT_NE(type, nullptr);
89   EXPECT_EQ(*type, ResourceType::kLayout);
90 
91   type = ParseResourceType("menu");
92   ASSERT_NE(type, nullptr);
93   EXPECT_EQ(*type, ResourceType::kMenu);
94 
95   type = ParseResourceType("mipmap");
96   ASSERT_NE(type, nullptr);
97   EXPECT_EQ(*type, ResourceType::kMipmap);
98 
99   type = ParseResourceType("navigation");
100   ASSERT_NE(type, nullptr);
101   EXPECT_EQ(*type, ResourceType::kNavigation);
102 
103   type = ParseResourceType("plurals");
104   ASSERT_NE(type, nullptr);
105   EXPECT_EQ(*type, ResourceType::kPlurals);
106 
107   type = ParseResourceType("raw");
108   ASSERT_NE(type, nullptr);
109   EXPECT_EQ(*type, ResourceType::kRaw);
110 
111   type = ParseResourceType("string");
112   ASSERT_NE(type, nullptr);
113   EXPECT_EQ(*type, ResourceType::kString);
114 
115   type = ParseResourceType("style");
116   ASSERT_NE(type, nullptr);
117   EXPECT_EQ(*type, ResourceType::kStyle);
118 
119   type = ParseResourceType("transition");
120   ASSERT_NE(type, nullptr);
121   EXPECT_EQ(*type, ResourceType::kTransition);
122 
123   type = ParseResourceType("xml");
124   ASSERT_NE(type, nullptr);
125   EXPECT_EQ(*type, ResourceType::kXml);
126 
127   type = ParseResourceType("blahaha");
128   EXPECT_EQ(type, nullptr);
129 }
130 
TEST(ResourceTypeTest,ParseResourceNamedType)131 TEST(ResourceTypeTest, ParseResourceNamedType) {
132   auto type = ParseResourceNamedType("anim");
133   EXPECT_THAT(type, Optional(Eq(ResourceNamedType("anim", ResourceType::kAnim))));
134 
135   type = ParseResourceNamedType("layout");
136   EXPECT_THAT(type, Optional(Eq(ResourceNamedType("layout", ResourceType::kLayout))));
137 
138   type = ParseResourceNamedType("layout.2");
139   EXPECT_THAT(type, Optional(Eq(ResourceNamedType("layout.2", ResourceType::kLayout))));
140 
141   type = ParseResourceNamedType("layout.another");
142   EXPECT_THAT(type, Optional(Eq(ResourceNamedType("layout.another", ResourceType::kLayout))));
143 
144   type = ParseResourceNamedType("layout.");
145   EXPECT_THAT(type, Eq(std::nullopt));
146 
147   type = ParseResourceNamedType("layout2");
148   EXPECT_THAT(type, Eq(std::nullopt));
149 
150   type = ParseResourceNamedType("blahaha");
151   EXPECT_THAT(type, Eq(std::nullopt));
152 }
153 
TEST(ResourceTypeTest,ResourceNamedTypeWithDefaultName)154 TEST(ResourceTypeTest, ResourceNamedTypeWithDefaultName) {
155   auto type = ResourceNamedTypeWithDefaultName(ResourceType::kAnim);
156   EXPECT_THAT(type, Eq(ResourceNamedType("anim", ResourceType::kAnim)));
157 
158   type = ResourceNamedTypeWithDefaultName(ResourceType::kAnimator);
159   EXPECT_THAT(type, Eq(ResourceNamedType("animator", ResourceType::kAnimator)));
160 
161   type = ResourceNamedTypeWithDefaultName(ResourceType::kArray);
162   EXPECT_THAT(type, Eq(ResourceNamedType("array", ResourceType::kArray)));
163 
164   type = ResourceNamedTypeWithDefaultName(ResourceType::kAttr);
165   EXPECT_THAT(type, Eq(ResourceNamedType("attr", ResourceType::kAttr)));
166 
167   type = ResourceNamedTypeWithDefaultName(ResourceType::kAttrPrivate);
168   EXPECT_THAT(type, Eq(ResourceNamedType("^attr-private", ResourceType::kAttrPrivate)));
169 
170   type = ResourceNamedTypeWithDefaultName(ResourceType::kBool);
171   EXPECT_THAT(type, Eq(ResourceNamedType("bool", ResourceType::kBool)));
172 
173   type = ResourceNamedTypeWithDefaultName(ResourceType::kColor);
174   EXPECT_THAT(type, Eq(ResourceNamedType("color", ResourceType::kColor)));
175 
176   type = ResourceNamedTypeWithDefaultName(ResourceType::kConfigVarying);
177   EXPECT_THAT(type, Eq(ResourceNamedType("configVarying", ResourceType::kConfigVarying)));
178 
179   type = ResourceNamedTypeWithDefaultName(ResourceType::kDimen);
180   EXPECT_THAT(type, Eq(ResourceNamedType("dimen", ResourceType::kDimen)));
181 
182   type = ResourceNamedTypeWithDefaultName(ResourceType::kDrawable);
183   EXPECT_THAT(type, Eq(ResourceNamedType("drawable", ResourceType::kDrawable)));
184 
185   type = ResourceNamedTypeWithDefaultName(ResourceType::kFont);
186   EXPECT_THAT(type, Eq(ResourceNamedType("font", ResourceType::kFont)));
187 
188   type = ResourceNamedTypeWithDefaultName(ResourceType::kFraction);
189   EXPECT_THAT(type, Eq(ResourceNamedType("fraction", ResourceType::kFraction)));
190 
191   type = ResourceNamedTypeWithDefaultName(ResourceType::kId);
192   EXPECT_THAT(type, Eq(ResourceNamedType("id", ResourceType::kId)));
193 
194   type = ResourceNamedTypeWithDefaultName(ResourceType::kInteger);
195   EXPECT_THAT(type, Eq(ResourceNamedType("integer", ResourceType::kInteger)));
196 
197   type = ResourceNamedTypeWithDefaultName(ResourceType::kInterpolator);
198   EXPECT_THAT(type, Eq(ResourceNamedType("interpolator", ResourceType::kInterpolator)));
199 
200   type = ResourceNamedTypeWithDefaultName(ResourceType::kLayout);
201   EXPECT_THAT(type, Eq(ResourceNamedType("layout", ResourceType::kLayout)));
202 
203   type = ResourceNamedTypeWithDefaultName(ResourceType::kMenu);
204   EXPECT_THAT(type, Eq(ResourceNamedType("menu", ResourceType::kMenu)));
205 
206   type = ResourceNamedTypeWithDefaultName(ResourceType::kMipmap);
207   EXPECT_THAT(type, Eq(ResourceNamedType("mipmap", ResourceType::kMipmap)));
208 
209   type = ResourceNamedTypeWithDefaultName(ResourceType::kNavigation);
210   EXPECT_THAT(type, Eq(ResourceNamedType("navigation", ResourceType::kNavigation)));
211 
212   type = ResourceNamedTypeWithDefaultName(ResourceType::kPlurals);
213   EXPECT_THAT(type, Eq(ResourceNamedType("plurals", ResourceType::kPlurals)));
214 
215   type = ResourceNamedTypeWithDefaultName(ResourceType::kRaw);
216   EXPECT_THAT(type, Eq(ResourceNamedType("raw", ResourceType::kRaw)));
217 
218   type = ResourceNamedTypeWithDefaultName(ResourceType::kString);
219   EXPECT_THAT(type, Eq(ResourceNamedType("string", ResourceType::kString)));
220 
221   type = ResourceNamedTypeWithDefaultName(ResourceType::kStyle);
222   EXPECT_THAT(type, Eq(ResourceNamedType("style", ResourceType::kStyle)));
223 
224   type = ResourceNamedTypeWithDefaultName(ResourceType::kTransition);
225   EXPECT_THAT(type, Eq(ResourceNamedType("transition", ResourceType::kTransition)));
226 
227   type = ResourceNamedTypeWithDefaultName(ResourceType::kXml);
228   EXPECT_THAT(type, Eq(ResourceNamedType("xml", ResourceType::kXml)));
229 }
230 
231 }  // namespace aapt
232