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 device_errors
19 from pylib.device import intent
20 from pylib.gtest.test_package import TestPackage
23 class TestPackageApk(TestPackage):
24 """A helper class for running APK-based native tests."""
26 def __init__(self, suite_name):
29 suite_name: Name of the test suite (e.g. base_unittests).
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']
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']
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)])
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'
58 def _ClearFifo(self, device):
59 device.RunShellCommand('rm -f ' + self._GetFifo())
61 def _WatchFifo(self, device, timeout, logfile=None):
63 if device.FileExists(self._GetFifo()):
64 logging.info('Fifo created.')
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)
74 def _StartActivity(self, device):
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.
84 def ClearApplicationState(self, device):
85 device.ClearApplicationState(self._package_info.package)
86 # Content shell creates a profile on the sdscard which accumulates cache
88 if self.suite_name == 'content_browsertests':
90 device.RunShellCommand(
91 'rm -r %s/content_shell' % device.GetExternalStoragePath(),
93 except device_errors.CommandFailedError:
94 # TODO(jbudorick) Handle this exception appropriately once the
95 # conversions are done.
99 def CreateCommandLineFileOnDevice(self, device, test_filter, test_arguments):
100 self._CreateCommandLineFileOnDevice(
101 device, '--gtest_filter=%s %s' % (test_filter, test_arguments))
104 def GetAllTests(self, device):
105 self._CreateCommandLineFileOnDevice(device, '--gtest_list_tests')
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')
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)
122 def SpawnTestProcess(self, device):
124 self.tool.SetupEnvironment()
125 self._ClearFifo(device)
126 self._StartActivity(device)
128 self.tool.CleanUpEnvironment()
129 logfile = android_commands.NewLineNormalizer(sys.stdout)
130 return self._WatchFifo(device, timeout=10, logfile=logfile)
133 def Install(self, device):
134 self.tool.CopyFiles(device)
135 device.Install(self.suite_path)