summaryrefslogtreecommitdiffstats
path: root/third_party/android_testrunner
diff options
context:
space:
mode:
authorsteveblock@chromium.org <steveblock@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-07 21:12:36 +0000
committersteveblock@chromium.org <steveblock@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-07 21:12:36 +0000
commit5f5ceac025eddcd970426f5814e1fdae9c4d4f70 (patch)
treed6b5edbb5772e8f98b676c672c166a681d983863 /third_party/android_testrunner
parenta3d5570ff21d1a43ae7bce3a91c1274d8b30c4d7 (diff)
downloadchromium_src-5f5ceac025eddcd970426f5814e1fdae9c4d4f70.zip
chromium_src-5f5ceac025eddcd970426f5814e1fdae9c4d4f70.tar.gz
chromium_src-5f5ceac025eddcd970426f5814e1fdae9c4d4f70.tar.bz2
Move third_party/android/testrunner to third_party/android_testrunner
The original intent was to have multiple subdirectories under third_party/android/, but the need for these has not arisen and it potentially adds complexity. Also update the license checker tool to prune this directory, as the code is used only for testing. 'tools/licenses.py scan | grep android' See http://codereview.chromium.org/8322008. Review URL: http://codereview.chromium.org/9622010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@125451 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'third_party/android_testrunner')
-rw-r--r--third_party/android_testrunner/LICENSE202
-rw-r--r--third_party/android_testrunner/README.chromium27
-rw-r--r--third_party/android_testrunner/adb_interface.py507
-rw-r--r--third_party/android_testrunner/am_instrument_parser.py169
-rw-r--r--third_party/android_testrunner/errors.py46
-rw-r--r--third_party/android_testrunner/logger.py96
-rw-r--r--third_party/android_testrunner/run_command.py196
7 files changed, 1243 insertions, 0 deletions
diff --git a/third_party/android_testrunner/LICENSE b/third_party/android_testrunner/LICENSE
new file mode 100644
index 0000000..d645695
--- /dev/null
+++ b/third_party/android_testrunner/LICENSE
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ 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.
diff --git a/third_party/android_testrunner/README.chromium b/third_party/android_testrunner/README.chromium
new file mode 100644
index 0000000..c8a97fa
--- /dev/null
+++ b/third_party/android_testrunner/README.chromium
@@ -0,0 +1,27 @@
+Name: Android Test runner script
+URL: http://source.android.com
+Version: 3.1.4
+License: Apache Version 2.0
+Security Critical: no
+
+Description:
+This package is the scripts used to run the unit test for Android and from
+Android Gingerbread.
+
+Local Modifications:
+This are no local modifications, all files were copied from
+<android_gingerbread_tree>/development/testrunner/
+
+Here is the detail steps
+1. Checkout Android source code
+
+$ repo init -u git://android.git.kernel.org/platform/manifest.git -b gingerbread
+$ repo sync
+$ cd development
+$ git reset --hard 76f63551d36b1de63c63f357e5f0646ed8c306bb
+
+2. Copy the related files from
+ <android_gingerbread_tree>/development/testrunner/
+
+More information can be found in
+http://source.android.com/source/downloading.html
diff --git a/third_party/android_testrunner/adb_interface.py b/third_party/android_testrunner/adb_interface.py
new file mode 100644
index 0000000..1928c73
--- /dev/null
+++ b/third_party/android_testrunner/adb_interface.py
@@ -0,0 +1,507 @@
+#!/usr/bin/python2.4
+#
+#
+# Copyright 2008, 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.
+
+"""Provides an interface to communicate with the device via the adb command.
+
+Assumes adb binary is currently on system path.
+"""
+# Python imports
+import os
+import string
+import time
+
+# local imports
+import am_instrument_parser
+import errors
+import logger
+import run_command
+
+
+class AdbInterface:
+ """Helper class for communicating with Android device via adb."""
+
+ # argument to pass to adb, to direct command to specific device
+ _target_arg = ""
+
+ DEVICE_TRACE_DIR = "/data/test_results/"
+
+ def SetEmulatorTarget(self):
+ """Direct all future commands to the only running emulator."""
+ self._target_arg = "-e"
+
+ def SetDeviceTarget(self):
+ """Direct all future commands to the only connected USB device."""
+ self._target_arg = "-d"
+
+ def SetTargetSerial(self, serial):
+ """Direct all future commands to Android target with the given serial."""
+ self._target_arg = "-s %s" % serial
+
+ def SendCommand(self, command_string, timeout_time=20, retry_count=3):
+ """Send a command via adb.
+
+ Args:
+ command_string: adb command to run
+ timeout_time: number of seconds to wait for command to respond before
+ retrying
+ retry_count: number of times to retry command before raising
+ WaitForResponseTimedOutError
+ Returns:
+ string output of command
+
+ Raises:
+ WaitForResponseTimedOutError if device does not respond to command within time
+ """
+ adb_cmd = "adb %s %s" % (self._target_arg, command_string)
+ logger.SilentLog("about to run %s" % adb_cmd)
+ return run_command.RunCommand(adb_cmd, timeout_time=timeout_time,
+ retry_count=retry_count)
+
+ def SendShellCommand(self, cmd, timeout_time=20, retry_count=3):
+ """Send a adb shell command.
+
+ Args:
+ cmd: adb shell command to run
+ timeout_time: number of seconds to wait for command to respond before
+ retrying
+ retry_count: number of times to retry command before raising
+ WaitForResponseTimedOutError
+
+ Returns:
+ string output of command
+
+ Raises:
+ WaitForResponseTimedOutError: if device does not respond to command
+ """
+ return self.SendCommand("shell %s" % cmd, timeout_time=timeout_time,
+ retry_count=retry_count)
+
+ def BugReport(self, path):
+ """Dumps adb bugreport to the file specified by the path.
+
+ Args:
+ path: Path of the file where adb bugreport is dumped to.
+ """
+ bug_output = self.SendShellCommand("bugreport", timeout_time=60)
+ bugreport_file = open(path, "w")
+ bugreport_file.write(bug_output)
+ bugreport_file.close()
+
+ def Push(self, src, dest):
+ """Pushes the file src onto the device at dest.
+
+ Args:
+ src: file path of host file to push
+ dest: destination absolute file path on device
+ """
+ self.SendCommand("push %s %s" % (src, dest), timeout_time=60)
+
+ def Pull(self, src, dest):
+ """Pulls the file src on the device onto dest on the host.
+
+ Args:
+ src: absolute file path of file on device to pull
+ dest: destination file path on host
+
+ Returns:
+ True if success and False otherwise.
+ """
+ # Create the base dir if it doesn't exist already
+ if not os.path.exists(os.path.dirname(dest)):
+ os.makedirs(os.path.dirname(dest))
+
+ if self.DoesFileExist(src):
+ self.SendCommand("pull %s %s" % (src, dest), timeout_time=60)
+ return True
+ else:
+ logger.Log("ADB Pull Failed: Source file %s does not exist." % src)
+ return False
+
+ def DoesFileExist(self, src):
+ """Checks if the given path exists on device target.
+
+ Args:
+ src: file path to be checked.
+
+ Returns:
+ True if file exists
+ """
+
+ output = self.SendShellCommand("ls %s" % src)
+ error = "No such file or directory"
+
+ if error in output:
+ return False
+ return True
+
+ def EnableAdbRoot(self):
+ """Enable adb root on device."""
+ output = self.SendCommand("root")
+ if "adbd is already running as root" in output:
+ return True
+ elif "restarting adbd as root" in output:
+ # device will disappear from adb, wait for it to come back
+ self.SendCommand("wait-for-device")
+ return True
+ else:
+ logger.Log("Unrecognized output from adb root: %s" % output)
+ return False
+
+ def StartInstrumentationForPackage(
+ self, package_name, runner_name, timeout_time=60*10,
+ no_window_animation=False, instrumentation_args={}):
+ """Run instrumentation test for given package and runner.
+
+ Equivalent to StartInstrumentation, except instrumentation path is
+ separated into its package and runner components.
+ """
+ instrumentation_path = "%s/%s" % (package_name, runner_name)
+ return self.StartInstrumentation(instrumentation_path, timeout_time=timeout_time,
+ no_window_animation=no_window_animation,
+ instrumentation_args=instrumentation_args)
+
+ def StartInstrumentation(
+ self, instrumentation_path, timeout_time=60*10, no_window_animation=False,
+ profile=False, instrumentation_args={}):
+
+ """Runs an instrumentation class on the target.
+
+ Returns a dictionary containing the key value pairs from the
+ instrumentations result bundle and a list of TestResults. Also handles the
+ interpreting of error output from the device and raises the necessary
+ exceptions.
+
+ Args:
+ instrumentation_path: string. It should be the fully classified package
+ name, and instrumentation test runner, separated by "/"
+ e.g. com.android.globaltimelaunch/.GlobalTimeLaunch
+ timeout_time: Timeout value for the am command.
+ no_window_animation: boolean, Whether you want window animations enabled
+ or disabled
+ profile: If True, profiling will be turned on for the instrumentation.
+ instrumentation_args: Dictionary of key value bundle arguments to pass to
+ instrumentation.
+
+ Returns:
+ (test_results, inst_finished_bundle)
+
+ test_results: a list of TestResults
+ inst_finished_bundle (dict): Key/value pairs contained in the bundle that
+ is passed into ActivityManager.finishInstrumentation(). Included in this
+ bundle is the return code of the Instrumentation process, any error
+ codes reported by the activity manager, and any results explicitly added
+ by the instrumentation code.
+
+ Raises:
+ WaitForResponseTimedOutError: if timeout occurred while waiting for
+ response to adb instrument command
+ DeviceUnresponsiveError: if device system process is not responding
+ InstrumentationError: if instrumentation failed to run
+ """
+
+ command_string = self._BuildInstrumentationCommandPath(
+ instrumentation_path, no_window_animation=no_window_animation,
+ profile=profile, raw_mode=True,
+ instrumentation_args=instrumentation_args)
+ logger.Log(command_string)
+ (test_results, inst_finished_bundle) = (
+ am_instrument_parser.ParseAmInstrumentOutput(
+ self.SendShellCommand(command_string, timeout_time=timeout_time,
+ retry_count=2)))
+
+ if "code" not in inst_finished_bundle:
+ raise errors.InstrumentationError("no test results... device setup "
+ "correctly?")
+
+ if inst_finished_bundle["code"] == "0":
+ short_msg_result = "no error message"
+ if "shortMsg" in inst_finished_bundle:
+ short_msg_result = inst_finished_bundle["shortMsg"]
+ logger.Log("Error! Test run failed: %s" % short_msg_result)
+ raise errors.InstrumentationError(short_msg_result)
+
+ if "INSTRUMENTATION_ABORTED" in inst_finished_bundle:
+ logger.Log("INSTRUMENTATION ABORTED!")
+ raise errors.DeviceUnresponsiveError
+
+ return (test_results, inst_finished_bundle)
+
+ def StartInstrumentationNoResults(
+ self, package_name, runner_name, no_window_animation=False,
+ raw_mode=False, instrumentation_args={}):
+ """Runs instrumentation and dumps output to stdout.
+
+ Equivalent to StartInstrumentation, but will dump instrumentation
+ 'normal' output to stdout, instead of parsing return results. Command will
+ never timeout.
+ """
+ adb_command_string = self.PreviewInstrumentationCommand(
+ package_name, runner_name, no_window_animation=no_window_animation,
+ raw_mode=raw_mode, instrumentation_args=instrumentation_args)
+ logger.Log(adb_command_string)
+ run_command.RunCommand(adb_command_string, return_output=False)
+
+ def PreviewInstrumentationCommand(
+ self, package_name, runner_name, no_window_animation=False,
+ raw_mode=False, instrumentation_args={}):
+ """Returns a string of adb command that will be executed."""
+ inst_command_string = self._BuildInstrumentationCommand(
+ package_name, runner_name, no_window_animation=no_window_animation,
+ raw_mode=raw_mode, instrumentation_args=instrumentation_args)
+ command_string = "adb %s shell %s" % (self._target_arg, inst_command_string)
+ return command_string
+
+ def _BuildInstrumentationCommand(
+ self, package, runner_name, no_window_animation=False, profile=False,
+ raw_mode=True, instrumentation_args={}):
+ instrumentation_path = "%s/%s" % (package, runner_name)
+
+ return self._BuildInstrumentationCommandPath(
+ instrumentation_path, no_window_animation=no_window_animation,
+ profile=profile, raw_mode=raw_mode,
+ instrumentation_args=instrumentation_args)
+
+ def _BuildInstrumentationCommandPath(
+ self, instrumentation_path, no_window_animation=False, profile=False,
+ raw_mode=True, instrumentation_args={}):
+ command_string = "am instrument"
+ if no_window_animation:
+ command_string += " --no_window_animation"
+ if profile:
+ self._CreateTraceDir()
+ command_string += (
+ " -p %s/%s.dmtrace" %
+ (self.DEVICE_TRACE_DIR, instrumentation_path.split(".")[-1]))
+
+ for key, value in instrumentation_args.items():
+ command_string += " -e %s '%s'" % (key, value)
+ if raw_mode:
+ command_string += " -r"
+ command_string += " -w %s" % instrumentation_path
+ return command_string
+
+ def _CreateTraceDir(self):
+ ls_response = self.SendShellCommand("ls /data/trace")
+ if ls_response.strip("#").strip(string.whitespace) != "":
+ self.SendShellCommand("create /data/trace", "mkdir /data/trace")
+ self.SendShellCommand("make /data/trace world writeable",
+ "chmod 777 /data/trace")
+
+ def WaitForDevicePm(self, wait_time=120):
+ """Waits for targeted device's package manager to be up.
+
+ Args:
+ wait_time: time in seconds to wait
+
+ Raises:
+ WaitForResponseTimedOutError if wait_time elapses and pm still does not
+ respond.
+ """
+ logger.Log("Waiting for device package manager...")
+ self.SendCommand("wait-for-device")
+ # Now the device is there, but may not be running.
+ # Query the package manager with a basic command
+ try:
+ self._WaitForShellCommandContents("pm path android", "package:",
+ wait_time)
+ except errors.WaitForResponseTimedOutError:
+ raise errors.WaitForResponseTimedOutError(
+ "Package manager did not respond after %s seconds" % wait_time)
+
+ def WaitForInstrumentation(self, package_name, runner_name, wait_time=120):
+ """Waits for given instrumentation to be present on device
+
+ Args:
+ wait_time: time in seconds to wait
+
+ Raises:
+ WaitForResponseTimedOutError if wait_time elapses and instrumentation
+ still not present.
+ """
+ instrumentation_path = "%s/%s" % (package_name, runner_name)
+ logger.Log("Waiting for instrumentation to be present")
+ # Query the package manager
+ try:
+ command = "pm list instrumentation | grep %s" % instrumentation_path
+ self._WaitForShellCommandContents(command, "instrumentation:", wait_time,
+ raise_abort=False)
+ except errors.WaitForResponseTimedOutError :
+ logger.Log(
+ "Could not find instrumentation %s on device. Does the "
+ "instrumentation in test's AndroidManifest.xml match definition"
+ "in test_defs.xml?" % instrumentation_path)
+ raise
+
+ def WaitForProcess(self, name, wait_time=120):
+ """Wait until a process is running on the device.
+
+ Args:
+ name: the process name as it appears in `ps`
+ wait_time: time in seconds to wait
+
+ Raises:
+ WaitForResponseTimedOutError if wait_time elapses and the process is
+ still not running
+ """
+ logger.Log("Waiting for process %s" % name)
+ self.SendCommand("wait-for-device")
+ self._WaitForShellCommandContents("ps", name, wait_time)
+
+ def WaitForProcessEnd(self, name, wait_time=120):
+ """Wait until a process is no longer running on the device.
+
+ Args:
+ name: the process name as it appears in `ps`
+ wait_time: time in seconds to wait
+
+ Raises:
+ WaitForResponseTimedOutError if wait_time elapses and the process is
+ still running
+ """
+ logger.Log("Waiting for process %s to end" % name)
+ self._WaitForShellCommandContents("ps", name, wait_time, invert=True)
+
+ def _WaitForShellCommandContents(self, command, expected, wait_time,
+ raise_abort=True, invert=False):
+ """Wait until the response to a command contains a given output.
+
+ Assumes that a only successful execution of "adb shell <command>" contains
+ the substring expected. Assumes that a device is present.
+
+ Args:
+ command: adb shell command to execute
+ expected: the string that should appear to consider the
+ command successful.
+ wait_time: time in seconds to wait
+ raise_abort: if False, retry when executing the command raises an
+ AbortError, rather than failing.
+ invert: if True, wait until the command output no longer contains the
+ expected contents.
+
+ Raises:
+ WaitForResponseTimedOutError: If wait_time elapses and the command has not
+ returned an output containing expected yet.
+ """
+ # Query the device with the command
+ success = False
+ attempts = 0
+ wait_period = 5
+ while not success and (attempts*wait_period) < wait_time:
+ # assume the command will always contain expected in the success case
+ try:
+ output = self.SendShellCommand(command, retry_count=1)
+ if ((not invert and expected in output)
+ or (invert and expected not in output)):
+ success = True
+ except errors.AbortError, e:
+ if raise_abort:
+ raise
+ # ignore otherwise
+
+ if not success:
+ time.sleep(wait_period)
+ attempts += 1
+
+ if not success:
+ raise errors.WaitForResponseTimedOutError()
+
+ def WaitForBootComplete(self, wait_time=120):
+ """Waits for targeted device's bootcomplete flag to be set.
+
+ Args:
+ wait_time: time in seconds to wait
+
+ Raises:
+ WaitForResponseTimedOutError if wait_time elapses and pm still does not
+ respond.
+ """
+ logger.Log("Waiting for boot complete...")
+ self.SendCommand("wait-for-device")
+ # Now the device is there, but may not be running.
+ # Query the package manager with a basic command
+ boot_complete = False
+ attempts = 0
+ wait_period = 5
+ while not boot_complete and (attempts*wait_period) < wait_time:
+ output = self.SendShellCommand("getprop dev.bootcomplete", retry_count=1)
+ output = output.strip()
+ if output == "1":
+ boot_complete = True
+ else:
+ time.sleep(wait_period)
+ attempts += 1
+ if not boot_complete:
+ raise errors.WaitForResponseTimedOutError(
+ "dev.bootcomplete flag was not set after %s seconds" % wait_time)
+
+ def Sync(self, retry_count=3, runtime_restart=False):
+ """Perform a adb sync.
+
+ Blocks until device package manager is responding.
+
+ Args:
+ retry_count: number of times to retry sync before failing
+ runtime_restart: stop runtime during sync and restart afterwards, useful
+ for syncing system libraries (core, framework etc)
+
+ Raises:
+ WaitForResponseTimedOutError if package manager does not respond
+ AbortError if unrecoverable error occurred
+ """
+ output = ""
+ error = None
+ if runtime_restart:
+ self.SendShellCommand("setprop ro.monkey 1", retry_count=retry_count)
+ # manual rest bootcomplete flag
+ self.SendShellCommand("setprop dev.bootcomplete 0",
+ retry_count=retry_count)
+ self.SendShellCommand("stop", retry_count=retry_count)
+
+ try:
+ output = self.SendCommand("sync", retry_count=retry_count)
+ except errors.AbortError, e:
+ error = e
+ output = e.msg
+ if "Read-only file system" in output:
+ logger.SilentLog(output)
+ logger.Log("Remounting read-only filesystem")
+ self.SendCommand("remount")
+ output = self.SendCommand("sync", retry_count=retry_count)
+ elif "No space left on device" in output:
+ logger.SilentLog(output)
+ logger.Log("Restarting device runtime")
+ self.SendShellCommand("stop", retry_count=retry_count)
+ output = self.SendCommand("sync", retry_count=retry_count)
+ self.SendShellCommand("start", retry_count=retry_count)
+ elif error is not None:
+ # exception occurred that cannot be recovered from
+ raise error
+ logger.SilentLog(output)
+ if runtime_restart:
+ # start runtime and wait till boot complete flag is set
+ self.SendShellCommand("start", retry_count=retry_count)
+ self.WaitForBootComplete()
+ # press the MENU key, this will disable key guard if runtime is started
+ # with ro.monkey set to 1
+ self.SendShellCommand("input keyevent 82", retry_count=retry_count)
+ else:
+ self.WaitForDevicePm()
+ return output
+
+ def GetSerialNumber(self):
+ """Returns the serial number of the targeted device."""
+ return self.SendCommand("get-serialno").strip()
diff --git a/third_party/android_testrunner/am_instrument_parser.py b/third_party/android_testrunner/am_instrument_parser.py
new file mode 100644
index 0000000..4554c4d
--- /dev/null
+++ b/third_party/android_testrunner/am_instrument_parser.py
@@ -0,0 +1,169 @@
+#!/usr/bin/python2.4
+#
+#
+# Copyright 2008, 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.
+
+"""Module that assists in parsing the output of "am instrument" commands run on
+the device."""
+
+import re
+import string
+
+
+def ParseAmInstrumentOutput(result):
+ """Given the raw output of an "am instrument" command that targets and
+ InstrumentationTestRunner, return structured data.
+
+ Args:
+ result (string): Raw output of "am instrument"
+
+ Return
+ (test_results, inst_finished_bundle)
+
+ test_results (list of am_output_parser.TestResult)
+ inst_finished_bundle (dict): Key/value pairs contained in the bundle that is
+ passed into ActivityManager.finishInstrumentation(). Included in this bundle is the return
+ code of the Instrumentation process, any error codes reported by the
+ activity manager, and any results explicity added by the instrumentation
+ code.
+ """
+
+ re_status_code = re.compile(r'INSTRUMENTATION_STATUS_CODE: (?P<status_code>-?\d)$')
+ test_results = []
+ inst_finished_bundle = {}
+
+ result_block_string = ""
+ for line in result.splitlines():
+ result_block_string += line + '\n'
+
+ if "INSTRUMENTATION_STATUS_CODE:" in line:
+ test_result = TestResult(result_block_string)
+ if test_result.GetStatusCode() == 1: # The test started
+ pass
+ elif test_result.GetStatusCode() in [0, -1, -2]:
+ test_results.append(test_result)
+ else:
+ pass
+ result_block_string = ""
+ if "INSTRUMENTATION_CODE:" in line:
+ inst_finished_bundle = _ParseInstrumentationFinishedBundle(result_block_string)
+ result_block_string = ""
+
+ return (test_results, inst_finished_bundle)
+
+
+def _ParseInstrumentationFinishedBundle(result):
+ """Given the raw output of "am instrument" returns a dictionary of the
+ key/value pairs from the bundle passed into
+ ActivityManager.finishInstrumentation().
+
+ Args:
+ result (string): Raw output of "am instrument"
+
+ Return:
+ inst_finished_bundle (dict): Key/value pairs contained in the bundle that is
+ passed into ActivityManager.finishInstrumentation(). Included in this bundle is the return
+ code of the Instrumentation process, any error codes reported by the
+ activity manager, and any results explicity added by the instrumentation
+ code.
+ """
+
+ re_result = re.compile(r'INSTRUMENTATION_RESULT: ([^=]+)=(.*)$')
+ re_code = re.compile(r'INSTRUMENTATION_CODE: (\-?\d)$')
+ result_dict = {}
+ key = ''
+ val = ''
+ last_tag = ''
+
+ for line in result.split('\n'):
+ line = line.strip(string.whitespace)
+ if re_result.match(line):
+ last_tag = 'INSTRUMENTATION_RESULT'
+ key = re_result.search(line).group(1).strip(string.whitespace)
+ if key.startswith('performance.'):
+ key = key[len('performance.'):]
+ val = re_result.search(line).group(2).strip(string.whitespace)
+ try:
+ result_dict[key] = float(val)
+ except ValueError:
+ result_dict[key] = val
+ except TypeError:
+ result_dict[key] = val
+ elif re_code.match(line):
+ last_tag = 'INSTRUMENTATION_CODE'
+ key = 'code'
+ val = re_code.search(line).group(1).strip(string.whitespace)
+ result_dict[key] = val
+ elif 'INSTRUMENTATION_ABORTED:' in line:
+ last_tag = 'INSTRUMENTATION_ABORTED'
+ key = 'INSTRUMENTATION_ABORTED'
+ val = ''
+ result_dict[key] = val
+ elif last_tag == 'INSTRUMENTATION_RESULT':
+ result_dict[key] += '\n' + line
+
+ if not result_dict.has_key('code'):
+ result_dict['code'] = '0'
+ result_dict['shortMsg'] = "No result returned from instrumentation"
+
+ return result_dict
+
+
+class TestResult(object):
+ """A class that contains information about a single test result."""
+
+ def __init__(self, result_block_string):
+ """
+ Args:
+ result_block_string (string): Is a single "block" of output. A single
+ "block" would be either a "test started" status report, or a "test
+ finished" status report.
+ """
+
+ self._test_name = None
+ self._status_code = None
+ self._failure_reason = None
+ self._fields_map = {}
+
+ re_status_code = re.search(r'INSTRUMENTATION_STATUS_CODE: '
+ '(?P<status_code>1|0|-1|-2)', result_block_string)
+ re_fields = re.compile(r'INSTRUMENTATION_STATUS: '
+ '(?P<key>[\w.]+)=(?P<value>.*?)(?=\nINSTRUMENTATION_STATUS)', re.DOTALL)
+
+ for field in re_fields.finditer(result_block_string):
+ key, value = (field.group('key').strip(), field.group('value').strip())
+ if key.startswith('performance.'):
+ key = key[len('performance.'):]
+ self._fields_map[key] = value
+ self._fields_map.setdefault('class')
+ self._fields_map.setdefault('test')
+
+ self._test_name = '%s:%s' % (self._fields_map['class'],
+ self._fields_map['test'])
+ self._status_code = int(re_status_code.group('status_code'))
+ if 'stack' in self._fields_map:
+ self._failure_reason = self._fields_map['stack']
+
+ def GetTestName(self):
+ return self._test_name
+
+ def GetStatusCode(self):
+ return self._status_code
+
+ def GetFailureReason(self):
+ return self._failure_reason
+
+ def GetResultFields(self):
+ return self._fields_map
diff --git a/third_party/android_testrunner/errors.py b/third_party/android_testrunner/errors.py
new file mode 100644
index 0000000..e163dd45
--- /dev/null
+++ b/third_party/android_testrunner/errors.py
@@ -0,0 +1,46 @@
+#!/usr/bin/python2.4
+#
+#
+# Copyright 2008, 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.
+
+"""Defines common exception classes for this package."""
+
+
+class MsgException(Exception):
+ """Generic exception with an optional string msg."""
+ def __init__(self, msg=""):
+ self.msg = msg
+
+
+class WaitForResponseTimedOutError(Exception):
+ """We sent a command and had to wait too long for response."""
+
+
+class DeviceUnresponsiveError(Exception):
+ """Device is unresponsive to command."""
+
+
+class InstrumentationError(Exception):
+ """Failed to run instrumentation."""
+
+
+class AbortError(MsgException):
+ """Generic exception that indicates a fatal error has occurred and program
+ execution should be aborted."""
+
+
+class ParseError(MsgException):
+ """Raised when xml data to parse has unrecognized format."""
+
diff --git a/third_party/android_testrunner/logger.py b/third_party/android_testrunner/logger.py
new file mode 100644
index 0000000..61463a1
--- /dev/null
+++ b/third_party/android_testrunner/logger.py
@@ -0,0 +1,96 @@
+#!/usr/bin/python2.4
+#
+#
+# Copyright 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.
+
+"""Simple logging utility. Dumps log messages to stdout, and optionally, to a
+log file.
+
+Init(path) must be called to enable logging to a file
+"""
+
+import datetime
+
+_LOG_FILE = None
+_verbose = False
+_log_time = True
+
+def Init(log_file_path):
+ """Set the path to the log file"""
+ global _LOG_FILE
+ _LOG_FILE = log_file_path
+ print "Using log file: %s" % _LOG_FILE
+
+def GetLogFilePath():
+ """Returns the path and name of the Log file"""
+ global _LOG_FILE
+ return _LOG_FILE
+
+def Log(new_str):
+ """Appends new_str to the end of _LOG_FILE and prints it to stdout.
+
+ Args:
+ # new_str is a string.
+ new_str: 'some message to log'
+ """
+ msg = _PrependTimeStamp(new_str)
+ print msg
+ _WriteLog(msg)
+
+def _WriteLog(msg):
+ global _LOG_FILE
+ if _LOG_FILE is not None:
+ file_handle = file(_LOG_FILE, 'a')
+ file_handle.write('\n' + str(msg))
+ file_handle.close()
+
+def _PrependTimeStamp(log_string):
+ """Returns the log_string prepended with current timestamp """
+ global _log_time
+ if _log_time:
+ return "# %s: %s" % (datetime.datetime.now().strftime("%m/%d/%y %H:%M:%S"),
+ log_string)
+ else:
+ # timestamp logging disabled
+ return log_string
+
+def SilentLog(new_str):
+ """Silently log new_str. Unless verbose mode is enabled, will log new_str
+ only to the log file
+ Args:
+ # new_str is a string.
+ new_str: 'some message to log'
+ """
+ global _verbose
+ msg = _PrependTimeStamp(new_str)
+ if _verbose:
+ print msg
+ _WriteLog(msg)
+
+def SetVerbose(new_verbose=True):
+ """ Enable or disable verbose logging"""
+ global _verbose
+ _verbose = new_verbose
+
+def SetTimestampLogging(new_timestamp=True):
+ """ Enable or disable outputting a timestamp with each log entry"""
+ global _log_time
+ _log_time = new_timestamp
+
+def main():
+ pass
+
+if __name__ == '__main__':
+ main()
diff --git a/third_party/android_testrunner/run_command.py b/third_party/android_testrunner/run_command.py
new file mode 100644
index 0000000..d398daa
--- /dev/null
+++ b/third_party/android_testrunner/run_command.py
@@ -0,0 +1,196 @@
+#!/usr/bin/python2.4
+#
+#
+# Copyright 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.
+
+# System imports
+import os
+import signal
+import subprocess
+import threading
+import time
+
+# local imports
+import errors
+import logger
+
+_abort_on_error = False
+
+def SetAbortOnError(abort=True):
+ """Sets behavior of RunCommand to throw AbortError if command process returns
+ a negative error code"""
+ global _abort_on_error
+ _abort_on_error = abort
+
+def RunCommand(cmd, timeout_time=None, retry_count=3, return_output=True,
+ stdin_input=None):
+ """Spawn and retry a subprocess to run the given shell command.
+
+ Args:
+ cmd: shell command to run
+ timeout_time: time in seconds to wait for command to run before aborting.
+ retry_count: number of times to retry command
+ return_output: if True return output of command as string. Otherwise,
+ direct output of command to stdout.
+ stdin_input: data to feed to stdin
+ Returns:
+ output of command
+ """
+ result = None
+ while True:
+ try:
+ result = RunOnce(cmd, timeout_time=timeout_time,
+ return_output=return_output, stdin_input=stdin_input)
+ except errors.WaitForResponseTimedOutError:
+ if retry_count == 0:
+ raise
+ retry_count -= 1
+ logger.Log("No response for %s, retrying" % cmd)
+ else:
+ # Success
+ return result
+
+def RunOnce(cmd, timeout_time=None, return_output=True, stdin_input=None):
+ """Spawns a subprocess to run the given shell command.
+
+ Args:
+ cmd: shell command to run
+ timeout_time: time in seconds to wait for command to run before aborting.
+ return_output: if True return output of command as string. Otherwise,
+ direct output of command to stdout.
+ stdin_input: data to feed to stdin
+ Returns:
+ output of command
+ Raises:
+ errors.WaitForResponseTimedOutError if command did not complete within
+ timeout_time seconds.
+ errors.AbortError is command returned error code and SetAbortOnError is on.
+ """
+ start_time = time.time()
+ so = []
+ pid = []
+ global _abort_on_error, error_occurred
+ error_occurred = False
+
+ def Run():
+ global error_occurred
+ if return_output:
+ output_dest = subprocess.PIPE
+ else:
+ # None means direct to stdout
+ output_dest = None
+ if stdin_input:
+ stdin_dest = subprocess.PIPE
+ else:
+ stdin_dest = None
+ pipe = subprocess.Popen(
+ cmd,
+ executable='/bin/bash',
+ stdin=stdin_dest,
+ stdout=output_dest,
+ stderr=subprocess.STDOUT,
+ shell=True)
+ pid.append(pipe.pid)
+ try:
+ output = pipe.communicate(input=stdin_input)[0]
+ if output is not None and len(output) > 0:
+ so.append(output)
+ except OSError, e:
+ logger.SilentLog("failed to retrieve stdout from: %s" % cmd)
+ logger.Log(e)
+ so.append("ERROR")
+ error_occurred = True
+ if pipe.returncode:
+ logger.SilentLog("Error: %s returned %d error code" %(cmd,
+ pipe.returncode))
+ error_occurred = True
+
+ t = threading.Thread(target=Run)
+ t.start()
+
+ break_loop = False
+ while not break_loop:
+ if not t.isAlive():
+ break_loop = True
+
+ # Check the timeout
+ if (not break_loop and timeout_time is not None
+ and time.time() > start_time + timeout_time):
+ try:
+ os.kill(pid[0], signal.SIGKILL)
+ except OSError:
+ # process already dead. No action required.
+ pass
+
+ logger.SilentLog("about to raise a timeout for: %s" % cmd)
+ raise errors.WaitForResponseTimedOutError
+ if not break_loop:
+ time.sleep(0.1)
+
+ t.join()
+ output = "".join(so)
+ if _abort_on_error and error_occurred:
+ raise errors.AbortError(msg=output)
+
+ return "".join(so)
+
+
+def RunHostCommand(binary, valgrind=False):
+ """Run a command on the host (opt using valgrind).
+
+ Runs the host binary and returns the exit code.
+ If successfull, the output (stdout and stderr) are discarded,
+ but printed in case of error.
+ The command can be run under valgrind in which case all the
+ output are always discarded.
+
+ Args:
+ binary: full path of the file to be run.
+ valgrind: If True the command will be run under valgrind.
+
+ Returns:
+ The command exit code (int)
+ """
+ if not valgrind:
+ subproc = subprocess.Popen(binary, stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT)
+ subproc.wait()
+ if subproc.returncode != 0: # In case of error print the output
+ print subproc.communicate()[0]
+ return subproc.returncode
+ else:
+ # Need the full path to valgrind to avoid other versions on the system.
+ subproc = subprocess.Popen(["/usr/bin/valgrind", "--tool=memcheck",
+ "--leak-check=yes", "-q", binary],
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+ # Cannot rely on the retcode of valgrind. Instead look for an empty output.
+ valgrind_out = subproc.communicate()[0].strip()
+ if valgrind_out:
+ print valgrind_out
+ return 1
+ else:
+ return 0
+
+
+def HasValgrind():
+ """Check that /usr/bin/valgrind exists.
+
+ We look for the fullpath to avoid picking up 'alternative' valgrind
+ on the system.
+
+ Returns:
+ True if a system valgrind was found.
+ """
+ return os.path.exists("/usr/bin/valgrind")