1 /* 2 * Copyright (C) 2007 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.ide.common.xml; 18 19 import com.android.ide.common.xml.ManifestData.UsesFeature; 20 import com.android.ide.common.xml.ManifestData.UsesLibrary; 21 import com.android.resources.Keyboard; 22 import com.android.resources.Navigation; 23 import com.android.resources.TouchScreen; 24 25 import java.io.InputStream; 26 27 import junit.framework.TestCase; 28 29 /** 30 * Tests for {@link AndroidManifestParser} 31 */ 32 public class AndroidManifestParserTest extends TestCase { 33 private ManifestData mManifestTestApp; 34 private ManifestData mManifestInstrumentation; 35 36 private static final String TESTDATA_PATH = 37 "/com/android/sdklib/testdata/"; //$NON-NLS-1$ 38 private static final String INSTRUMENTATION_XML = TESTDATA_PATH + 39 "AndroidManifest-instrumentation.xml"; //$NON-NLS-1$ 40 private static final String TESTAPP_XML = TESTDATA_PATH + 41 "AndroidManifest-testapp.xml"; //$NON-NLS-1$ 42 private static final String PACKAGE_NAME = "com.android.testapp"; //$NON-NLS-1$ 43 private static final Integer VERSION_CODE = 42; 44 private static final String ACTIVITY_NAME = "com.android.testapp.MainActivity"; //$NON-NLS-1$ 45 private static final String LIBRARY_NAME = "android.test.runner"; //$NON-NLS-1$ 46 private static final String LIBRARY_NAME2 = "android.test.runner2"; //$NON-NLS-1$ 47 private static final String FEATURE_NAME = "com.foo.feature"; //$NON-NLS-1$ 48 private static final String INSTRUMENTATION_NAME = "android.test.InstrumentationTestRunner"; //$NON-NLS-1$ 49 private static final String INSTRUMENTATION_TARGET = "com.android.AndroidProject"; //$NON-NLS-1$ 50 51 @Override setUp()52 protected void setUp() throws Exception { 53 super.setUp(); 54 55 InputStream manifestStream = this.getClass().getResourceAsStream(TESTAPP_XML); 56 57 mManifestTestApp = AndroidManifestParser.parse(manifestStream); 58 assertNotNull(mManifestTestApp); 59 60 manifestStream = this.getClass().getResourceAsStream(INSTRUMENTATION_XML); 61 mManifestInstrumentation = AndroidManifestParser.parse(manifestStream); 62 assertNotNull(mManifestInstrumentation); 63 } 64 testGetInstrumentationInformation()65 public void testGetInstrumentationInformation() { 66 assertEquals(1, mManifestInstrumentation.getInstrumentations().length); 67 assertEquals(INSTRUMENTATION_NAME, 68 mManifestInstrumentation.getInstrumentations()[0].getName()); 69 assertEquals(INSTRUMENTATION_TARGET, 70 mManifestInstrumentation.getInstrumentations()[0].getTargetPackage()); 71 } 72 testGetPackage()73 public void testGetPackage() { 74 assertEquals(PACKAGE_NAME, mManifestTestApp.getPackage()); 75 } 76 testGetVersionCode()77 public void testGetVersionCode() { 78 assertEquals(VERSION_CODE, mManifestTestApp.getVersionCode()); 79 assertEquals(null, mManifestInstrumentation.getVersionCode()); 80 } 81 testMinSdkVersion()82 public void testMinSdkVersion() { 83 assertEquals(7, mManifestTestApp.getMinSdkVersion()); 84 assertEquals(8, mManifestTestApp.getTargetSdkVersion()); 85 86 assertEquals("foo", mManifestInstrumentation.getMinSdkVersionString()); 87 assertEquals(ManifestData.MIN_SDK_CODENAME, mManifestInstrumentation.getMinSdkVersion()); 88 } 89 testGetActivities()90 public void testGetActivities() { 91 assertEquals(1, mManifestTestApp.getActivities().length); 92 ManifestData.Activity activity = mManifestTestApp.getActivities()[0]; 93 assertEquals(ACTIVITY_NAME, activity.getName()); 94 assertTrue(activity.hasAction()); 95 assertTrue(activity.isHomeActivity()); 96 assertTrue(activity.hasAction()); 97 assertEquals(activity, mManifestTestApp.getActivities()[0]); 98 } 99 testGetLauncherActivity()100 public void testGetLauncherActivity() { 101 ManifestData.Activity activity = mManifestTestApp.getLauncherActivity(); 102 assertEquals(ACTIVITY_NAME, activity.getName()); 103 assertTrue(activity.hasAction()); 104 assertTrue(activity.isHomeActivity()); 105 } 106 testSupportsScreen()107 public void testSupportsScreen() { 108 ManifestData.SupportsScreens supportsScreens = 109 mManifestTestApp.getSupportsScreensFromManifest(); 110 111 assertNotNull(supportsScreens); 112 assertEquals(Boolean.TRUE, supportsScreens.getAnyDensity()); 113 assertEquals(Boolean.TRUE, supportsScreens.getResizeable()); 114 assertEquals(Boolean.TRUE, supportsScreens.getSmallScreens()); 115 assertEquals(Boolean.TRUE, supportsScreens.getNormalScreens()); 116 assertEquals(Boolean.TRUE, supportsScreens.getLargeScreens()); 117 } 118 testUsesConfiguration()119 public void testUsesConfiguration() { 120 ManifestData.UsesConfiguration usesConfig = mManifestTestApp.getUsesConfiguration(); 121 122 assertNotNull(usesConfig); 123 assertEquals(Boolean.TRUE, usesConfig.getReqFiveWayNav()); 124 assertEquals(Navigation.NONAV, usesConfig.getReqNavigation()); 125 assertEquals(Boolean.TRUE, usesConfig.getReqHardKeyboard()); 126 assertEquals(Keyboard.TWELVEKEY, usesConfig.getReqKeyboardType()); 127 assertEquals(TouchScreen.FINGER, usesConfig.getReqTouchScreen()); 128 } 129 assertEquals(ManifestData.Activity lhs, ManifestData.Activity rhs)130 private void assertEquals(ManifestData.Activity lhs, ManifestData.Activity rhs) { 131 assertTrue(lhs == rhs || (lhs != null && rhs != null)); 132 if (lhs != null && rhs != null) { 133 assertEquals(lhs.getName(), rhs.getName()); 134 assertEquals(lhs.isExported(), rhs.isExported()); 135 assertEquals(lhs.hasAction(), rhs.hasAction()); 136 assertEquals(lhs.isHomeActivity(), rhs.isHomeActivity()); 137 } 138 } 139 testGetUsesLibraries()140 public void testGetUsesLibraries() { 141 UsesLibrary[] libraries = mManifestTestApp.getUsesLibraries(); 142 143 assertEquals(2, libraries.length); 144 assertEquals(LIBRARY_NAME, libraries[0].getName()); 145 assertEquals(Boolean.FALSE, libraries[0].getRequired()); 146 assertEquals(LIBRARY_NAME2, libraries[1].getName()); 147 assertEquals(Boolean.TRUE, libraries[1].getRequired()); 148 } 149 testGetUsesFeatures()150 public void testGetUsesFeatures() { 151 UsesFeature[] features = mManifestTestApp.getUsesFeatures(); 152 153 assertEquals(2, features.length); 154 assertEquals(0x00020001, features[0].mGlEsVersion); 155 assertEquals(Boolean.TRUE, features[0].getRequired()); 156 assertEquals(FEATURE_NAME, features[1].getName()); 157 assertEquals(Boolean.TRUE, features[1].getRequired()); 158 } 159 testGetPackageName()160 public void testGetPackageName() { 161 assertEquals(PACKAGE_NAME, mManifestTestApp.getPackage()); 162 } 163 } 164