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