blob: 5ebf508807e8a677eee902259b3e7576eed2917c [file] [log] [blame]
Adam Lesinskica5638f2015-10-21 14:42:43 -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 "java/ManifestClassGenerator.h"
Adam Lesinskice5e56e22016-10-21 17:56:45 -070018
Adam Lesinskid0f116b2016-07-08 15:00:32 -070019#include "test/Test.h"
Adam Lesinskica5638f2015-10-21 14:42:43 -070020
21namespace aapt {
22
Adam Lesinskice5e56e22016-10-21 17:56:45 -070023static ::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 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070031
Adam Lesinskice5e56e22016-10-21 17:56:45 -070032 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 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070037
Adam Lesinskice5e56e22016-10-21 17:56:45 -070038 *out_str = out.str();
39 return ::testing::AssertionSuccess();
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070040}
41
Adam Lesinskica5638f2015-10-21 14:42:43 -070042TEST(ManifestClassGeneratorTest, NameIsProperlyGeneratedFromSymbol) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070043 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
44 std::unique_ptr<xml::XmlResource> manifest = test::BuildXmlDom(R"EOF(
Adam Lesinskica5638f2015-10-21 14:42:43 -070045 <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
Adam Lesinskice5e56e22016-10-21 17:56:45 -070052 std::string actual;
53 ASSERT_TRUE(GetManifestClassText(context.get(), manifest.get(), &actual));
Adam Lesinskica5638f2015-10-21 14:42:43 -070054
Adam Lesinskice5e56e22016-10-21 17:56:45 -070055 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);
Adam Lesinskica5638f2015-10-21 14:42:43 -070061
Adam Lesinskice5e56e22016-10-21 17:56:45 -070062 //
63 // Make sure these permissions are in the permission class.
64 //
Adam Lesinskica5638f2015-10-21 14:42:43 -070065
Adam Lesinskice5e56e22016-10-21 17:56:45 -070066 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);
Adam Lesinskica5638f2015-10-21 14:42:43 -070071
Adam Lesinskice5e56e22016-10-21 17:56:45 -070072 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);
Adam Lesinskica5638f2015-10-21 14:42:43 -070077
Adam Lesinskice5e56e22016-10-21 17:56:45 -070078 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);
Adam Lesinskica5638f2015-10-21 14:42:43 -070082
Adam Lesinskice5e56e22016-10-21 17:56:45 -070083 //
84 // Make sure these permissions are in the permission_group class
85 //
Adam Lesinskica5638f2015-10-21 14:42:43 -070086
Adam Lesinskice5e56e22016-10-21 17:56:45 -070087 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);
Adam Lesinskica5638f2015-10-21 14:42:43 -070092}
93
94TEST(ManifestClassGeneratorTest, CommentsAndAnnotationsArePresent) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070095 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
96 std::unique_ptr<xml::XmlResource> manifest = test::BuildXmlDom(R"EOF(
Adam Lesinskica5638f2015-10-21 14:42:43 -070097 <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
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700109 std::string actual;
110 ASSERT_TRUE(GetManifestClassText(context.get(), manifest.get(), &actual));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700111
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700112 const char* expected_access_internet =
113 R"EOF( /**
Adam Lesinskica5638f2015-10-21 14:42:43 -0700114 * Required to access the internet.
115 * Added in API 1.
116 */
Adam Lesinski803c7c82016-04-06 16:09:43 -0700117 public static final String ACCESS_INTERNET="android.permission.ACCESS_INTERNET";)EOF";
Adam Lesinskica5638f2015-10-21 14:42:43 -0700118
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700119 EXPECT_NE(std::string::npos, actual.find(expected_access_internet));
Adam Lesinski803c7c82016-04-06 16:09:43 -0700120
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700121 const char* expected_play_outside =
122 R"EOF( /**
Adam Lesinskica5638f2015-10-21 14:42:43 -0700123 * @deprecated This permission is for playing outside.
124 */
125 @Deprecated
Adam Lesinski803c7c82016-04-06 16:09:43 -0700126 public static final String PLAY_OUTSIDE="android.permission.PLAY_OUTSIDE";)EOF";
Adam Lesinskica5638f2015-10-21 14:42:43 -0700127
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700128 EXPECT_NE(std::string::npos, actual.find(expected_play_outside));
Adam Lesinski803c7c82016-04-06 16:09:43 -0700129
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700130 const char* expected_secret =
131 R"EOF( /**
Adam Lesinskica5638f2015-10-21 14:42:43 -0700132 * This is a private permission for system only!
133 * @hide
Adam Lesinskica5638f2015-10-21 14:42:43 -0700134 */
Adam Lesinskib274e352015-11-06 15:14:35 -0800135 @android.annotation.SystemApi
Adam Lesinski803c7c82016-04-06 16:09:43 -0700136 public static final String SECRET="android.permission.SECRET";)EOF";
137
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700138 EXPECT_NE(std::string::npos, actual.find(expected_secret));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700139}
140
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700141} // namespace aapt