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 "java/ManifestClassGenerator.h"
18
19 #include "test/Test.h"
20
21 namespace aapt {
22
GetManifestClassText(IAaptContext * context,xml::XmlResource * res,std::string * out_str)23 static ::testing::AssertionResult GetManifestClassText(IAaptContext* context,
24 xml::XmlResource* res,
25 std::string* out_str) {
26 std::unique_ptr<ClassDefinition> manifest_class =
27 GenerateManifestClass(context->GetDiagnostics(), res);
28 if (!manifest_class) {
29 return ::testing::AssertionFailure() << "manifest_class == nullptr";
30 }
31
32 std::stringstream out;
33 if (!manifest_class->WriteJavaFile(manifest_class.get(), "android", true,
34 &out)) {
35 return ::testing::AssertionFailure() << "failed to write java file";
36 }
37
38 *out_str = out.str();
39 return ::testing::AssertionSuccess();
40 }
41
TEST(ManifestClassGeneratorTest,NameIsProperlyGeneratedFromSymbol)42 TEST(ManifestClassGeneratorTest, NameIsProperlyGeneratedFromSymbol) {
43 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
44 std::unique_ptr<xml::XmlResource> manifest = test::BuildXmlDom(R"EOF(
45 <manifest xmlns:android="http://schemas.android.com/apk/res/android">
46 <permission android:name="android.permission.ACCESS_INTERNET" />
47 <permission android:name="android.DO_DANGEROUS_THINGS" />
48 <permission android:name="com.test.sample.permission.HUH" />
49 <permission-group android:name="foo.bar.PERMISSION" />
50 </manifest>)EOF");
51
52 std::string actual;
53 ASSERT_TRUE(GetManifestClassText(context.get(), manifest.get(), &actual));
54
55 const size_t permission_class_pos =
56 actual.find("public static final class permission {");
57 const size_t permission_croup_class_pos =
58 actual.find("public static final class permission_group {");
59 ASSERT_NE(std::string::npos, permission_class_pos);
60 ASSERT_NE(std::string::npos, permission_croup_class_pos);
61
62 //
63 // Make sure these permissions are in the permission class.
64 //
65
66 size_t pos = actual.find(
67 "public static final String ACCESS_INTERNET="
68 "\"android.permission.ACCESS_INTERNET\";");
69 EXPECT_GT(pos, permission_class_pos);
70 EXPECT_LT(pos, permission_croup_class_pos);
71
72 pos = actual.find(
73 "public static final String DO_DANGEROUS_THINGS="
74 "\"android.DO_DANGEROUS_THINGS\";");
75 EXPECT_GT(pos, permission_class_pos);
76 EXPECT_LT(pos, permission_croup_class_pos);
77
78 pos = actual.find(
79 "public static final String HUH=\"com.test.sample.permission.HUH\";");
80 EXPECT_GT(pos, permission_class_pos);
81 EXPECT_LT(pos, permission_croup_class_pos);
82
83 //
84 // Make sure these permissions are in the permission_group class
85 //
86
87 pos = actual.find(
88 "public static final String PERMISSION="
89 "\"foo.bar.PERMISSION\";");
90 EXPECT_GT(pos, permission_croup_class_pos);
91 EXPECT_LT(pos, std::string::npos);
92 }
93
94 TEST(ManifestClassGeneratorTest, CommentsAndAnnotationsArePresent) {
95 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
96 std::unique_ptr<xml::XmlResource> manifest = test::BuildXmlDom(R"EOF(
97 <manifest xmlns:android="http://schemas.android.com/apk/res/android">
98 <!-- Required to access the internet.
99 Added in API 1. -->
100 <permission android:name="android.permission.ACCESS_INTERNET" />
101 <!-- @deprecated This permission is for playing outside. -->
102 <permission android:name="android.permission.PLAY_OUTSIDE" />
103 <!-- This is a private permission for system only!
104 @hide
105 @SystemApi -->
106 <permission android:name="android.permission.SECRET" />
107 </manifest>)EOF");
108
109 std::string actual;
110 ASSERT_TRUE(GetManifestClassText(context.get(), manifest.get(), &actual));
111
112 const char* expected_access_internet =
113 R"EOF( /**
114 * Required to access the internet.
115 * Added in API 1.
116 */
117 public static final String ACCESS_INTERNET="android.permission.ACCESS_INTERNET";)EOF";
118
119 EXPECT_NE(std::string::npos, actual.find(expected_access_internet));
120
121 const char* expected_play_outside =
122 R"EOF( /**
123 * @deprecated This permission is for playing outside.
124 */
125 @Deprecated
126 public static final String PLAY_OUTSIDE="android.permission.PLAY_OUTSIDE";)EOF";
127
128 EXPECT_NE(std::string::npos, actual.find(expected_play_outside));
129
130 const char* expected_secret =
131 R"EOF( /**
132 * This is a private permission for system only!
133 * @hide
134 */
135 @android.annotation.SystemApi
136 public static final String SECRET="android.permission.SECRET";)EOF";
137
138 EXPECT_NE(std::string::npos, actual.find(expected_secret));
139 }
140
141 } // namespace aapt
142