Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / build / android / pylib / gtest / test_package_apk.py
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.
4
5 """Defines TestPackageApk to help run APK-based native tests."""
6 # pylint: disable=W0212
7
8 import logging
9 import os
10 import shlex
11 import sys
12 import tempfile
13 import time
14
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
20
21
22 class TestPackageApk(TestPackage):
23   """A helper class for running APK-based native tests."""
24
25   def __init__(self, suite_name):
26     """
27     Args:
28       suite_name: Name of the test suite (e.g. base_unittests).
29     """
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']
35     else:
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']
40
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)
49
50   def _GetFifo(self):
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'
56
57   def _ClearFifo(self, device):
58     device.old_interface.RunShellCommand('rm -f ' + self._GetFifo())
59
60   def _WatchFifo(self, device, timeout, logfile=None):
61     for i in range(10):
62       if device.old_interface.FileExistsOnDevice(self._GetFifo()):
63         logging.info('Fifo created.')
64         break
65       time.sleep(i)
66     else:
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)
72
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',
79         force_stop=True)
80
81   #override
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
85     # files over time.
86     if self.suite_name == 'content_browsertests':
87       device.old_interface.RunShellCommand(
88           'rm -r %s/content_shell' % device.old_interface.GetExternalStorage(),
89           timeout_time=60 * 2)
90
91   #override
92   def CreateCommandLineFileOnDevice(self, device, test_filter, test_arguments):
93     self._CreateCommandLineFileOnDevice(
94         device, '--gtest_filter=%s %s' % (test_filter, test_arguments))
95
96   #override
97   def GetAllTests(self, device):
98     self._CreateCommandLineFileOnDevice(device, '--gtest_list_tests')
99     try:
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')
107       p.close()
108     finally:
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)
113
114   #override
115   def SpawnTestProcess(self, device):
116     try:
117       self.tool.SetupEnvironment()
118       self._ClearFifo(device)
119       self._StartActivity(device)
120     finally:
121       self.tool.CleanUpEnvironment()
122     logfile = android_commands.NewLineNormalizer(sys.stdout)
123     return self._WatchFifo(device, timeout=10, logfile=logfile)
124
125   #override
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)