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."""
14 from pylib import android_commands
15 from pylib import cmd_helper
16 from pylib import constants
17 from pylib import pexpect
18 from pylib.android_commands import errors
20 from 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, adb, 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 adb.PushIfNeeded(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, adb):
58 adb.RunShellCommand('rm -f ' + self._GetFifo())
60 def _WatchFifo(self, adb, timeout, logfile=None):
62 if adb.FileExistsOnDevice(self._GetFifo()):
63 logging.info('Fifo created.')
67 raise errors.DeviceUnresponsiveError(
68 'Unable to find fifo on device %s ' % self._GetFifo())
69 args = shlex.split(adb.Adb()._target_arg)
70 args += ['shell', 'cat', self._GetFifo()]
71 return pexpect.spawn('adb', args, timeout=timeout, logfile=logfile)
73 def _StartActivity(self, adb):
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, adb):
83 adb.ClearApplicationState(self._package_info.package)
84 # Content shell creates a profile on the sdscard which accumulates cache
86 if self.suite_name == 'content_browsertests':
88 'rm -r %s/content_shell' % adb.GetExternalStorage(),
92 def CreateCommandLineFileOnDevice(self, adb, test_filter, test_arguments):
93 self._CreateCommandLineFileOnDevice(
94 adb, '--gtest_filter=%s %s' % (test_filter, test_arguments))
97 def GetAllTests(self, adb):
98 self._CreateCommandLineFileOnDevice(adb, '--gtest_list_tests')
100 self.tool.SetupEnvironment()
101 # Clear and start monitoring logcat.
103 self._StartActivity(adb)
104 # Wait for native test to complete.
105 p = self._WatchFifo(adb, 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, adb):
117 self.tool.SetupEnvironment()
119 self._StartActivity(adb)
121 self.tool.CleanUpEnvironment()
122 logfile = android_commands.NewLineNormalizer(sys.stdout)
123 return self._WatchFifo(adb, timeout=10, logfile=logfile)
126 def Install(self, adb):
127 self.tool.CopyFiles()
128 adb.ManagedInstall(self.suite_path, False,
129 package_name=self._package_info.package)