- add sources.
[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
7 import logging
8 import os
9 import shlex
10 import sys
11 import tempfile
12 import time
13
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
19
20 from test_package import TestPackage
21
22
23 class TestPackageApk(TestPackage):
24   """A helper class for running APK-based native tests."""
25
26   def __init__(self, suite_name):
27     """
28     Args:
29       suite_name: Name of the test suite (e.g. base_unittests).
30     """
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']
36     else:
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']
41
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)
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, adb):
58     adb.RunShellCommand('rm -f ' + self._GetFifo())
59
60   def _WatchFifo(self, adb, timeout, logfile=None):
61     for i in range(10):
62       if adb.FileExistsOnDevice(self._GetFifo()):
63         logging.info('Fifo created.')
64         break
65       time.sleep(i)
66     else:
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)
72
73   def _StartActivity(self, adb):
74     adb.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, adb):
83     adb.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       adb.RunShellCommand(
88           'rm -r %s/content_shell' % adb.GetExternalStorage(),
89           timeout_time=60 * 2)
90
91   #override
92   def CreateCommandLineFileOnDevice(self, adb, test_filter, test_arguments):
93     self._CreateCommandLineFileOnDevice(
94         adb, '--gtest_filter=%s %s' % (test_filter, test_arguments))
95
96   #override
97   def GetAllTests(self, adb):
98     self._CreateCommandLineFileOnDevice(adb, '--gtest_list_tests')
99     try:
100       self.tool.SetupEnvironment()
101       # Clear and start monitoring logcat.
102       self._ClearFifo(adb)
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')
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, adb):
116     try:
117       self.tool.SetupEnvironment()
118       self._ClearFifo(adb)
119       self._StartActivity(adb)
120     finally:
121       self.tool.CleanUpEnvironment()
122     logfile = android_commands.NewLineNormalizer(sys.stdout)
123     return self._WatchFifo(adb, timeout=10, logfile=logfile)
124
125   #override
126   def Install(self, adb):
127     self.tool.CopyFiles()
128     adb.ManagedInstall(self.suite_path, False,
129                             package_name=self._package_info.package)