From 34fb7f6b0c1e8375f168e5a9d4e5c7a93d4ecd7f Mon Sep 17 00:00:00 2001 From: Brett Chabot Date: Wed, 21 Oct 2009 11:16:42 -0700 Subject: [PATCH] Add ability to retrieve instrumentations from android_manifest parser. Also add a simple unit test for the parser. --- testrunner/android_manifest.py | 18 ++++++++++ testrunner/tests/AndroidManifest.xml | 30 ++++++++++++++++ testrunner/tests/android_manifest_tests.py | 42 ++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 testrunner/tests/AndroidManifest.xml create mode 100755 testrunner/tests/android_manifest_tests.py diff --git a/testrunner/android_manifest.py b/testrunner/android_manifest.py index 5406f56ee..5825118f1 100644 --- a/testrunner/android_manifest.py +++ b/testrunner/android_manifest.py @@ -36,6 +36,10 @@ class AndroidManifest(object): if app_path: self.ParseManifest(app_path) + def GetAppPath(self): + """Retrieve file system path to this manifest file's directory.""" + return self._app_path + def GetPackageName(self): """Retrieve package name defined at . @@ -56,6 +60,7 @@ class AndroidManifest(object): IOError: AndroidManifest.xml cannot be found at given path, or cannot be opened for reading """ + self._app_path = app_path self._manifest_path = os.path.join(app_path, self.FILENAME) self._dom = xml.dom.minidom.parse(self._manifest_path) @@ -76,6 +81,18 @@ class AndroidManifest(object): uses_sdk_element.setAttribute('android:minSdkVersion', min_sdk_version) self._SaveXml() + def GetInstrumentationNames(self): + """Get the instrumentation names from manifest. + + Returns: + list of names, might be empty + """ + instr_elements = self._dom.getElementsByTagName('instrumentation') + instrs = [] + for element in instr_elements: + instrs.append(element.getAttribute('android:name')) + return instrs + def _GetManifestElement(self): """Retrieve the root manifest element. @@ -90,3 +107,4 @@ class AndroidManifest(object): def _SaveXml(self): """Saves the manifest to disk.""" self._dom.writexml(open(self._manifest_path, mode='w'), encoding='utf-8') + diff --git a/testrunner/tests/AndroidManifest.xml b/testrunner/tests/AndroidManifest.xml new file mode 100644 index 000000000..e867f730f --- /dev/null +++ b/testrunner/tests/AndroidManifest.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + diff --git a/testrunner/tests/android_manifest_tests.py b/testrunner/tests/android_manifest_tests.py new file mode 100755 index 000000000..2817726c4 --- /dev/null +++ b/testrunner/tests/android_manifest_tests.py @@ -0,0 +1,42 @@ +#!/usr/bin/python2.4 +# +# +# Copyright 2009, 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. + +import sys +import unittest +sys.path.append('../..') + +from testrunner import android_manifest + + +class AndroidManifestTest(unittest.TestCase): + """Unit tests for AndroidManifest.""" + + def setUp(self): + """Create android_mainfest for testing from sample file.""" + self._manifest = android_manifest.AndroidManifest(app_path='.') + + def testGetPackageName(self): + self.assertEquals('com.example.android.tests', + self._manifest.GetPackageName()) + + def testGetInstrumentationNames(self): + self.assertEquals(['android.test.InstrumentationTestRunner'], + self._manifest.GetInstrumentationNames()) + + +if __name__ == '__main__': + unittest.main()