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