| 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 """Defines TestPackageApk to help run APK-based native tests.""" | 5 """Defines TestPackageApk to help run APK-based native tests.""" |
| 6 | 6 |
| 7 import logging | 7 import logging |
| 8 import os | 8 import os |
| 9 import shlex | 9 import shlex |
| 10 import sys | 10 import sys |
| 11 import tempfile | 11 import tempfile |
| 12 import time | 12 import time |
| 13 | 13 |
| 14 from pylib import android_commands | 14 from pylib import android_commands |
| 15 from pylib import constants | 15 from pylib import constants |
| 16 from pylib import pexpect | 16 from pylib import pexpect |
| 17 from pylib.android_commands import errors | 17 from pylib.android_commands import errors |
| 18 | 18 |
| 19 from test_package import TestPackage | 19 from test_package import TestPackage |
| 20 | 20 |
| 21 | 21 |
| 22 class TestPackageApk(TestPackage): | 22 class TestPackageApk(TestPackage): |
| 23 """A helper class for running APK-based native tests. | 23 """A helper class for running APK-based native tests.""" |
| 24 | 24 |
| 25 Args: | 25 def __init__(self, adb, device, test_suite, tool, test_apk_package_name, |
| 26 adb: ADB interface the tests are using. | |
| 27 device: Device to run the tests. | |
| 28 test_suite: A specific test suite to run, empty to run all. | |
| 29 timeout: Timeout for each test. | |
| 30 cleanup_test_files: Whether or not to cleanup test files on device. | |
| 31 tool: Name of the Valgrind tool. | |
| 32 """ | |
| 33 | |
| 34 def __init__(self, adb, device, test_suite, timeout, | |
| 35 cleanup_test_files, tool, apk_package_name, | |
| 36 test_activity_name, command_line_file): | 26 test_activity_name, command_line_file): |
| 37 TestPackage.__init__(self, adb, device, test_suite, timeout, | 27 """ |
| 38 cleanup_test_files, tool) | 28 Args: |
| 39 self._apk_package_name = apk_package_name | 29 adb: ADB interface the tests are using. |
| 30 device: Device to run the tests. |
| 31 test_suite: A specific test suite to run, empty to run all. |
| 32 tool: Name of the Valgrind tool. |
| 33 test_apk_package_name: Apk package name for tests running in APKs. |
| 34 test_activity_name: Test activity to invoke for APK tests. |
| 35 command_line_file: Filename to use to pass arguments to tests. |
| 36 """ |
| 37 TestPackage.__init__(self, adb, device, test_suite, tool) |
| 38 self._test_apk_package_name = test_apk_package_name |
| 40 self._test_activity_name = test_activity_name | 39 self._test_activity_name = test_activity_name |
| 41 self._command_line_file = command_line_file | 40 self._command_line_file = command_line_file |
| 42 | 41 |
| 43 def _CreateTestRunnerScript(self, options): | 42 def _CreateCommandLineFileOnDevice(self, options): |
| 44 command_line_file = tempfile.NamedTemporaryFile() | 43 command_line_file = tempfile.NamedTemporaryFile() |
| 45 # GTest expects argv[0] to be the executable path. | 44 # GTest expects argv[0] to be the executable path. |
| 46 command_line_file.write(self.test_suite_basename + ' ' + options) | 45 command_line_file.write(self.test_suite_basename + ' ' + options) |
| 47 command_line_file.flush() | 46 command_line_file.flush() |
| 48 self.adb.PushIfNeeded(command_line_file.name, | 47 self.adb.PushIfNeeded(command_line_file.name, |
| 49 constants.TEST_EXECUTABLE_DIR + '/' + | 48 constants.TEST_EXECUTABLE_DIR + '/' + |
| 50 self._command_line_file) | 49 self._command_line_file) |
| 51 | 50 |
| 52 def _GetGTestReturnCode(self): | 51 def _GetGTestReturnCode(self): |
| 53 return None | 52 return None |
| 54 | 53 |
| 55 def _GetFifo(self): | 54 def _GetFifo(self): |
| 56 # The test.fifo path is determined by: | 55 # The test.fifo path is determined by: |
| 57 # testing/android/java/src/org/chromium/native_test/ | 56 # testing/android/java/src/org/chromium/native_test/ |
| 58 # ChromeNativeTestActivity.java and | 57 # ChromeNativeTestActivity.java and |
| 59 # testing/android/native_test_launcher.cc | 58 # testing/android/native_test_launcher.cc |
| 60 return '/data/data/' + self._apk_package_name + '/files/test.fifo' | 59 return '/data/data/' + self._test_apk_package_name + '/files/test.fifo' |
| 61 | 60 |
| 62 def _ClearFifo(self): | 61 def _ClearFifo(self): |
| 63 self.adb.RunShellCommand('rm -f ' + self._GetFifo()) | 62 self.adb.RunShellCommand('rm -f ' + self._GetFifo()) |
| 64 | 63 |
| 65 def _WatchFifo(self, timeout, logfile=None): | 64 def _WatchFifo(self, timeout, logfile=None): |
| 66 for i in range(10): | 65 for i in range(10): |
| 67 if self.adb.FileExistsOnDevice(self._GetFifo()): | 66 if self.adb.FileExistsOnDevice(self._GetFifo()): |
| 68 logging.info('Fifo created.') | 67 logging.info('Fifo created.') |
| 69 break | 68 break |
| 70 time.sleep(i) | 69 time.sleep(i) |
| 71 else: | 70 else: |
| 72 raise errors.DeviceUnresponsiveError( | 71 raise errors.DeviceUnresponsiveError( |
| 73 'Unable to find fifo on device %s ' % self._GetFifo()) | 72 'Unable to find fifo on device %s ' % self._GetFifo()) |
| 74 args = shlex.split(self.adb.Adb()._target_arg) | 73 args = shlex.split(self.adb.Adb()._target_arg) |
| 75 args += ['shell', 'cat', self._GetFifo()] | 74 args += ['shell', 'cat', self._GetFifo()] |
| 76 return pexpect.spawn('adb', args, timeout=timeout, logfile=logfile) | 75 return pexpect.spawn('adb', args, timeout=timeout, logfile=logfile) |
| 77 | 76 |
| 78 def ClearApplicationState(self): | |
| 79 """Clear the application state.""" | |
| 80 self.adb.ClearApplicationState(self._apk_package_name) | |
| 81 | |
| 82 def _StartActivity(self): | 77 def _StartActivity(self): |
| 83 self.adb.StartActivity( | 78 self.adb.StartActivity( |
| 84 self._apk_package_name, | 79 self._test_apk_package_name, |
| 85 self._test_activity_name, | 80 self._test_activity_name, |
| 86 wait_for_completion=True, | 81 wait_for_completion=True, |
| 87 action='android.intent.action.MAIN', | 82 action='android.intent.action.MAIN', |
| 88 force_stop=True) | 83 force_stop=True) |
| 89 | 84 |
| 85 def _NeedsInstall(self): |
| 86 installed_apk_path = self.adb.GetApplicationPath( |
| 87 self._test_apk_package_name) |
| 88 if installed_apk_path: |
| 89 return not self.adb.CheckMd5Sum( |
| 90 self.test_suite_full, installed_apk_path, ignore_paths=True) |
| 91 else: |
| 92 return True |
| 93 |
| 94 def _GetTestSuiteBaseName(self): |
| 95 """Returns the base name of the test suite.""" |
| 96 # APK test suite names end with '-debug.apk' |
| 97 return os.path.basename(self.test_suite).rsplit('-debug', 1)[0] |
| 98 |
| 99 #override |
| 100 def ClearApplicationState(self): |
| 101 self.adb.ClearApplicationState(self._test_apk_package_name) |
| 102 |
| 103 #override |
| 104 def CreateCommandLineFileOnDevice(self, test_filter, test_arguments): |
| 105 self._CreateCommandLineFileOnDevice( |
| 106 '--gtest_filter=%s %s' % (test_filter, test_arguments)) |
| 107 |
| 108 #override |
| 90 def GetAllTests(self): | 109 def GetAllTests(self): |
| 91 """Returns a list of all tests available in the test suite.""" | 110 self._CreateCommandLineFileOnDevice('--gtest_list_tests') |
| 92 self._CreateTestRunnerScript('--gtest_list_tests') | |
| 93 try: | 111 try: |
| 94 self.tool.SetupEnvironment() | 112 self.tool.SetupEnvironment() |
| 95 # Clear and start monitoring logcat. | 113 # Clear and start monitoring logcat. |
| 96 self._ClearFifo() | 114 self._ClearFifo() |
| 97 self._StartActivity() | 115 self._StartActivity() |
| 98 # Wait for native test to complete. | 116 # Wait for native test to complete. |
| 99 p = self._WatchFifo(timeout=30 * self.tool.GetTimeoutScale()) | 117 p = self._WatchFifo(timeout=30 * self.tool.GetTimeoutScale()) |
| 100 p.expect('<<ScopedMainEntryLogger') | 118 p.expect('<<ScopedMainEntryLogger') |
| 101 p.close() | 119 p.close() |
| 102 finally: | 120 finally: |
| 103 self.tool.CleanUpEnvironment() | 121 self.tool.CleanUpEnvironment() |
| 104 # We need to strip the trailing newline. | 122 # We need to strip the trailing newline. |
| 105 content = [line.rstrip() for line in p.before.splitlines()] | 123 content = [line.rstrip() for line in p.before.splitlines()] |
| 106 ret = self._ParseGTestListTests(content) | 124 return self._ParseGTestListTests(content) |
| 107 return ret | |
| 108 | 125 |
| 109 def CreateTestRunnerScript(self, test_filter, test_arguments): | 126 #override |
| 110 self._CreateTestRunnerScript('--gtest_filter=%s %s' % (test_filter, | 127 def SpawnTestProcess(self): |
| 111 test_arguments)) | |
| 112 | |
| 113 def RunTestsAndListResults(self): | |
| 114 try: | 128 try: |
| 115 self.tool.SetupEnvironment() | 129 self.tool.SetupEnvironment() |
| 116 self._ClearFifo() | 130 self._ClearFifo() |
| 117 self._StartActivity() | 131 self._StartActivity() |
| 118 finally: | 132 finally: |
| 119 self.tool.CleanUpEnvironment() | 133 self.tool.CleanUpEnvironment() |
| 120 logfile = android_commands.NewLineNormalizer(sys.stdout) | 134 logfile = android_commands.NewLineNormalizer(sys.stdout) |
| 121 return self._WatchTestOutput(self._WatchFifo(timeout=10, logfile=logfile)) | 135 return self._WatchFifo(timeout=10, logfile=logfile) |
| 122 | 136 |
| 123 def _NeedsInstall(self): | 137 #override |
| 124 installed_apk_path = self.adb.GetApplicationPath(self._apk_package_name) | 138 def Install(self): |
| 125 if installed_apk_path: | |
| 126 return not self.adb.CheckMd5Sum( | |
| 127 self.test_suite_full, installed_apk_path, ignore_paths=True) | |
| 128 else: | |
| 129 return True | |
| 130 | |
| 131 def StripAndCopyExecutable(self): | |
| 132 self.tool.CopyFiles() | 139 self.tool.CopyFiles() |
| 133 if self._NeedsInstall(): | 140 if self._NeedsInstall(): |
| 134 # Always uninstall the previous one (by activity name); we don't | 141 # Always uninstall the previous one (by activity name); we don't |
| 135 # know what was embedded in it. | 142 # know what was embedded in it. |
| 136 self.adb.ManagedInstall(self.test_suite_full, False, | 143 self.adb.ManagedInstall(self.test_suite_full, False, |
| 137 package_name=self._apk_package_name) | 144 package_name=self._test_apk_package_name) |
| 138 | 145 |
| 139 def _GetTestSuiteBaseName(self): | |
| 140 """Returns the base name of the test suite.""" | |
| 141 # APK test suite names end with '-debug.apk' | |
| 142 return os.path.basename(self.test_suite).rsplit('-debug', 1)[0] | |
| OLD | NEW |