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 = "9";
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=\"9\" />\r\n"
38         + "%s"
39         + "  <application>\r\n"
40         + "%s"
41         + "  </application>\r\n"
42         + "  <instrumentation android:name=\"test.package.TestInstrument\" "
43         + "android:targetPackage=\"test.package\" />\r\n"
44         + "</manifest>";
45     private static final String PERMISSION = "  <uses-permission android:name=\"%s\" />\r\n";
46     private static final String PERMISSION_A = "android.permission.PermissionA";
47     private static final String PERMISSION_B = "android.permission.PermissionB";
48     private static final String ACTIVITY = "    <activity android:name=\"%s\" />\r\n";
49     private static final String ACTIVITY_A = "test.package.ActivityA";
50     private static final String ACTIVITY_B = "test.package.ActivityB";
51 
testManifest()52     public void testManifest() throws Exception {
53         List<String> permissions = new ArrayList<>();
54         permissions.add(PERMISSION_A);
55         permissions.add(PERMISSION_B);
56         List<String> activities = new ArrayList<>();
57         activities.add(ACTIVITY_A);
58         activities.add(ACTIVITY_B);
59         OutputStream output = new OutputStream() {
60             private StringBuilder string = new StringBuilder();
61             @Override
62             public void write(int b) throws IOException {
63                 this.string.append((char) b);
64             }
65 
66             @Override
67             public String toString(){
68                 return this.string.toString();
69             }
70         };
71         ManifestGenerator.generate(output, PACKAGE, INSTRUMENT, MIN_SDK, TARGET_SDK,
72             permissions, activities);
73         String permissionXml = String.format(PERMISSION, PERMISSION_A)
74                 + String.format(PERMISSION, PERMISSION_B);
75         String activityXml = String.format(ACTIVITY, ACTIVITY_A)
76                 + String.format(ACTIVITY, ACTIVITY_B);
77         String expected = String.format(MANIFEST, permissionXml, activityXml);
78         assertEquals("Wrong manifest output", expected, output.toString());
79     }
80 
81 }
82