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