summaryrefslogtreecommitdiffstats
path: root/build/android
diff options
context:
space:
mode:
authorgkanwar@chromium.org <gkanwar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-20 19:11:30 +0000
committergkanwar@chromium.org <gkanwar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-20 19:11:30 +0000
commit803f65a729175e8b635e06e7c25fd4f8aed70382 (patch)
treebde23adc9d01e163ce5aad9756794e225977fb3d /build/android
parent8c77b1c24f28c47e71831e2ce484caf73cd13c1f (diff)
downloadchromium_src-803f65a729175e8b635e06e7c25fd4f8aed70382.zip
chromium_src-803f65a729175e8b635e06e7c25fd4f8aed70382.tar.gz
chromium_src-803f65a729175e8b635e06e7c25fd4f8aed70382.tar.bz2
[Android] Adds python option for EMMA coverage
This is a piece of the overall java coverage change. See: https://codereview.chromium.org/20210002/ NOTRY=True BUG=255644 Review URL: https://chromiumcodereview.appspot.com/23293006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@218531 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'build/android')
-rwxr-xr-xbuild/android/generate_emma_html.py80
-rw-r--r--build/android/pylib/constants.py1
-rw-r--r--build/android/pylib/instrumentation/test_options.py1
-rw-r--r--build/android/pylib/instrumentation/test_runner.py28
-rw-r--r--build/android/pylib/utils/command_option_parser.py76
-rwxr-xr-xbuild/android/test_runner.py48
6 files changed, 188 insertions, 46 deletions
diff --git a/build/android/generate_emma_html.py b/build/android/generate_emma_html.py
new file mode 100755
index 0000000..fa72792
--- /dev/null
+++ b/build/android/generate_emma_html.py
@@ -0,0 +1,80 @@
+#!/usr/bin/env python
+
+# Copyright 2013 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.
+
+"""Aggregates EMMA coverage files to produce html output."""
+
+import fnmatch
+import json
+import optparse
+import os
+import sys
+
+from pylib import cmd_helper
+from pylib import constants
+
+
+def _GetFilesWithExt(root_dir, ext):
+ """Gets all files with a given extension.
+
+ Args:
+ root_dir: Directory in which to search for files.
+ ext: Extension to look for (including dot)
+
+ Returns:
+ A list of absolute paths to files that match.
+ """
+ files = []
+ for root, _, filenames in os.walk(root_dir):
+ basenames = fnmatch.filter(filenames, '*.' + ext)
+ files.extend([os.path.join(root, basename)
+ for basename in basenames])
+
+ return files
+
+
+def main(argv):
+ option_parser = optparse.OptionParser()
+ option_parser.add_option('-o', '--output', help='HTML output filename.')
+ option_parser.add_option('-c', '--coverage-dir', default=None,
+ help=('Root of the directory in which to search for '
+ 'coverage data (.ec) files.'))
+ option_parser.add_option('-m', '--metadata-dir', default=None,
+ help=('Root of the directory in which to search for '
+ 'coverage metadata (.em) files.'))
+ options, args = option_parser.parse_args()
+
+ if not (options.coverage_dir and options.metadata_dir and options.output):
+ option_parser.error('All arguments are required.')
+
+ coverage_files = _GetFilesWithExt(options.coverage_dir, 'ec')
+ metadata_files = _GetFilesWithExt(options.metadata_dir, 'em')
+ print 'Found coverage files: %s' % str(coverage_files)
+ print 'Found metadata files: %s' % str(metadata_files)
+ sources_files = []
+ for f in metadata_files:
+ sources_file = os.path.join(os.path.dirname(f), 'emma_sources.txt')
+ with open(sources_file, 'r') as f:
+ sources_files.extend(json.load(f))
+ sources_files = [os.path.join(constants.DIR_SOURCE_ROOT, s)
+ for s in sources_files]
+
+ input_args = []
+ for f in coverage_files + metadata_files:
+ input_args.append('-in')
+ input_args.append(f)
+
+ output_args = ['-Dreport.html.out.file', options.output]
+ source_args = ['-sp', ','.join(sources_files)]
+
+ return cmd_helper.RunCmd(
+ ['java', '-cp',
+ os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'lib', 'emma.jar'),
+ 'emma', 'report', '-r', 'html']
+ + input_args + output_args + source_args)
+
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv))
diff --git a/build/android/pylib/constants.py b/build/android/pylib/constants.py
index bf35fff..b519282 100644
--- a/build/android/pylib/constants.py
+++ b/build/android/pylib/constants.py
@@ -14,7 +14,6 @@ DIR_SOURCE_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__),
ISOLATE_DEPS_DIR = os.path.join(DIR_SOURCE_ROOT, 'isolate_deps_dir')
EMULATOR_SDK_ROOT = os.path.abspath(os.path.join(DIR_SOURCE_ROOT, os.pardir,
os.pardir))
-
CHROME_PACKAGE = 'com.google.android.apps.chrome'
CHROME_ACTIVITY = 'com.google.android.apps.chrome.Main'
CHROME_DEVTOOLS_SOCKET = 'chrome_devtools_remote'
diff --git a/build/android/pylib/instrumentation/test_options.py b/build/android/pylib/instrumentation/test_options.py
index 63aee8b..4077ba1 100644
--- a/build/android/pylib/instrumentation/test_options.py
+++ b/build/android/pylib/instrumentation/test_options.py
@@ -17,6 +17,7 @@ InstrumentationOptions = collections.namedtuple('InstrumentationOptions', [
'save_perf_json',
'screenshot_failures',
'wait_for_debugger',
+ 'coverage_dir',
'test_apk',
'test_apk_path',
'test_apk_jar_path'])
diff --git a/build/android/pylib/instrumentation/test_runner.py b/build/android/pylib/instrumentation/test_runner.py
index ffa805c..73440f5 100644
--- a/build/android/pylib/instrumentation/test_runner.py
+++ b/build/android/pylib/instrumentation/test_runner.py
@@ -44,6 +44,7 @@ class TestRunner(base_test_runner.BaseTestRunner):
"""Responsible for running a series of tests connected to a single device."""
_DEVICE_DATA_DIR = 'chrome/test/data'
+ _DEVICE_COVERAGE_DIR = 'chrome/test/coverage'
_HOSTMACHINE_PERF_OUTPUT_FILE = '/tmp/chrome-profile'
_DEVICE_PERF_OUTPUT_SEARCH_PREFIX = (constants.DEVICE_PERF_OUTPUT_DIR +
'/chrome-profile*')
@@ -69,6 +70,7 @@ class TestRunner(base_test_runner.BaseTestRunner):
self.options = test_options
self.test_pkg = test_pkg
self.ports_to_forward = ports_to_forward
+ self.coverage_dir = test_options.coverage_dir
#override
def InstallTestPackage(self):
@@ -98,11 +100,11 @@ class TestRunner(base_test_runner.BaseTestRunner):
dst_src = dest_host_pair.split(':',1)
dst_layer = dst_src[0]
host_src = dst_src[1]
- host_test_files_path = constants.DIR_SOURCE_ROOT + '/' + host_src
+ host_test_files_path = '%s/%s' % (constants.DIR_SOURCE_ROOT, host_src)
if os.path.exists(host_test_files_path):
- self.adb.PushIfNeeded(host_test_files_path,
- self.adb.GetExternalStorage() + '/' +
- TestRunner._DEVICE_DATA_DIR + '/' + dst_layer)
+ self.adb.PushIfNeeded(host_test_files_path, '%s/%s/%s' % (
+ self.adb.GetExternalStorage(), TestRunner._DEVICE_DATA_DIR,
+ dst_layer))
self.tool.CopyFiles()
TestRunner._DEVICE_HAS_TEST_FILES[self.device] = True
@@ -110,11 +112,15 @@ class TestRunner(base_test_runner.BaseTestRunner):
ret = {}
if self.options.wait_for_debugger:
ret['debug'] = 'true'
+ if self.coverage_dir:
+ ret['coverage'] = 'true'
+ ret['coverageFile'] = self.coverage_device_file
+
return ret
def _TakeScreenshot(self, test):
"""Takes a screenshot from the device."""
- screenshot_name = os.path.join(constants.SCREENSHOTS_DIR, test + '.png')
+ screenshot_name = os.path.join(constants.SCREENSHOTS_DIR, '%s.png' % test)
logging.info('Taking screenshot named %s', screenshot_name)
self.adb.TakeScreenshot(screenshot_name)
@@ -156,6 +162,14 @@ class TestRunner(base_test_runner.BaseTestRunner):
# Make sure the forwarder is still running.
self._RestartHttpServerForwarderIfNecessary()
+ if self.coverage_dir:
+ coverage_basename = '%s.ec' % test
+ self.coverage_device_file = '%s/%s/%s' % (self.adb.GetExternalStorage(),
+ TestRunner._DEVICE_COVERAGE_DIR,
+ coverage_basename)
+ self.coverage_host_file = os.path.join(
+ self.coverage_dir, coverage_basename)
+
def _IsPerfTest(self, test):
"""Determines whether a test is a performance test.
@@ -198,6 +212,10 @@ class TestRunner(base_test_runner.BaseTestRunner):
self.TearDownPerfMonitoring(test)
+ if self.coverage_dir:
+ self.adb.Adb().Pull(self.coverage_device_file, self.coverage_host_file)
+ self.adb.RunShellCommand('rm -f %s' % self.coverage_device_file)
+
def TearDownPerfMonitoring(self, test):
"""Cleans up performance monitoring if the specified test required it.
diff --git a/build/android/pylib/utils/command_option_parser.py b/build/android/pylib/utils/command_option_parser.py
new file mode 100644
index 0000000..636d7e1
--- /dev/null
+++ b/build/android/pylib/utils/command_option_parser.py
@@ -0,0 +1,76 @@
+# Copyright 2013 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.
+
+"""An option parser which handles the first arg as a command.
+
+Add other nice functionality such as printing a list of commands
+and an example in usage.
+"""
+
+import optparse
+import sys
+
+
+class CommandOptionParser(optparse.OptionParser):
+ """Wrapper class for OptionParser to help with listing commands."""
+
+ def __init__(self, *args, **kwargs):
+ """Creates a CommandOptionParser.
+
+ Args:
+ commands_dict: A dictionary mapping command strings to an object defining
+ - add_options_func: Adds options to the option parser
+ - run_command_func: Runs the command itself.
+ example: An example command.
+ everything else: Passed to optparse.OptionParser contructor.
+ """
+ self.commands_dict = kwargs.pop('commands_dict', [])
+ self.example = kwargs.pop('example', '')
+ if not 'usage' in kwargs:
+ kwargs['usage'] = 'Usage: %prog <command> [options]'
+ optparse.OptionParser.__init__(self, *args, **kwargs)
+
+ #override
+ def get_usage(self):
+ normal_usage = optparse.OptionParser.get_usage(self)
+ command_list = self.get_command_list()
+ example = self.get_example()
+ return self.expand_prog_name(normal_usage + example + command_list)
+
+ #override
+ def get_command_list(self):
+ if self.commands_dict.keys():
+ return '\nCommands:\n %s\n' % '\n '.join(
+ sorted(self.commands_dict.keys()))
+ return ''
+
+ def get_example(self):
+ if self.example:
+ return '\nExample:\n %s\n' % self.example
+ return ''
+
+
+def ParseAndExecute(option_parser, argv=None):
+ """Parses options/args from argv and runs the specified command.
+
+ Args:
+ option_parser: A CommandOptionParser object.
+ argv: Command line arguments. If None, automatically draw from sys.argv.
+
+ Returns:
+ An exit code.
+ """
+ if not argv:
+ argv = sys.argv
+
+ if len(argv) < 2 or argv[1] not in option_parser.commands_dict:
+ # Parse args first, if this is '--help', optparse will print help and exit
+ option_parser.parse_args(argv)
+ option_parser.error('Invalid command.')
+
+ command = argv[1]
+ option_parser.commands_dict[command].add_options_func(option_parser)
+ options, args = option_parser.parse_args(argv)
+ return option_parser.commands_dict[command].run_command_func(
+ command, options, args, option_parser)
diff --git a/build/android/test_runner.py b/build/android/test_runner.py
index e83467b..2c87df9 100755
--- a/build/android/test_runner.py
+++ b/build/android/test_runner.py
@@ -35,6 +35,7 @@ from pylib.perf import test_options as perf_test_options
from pylib.perf import test_runner as perf_test_runner
from pylib.uiautomator import setup as uiautomator_setup
from pylib.uiautomator import test_options as uiautomator_test_options
+from pylib.utils import command_option_parser
from pylib.utils import report_results
from pylib.utils import run_tests_helper
@@ -230,6 +231,9 @@ def AddInstrumentationTestOptions(option_parser):
help=('The name of the apk containing the tests '
'(without the .apk extension; e.g. "ContentShellTest"). '
'Alternatively, this can be a full path to the apk.'))
+ option_parser.add_option('--coverage-dir',
+ help=('Directory in which to place all generated '
+ 'EMMA coverage files.'))
def ProcessInstrumentationOptions(options, error_func):
@@ -287,6 +291,7 @@ def ProcessInstrumentationOptions(options, error_func):
options.save_perf_json,
options.screenshot_failures,
options.wait_for_debugger,
+ options.coverage_dir,
options.test_apk,
options.test_apk_path,
options.test_apk_jar_path)
@@ -721,47 +726,10 @@ VALID_COMMANDS = {
}
-class CommandOptionParser(optparse.OptionParser):
- """Wrapper class for OptionParser to help with listing commands."""
-
- def __init__(self, *args, **kwargs):
- self.command_list = kwargs.pop('command_list', [])
- self.example = kwargs.pop('example', '')
- optparse.OptionParser.__init__(self, *args, **kwargs)
-
- #override
- def get_usage(self):
- normal_usage = optparse.OptionParser.get_usage(self)
- command_list = self.get_command_list()
- example = self.get_example()
- return self.expand_prog_name(normal_usage + example + command_list)
-
- #override
- def get_command_list(self):
- if self.command_list:
- return '\nCommands:\n %s\n' % '\n '.join(sorted(self.command_list))
- return ''
-
- def get_example(self):
- if self.example:
- return '\nExample:\n %s\n' % self.example
- return ''
-
-
def main(argv):
- option_parser = CommandOptionParser(
- usage='Usage: %prog <command> [options]',
- command_list=VALID_COMMANDS.keys())
-
- if len(argv) < 2 or argv[1] not in VALID_COMMANDS:
- # Parse args first, if this is '--help', optparse will display help and exit
- option_parser.parse_args(argv)
- option_parser.error('Invalid command.')
- command = argv[1]
- VALID_COMMANDS[command].add_options_func(option_parser)
- options, args = option_parser.parse_args(argv)
- return VALID_COMMANDS[command].run_command_func(
- command, options, args, option_parser)
+ option_parser = command_option_parser.CommandOptionParser(
+ commands_dict=VALID_COMMANDS)
+ return command_option_parser.ParseAndExecute(option_parser)
if __name__ == '__main__':