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