media/webrtc/trunk/build/android/pylib/test_package_apk.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/build/android/pylib/test_package_apk.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,121 @@
     1.4 +# Copyright (c) 2012 The Chromium Authors. All rights reserved.
     1.5 +# Use of this source code is governed by a BSD-style license that can be
     1.6 +# found in the LICENSE file.
     1.7 +
     1.8 +
     1.9 +import os
    1.10 +import shlex
    1.11 +import sys
    1.12 +import tempfile
    1.13 +import time
    1.14 +
    1.15 +import android_commands
    1.16 +import constants
    1.17 +from test_package import TestPackage
    1.18 +from pylib import pexpect
    1.19 +
    1.20 +class TestPackageApk(TestPackage):
    1.21 +  """A helper class for running APK-based native tests.
    1.22 +
    1.23 +  Args:
    1.24 +    adb: ADB interface the tests are using.
    1.25 +    device: Device to run the tests.
    1.26 +    test_suite: A specific test suite to run, empty to run all.
    1.27 +    timeout: Timeout for each test.
    1.28 +    rebaseline: Whether or not to run tests in isolation and update the filter.
    1.29 +    performance_test: Whether or not performance test(s).
    1.30 +    cleanup_test_files: Whether or not to cleanup test files on device.
    1.31 +    tool: Name of the Valgrind tool.
    1.32 +    dump_debug_info: A debug_info object.
    1.33 +  """
    1.34 +
    1.35 +  def __init__(self, adb, device, test_suite, timeout, rebaseline,
    1.36 +               performance_test, cleanup_test_files, tool,
    1.37 +               dump_debug_info):
    1.38 +    TestPackage.__init__(self, adb, device, test_suite, timeout,
    1.39 +                         rebaseline, performance_test, cleanup_test_files,
    1.40 +                         tool, dump_debug_info)
    1.41 +
    1.42 +  def _CreateTestRunnerScript(self, options):
    1.43 +    command_line_file = tempfile.NamedTemporaryFile()
    1.44 +    # GTest expects argv[0] to be the executable path.
    1.45 +    command_line_file.write(self.test_suite_basename + ' ' + options)
    1.46 +    command_line_file.flush()
    1.47 +    self.adb.PushIfNeeded(command_line_file.name,
    1.48 +                          constants.TEST_EXECUTABLE_DIR +
    1.49 +                          '/chrome-native-tests-command-line')
    1.50 +
    1.51 +  def _GetGTestReturnCode(self):
    1.52 +    return None
    1.53 +
    1.54 +  def _GetFifo(self):
    1.55 +    # The test.fifo path is determined by:
    1.56 +    # testing/android/java/src/org/chromium/native_test/
    1.57 +    #     ChromeNativeTestActivity.java and
    1.58 +    # testing/android/native_test_launcher.cc
    1.59 +    return '/data/data/org.chromium.native_test/files/test.fifo'
    1.60 +
    1.61 +  def _ClearFifo(self):
    1.62 +    self.adb.RunShellCommand('rm -f ' + self._GetFifo())
    1.63 +
    1.64 +  def _WatchFifo(self, timeout, logfile=None):
    1.65 +    for i in range(10):
    1.66 +      if self.adb.FileExistsOnDevice(self._GetFifo()):
    1.67 +        print 'Fifo created...'
    1.68 +        break
    1.69 +      time.sleep(i)
    1.70 +    else:
    1.71 +      raise Exception('Unable to find fifo on device %s ' % self._GetFifo())
    1.72 +    args = shlex.split(self.adb.Adb()._target_arg)
    1.73 +    args += ['shell', 'cat', self._GetFifo()]
    1.74 +    return pexpect.spawn('adb', args, timeout=timeout, logfile=logfile)
    1.75 +
    1.76 +  def GetAllTests(self):
    1.77 +    """Returns a list of all tests available in the test suite."""
    1.78 +    self._CreateTestRunnerScript('--gtest_list_tests')
    1.79 +    try:
    1.80 +      self.tool.SetupEnvironment()
    1.81 +      # Clear and start monitoring logcat.
    1.82 +      self._ClearFifo()
    1.83 +      self.adb.RunShellCommand(
    1.84 +          'am start -n '
    1.85 +          'org.chromium.native_test/'
    1.86 +          'org.chromium.native_test.ChromeNativeTestActivity')
    1.87 +      # Wait for native test to complete.
    1.88 +      p = self._WatchFifo(timeout=30 * self.tool.GetTimeoutScale())
    1.89 +      p.expect("<<ScopedMainEntryLogger")
    1.90 +      p.close()
    1.91 +    finally:
    1.92 +      self.tool.CleanUpEnvironment()
    1.93 +    # We need to strip the trailing newline.
    1.94 +    content = [line.rstrip() for line in p.before.splitlines()]
    1.95 +    ret = self._ParseGTestListTests(content)
    1.96 +    return ret
    1.97 +
    1.98 +  def CreateTestRunnerScript(self, gtest_filter, test_arguments):
    1.99 +    self._CreateTestRunnerScript('--gtest_filter=%s %s' % (gtest_filter,
   1.100 +                                                           test_arguments))
   1.101 +
   1.102 +  def RunTestsAndListResults(self):
   1.103 +    try:
   1.104 +      self.tool.SetupEnvironment()
   1.105 +      self._ClearFifo()
   1.106 +      self.adb.RunShellCommand(
   1.107 +       'am start -n '
   1.108 +        'org.chromium.native_test/'
   1.109 +        'org.chromium.native_test.ChromeNativeTestActivity')
   1.110 +    finally:
   1.111 +      self.tool.CleanUpEnvironment()
   1.112 +    logfile = android_commands.NewLineNormalizer(sys.stdout)
   1.113 +    return self._WatchTestOutput(self._WatchFifo(timeout=10, logfile=logfile))
   1.114 +
   1.115 +  def StripAndCopyExecutable(self):
   1.116 +    # Always uninstall the previous one (by activity name); we don't
   1.117 +    # know what was embedded in it.
   1.118 +    self.adb.ManagedInstall(self.test_suite_full, False,
   1.119 +                            package_name='org.chromium.native_test')
   1.120 +
   1.121 +  def _GetTestSuiteBaseName(self):
   1.122 +    """Returns the  base name of the test suite."""
   1.123 +    # APK test suite names end with '-debug.apk'
   1.124 +    return os.path.basename(self.test_suite).rsplit('-debug', 1)[0]

mercurial