Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 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" |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 18 | #include "test/Builders.h" |
| 19 | #include "test/Context.h" |
| 20 | |
| 21 | #include <gtest/gtest.h> |
| 22 | |
| 23 | namespace aapt { |
| 24 | |
| 25 | TEST(ManifestClassGeneratorTest, NameIsProperlyGeneratedFromSymbol) { |
| 26 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().build(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 27 | std::unique_ptr<xml::XmlResource> manifest = test::buildXmlDom(R"EOF( |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 28 | <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 Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 41 | 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 Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 44 | 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 | |
| 75 | TEST(ManifestClassGeneratorTest, CommentsAndAnnotationsArePresent) { |
| 76 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().build(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 77 | std::unique_ptr<xml::XmlResource> manifest = test::buildXmlDom(R"EOF( |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 78 | <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( |
| 97 | R"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( |
| 104 | R"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( |
| 111 | R"EOF( /** |
| 112 | * This is a private permission for system only! |
| 113 | * @hide |
| 114 | * @SystemApi |
| 115 | */ |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 116 | @android.annotation.SystemApi |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 117 | public static final String SECRET="android.permission.SECRET";)EOF")); |
| 118 | } |
| 119 | |
| 120 | } // namespace aapt |