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.
5 """Defines TestPackageApk to help run APK-based native tests."""
6 # pylint: disable=W0212
15 from pylib import android_commands
16 from pylib import constants
17 from pylib import pexpect
18 from pylib.device import adb_wrapper
19 from pylib.gtest.test_package import TestPackage
22 class TestPackageApk(TestPackage):
23 """A helper class for running APK-based native tests."""
25 def __init__(self, suite_name):
28 suite_name: Name of the test suite (e.g. base_unittests).
30 TestPackage.__init__(self, suite_name)
31 if suite_name == 'content_browsertests':
32 self.suite_path = os.path.join(
33 constants.GetOutDirectory(), 'apks', '%s.apk' % suite_name)
34 self._package_info = constants.PACKAGE_INFO['content_browsertests']
36 self.suite_path = os.path.join(
37 constants.GetOutDirectory(), '%s_apk' % suite_name,
38 '%s-debug.apk' % suite_name)
39 self._package_info = constants.PACKAGE_INFO['gtest']
41 def _CreateCommandLineFileOnDevice(self, device, options):
42 command_line_file = tempfile.NamedTemporaryFile()
43 # GTest expects argv[0] to be the executable path.
44 command_line_file.write(self.suite_name + ' ' + options)
45 command_line_file.flush()
46 device.old_interface.PushIfNeeded(
47 command_line_file.name,
48 self._package_info.cmdline_file)
51 # The test.fifo path is determined by:
52 # testing/android/java/src/org/chromium/native_test/
53 # ChromeNativeTestActivity.java and
54 # testing/android/native_test_launcher.cc
55 return '/data/data/' + self._package_info.package + '/files/test.fifo'
57 def _ClearFifo(self, device):
58 device.old_interface.RunShellCommand('rm -f ' + self._GetFifo())
60 def _WatchFifo(self, device, timeout, logfile=None):
62 if device.old_interface.FileExistsOnDevice(self._GetFifo()):
63 logging.info('Fifo created.')
67 raise adb_wrapper.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)
73 def _StartActivity(self, device):
74 device.old_interface.StartActivity(
75 self._package_info.package,
76 self._package_info.activity,
77 wait_for_completion=True,
78 action='android.intent.action.MAIN',
82 def ClearApplicationState(self, device):
83 device.old_interface.ClearApplicationState(self._package_info.package)
84 # Content shell creates a profile on the sdscard which accumulates cache
86 if self.suite_name == 'content_browsertests':
87 device.old_interface.RunShellCommand(
88 'rm -r %s/content_shell' % device.old_interface.GetExternalStorage(),
92 def CreateCommandLineFileOnDevice(self, device, test_filter, test_arguments):
93 self._CreateCommandLineFileOnDevice(
94 device, '--gtest_filter=%s %s' % (test_filter, test_arguments))
97 def GetAllTests(self, device):
98 self._CreateCommandLineFileOnDevice(device, '--gtest_list_tests')
100 self.tool.SetupEnvironment()
101 # Clear and start monitoring logcat.
102 self._ClearFifo(device)
103 self._StartActivity(device)
104 # Wait for native test to complete.
105 p = self._WatchFifo(device, timeout=30 * self.tool.GetTimeoutScale())
106 p.expect('<<ScopedMainEntryLogger')
109 self.tool.CleanUpEnvironment()
110 # We need to strip the trailing newline.
111 content = [line.rstrip() for line in p.before.splitlines()]
112 return self._ParseGTestListTests(content)
115 def SpawnTestProcess(self, device):
117 self.tool.SetupEnvironment()
118 self._ClearFifo(device)
119 self._StartActivity(device)
121 self.tool.CleanUpEnvironment()
122 logfile = android_commands.NewLineNormalizer(sys.stdout)
123 return self._WatchFifo(device, timeout=10, logfile=logfile)
126 def Install(self, device):
127 self.tool.CopyFiles()
128 device.old_interface.ManagedInstall(
129 self.suite_path, False, package_name=self._package_info.package)