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 "util/Util.h"
18 
19 #include <string>
20 
21 #include "test/Test.h"
22 
23 using ::android::StringPiece;
24 using ::testing::Eq;
25 using ::testing::Ne;
26 using ::testing::SizeIs;
27 
28 namespace aapt {
29 
TEST(UtilTest,TrimOnlyWhitespace)30 TEST(UtilTest, TrimOnlyWhitespace) {
31   const StringPiece trimmed = util::TrimWhitespace("\n        ");
32   EXPECT_TRUE(trimmed.empty());
33   EXPECT_THAT(trimmed, SizeIs(0u));
34 }
35 
TEST(UtilTest,StringEndsWith)36 TEST(UtilTest, StringEndsWith) {
37   EXPECT_TRUE(util::EndsWith("hello.xml", ".xml"));
38 }
39 
TEST(UtilTest,StringStartsWith)40 TEST(UtilTest, StringStartsWith) {
41   EXPECT_TRUE(util::StartsWith("hello.xml", "he"));
42 }
43 
TEST(UtilTest,TokenizeInput)44 TEST(UtilTest, TokenizeInput) {
45   auto tokenizer = util::Tokenize(StringPiece("this| is|the|end"), '|');
46   auto iter = tokenizer.begin();
47   ASSERT_THAT(*iter, Eq("this"));
48   ++iter;
49   ASSERT_THAT(*iter, Eq(" is"));
50   ++iter;
51   ASSERT_THAT(*iter, Eq("the"));
52   ++iter;
53   ASSERT_THAT(*iter, Eq("end"));
54   ++iter;
55   ASSERT_THAT(iter, Eq(tokenizer.end()));
56 }
57 
TEST(UtilTest,TokenizeEmptyString)58 TEST(UtilTest, TokenizeEmptyString) {
59   auto tokenizer = util::Tokenize(StringPiece(""), '|');
60   auto iter = tokenizer.begin();
61   ASSERT_THAT(iter, Ne(tokenizer.end()));
62   ASSERT_THAT(*iter, Eq(StringPiece()));
63   ++iter;
64   ASSERT_THAT(iter, Eq(tokenizer.end()));
65 }
66 
TEST(UtilTest,TokenizeAtEnd)67 TEST(UtilTest, TokenizeAtEnd) {
68   auto tokenizer = util::Tokenize(StringPiece("one."), '.');
69   auto iter = tokenizer.begin();
70   ASSERT_THAT(*iter, Eq("one"));
71   ++iter;
72   ASSERT_THAT(iter, Ne(tokenizer.end()));
73   ASSERT_THAT(*iter, Eq(StringPiece()));
74 }
75 
TEST(UtilTest,IsJavaClassName)76 TEST(UtilTest, IsJavaClassName) {
77   EXPECT_TRUE(util::IsJavaClassName("android.test.Class"));
78   EXPECT_TRUE(util::IsJavaClassName("android.test.Class$Inner"));
79   EXPECT_TRUE(util::IsJavaClassName("android_test.test.Class"));
80   EXPECT_TRUE(util::IsJavaClassName("_android_.test._Class_"));
81   EXPECT_TRUE(util::IsJavaClassName("android.test.$Inner"));
82   EXPECT_TRUE(util::IsJavaClassName("android.test.Inner$"));
83   EXPECT_TRUE(util::IsJavaClassName("com.foo.FøøBar"));
84 
85   EXPECT_FALSE(util::IsJavaClassName(".test.Class"));
86   EXPECT_FALSE(util::IsJavaClassName("android"));
87   EXPECT_FALSE(util::IsJavaClassName("FooBar"));
88 }
89 
TEST(UtilTest,IsJavaPackageName)90 TEST(UtilTest, IsJavaPackageName) {
91   EXPECT_TRUE(util::IsJavaPackageName("android"));
92   EXPECT_TRUE(util::IsJavaPackageName("android.test"));
93   EXPECT_TRUE(util::IsJavaPackageName("android.test_thing"));
94   EXPECT_TRUE(util::IsJavaPackageName("_android"));
95   EXPECT_TRUE(util::IsJavaPackageName("android_"));
96   EXPECT_TRUE(util::IsJavaPackageName("android._test"));
97   EXPECT_TRUE(util::IsJavaPackageName("cøm.foo"));
98 
99   EXPECT_FALSE(util::IsJavaPackageName("android."));
100   EXPECT_FALSE(util::IsJavaPackageName(".android"));
101   EXPECT_FALSE(util::IsJavaPackageName(".."));
102 }
103 
TEST(UtilTest,IsAndroidPackageName)104 TEST(UtilTest, IsAndroidPackageName) {
105   EXPECT_TRUE(util::IsAndroidPackageName("android"));
106   EXPECT_TRUE(util::IsAndroidPackageName("android.test"));
107   EXPECT_TRUE(util::IsAndroidPackageName("com.foo"));
108   EXPECT_TRUE(util::IsAndroidPackageName("com.foo.test_thing"));
109   EXPECT_TRUE(util::IsAndroidPackageName("com.foo.testing_thing_"));
110   EXPECT_TRUE(util::IsAndroidPackageName("com.foo.test_99_"));
111 
112   EXPECT_FALSE(util::IsAndroidPackageName("android._test"));
113   EXPECT_FALSE(util::IsAndroidPackageName("com"));
114   EXPECT_FALSE(util::IsAndroidPackageName("_android"));
115   EXPECT_FALSE(util::IsAndroidPackageName("android."));
116   EXPECT_FALSE(util::IsAndroidPackageName(".android"));
117   EXPECT_FALSE(util::IsAndroidPackageName(".."));
118   EXPECT_FALSE(util::IsAndroidPackageName("cøm.foo"));
119 }
120 
TEST(UtilTest,FullyQualifiedClassName)121 TEST(UtilTest, FullyQualifiedClassName) {
122   EXPECT_THAT(util::GetFullyQualifiedClassName("android", ".asdf"), Eq("android.asdf"));
123   EXPECT_THAT(util::GetFullyQualifiedClassName("android", ".a.b"), Eq("android.a.b"));
124   EXPECT_THAT(util::GetFullyQualifiedClassName("android", "a.b"), Eq("a.b"));
125   EXPECT_THAT(util::GetFullyQualifiedClassName("", "a.b"), Eq("a.b"));
126   EXPECT_THAT(util::GetFullyQualifiedClassName("android", "Class"), Eq("android.Class"));
127   EXPECT_FALSE(util::GetFullyQualifiedClassName("", ""));
128   EXPECT_FALSE(util::GetFullyQualifiedClassName("android", "./Apple"));
129 }
130 
TEST(UtilTest,ExtractResourcePathComponents)131 TEST(UtilTest, ExtractResourcePathComponents) {
132   StringPiece prefix, entry, suffix;
133   ASSERT_TRUE(util::ExtractResFilePathParts("res/xml-sw600dp/entry.xml", &prefix, &entry, &suffix));
134   EXPECT_THAT(prefix, Eq("res/xml-sw600dp/"));
135   EXPECT_THAT(entry, Eq("entry"));
136   EXPECT_THAT(suffix, Eq(".xml"));
137 
138   ASSERT_TRUE(util::ExtractResFilePathParts("res/xml-sw600dp/entry.9.png", &prefix, &entry, &suffix));
139   EXPECT_THAT(prefix, Eq("res/xml-sw600dp/"));
140   EXPECT_THAT(entry, Eq("entry"));
141   EXPECT_THAT(suffix, Eq(".9.png"));
142 
143   ASSERT_TRUE(util::ExtractResFilePathParts("res//.", &prefix, &entry, &suffix));
144   EXPECT_THAT(prefix, Eq("res//"));
145   EXPECT_THAT(entry, Eq(""));
146   EXPECT_THAT(suffix, Eq("."));
147 
148   EXPECT_FALSE(util::ExtractResFilePathParts("AndroidManifest.xml", &prefix, &entry, &suffix));
149   EXPECT_FALSE(util::ExtractResFilePathParts("res/.xml", &prefix, &entry, &suffix));
150 }
151 
TEST(UtilTest,VerifyJavaStringFormat)152 TEST(UtilTest, VerifyJavaStringFormat) {
153   ASSERT_TRUE(util::VerifyJavaStringFormat("%09.34f"));
154   ASSERT_TRUE(util::VerifyJavaStringFormat("%9$.34f %8$"));
155   ASSERT_TRUE(util::VerifyJavaStringFormat("%% %%"));
156   ASSERT_FALSE(util::VerifyJavaStringFormat("%09$f %f"));
157   ASSERT_FALSE(util::VerifyJavaStringFormat("%09f %08s"));
158 }
159 
160 }  // namespace aapt
161