def run_tests(adb_location, serial_number,
        package_name, test_runner_name, enable_coverage):
    u'''
    Run tests and output result as XML.

    Parameters:
        adb_location : Location of adb.
        serial_number : Serial number of device or emulator. None is OK.
        test_runner_name : Name of test runner. None is OK.
        enable_coverage : Whether coverage measuring is enabled.
    Return:
        ElementTree of result. None if tests did not run.
    '''
    AndroidDebugBridge.init(False)

    connecting_event = DeviceConnectingEvent(serial_number)
    AndroidDebugBridge.addDeviceChangeListener(connecting_event)

    debug_bridge = AndroidDebugBridge.createBridge(adb_location, False)
    connecting_event.wait()

    if not debug_bridge.isConnected():
        print >>sys.stderr, u'bridge is not connected.'
        return None

    device = get_device(serial_number, debug_bridge)

    if not device:
        print >>sys.stderr, u'There is not device.'
        return None

    test_runner = RemoteAndroidTestRunner(package_name, test_runner_name, device)
    test_runner.setCoverage(enable_coverage)

    formatter = TestResultXmlFormatter(device.getProperties())
    test_runner.run(formatter)

    return formatter.get_result()
def run_tests(adb_location, serial_number,
        package_name, test_runner_name, enable_coverage, coverage_file):
    u'''
    Run tests and output result as XML.

    Parameters:
        adb_location : Location of adb.
        serial_number : Serial number of device or emulator. None is OK.
        package_name : Package name of test application package.
        test_runner_name : Name of test runner. None is OK.
        enable_coverage : Whether coverage measuring is enabled.
        coverage_file : Path of file that will be saved coverage result.
            None is OK.
    Return:
        ElementTree of result. None if tests did not run.
    '''
    AndroidDebugBridge.init(False)

    try:
        # Connect to Android debug bridge.
        connecting_event = DeviceConnectingEvent(serial_number)
        AndroidDebugBridge.addDeviceChangeListener(connecting_event)
        debug_bridge = AndroidDebugBridge.createBridge(adb_location, False)

        # Wait until device is connected.
        connecting_event.wait()
        if not debug_bridge.isConnected():
            print >>sys.stderr, u'bridge is not connected.'
            return None

        # Get the device.
        device = get_device(serial_number, debug_bridge)
        if not device:
            print >>sys.stderr, u'There is not device.'
            return None

        # Run the test runner on the device.
        test_runner = RemoteAndroidTestRunner(
            package_name, test_runner_name, device)
        test_runner.setCoverage(enable_coverage)
        if enable_coverage and coverage_file:
            test_runner.addInstrumentationArg('coverageFile', coverage_file)
        formatter = TestResultXmlFormatter(device.getProperties())
        test_runner.run([formatter])

        return formatter.get_result()
    finally:
        # Disconnect from Android debug bridge.
        AndroidDebugBridge.terminate()