blob: 16ef21c6e449030cb8aca8cc2cf2cca115a7ec0d [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001# 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 Rudominer8273be32015-07-08 11:07:52 -07008import itertools
James Robinson646469d2014-10-03 15:33:28 -07009import logging
10import os
Mitch Rudominer8273be32015-07-08 11:07:52 -070011import posixpath
James Robinson646469d2014-10-03 15:33:28 -070012import shlex
13import sys
14import tempfile
15import time
16
17from pylib import android_commands
18from pylib import constants
19from pylib import pexpect
20from pylib.device import device_errors
21from pylib.device import intent
Etienne Membrives175837a2014-12-19 15:45:38 +010022from pylib.gtest import gtest_test_instance
Mitch Rudominer8273be32015-07-08 11:07:52 -070023from pylib.gtest import local_device_gtest_run
James Robinson646469d2014-10-03 15:33:28 -070024from pylib.gtest.test_package import TestPackage
25
26
27class 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 Luu235cf3d2015-06-11 10:01:25 -070036 self.suite_path = os.path.join(
37 constants.GetOutDirectory(), '%s_apk' % suite_name,
38 '%s-debug.apk' % suite_name)
James Robinson646469d2014-10-03 15:33:28 -070039 if suite_name == 'content_browsertests':
James Robinson646469d2014-10-03 15:33:28 -070040 self._package_info = constants.PACKAGE_INFO['content_browsertests']
James Robinson0fae0002015-05-05 16:31:51 -070041 elif suite_name == 'components_browsertests':
James Robinson0fae0002015-05-05 16:31:51 -070042 self._package_info = constants.PACKAGE_INFO['components_browsertests']
James Robinson646469d2014-10-03 15:33:28 -070043 else:
James Robinson646469d2014-10-03 15:33:28 -070044 self._package_info = constants.PACKAGE_INFO['gtest']
45
46 def _CreateCommandLineFileOnDevice(self, device, options):
James Robinson6a64b812014-12-03 13:38:42 -080047 device.WriteFile(self._package_info.cmdline_file,
48 self.suite_name + ' ' + options)
James Robinson646469d2014-10-03 15:33:28 -070049
50 def _GetFifo(self):
51 # The test.fifo path is determined by:
James Robinson0fae0002015-05-05 16:31:51 -070052 # testing/android/native_test/java/src/org/chromium/native_test/
James Robinsonf19b1022015-05-05 18:12:15 -070053 # NativeTestActivity.java and
James Robinson646469d2014-10-03 15:33:28 -070054 # 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 Lermandf06e5f2015-01-22 13:22:57 +010061 for i in range(100):
James Robinson646469d2014-10-03 15:33:28 -070062 if device.FileExists(self._GetFifo()):
Benjamin Lermandf06e5f2015-01-22 13:22:57 +010063 logging.info('Fifo created. Slept for %f secs' % (i * 0.5))
James Robinson646469d2014-10-03 15:33:28 -070064 break
Benjamin Lermandf06e5f2015-01-22 13:22:57 +010065 time.sleep(0.5)
James Robinson646469d2014-10-03 15:33:28 -070066 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 Lermandf06e5f2015-01-22 13:22:57 +010073 def _StartActivity(self, device, force_stop=True):
James Robinson646469d2014-10-03 15:33:28 -070074 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 Lermandf06e5f2015-01-22 13:22:57 +010080 force_stop=force_stop)
James Robinson646469d2014-10-03 15:33:28 -070081
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 Robinson0fae0002015-05-05 16:31:51 -070096 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 Robinson646469d2014-10-03 15:33:28 -0700105
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 Membrives175837a2014-12-19 15:45:38 +0100127 return gtest_test_instance.ParseGTestListTests(content)
James Robinson646469d2014-10-03 15:33:28 -0700128
129 #override
130 def SpawnTestProcess(self, device):
131 try:
132 self.tool.SetupEnvironment()
133 self._ClearFifo(device)
Benjamin Lermandf06e5f2015-01-22 13:22:57 +0100134 # 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 Robinson646469d2014-10-03 15:33:28 -0700138 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 Robinsone2ac7e82014-10-15 13:21:59 -0700145 self.tool.CopyFiles(device)
James Robinson646469d2014-10-03 15:33:28 -0700146 device.Install(self.suite_path)
Mitch Rudominer8273be32015-07-08 11:07:52 -0700147
148 #override
149 def PullAppFiles(self, device, files, directory):
150 local_device_gtest_run.PullAppFilesImpl(
151 device, self._package_info.package, files, directory)