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