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 package com.android.compatibility.common.generator; 18 19 import java.io.IOException; 20 import java.io.OutputStream; 21 import java.nio.charset.StandardCharsets; 22 import java.util.ArrayList; 23 import java.util.List; 24 25 import junit.framework.TestCase; 26 27 /** Unit tests for {@link ManifestGenerator}. */ 28 public class ManifestGeneratorTest extends TestCase { 29 30 private static final String PACKAGE = "test.package"; 31 private static final String INSTRUMENT = "test.package.TestInstrument"; 32 private static final String MIN_SDK = "8"; 33 private static final String TARGET_SDK = "17"; 34 private static final String MANIFEST = "<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>\r\n" 35 + "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\" " 36 + "package=\"test.package\">\r\n" 37 + " <uses-sdk android:minSdkVersion=\"8\" android:targetSdkVersion=\"17\" />\r\n" 38 + "%s" 39 + " <application>\r\n" 40 + "%s" 41 + "%s" 42 + " </application>\r\n" 43 + " <instrumentation android:name=\"test.package.TestInstrument\" " 44 + "android:targetPackage=\"test.package\" />\r\n" 45 + "</manifest>"; 46 private static final String PERMISSION = " <uses-permission android:name=\"%s\" />\r\n"; 47 private static final String PERMISSION_A = "android.permission.PermissionA"; 48 private static final String PERMISSION_B = "android.permission.PermissionB"; 49 private static final String ACTIVITY = " <activity android:name=\"%s\" />\r\n"; 50 private static final String ACTIVITY_A = "test.package.ActivityA"; 51 private static final String ACTIVITY_B = "test.package.ActivityB"; 52 private static final String USES_LIBRARY = " <uses-library android:name=\"%s\" />\r\n"; 53 private static final String LIBRARY = "test.runner.library"; 54 testManifest()55 public void testManifest() throws Exception { 56 List<String> permissions = new ArrayList<>(); 57 permissions.add(PERMISSION_A); 58 permissions.add(PERMISSION_B); 59 List<String> activities = new ArrayList<>(); 60 activities.add(ACTIVITY_A); 61 activities.add(ACTIVITY_B); 62 List<String> libraries = new ArrayList<>(); 63 libraries.add(LIBRARY); 64 OutputStream output = new OutputStream() { 65 private StringBuilder string = new StringBuilder(); 66 @Override 67 public void write(int b) throws IOException { 68 this.string.append((char) b); 69 } 70 71 @Override 72 public String toString(){ 73 return this.string.toString(); 74 } 75 }; 76 ManifestGenerator.generate(output, PACKAGE, INSTRUMENT, MIN_SDK, TARGET_SDK, 77 permissions, activities, libraries); 78 String permissionXml = String.format(PERMISSION, PERMISSION_A) 79 + String.format(PERMISSION, PERMISSION_B); 80 String activityXml = String.format(ACTIVITY, ACTIVITY_A) 81 + String.format(ACTIVITY, ACTIVITY_B); 82 String libraryXml = String.format(USES_LIBRARY, LIBRARY); 83 String expected = String.format(MANIFEST, permissionXml, libraryXml, activityXml); 84 assertEquals("Wrong manifest output", expected, output.toString()); 85 } 86 87 } 88