| 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 # pylint: disable=W0212 | 6 # pylint: disable=W0212 |
| 7 | 7 |
| 8 import logging | 8 import logging |
| 9 import os | 9 import os |
| 10 import shlex | 10 import shlex |
| 11 import sys | 11 import sys |
| 12 import tempfile | 12 import tempfile |
| 13 import time | 13 import time |
| 14 | 14 |
| 15 from pylib import android_commands | 15 from pylib import android_commands |
| 16 from pylib import constants | 16 from pylib import constants |
| 17 from pylib import pexpect | 17 from pylib import pexpect |
| 18 from pylib.android_commands import errors | 18 from pylib.device import adb_wrapper |
| 19 from pylib.gtest.test_package import TestPackage | 19 from pylib.gtest.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 def __init__(self, suite_name): | 25 def __init__(self, suite_name): |
| 26 """ | 26 """ |
| 27 Args: | 27 Args: |
| 28 suite_name: Name of the test suite (e.g. base_unittests). | 28 suite_name: Name of the test suite (e.g. base_unittests). |
| 29 """ | 29 """ |
| 30 TestPackage.__init__(self, suite_name) | 30 TestPackage.__init__(self, suite_name) |
| 31 if suite_name == 'content_browsertests': | 31 if suite_name == 'content_browsertests': |
| 32 self.suite_path = os.path.join( | 32 self.suite_path = os.path.join( |
| 33 constants.GetOutDirectory(), 'apks', '%s.apk' % suite_name) | 33 constants.GetOutDirectory(), 'apks', '%s.apk' % suite_name) |
| 34 self._package_info = constants.PACKAGE_INFO['content_browsertests'] | 34 self._package_info = constants.PACKAGE_INFO['content_browsertests'] |
| 35 else: | 35 else: |
| 36 self.suite_path = os.path.join( | 36 self.suite_path = os.path.join( |
| 37 constants.GetOutDirectory(), '%s_apk' % suite_name, | 37 constants.GetOutDirectory(), '%s_apk' % suite_name, |
| 38 '%s-debug.apk' % suite_name) | 38 '%s-debug.apk' % suite_name) |
| 39 self._package_info = constants.PACKAGE_INFO['gtest'] | 39 self._package_info = constants.PACKAGE_INFO['gtest'] |
| 40 | 40 |
| 41 def _CreateCommandLineFileOnDevice(self, adb, options): | 41 def _CreateCommandLineFileOnDevice(self, device, options): |
| 42 command_line_file = tempfile.NamedTemporaryFile() | 42 command_line_file = tempfile.NamedTemporaryFile() |
| 43 # GTest expects argv[0] to be the executable path. | 43 # GTest expects argv[0] to be the executable path. |
| 44 command_line_file.write(self.suite_name + ' ' + options) | 44 command_line_file.write(self.suite_name + ' ' + options) |
| 45 command_line_file.flush() | 45 command_line_file.flush() |
| 46 adb.PushIfNeeded(command_line_file.name, | 46 device.old_interface.PushIfNeeded( |
| 47 self._package_info.cmdline_file) | 47 command_line_file.name, |
| 48 self._package_info.cmdline_file) |
| 48 | 49 |
| 49 def _GetFifo(self): | 50 def _GetFifo(self): |
| 50 # The test.fifo path is determined by: | 51 # The test.fifo path is determined by: |
| 51 # testing/android/java/src/org/chromium/native_test/ | 52 # testing/android/java/src/org/chromium/native_test/ |
| 52 # ChromeNativeTestActivity.java and | 53 # ChromeNativeTestActivity.java and |
| 53 # testing/android/native_test_launcher.cc | 54 # testing/android/native_test_launcher.cc |
| 54 return '/data/data/' + self._package_info.package + '/files/test.fifo' | 55 return '/data/data/' + self._package_info.package + '/files/test.fifo' |
| 55 | 56 |
| 56 def _ClearFifo(self, adb): | 57 def _ClearFifo(self, device): |
| 57 adb.RunShellCommand('rm -f ' + self._GetFifo()) | 58 device.old_interface.RunShellCommand('rm -f ' + self._GetFifo()) |
| 58 | 59 |
| 59 def _WatchFifo(self, adb, timeout, logfile=None): | 60 def _WatchFifo(self, device, timeout, logfile=None): |
| 60 for i in range(10): | 61 for i in range(10): |
| 61 if adb.FileExistsOnDevice(self._GetFifo()): | 62 if device.old_interface.FileExistsOnDevice(self._GetFifo()): |
| 62 logging.info('Fifo created.') | 63 logging.info('Fifo created.') |
| 63 break | 64 break |
| 64 time.sleep(i) | 65 time.sleep(i) |
| 65 else: | 66 else: |
| 66 raise errors.DeviceUnresponsiveError( | 67 raise adb_wrapper.DeviceUnreachableError( |
| 67 'Unable to find fifo on device %s ' % self._GetFifo()) | 68 'Unable to find fifo on device %s ' % self._GetFifo()) |
| 68 args = shlex.split(adb.Adb()._target_arg) | 69 args = shlex.split(device.old_interface.Adb()._target_arg) |
| 69 args += ['shell', 'cat', self._GetFifo()] | 70 args += ['shell', 'cat', self._GetFifo()] |
| 70 return pexpect.spawn('adb', args, timeout=timeout, logfile=logfile) | 71 return pexpect.spawn('adb', args, timeout=timeout, logfile=logfile) |
| 71 | 72 |
| 72 def _StartActivity(self, adb): | 73 def _StartActivity(self, device): |
| 73 adb.StartActivity( | 74 device.old_interface.StartActivity( |
| 74 self._package_info.package, | 75 self._package_info.package, |
| 75 self._package_info.activity, | 76 self._package_info.activity, |
| 76 wait_for_completion=True, | 77 wait_for_completion=True, |
| 77 action='android.intent.action.MAIN', | 78 action='android.intent.action.MAIN', |
| 78 force_stop=True) | 79 force_stop=True) |
| 79 | 80 |
| 80 #override | 81 #override |
| 81 def ClearApplicationState(self, adb): | 82 def ClearApplicationState(self, device): |
| 82 adb.ClearApplicationState(self._package_info.package) | 83 device.old_interface.ClearApplicationState(self._package_info.package) |
| 83 # Content shell creates a profile on the sdscard which accumulates cache | 84 # Content shell creates a profile on the sdscard which accumulates cache |
| 84 # files over time. | 85 # files over time. |
| 85 if self.suite_name == 'content_browsertests': | 86 if self.suite_name == 'content_browsertests': |
| 86 adb.RunShellCommand( | 87 device.old_interface.RunShellCommand( |
| 87 'rm -r %s/content_shell' % adb.GetExternalStorage(), | 88 'rm -r %s/content_shell' % device.old_interface.GetExternalStorage(), |
| 88 timeout_time=60 * 2) | 89 timeout_time=60 * 2) |
| 89 | 90 |
| 90 #override | 91 #override |
| 91 def CreateCommandLineFileOnDevice(self, adb, test_filter, test_arguments): | 92 def CreateCommandLineFileOnDevice(self, device, test_filter, test_arguments): |
| 92 self._CreateCommandLineFileOnDevice( | 93 self._CreateCommandLineFileOnDevice( |
| 93 adb, '--gtest_filter=%s %s' % (test_filter, test_arguments)) | 94 device, '--gtest_filter=%s %s' % (test_filter, test_arguments)) |
| 94 | 95 |
| 95 #override | 96 #override |
| 96 def GetAllTests(self, adb): | 97 def GetAllTests(self, device): |
| 97 self._CreateCommandLineFileOnDevice(adb, '--gtest_list_tests') | 98 self._CreateCommandLineFileOnDevice(device, '--gtest_list_tests') |
| 98 try: | 99 try: |
| 99 self.tool.SetupEnvironment() | 100 self.tool.SetupEnvironment() |
| 100 # Clear and start monitoring logcat. | 101 # Clear and start monitoring logcat. |
| 101 self._ClearFifo(adb) | 102 self._ClearFifo(device) |
| 102 self._StartActivity(adb) | 103 self._StartActivity(device) |
| 103 # Wait for native test to complete. | 104 # Wait for native test to complete. |
| 104 p = self._WatchFifo(adb, timeout=30 * self.tool.GetTimeoutScale()) | 105 p = self._WatchFifo(device, timeout=30 * self.tool.GetTimeoutScale()) |
| 105 p.expect('<<ScopedMainEntryLogger') | 106 p.expect('<<ScopedMainEntryLogger') |
| 106 p.close() | 107 p.close() |
| 107 finally: | 108 finally: |
| 108 self.tool.CleanUpEnvironment() | 109 self.tool.CleanUpEnvironment() |
| 109 # We need to strip the trailing newline. | 110 # We need to strip the trailing newline. |
| 110 content = [line.rstrip() for line in p.before.splitlines()] | 111 content = [line.rstrip() for line in p.before.splitlines()] |
| 111 return self._ParseGTestListTests(content) | 112 return self._ParseGTestListTests(content) |
| 112 | 113 |
| 113 #override | 114 #override |
| 114 def SpawnTestProcess(self, adb): | 115 def SpawnTestProcess(self, device): |
| 115 try: | 116 try: |
| 116 self.tool.SetupEnvironment() | 117 self.tool.SetupEnvironment() |
| 117 self._ClearFifo(adb) | 118 self._ClearFifo(device) |
| 118 self._StartActivity(adb) | 119 self._StartActivity(device) |
| 119 finally: | 120 finally: |
| 120 self.tool.CleanUpEnvironment() | 121 self.tool.CleanUpEnvironment() |
| 121 logfile = android_commands.NewLineNormalizer(sys.stdout) | 122 logfile = android_commands.NewLineNormalizer(sys.stdout) |
| 122 return self._WatchFifo(adb, timeout=10, logfile=logfile) | 123 return self._WatchFifo(device, timeout=10, logfile=logfile) |
| 123 | 124 |
| 124 #override | 125 #override |
| 125 def Install(self, adb): | 126 def Install(self, device): |
| 126 self.tool.CopyFiles() | 127 self.tool.CopyFiles() |
| 127 adb.ManagedInstall(self.suite_path, False, | 128 device.old_interface.ManagedInstall( |
| 128 package_name=self._package_info.package) | 129 self.suite_path, False, package_name=self._package_info.package) |
| OLD | NEW |