blob: fc57ae6fd8ffc8c77b15e2352b90f81fd977f0a3 [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 Lesinskica5638f2015-10-21 14:42:43 -070018#include "test/Builders.h"
19#include "test/Context.h"
20
21#include <gtest/gtest.h>
22
23namespace aapt {
24
25TEST(ManifestClassGeneratorTest, NameIsProperlyGeneratedFromSymbol) {
26 std::unique_ptr<IAaptContext> context = test::ContextBuilder().build();
Adam Lesinski467f1712015-11-16 17:35:44 -080027 std::unique_ptr<xml::XmlResource> manifest = test::buildXmlDom(R"EOF(
Adam Lesinskica5638f2015-10-21 14:42:43 -070028 <manifest xmlns:android="http://schemas.android.com/apk/res/android">
29 <permission android:name="android.permission.ACCESS_INTERNET" />
30 <permission android:name="android.DO_DANGEROUS_THINGS" />
31 <permission android:name="com.test.sample.permission.HUH" />
32 <permission-group android:name="foo.bar.PERMISSION" />
33 </manifest>)EOF");
34
35 std::stringstream out;
36 ManifestClassGenerator generator;
37 ASSERT_TRUE(generator.generate(context->getDiagnostics(), u"android", manifest.get(), &out));
38
39 std::string actual = out.str();
40
Adam Lesinskib274e352015-11-06 15:14:35 -080041 const size_t permissionClassPos = actual.find("public static final class permission {");
42 const size_t permissionGroupClassPos =
43 actual.find("public static final class permission_group {");
Adam Lesinskica5638f2015-10-21 14:42:43 -070044 ASSERT_NE(std::string::npos, permissionClassPos);
45 ASSERT_NE(std::string::npos, permissionGroupClassPos);
46
47 //
48 // Make sure these permissions are in the permission class.
49 //
50
51 size_t pos = actual.find("public static final String ACCESS_INTERNET="
52 "\"android.permission.ACCESS_INTERNET\";");
53 EXPECT_GT(pos, permissionClassPos);
54 EXPECT_LT(pos, permissionGroupClassPos);
55
56 pos = actual.find("public static final String DO_DANGEROUS_THINGS="
57 "\"android.DO_DANGEROUS_THINGS\";");
58 EXPECT_GT(pos, permissionClassPos);
59 EXPECT_LT(pos, permissionGroupClassPos);
60
61 pos = actual.find("public static final String HUH=\"com.test.sample.permission.HUH\";");
62 EXPECT_GT(pos, permissionClassPos);
63 EXPECT_LT(pos, permissionGroupClassPos);
64
65 //
66 // Make sure these permissions are in the permission_group class
67 //
68
69 pos = actual.find("public static final String PERMISSION="
70 "\"foo.bar.PERMISSION\";");
71 EXPECT_GT(pos, permissionGroupClassPos);
72 EXPECT_LT(pos, std::string::npos);
73}
74
75TEST(ManifestClassGeneratorTest, CommentsAndAnnotationsArePresent) {
76 std::unique_ptr<IAaptContext> context = test::ContextBuilder().build();
Adam Lesinski467f1712015-11-16 17:35:44 -080077 std::unique_ptr<xml::XmlResource> manifest = test::buildXmlDom(R"EOF(
Adam Lesinskica5638f2015-10-21 14:42:43 -070078 <manifest xmlns:android="http://schemas.android.com/apk/res/android">
79 <!-- Required to access the internet.
80 Added in API 1. -->
81 <permission android:name="android.permission.ACCESS_INTERNET" />
82 <!-- @deprecated This permission is for playing outside. -->
83 <permission android:name="android.permission.PLAY_OUTSIDE" />
84 <!-- This is a private permission for system only!
85 @hide
86 @SystemApi -->
87 <permission android:name="android.permission.SECRET" />
88 </manifest>)EOF");
89
90 std::stringstream out;
91 ManifestClassGenerator generator;
92 ASSERT_TRUE(generator.generate(context->getDiagnostics(), u"android", manifest.get(), &out));
93
94 std::string actual = out.str();
95
96 EXPECT_NE(std::string::npos, actual.find(
97R"EOF( /**
98 * Required to access the internet.
99 * Added in API 1.
100 */
101 public static final String ACCESS_INTERNET="android.permission.ACCESS_INTERNET";)EOF"));
102
103 EXPECT_NE(std::string::npos, actual.find(
104R"EOF( /**
105 * @deprecated This permission is for playing outside.
106 */
107 @Deprecated
108 public static final String PLAY_OUTSIDE="android.permission.PLAY_OUTSIDE";)EOF"));
109
110 EXPECT_NE(std::string::npos, actual.find(
111R"EOF( /**
112 * This is a private permission for system only!
113 * @hide
114 * @SystemApi
115 */
Adam Lesinskib274e352015-11-06 15:14:35 -0800116 @android.annotation.SystemApi
Adam Lesinskica5638f2015-10-21 14:42:43 -0700117 public static final String SECRET="android.permission.SECRET";)EOF"));
118}
119
120} // namespace aapt