James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Defines TestPackageApk to help run APK-based native tests.""" |
| 6 | # pylint: disable=W0212 |
| 7 | |
Mitch Rudominer | 8273be3 | 2015-07-08 11:07:52 -0700 | [diff] [blame] | 8 | import itertools |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 9 | import logging |
| 10 | import os |
Mitch Rudominer | 8273be3 | 2015-07-08 11:07:52 -0700 | [diff] [blame] | 11 | import posixpath |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 12 | import shlex |
| 13 | import sys |
| 14 | import tempfile |
| 15 | import time |
| 16 | |
| 17 | from pylib import android_commands |
| 18 | from pylib import constants |
| 19 | from pylib import pexpect |
| 20 | from pylib.device import device_errors |
| 21 | from pylib.device import intent |
Etienne Membrives | 175837a | 2014-12-19 15:45:38 +0100 | [diff] [blame] | 22 | from pylib.gtest import gtest_test_instance |
Mitch Rudominer | 8273be3 | 2015-07-08 11:07:52 -0700 | [diff] [blame] | 23 | from pylib.gtest import local_device_gtest_run |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 24 | from pylib.gtest.test_package import TestPackage |
| 25 | |
| 26 | |
| 27 | class TestPackageApk(TestPackage): |
| 28 | """A helper class for running APK-based native tests.""" |
| 29 | |
| 30 | def __init__(self, suite_name): |
| 31 | """ |
| 32 | Args: |
| 33 | suite_name: Name of the test suite (e.g. base_unittests). |
| 34 | """ |
| 35 | TestPackage.__init__(self, suite_name) |
Viet-Trung Luu | 235cf3d | 2015-06-11 10:01:25 -0700 | [diff] [blame] | 36 | self.suite_path = os.path.join( |
| 37 | constants.GetOutDirectory(), '%s_apk' % suite_name, |
| 38 | '%s-debug.apk' % suite_name) |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 39 | if suite_name == 'content_browsertests': |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 40 | self._package_info = constants.PACKAGE_INFO['content_browsertests'] |
James Robinson | 0fae000 | 2015-05-05 16:31:51 -0700 | [diff] [blame] | 41 | elif suite_name == 'components_browsertests': |
James Robinson | 0fae000 | 2015-05-05 16:31:51 -0700 | [diff] [blame] | 42 | self._package_info = constants.PACKAGE_INFO['components_browsertests'] |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 43 | else: |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 44 | self._package_info = constants.PACKAGE_INFO['gtest'] |
| 45 | |
| 46 | def _CreateCommandLineFileOnDevice(self, device, options): |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 47 | device.WriteFile(self._package_info.cmdline_file, |
| 48 | self.suite_name + ' ' + options) |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 49 | |
| 50 | def _GetFifo(self): |
| 51 | # The test.fifo path is determined by: |
James Robinson | 0fae000 | 2015-05-05 16:31:51 -0700 | [diff] [blame] | 52 | # testing/android/native_test/java/src/org/chromium/native_test/ |
James Robinson | f19b102 | 2015-05-05 18:12:15 -0700 | [diff] [blame] | 53 | # NativeTestActivity.java and |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 54 | # testing/android/native_test_launcher.cc |
| 55 | return '/data/data/' + self._package_info.package + '/files/test.fifo' |
| 56 | |
| 57 | def _ClearFifo(self, device): |
| 58 | device.RunShellCommand('rm -f ' + self._GetFifo()) |
| 59 | |
| 60 | def _WatchFifo(self, device, timeout, logfile=None): |
Benjamin Lerman | df06e5f | 2015-01-22 13:22:57 +0100 | [diff] [blame] | 61 | for i in range(100): |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 62 | if device.FileExists(self._GetFifo()): |
Benjamin Lerman | df06e5f | 2015-01-22 13:22:57 +0100 | [diff] [blame] | 63 | logging.info('Fifo created. Slept for %f secs' % (i * 0.5)) |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 64 | break |
Benjamin Lerman | df06e5f | 2015-01-22 13:22:57 +0100 | [diff] [blame] | 65 | time.sleep(0.5) |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 66 | else: |
| 67 | raise device_errors.DeviceUnreachableError( |
| 68 | 'Unable to find fifo on device %s ' % self._GetFifo()) |
| 69 | args = shlex.split(device.old_interface.Adb()._target_arg) |
| 70 | args += ['shell', 'cat', self._GetFifo()] |
| 71 | return pexpect.spawn('adb', args, timeout=timeout, logfile=logfile) |
| 72 | |
Benjamin Lerman | df06e5f | 2015-01-22 13:22:57 +0100 | [diff] [blame] | 73 | def _StartActivity(self, device, force_stop=True): |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 74 | device.StartActivity( |
| 75 | intent.Intent(package=self._package_info.package, |
| 76 | activity=self._package_info.activity, |
| 77 | action='android.intent.action.MAIN'), |
| 78 | # No wait since the runner waits for FIFO creation anyway. |
| 79 | blocking=False, |
Benjamin Lerman | df06e5f | 2015-01-22 13:22:57 +0100 | [diff] [blame] | 80 | force_stop=force_stop) |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 81 | |
| 82 | #override |
| 83 | def ClearApplicationState(self, device): |
| 84 | device.ClearApplicationState(self._package_info.package) |
| 85 | # Content shell creates a profile on the sdscard which accumulates cache |
| 86 | # files over time. |
| 87 | if self.suite_name == 'content_browsertests': |
| 88 | try: |
| 89 | device.RunShellCommand( |
| 90 | 'rm -r %s/content_shell' % device.GetExternalStoragePath(), |
| 91 | timeout=60 * 2) |
| 92 | except device_errors.CommandFailedError: |
| 93 | # TODO(jbudorick) Handle this exception appropriately once the |
| 94 | # conversions are done. |
| 95 | pass |
James Robinson | 0fae000 | 2015-05-05 16:31:51 -0700 | [diff] [blame] | 96 | elif self.suite_name == 'components_browsertests': |
| 97 | try: |
| 98 | device.RunShellCommand( |
| 99 | 'rm -r %s/components_shell' % device.GetExternalStoragePath(), |
| 100 | timeout=60 * 2) |
| 101 | except device_errors.CommandFailedError: |
| 102 | # TODO(jbudorick) Handle this exception appropriately once the |
| 103 | # conversions are done. |
| 104 | pass |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 105 | |
| 106 | #override |
| 107 | def CreateCommandLineFileOnDevice(self, device, test_filter, test_arguments): |
| 108 | self._CreateCommandLineFileOnDevice( |
| 109 | device, '--gtest_filter=%s %s' % (test_filter, test_arguments)) |
| 110 | |
| 111 | #override |
| 112 | def GetAllTests(self, device): |
| 113 | self._CreateCommandLineFileOnDevice(device, '--gtest_list_tests') |
| 114 | try: |
| 115 | self.tool.SetupEnvironment() |
| 116 | # Clear and start monitoring logcat. |
| 117 | self._ClearFifo(device) |
| 118 | self._StartActivity(device) |
| 119 | # Wait for native test to complete. |
| 120 | p = self._WatchFifo(device, timeout=30 * self.tool.GetTimeoutScale()) |
| 121 | p.expect('<<ScopedMainEntryLogger') |
| 122 | p.close() |
| 123 | finally: |
| 124 | self.tool.CleanUpEnvironment() |
| 125 | # We need to strip the trailing newline. |
| 126 | content = [line.rstrip() for line in p.before.splitlines()] |
Etienne Membrives | 175837a | 2014-12-19 15:45:38 +0100 | [diff] [blame] | 127 | return gtest_test_instance.ParseGTestListTests(content) |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 128 | |
| 129 | #override |
| 130 | def SpawnTestProcess(self, device): |
| 131 | try: |
| 132 | self.tool.SetupEnvironment() |
| 133 | self._ClearFifo(device) |
Benjamin Lerman | df06e5f | 2015-01-22 13:22:57 +0100 | [diff] [blame] | 134 | # Doesn't need to stop an Activity because ClearApplicationState() is |
| 135 | # always called before this call and so it is already stopped at this |
| 136 | # point. |
| 137 | self._StartActivity(device, force_stop=False) |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 138 | finally: |
| 139 | self.tool.CleanUpEnvironment() |
| 140 | logfile = android_commands.NewLineNormalizer(sys.stdout) |
| 141 | return self._WatchFifo(device, timeout=10, logfile=logfile) |
| 142 | |
| 143 | #override |
| 144 | def Install(self, device): |
James Robinson | e2ac7e8 | 2014-10-15 13:21:59 -0700 | [diff] [blame] | 145 | self.tool.CopyFiles(device) |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 146 | device.Install(self.suite_path) |
Mitch Rudominer | 8273be3 | 2015-07-08 11:07:52 -0700 | [diff] [blame] | 147 | |
| 148 | #override |
| 149 | def PullAppFiles(self, device, files, directory): |
| 150 | local_device_gtest_run.PullAppFilesImpl( |
| 151 | device, self._package_info.package, files, directory) |