Enable components_browsertests on Android
[chromium-blink-merge.git] / build / android / pylib / gtest / test_package_apk.py
blob8da3c749dbc63e29f1a33c744f53195dac3980e2
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
8 import logging
9 import os
10 import shlex
11 import sys
12 import tempfile
13 import time
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 import gtest_test_instance
21 from pylib.gtest.test_package import TestPackage
24 class TestPackageApk(TestPackage):
25 """A helper class for running APK-based native tests."""
27 def __init__(self, suite_name):
28 """
29 Args:
30 suite_name: Name of the test suite (e.g. base_unittests).
31 """
32 TestPackage.__init__(self, suite_name)
33 if suite_name == 'content_browsertests':
34 self.suite_path = os.path.join(
35 constants.GetOutDirectory(), 'apks', '%s.apk' % suite_name)
36 self._package_info = constants.PACKAGE_INFO['content_browsertests']
37 elif suite_name == 'components_browsertests':
38 self.suite_path = os.path.join(
39 constants.GetOutDirectory(), 'apks', '%s.apk' % suite_name)
40 self._package_info = constants.PACKAGE_INFO['components_browsertests']
41 else:
42 self.suite_path = os.path.join(
43 constants.GetOutDirectory(), '%s_apk' % suite_name,
44 '%s-debug.apk' % suite_name)
45 self._package_info = constants.PACKAGE_INFO['gtest']
47 def _CreateCommandLineFileOnDevice(self, device, options):
48 device.WriteFile(self._package_info.cmdline_file,
49 self.suite_name + ' ' + options)
51 def _GetFifo(self):
52 # The test.fifo path is determined by:
53 # testing/android/native_test/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):
62 for i in range(100):
63 if device.FileExists(self._GetFifo()):
64 logging.info('Fifo created. Slept for %f secs' % (i * 0.5))
65 break
66 time.sleep(0.5)
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)
74 def _StartActivity(self, device, force_stop=True):
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=force_stop)
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 elif self.suite_name == 'components_browsertests':
98 try:
99 device.RunShellCommand(
100 'rm -r %s/components_shell' % device.GetExternalStoragePath(),
101 timeout=60 * 2)
102 except device_errors.CommandFailedError:
103 # TODO(jbudorick) Handle this exception appropriately once the
104 # conversions are done.
105 pass
107 #override
108 def CreateCommandLineFileOnDevice(self, device, test_filter, test_arguments):
109 self._CreateCommandLineFileOnDevice(
110 device, '--gtest_filter=%s %s' % (test_filter, test_arguments))
112 #override
113 def GetAllTests(self, device):
114 self._CreateCommandLineFileOnDevice(device, '--gtest_list_tests')
115 try:
116 self.tool.SetupEnvironment()
117 # Clear and start monitoring logcat.
118 self._ClearFifo(device)
119 self._StartActivity(device)
120 # Wait for native test to complete.
121 p = self._WatchFifo(device, timeout=30 * self.tool.GetTimeoutScale())
122 p.expect('<<ScopedMainEntryLogger')
123 p.close()
124 finally:
125 self.tool.CleanUpEnvironment()
126 # We need to strip the trailing newline.
127 content = [line.rstrip() for line in p.before.splitlines()]
128 return gtest_test_instance.ParseGTestListTests(content)
130 #override
131 def SpawnTestProcess(self, device):
132 try:
133 self.tool.SetupEnvironment()
134 self._ClearFifo(device)
135 # Doesn't need to stop an Activity because ClearApplicationState() is
136 # always called before this call and so it is already stopped at this
137 # point.
138 self._StartActivity(device, force_stop=False)
139 finally:
140 self.tool.CleanUpEnvironment()
141 logfile = android_commands.NewLineNormalizer(sys.stdout)
142 return self._WatchFifo(device, timeout=10, logfile=logfile)
144 #override
145 def Install(self, device):
146 self.tool.CopyFiles(device)
147 device.Install(self.suite_path)