Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | # Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | # found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | |
michael@0 | 6 | import os |
michael@0 | 7 | import shlex |
michael@0 | 8 | import sys |
michael@0 | 9 | import tempfile |
michael@0 | 10 | import time |
michael@0 | 11 | |
michael@0 | 12 | import android_commands |
michael@0 | 13 | import constants |
michael@0 | 14 | from test_package import TestPackage |
michael@0 | 15 | from pylib import pexpect |
michael@0 | 16 | |
michael@0 | 17 | class TestPackageApk(TestPackage): |
michael@0 | 18 | """A helper class for running APK-based native tests. |
michael@0 | 19 | |
michael@0 | 20 | Args: |
michael@0 | 21 | adb: ADB interface the tests are using. |
michael@0 | 22 | device: Device to run the tests. |
michael@0 | 23 | test_suite: A specific test suite to run, empty to run all. |
michael@0 | 24 | timeout: Timeout for each test. |
michael@0 | 25 | rebaseline: Whether or not to run tests in isolation and update the filter. |
michael@0 | 26 | performance_test: Whether or not performance test(s). |
michael@0 | 27 | cleanup_test_files: Whether or not to cleanup test files on device. |
michael@0 | 28 | tool: Name of the Valgrind tool. |
michael@0 | 29 | dump_debug_info: A debug_info object. |
michael@0 | 30 | """ |
michael@0 | 31 | |
michael@0 | 32 | def __init__(self, adb, device, test_suite, timeout, rebaseline, |
michael@0 | 33 | performance_test, cleanup_test_files, tool, |
michael@0 | 34 | dump_debug_info): |
michael@0 | 35 | TestPackage.__init__(self, adb, device, test_suite, timeout, |
michael@0 | 36 | rebaseline, performance_test, cleanup_test_files, |
michael@0 | 37 | tool, dump_debug_info) |
michael@0 | 38 | |
michael@0 | 39 | def _CreateTestRunnerScript(self, options): |
michael@0 | 40 | command_line_file = tempfile.NamedTemporaryFile() |
michael@0 | 41 | # GTest expects argv[0] to be the executable path. |
michael@0 | 42 | command_line_file.write(self.test_suite_basename + ' ' + options) |
michael@0 | 43 | command_line_file.flush() |
michael@0 | 44 | self.adb.PushIfNeeded(command_line_file.name, |
michael@0 | 45 | constants.TEST_EXECUTABLE_DIR + |
michael@0 | 46 | '/chrome-native-tests-command-line') |
michael@0 | 47 | |
michael@0 | 48 | def _GetGTestReturnCode(self): |
michael@0 | 49 | return None |
michael@0 | 50 | |
michael@0 | 51 | def _GetFifo(self): |
michael@0 | 52 | # The test.fifo path is determined by: |
michael@0 | 53 | # testing/android/java/src/org/chromium/native_test/ |
michael@0 | 54 | # ChromeNativeTestActivity.java and |
michael@0 | 55 | # testing/android/native_test_launcher.cc |
michael@0 | 56 | return '/data/data/org.chromium.native_test/files/test.fifo' |
michael@0 | 57 | |
michael@0 | 58 | def _ClearFifo(self): |
michael@0 | 59 | self.adb.RunShellCommand('rm -f ' + self._GetFifo()) |
michael@0 | 60 | |
michael@0 | 61 | def _WatchFifo(self, timeout, logfile=None): |
michael@0 | 62 | for i in range(10): |
michael@0 | 63 | if self.adb.FileExistsOnDevice(self._GetFifo()): |
michael@0 | 64 | print 'Fifo created...' |
michael@0 | 65 | break |
michael@0 | 66 | time.sleep(i) |
michael@0 | 67 | else: |
michael@0 | 68 | raise Exception('Unable to find fifo on device %s ' % self._GetFifo()) |
michael@0 | 69 | args = shlex.split(self.adb.Adb()._target_arg) |
michael@0 | 70 | args += ['shell', 'cat', self._GetFifo()] |
michael@0 | 71 | return pexpect.spawn('adb', args, timeout=timeout, logfile=logfile) |
michael@0 | 72 | |
michael@0 | 73 | def GetAllTests(self): |
michael@0 | 74 | """Returns a list of all tests available in the test suite.""" |
michael@0 | 75 | self._CreateTestRunnerScript('--gtest_list_tests') |
michael@0 | 76 | try: |
michael@0 | 77 | self.tool.SetupEnvironment() |
michael@0 | 78 | # Clear and start monitoring logcat. |
michael@0 | 79 | self._ClearFifo() |
michael@0 | 80 | self.adb.RunShellCommand( |
michael@0 | 81 | 'am start -n ' |
michael@0 | 82 | 'org.chromium.native_test/' |
michael@0 | 83 | 'org.chromium.native_test.ChromeNativeTestActivity') |
michael@0 | 84 | # Wait for native test to complete. |
michael@0 | 85 | p = self._WatchFifo(timeout=30 * self.tool.GetTimeoutScale()) |
michael@0 | 86 | p.expect("<<ScopedMainEntryLogger") |
michael@0 | 87 | p.close() |
michael@0 | 88 | finally: |
michael@0 | 89 | self.tool.CleanUpEnvironment() |
michael@0 | 90 | # We need to strip the trailing newline. |
michael@0 | 91 | content = [line.rstrip() for line in p.before.splitlines()] |
michael@0 | 92 | ret = self._ParseGTestListTests(content) |
michael@0 | 93 | return ret |
michael@0 | 94 | |
michael@0 | 95 | def CreateTestRunnerScript(self, gtest_filter, test_arguments): |
michael@0 | 96 | self._CreateTestRunnerScript('--gtest_filter=%s %s' % (gtest_filter, |
michael@0 | 97 | test_arguments)) |
michael@0 | 98 | |
michael@0 | 99 | def RunTestsAndListResults(self): |
michael@0 | 100 | try: |
michael@0 | 101 | self.tool.SetupEnvironment() |
michael@0 | 102 | self._ClearFifo() |
michael@0 | 103 | self.adb.RunShellCommand( |
michael@0 | 104 | 'am start -n ' |
michael@0 | 105 | 'org.chromium.native_test/' |
michael@0 | 106 | 'org.chromium.native_test.ChromeNativeTestActivity') |
michael@0 | 107 | finally: |
michael@0 | 108 | self.tool.CleanUpEnvironment() |
michael@0 | 109 | logfile = android_commands.NewLineNormalizer(sys.stdout) |
michael@0 | 110 | return self._WatchTestOutput(self._WatchFifo(timeout=10, logfile=logfile)) |
michael@0 | 111 | |
michael@0 | 112 | def StripAndCopyExecutable(self): |
michael@0 | 113 | # Always uninstall the previous one (by activity name); we don't |
michael@0 | 114 | # know what was embedded in it. |
michael@0 | 115 | self.adb.ManagedInstall(self.test_suite_full, False, |
michael@0 | 116 | package_name='org.chromium.native_test') |
michael@0 | 117 | |
michael@0 | 118 | def _GetTestSuiteBaseName(self): |
michael@0 | 119 | """Returns the base name of the test suite.""" |
michael@0 | 120 | # APK test suite names end with '-debug.apk' |
michael@0 | 121 | return os.path.basename(self.test_suite).rsplit('-debug', 1)[0] |