michael@0: # Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: # Use of this source code is governed by a BSD-style license that can be michael@0: # found in the LICENSE file. michael@0: michael@0: michael@0: import os michael@0: import shlex michael@0: import sys michael@0: import tempfile michael@0: import time michael@0: michael@0: import android_commands michael@0: import constants michael@0: from test_package import TestPackage michael@0: from pylib import pexpect michael@0: michael@0: class TestPackageApk(TestPackage): michael@0: """A helper class for running APK-based native tests. michael@0: michael@0: Args: michael@0: adb: ADB interface the tests are using. michael@0: device: Device to run the tests. michael@0: test_suite: A specific test suite to run, empty to run all. michael@0: timeout: Timeout for each test. michael@0: rebaseline: Whether or not to run tests in isolation and update the filter. michael@0: performance_test: Whether or not performance test(s). michael@0: cleanup_test_files: Whether or not to cleanup test files on device. michael@0: tool: Name of the Valgrind tool. michael@0: dump_debug_info: A debug_info object. michael@0: """ michael@0: michael@0: def __init__(self, adb, device, test_suite, timeout, rebaseline, michael@0: performance_test, cleanup_test_files, tool, michael@0: dump_debug_info): michael@0: TestPackage.__init__(self, adb, device, test_suite, timeout, michael@0: rebaseline, performance_test, cleanup_test_files, michael@0: tool, dump_debug_info) michael@0: michael@0: def _CreateTestRunnerScript(self, options): michael@0: command_line_file = tempfile.NamedTemporaryFile() michael@0: # GTest expects argv[0] to be the executable path. michael@0: command_line_file.write(self.test_suite_basename + ' ' + options) michael@0: command_line_file.flush() michael@0: self.adb.PushIfNeeded(command_line_file.name, michael@0: constants.TEST_EXECUTABLE_DIR + michael@0: '/chrome-native-tests-command-line') michael@0: michael@0: def _GetGTestReturnCode(self): michael@0: return None michael@0: michael@0: def _GetFifo(self): michael@0: # The test.fifo path is determined by: michael@0: # testing/android/java/src/org/chromium/native_test/ michael@0: # ChromeNativeTestActivity.java and michael@0: # testing/android/native_test_launcher.cc michael@0: return '/data/data/org.chromium.native_test/files/test.fifo' michael@0: michael@0: def _ClearFifo(self): michael@0: self.adb.RunShellCommand('rm -f ' + self._GetFifo()) michael@0: michael@0: def _WatchFifo(self, timeout, logfile=None): michael@0: for i in range(10): michael@0: if self.adb.FileExistsOnDevice(self._GetFifo()): michael@0: print 'Fifo created...' michael@0: break michael@0: time.sleep(i) michael@0: else: michael@0: raise Exception('Unable to find fifo on device %s ' % self._GetFifo()) michael@0: args = shlex.split(self.adb.Adb()._target_arg) michael@0: args += ['shell', 'cat', self._GetFifo()] michael@0: return pexpect.spawn('adb', args, timeout=timeout, logfile=logfile) michael@0: michael@0: def GetAllTests(self): michael@0: """Returns a list of all tests available in the test suite.""" michael@0: self._CreateTestRunnerScript('--gtest_list_tests') michael@0: try: michael@0: self.tool.SetupEnvironment() michael@0: # Clear and start monitoring logcat. michael@0: self._ClearFifo() michael@0: self.adb.RunShellCommand( michael@0: 'am start -n ' michael@0: 'org.chromium.native_test/' michael@0: 'org.chromium.native_test.ChromeNativeTestActivity') michael@0: # Wait for native test to complete. michael@0: p = self._WatchFifo(timeout=30 * self.tool.GetTimeoutScale()) michael@0: p.expect("<