diff options
Diffstat (limited to 'sdkmanager')
4 files changed, 188 insertions, 2 deletions
diff --git a/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/AndroidManifestParser.java b/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/AndroidManifestParser.java index 2330020..3b9cd0b 100644 --- a/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/AndroidManifestParser.java +++ b/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/AndroidManifestParser.java @@ -35,6 +35,7 @@ import org.xml.sax.helpers.DefaultHandler; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; @@ -142,7 +143,7 @@ public class AndroidManifestParser { AndroidManifest.ATTRIBUTE_VERSIONCODE, true); if (tmp != null) { try { - mManifestData.mVersionCode = Integer.getInteger(tmp); + mManifestData.mVersionCode = Integer.valueOf(tmp); } catch (NumberFormatException e) { // keep null in the field. } @@ -427,7 +428,6 @@ public class AndroidManifestParser { AndroidManifest.ATTRIBUTE_LARGESCREENS, true /*hasNamespace*/)); } - /** * Searches through the attributes list for a particular one and returns its value. * @param attributes the attribute list to search through @@ -525,6 +525,33 @@ public class AndroidManifestParser { } /** + * Parses the Android Manifest from an {@link InputStream}, and returns a {@link ManifestData} + * object containing the result of the parsing. + * + * @param manifestFileStream the {@link InputStream} representing the manifest file. + * @return + * @throws StreamException + * @throws IOException + * @throws SAXException + * @throws ParserConfigurationException + */ + public static ManifestData parse(InputStream manifestFileStream) + throws SAXException, IOException, StreamException, ParserConfigurationException { + if (manifestFileStream != null) { + SAXParser parser = sParserFactory.newSAXParser(); + + ManifestData data = new ManifestData(); + + ManifestHandler manifestHandler = new ManifestHandler(null, data, null); + parser.parse(new InputSource(manifestFileStream), manifestHandler); + + return data; + } + + return null; + } + + /** * Returns an {@link IAbstractFile} object representing the manifest for the given project. * * @param project The project containing the manifest file. diff --git a/sdkmanager/libs/sdklib/tests/com/android/sdklib/testdata/AndroidManifest-instrumentation.xml b/sdkmanager/libs/sdklib/tests/com/android/sdklib/testdata/AndroidManifest-instrumentation.xml new file mode 100644 index 0000000..b380f96 --- /dev/null +++ b/sdkmanager/libs/sdklib/tests/com/android/sdklib/testdata/AndroidManifest-instrumentation.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="utf-8"?> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.AndroidProject.tests"> + + <!-- + This declares that this app uses the instrumentation test runner targeting + the package of com.android.samples. To run the tests use the command: + "adb shell am instrument -w com.android.samples.tests/android.test.InstrumentationTestRunner" + --> + <instrumentation android:name="android.test.InstrumentationTestRunner" + android:targetPackage="com.android.AndroidProject" + android:label="Sample test for deployment."/> + + <application> + <uses-library android:name="android.test.runner" /> + </application> + +</manifest> diff --git a/sdkmanager/libs/sdklib/tests/com/android/sdklib/testdata/AndroidManifest-testapp.xml b/sdkmanager/libs/sdklib/tests/com/android/sdklib/testdata/AndroidManifest-testapp.xml new file mode 100644 index 0000000..cf58934 --- /dev/null +++ b/sdkmanager/libs/sdklib/tests/com/android/sdklib/testdata/AndroidManifest-testapp.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="utf-8"?> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.testapp" + android:versionCode="42" + android:versionName="1.42"> + <application android:icon="@drawable/icon"> + <activity android:name="com.android.testapp.MainActivity" + android:label="@string/app_name"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" />" + <category android:name="android.intent.category.DEFAULT" /> + </intent-filter> + </activity> + <uses-library android:name="android.test.runner"/> + </application>" + <uses-sdk android:minSdkVersion="7"/> + <supports-screens android:resizeable="true" android:smallScreens="true" + android:anyDensity="true" android:largeScreens="true" android:normalScreens="true"/> + <uses-configuration android:reqKeyboardType="twelvekey" + android:reqTouchScreen="finger" android:reqFiveWayNav="true" android:reqHardKeyboard="true" + android:reqNavigation="nonav"/> + <uses-feature android:glEsVersion="1"/> + <uses-feature android:name="com.foo.feature"/> + <instrumentation android:name="android.test.InstrumentationTestRunner" + android:targetPackage="com.example.android.apis" + android:label="Tests for Api Demos."/> +</manifest>
\ No newline at end of file diff --git a/sdkmanager/libs/sdklib/tests/com/android/sdklib/xml/AndroidManifestParserTest.java b/sdkmanager/libs/sdklib/tests/com/android/sdklib/xml/AndroidManifestParserTest.java new file mode 100644 index 0000000..c2ee943 --- /dev/null +++ b/sdkmanager/libs/sdklib/tests/com/android/sdklib/xml/AndroidManifestParserTest.java @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.sdklib.xml; + +import java.io.InputStream; + +import junit.framework.TestCase; + +/** + * Tests for {@link AndroidManifestParser} + */ +public class AndroidManifestParserTest extends TestCase { + private ManifestData mManifestTestApp; + private ManifestData mManifestInstrumentation; + + private static final String TESTDATA_PATH = + "/com/android/sdklib/testdata/"; //$NON-NLS-1$ + private static final String INSTRUMENTATION_XML = TESTDATA_PATH + + "AndroidManifest-instrumentation.xml"; //$NON-NLS-1$ + private static final String TESTAPP_XML = TESTDATA_PATH + + "AndroidManifest-testapp.xml"; //$NON-NLS-1$ + private static final String PACKAGE_NAME = "com.android.testapp"; //$NON-NLS-1$ + private static final Integer VERSION_CODE = 42; + private static final String ACTIVITY_NAME = "com.android.testapp.MainActivity"; //$NON-NLS-1$ + private static final String LIBRARY_NAME = "android.test.runner"; //$NON-NLS-1$ + private static final String INSTRUMENTATION_NAME = "android.test.InstrumentationTestRunner"; //$NON-NLS-1$ + private static final String INSTRUMENTATION_TARGET = "com.android.AndroidProject"; //$NON-NLS-1$ + + @Override + protected void setUp() throws Exception { + super.setUp(); + + InputStream manifestStream = this.getClass().getResourceAsStream(TESTAPP_XML); + + mManifestTestApp = AndroidManifestParser.parse(manifestStream); + assertNotNull(mManifestTestApp); + + manifestStream = this.getClass().getResourceAsStream(INSTRUMENTATION_XML); + mManifestInstrumentation = AndroidManifestParser.parse(manifestStream); + assertNotNull(mManifestInstrumentation); + } + + public void testGetInstrumentationInformation() { + assertEquals(1, mManifestInstrumentation.getInstrumentations().length); + assertEquals(INSTRUMENTATION_NAME, + mManifestInstrumentation.getInstrumentations()[0].getName()); + assertEquals(INSTRUMENTATION_TARGET, + mManifestInstrumentation.getInstrumentations()[0].getTargetPackage()); + } + + public void testGetPackage() { + assertEquals(PACKAGE_NAME, mManifestTestApp.getPackage()); + } + + public void testGetVersionCode() { + assertEquals(VERSION_CODE, mManifestTestApp.getVersionCode()); + assertEquals(null, mManifestInstrumentation.getVersionCode()); + } + + public void testMinSdkVersion() { + assertEquals("7", mManifestTestApp.getApiLevelRequirement()); + } + + public void testGetActivities() { + assertEquals(1, mManifestTestApp.getActivities().length); + ManifestData.Activity activity = mManifestTestApp.getActivities()[0]; + assertEquals(ACTIVITY_NAME, activity.getName()); + assertTrue(activity.hasAction()); + assertTrue(activity.isHomeActivity()); + assertTrue(activity.hasAction()); + assertEquals(activity, mManifestTestApp.getActivities()[0]); + } + + public void testGetLauncherActivity() { + ManifestData.Activity activity = mManifestTestApp.getLauncherActivity(); + assertEquals(ACTIVITY_NAME, activity.getName()); + assertTrue(activity.hasAction()); + assertTrue(activity.isHomeActivity()); + } + + private void assertEquals(ManifestData.Activity lhs, ManifestData.Activity rhs) { + assertTrue(lhs == rhs || (lhs != null && rhs != null)); + if (lhs != null && rhs != null) { + assertEquals(lhs.getName(), rhs.getName()); + assertEquals(lhs.isExported(), rhs.isExported()); + assertEquals(lhs.hasAction(), rhs.hasAction()); + assertEquals(lhs.isHomeActivity(), rhs.isHomeActivity()); + } + } + + public void testGetUsesLibraries() { + assertEquals(1, mManifestTestApp.getUsesLibraries().length); + assertEquals(LIBRARY_NAME, mManifestTestApp.getUsesLibraries()[0]); + } + + public void testGetPackageName() { + assertEquals(PACKAGE_NAME, mManifestTestApp.getPackage()); + } +} |