summaryrefslogtreecommitdiffstats
path: root/media/webrtc/trunk/build/android/pylib/test_package_apk.py
blob: 42b9ade68b33a506b3dc80fec29945b8f1706d63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.


import os
import shlex
import sys
import tempfile
import time

import android_commands
import constants
from test_package import TestPackage
from pylib import pexpect

class TestPackageApk(TestPackage):
  """A helper class for running APK-based native tests.

  Args:
    adb: ADB interface the tests are using.
    device: Device to run the tests.
    test_suite: A specific test suite to run, empty to run all.
    timeout: Timeout for each test.
    rebaseline: Whether or not to run tests in isolation and update the filter.
    performance_test: Whether or not performance test(s).
    cleanup_test_files: Whether or not to cleanup test files on device.
    tool: Name of the Valgrind tool.
    dump_debug_info: A debug_info object.
  """

  def __init__(self, adb, device, test_suite, timeout, rebaseline,
               performance_test, cleanup_test_files, tool,
               dump_debug_info):
    TestPackage.__init__(self, adb, device, test_suite, timeout,
                         rebaseline, performance_test, cleanup_test_files,
                         tool, dump_debug_info)

  def _CreateTestRunnerScript(self, options):
    command_line_file = tempfile.NamedTemporaryFile()
    # GTest expects argv[0] to be the executable path.
    command_line_file.write(self.test_suite_basename + ' ' + options)
    command_line_file.flush()
    self.adb.PushIfNeeded(command_line_file.name,
                          constants.TEST_EXECUTABLE_DIR +
                          '/chrome-native-tests-command-line')

  def _GetGTestReturnCode(self):
    return None

  def _GetFifo(self):
    # The test.fifo path is determined by:
    # testing/android/java/src/org/chromium/native_test/
    #     ChromeNativeTestActivity.java and
    # testing/android/native_test_launcher.cc
    return '/data/data/org.chromium.native_test/files/test.fifo'

  def _ClearFifo(self):
    self.adb.RunShellCommand('rm -f ' + self._GetFifo())

  def _WatchFifo(self, timeout, logfile=None):
    for i in range(10):
      if self.adb.FileExistsOnDevice(self._GetFifo()):
        print 'Fifo created...'
        break
      time.sleep(i)
    else:
      raise Exception('Unable to find fifo on device %s ' % self._GetFifo())
    args = shlex.split(self.adb.Adb()._target_arg)
    args += ['shell', 'cat', self._GetFifo()]
    return pexpect.spawn('adb', args, timeout=timeout, logfile=logfile)

  def GetAllTests(self):
    """Returns a list of all tests available in the test suite."""
    self._CreateTestRunnerScript('--gtest_list_tests')
    try:
      self.tool.SetupEnvironment()
      # Clear and start monitoring logcat.
      self._ClearFifo()
      self.adb.RunShellCommand(
          'am start -n '
          'org.chromium.native_test/'
          'org.chromium.native_test.ChromeNativeTestActivity')
      # Wait for native test to complete.
      p = self._WatchFifo(timeout=30 * self.tool.GetTimeoutScale())
      p.expect("<<ScopedMainEntryLogger")
      p.close()
    finally:
      self.tool.CleanUpEnvironment()
    # We need to strip the trailing newline.
    content = [line.rstrip() for line in p.before.splitlines()]
    ret = self._ParseGTestListTests(content)
    return ret

  def CreateTestRunnerScript(self, gtest_filter, test_arguments):
    self._CreateTestRunnerScript('--gtest_filter=%s %s' % (gtest_filter,
                                                           test_arguments))

  def RunTestsAndListResults(self):
    try:
      self.tool.SetupEnvironment()
      self._ClearFifo()
      self.adb.RunShellCommand(
       'am start -n '
        'org.chromium.native_test/'
        'org.chromium.native_test.ChromeNativeTestActivity')
    finally:
      self.tool.CleanUpEnvironment()
    logfile = android_commands.NewLineNormalizer(sys.stdout)
    return self._WatchTestOutput(self._WatchFifo(timeout=10, logfile=logfile))

  def StripAndCopyExecutable(self):
    # Always uninstall the previous one (by activity name); we don't
    # know what was embedded in it.
    self.adb.ManagedInstall(self.test_suite_full, False,
                            package_name='org.chromium.native_test')

  def _GetTestSuiteBaseName(self):
    """Returns the  base name of the test suite."""
    # APK test suite names end with '-debug.apk'
    return os.path.basename(self.test_suite).rsplit('-debug', 1)[0]