1 #!/usr/bin/python2.4 2 # 3 # 4 # Copyright 2009, The Android Open Source Project 5 # 6 # Licensed under the Apache License, Version 2.0 (the "License"); 7 # you may not use this file except in compliance with the License. 8 # You may obtain a copy of the License at 9 # 10 # http://www.apache.org/licenses/LICENSE-2.0 11 # 12 # Unless required by applicable law or agreed to in writing, software 13 # distributed under the License is distributed on an "AS IS" BASIS, 14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 # See the License for the specific language governing permissions and 16 # limitations under the License. 17 18 """Parser for test definition xml files.""" 19 20 # python imports 21 import os 22 23 import errors 24 import logger 25 import run_command 26 import test_suite 27 28 29 class HostTestSuite(test_suite.AbstractTestSuite): 30 """A test suite for running hosttestlib java tests.""" 31 32 _JUNIT_JAR_NAME = "junit.jar" 33 _HOSTTESTLIB_NAME = "hosttestlib.jar" 34 _DDMLIB_NAME = "ddmlib-prebuilt.jar" 35 _lib_names = [_JUNIT_JAR_NAME, _HOSTTESTLIB_NAME, _DDMLIB_NAME] 36 37 _JUNIT_BUILD_PATH = os.path.join("external", "junit") 38 _HOSTTESTLIB_BUILD_PATH = os.path.join("development", "tools", "hosttestlib") 39 _LIB_BUILD_PATHS = [_JUNIT_BUILD_PATH, _HOSTTESTLIB_BUILD_PATH ] 40 41 # main class for running host tests 42 # TODO: should other runners be supported, and make runner an attribute of 43 # the test suite? 44 _TEST_RUNNER = "com.android.hosttest.DeviceTestRunner" 45 46 def __init__(self): 47 test_suite.AbstractTestSuite.__init__(self) 48 self._jar_name = None 49 self._class_name = None 50 51 def GetBuildDependencies(self, options): 52 """Override parent to tag on building host libs.""" 53 return self._LIB_BUILD_PATHS 54 55 def GetClassName(self): 56 return self._class_name 57 58 def SetClassName(self, class_name): 59 self._class_name = class_name 60 return self 61 62 def GetJarName(self): 63 """Returns the name of the host jar that contains the tests.""" 64 return self._jar_name 65 66 def SetJarName(self, jar_name): 67 self._jar_name = jar_name 68 return self 69 70 def Run(self, options, adb_interface): 71 """Runs the host test. 72 73 Results will be displayed on stdout. Assumes 'java' is on system path. 74 75 Args: 76 options: command line options for running host tests. Expected member 77 fields: 78 host_lib_path: path to directory that contains host library files 79 test_data_path: path to directory that contains test data files 80 preview: if true, do not execute, display commands only 81 adb_interface: reference to device under test 82 83 Raises: 84 errors.AbortError: if fatal error occurs 85 """ 86 # get the serial number of the device under test, so it can be passed to 87 # hosttestlib. 88 serial_number = adb_interface.GetSerialNumber() 89 self._lib_names.append(self.GetJarName()) 90 # gather all the host jars that are needed to run tests 91 full_lib_paths = [] 92 for lib in self._lib_names: 93 path = os.path.join(options.host_lib_path, lib) 94 # make sure jar file exists on host 95 if not os.path.exists(path): 96 raise errors.AbortError(msg="Could not find jar %s" % path) 97 full_lib_paths.append(path) 98 99 # java -cp <libs> <runner class> <test suite class> -s <device serial> 100 # -p <test data path> 101 cmd = "java -cp %s %s %s -s %s -p %s" % (":".join(full_lib_paths), 102 self._TEST_RUNNER, 103 self.GetClassName(), serial_number, 104 options.test_data_path) 105 logger.Log(cmd) 106 if not options.preview: 107 run_command.RunOnce(cmd, return_output=False) 108