Home | History | Annotate | Download | only in test_defs
      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.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   _DDMLIB_BUILD_PATH = os.path.join("development", "tools", "ddms", "libs",
     40                                     "ddmlib")
     41   _LIB_BUILD_PATHS = [_JUNIT_BUILD_PATH, _HOSTTESTLIB_BUILD_PATH,
     42                       _DDMLIB_BUILD_PATH]
     43 
     44   # main class for running host tests
     45   # TODO: should other runners be supported, and make runner an attribute of
     46   # the test suite?
     47   _TEST_RUNNER = "com.android.hosttest.DeviceTestRunner"
     48 
     49   def __init__(self):
     50     test_suite.AbstractTestSuite.__init__(self)
     51     self._jar_name = None
     52     self._class_name = None
     53 
     54   def GetBuildDependencies(self, options):
     55     """Override parent to tag on building host libs."""
     56     return self._LIB_BUILD_PATHS
     57 
     58   def GetClassName(self):
     59     return self._class_name
     60 
     61   def SetClassName(self, class_name):
     62     self._class_name = class_name
     63     return self
     64 
     65   def GetJarName(self):
     66     """Returns the name of the host jar that contains the tests."""
     67     return self._jar_name
     68 
     69   def SetJarName(self, jar_name):
     70     self._jar_name = jar_name
     71     return self
     72 
     73   def Run(self, options, adb_interface):
     74     """Runs the host test.
     75 
     76     Results will be displayed on stdout. Assumes 'java' is on system path.
     77 
     78     Args:
     79       options: command line options for running host tests. Expected member
     80         fields:
     81         host_lib_path: path to directory that contains host library files
     82         test_data_path: path to directory that contains test data files
     83         preview: if true, do not execute, display commands only
     84       adb_interface: reference to device under test
     85 
     86     Raises:
     87       errors.AbortError: if fatal error occurs
     88     """
     89     # get the serial number of the device under test, so it can be passed to
     90     # hosttestlib.
     91     serial_number = adb_interface.GetSerialNumber()
     92     self._lib_names.append(self.GetJarName())
     93     # gather all the host jars that are needed to run tests
     94     full_lib_paths = []
     95     for lib in self._lib_names:
     96       path = os.path.join(options.host_lib_path, lib)
     97       # make sure jar file exists on host
     98       if not os.path.exists(path):
     99         raise errors.AbortError(msg="Could not find jar %s" % path)
    100       full_lib_paths.append(path)
    101 
    102     # java -cp <libs> <runner class> <test suite class> -s <device serial>
    103     # -p <test data path>
    104     cmd = "java -cp %s %s %s -s %s -p %s" % (":".join(full_lib_paths),
    105                                              self._TEST_RUNNER,
    106                                              self.GetClassName(), serial_number,
    107                                              options.test_data_path)
    108     logger.Log(cmd)
    109     if not options.preview:
    110       run_command.RunOnce(cmd, return_output=False)
    111