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