[Android] Launch chrome using an intent before running uiautomator tests.

Also use PACKAGE_INFO constants for specifying the package under test.

BUG=None
R=craigdh@chromium.org

Review URL: https://codereview.chromium.org/26422003

git-svn-id: http://src.chromium.org/svn/trunk/src/build@227535 4ff67af0-8c30-449e-8e8b-ad334ec8d88c
This commit is contained in:
frankf@chromium.org 2013-10-08 17:29:30 +00:00
Родитель a8aa673f38
Коммит a8881193b0
5 изменённых файлов: 49 добавлений и 34 удалений

Просмотреть файл

@ -8,8 +8,7 @@ import collections
MonkeyOptions = collections.namedtuple('MonkeyOptions', [
'verbose_count',
'package_name',
'activity_name',
'package',
'event_count',
'category',
'throttle',

Просмотреть файл

@ -6,6 +6,7 @@
import random
from pylib import constants
from pylib.base import base_test_result
from pylib.base import base_test_runner
@ -13,9 +14,11 @@ from pylib.base import base_test_runner
class TestRunner(base_test_runner.BaseTestRunner):
"""A TestRunner instance runs a monkey test on a single device."""
def __init__(self, test_options, device, shard_index):
def __init__(self, test_options, device, _):
super(TestRunner, self).__init__(device, None)
self.options = test_options
self._options = test_options
self._package = constants.PACKAGE_INFO[self._options.package].package
self._activity = constants.PACKAGE_INFO[self._options.package].activity
def _LaunchMonkeyTest(self):
"""Runs monkey test for a given package.
@ -24,18 +27,18 @@ class TestRunner(base_test_runner.BaseTestRunner):
Output from the monkey command on the device.
"""
timeout_ms = self.options.event_count * self.options.throttle * 1.5
timeout_ms = self._options.event_count * self._options.throttle * 1.5
cmd = ['monkey',
'-p %s' % self.options.package_name,
' '.join(['-c %s' % c for c in self.options.category]),
'--throttle %d' % self.options.throttle,
'-s %d' % (self.options.seed or random.randint(1, 100)),
'-v ' * self.options.verbose_count,
'-p %s' % self._package,
' '.join(['-c %s' % c for c in self._options.category]),
'--throttle %d' % self._options.throttle,
'-s %d' % (self._options.seed or random.randint(1, 100)),
'-v ' * self._options.verbose_count,
'--monitor-native-crashes',
'--kill-process-after-error',
self.options.extra_args,
'%d' % self.options.event_count]
self._options.extra_args,
'%d' % self._options.event_count]
return self.adb.RunShellCommand(' '.join(cmd), timeout_time=timeout_ms)
def RunTest(self, test_name):
@ -47,27 +50,27 @@ class TestRunner(base_test_runner.BaseTestRunner):
Returns:
A tuple of (TestRunResults, retry).
"""
self.adb.StartActivity(self.options.package_name,
self.options.activity_name,
self.adb.StartActivity(self._package,
self._activity,
wait_for_completion=True,
action='android.intent.action.MAIN',
force_stop=True)
# Chrome crashes are not always caught by Monkey test runner.
# Verify Chrome has the same PID before and after the test.
before_pids = self.adb.ExtractPid(self.options.package_name)
before_pids = self.adb.ExtractPid(self._package)
# Run the test.
output = ''
if before_pids:
output = '\n'.join(self._LaunchMonkeyTest())
after_pids = self.adb.ExtractPid(self.options.package_name)
after_pids = self.adb.ExtractPid(self._package)
crashed = (not before_pids or not after_pids
or after_pids[0] != before_pids[0])
results = base_test_result.TestRunResults()
success_pattern = 'Events injected: %d' % self.options.event_count
success_pattern = 'Events injected: %d' % self._options.event_count
if success_pattern in output and not crashed:
result = base_test_result.BaseTestResult(
test_name, base_test_result.ResultType.PASS, log=output)

Просмотреть файл

@ -18,4 +18,4 @@ UIAutomatorOptions = collections.namedtuple('UIAutomatorOptions', [
'screenshot_failures',
'uiautomator_jar',
'uiautomator_info_jar',
'package_name'])
'package'])

Просмотреть файл

@ -4,6 +4,7 @@
"""Class for running uiautomator tests on a single device."""
from pylib import constants
from pylib.instrumentation import test_options as instr_test_options
from pylib.instrumentation import test_runner as instr_test_runner
@ -42,7 +43,8 @@ class TestRunner(instr_test_runner.TestRunner):
super(TestRunner, self).__init__(instrumentation_options, device,
shard_index, test_pkg, ports_to_forward)
self.package_name = test_options.package_name
self._package = constants.PACKAGE_INFO[test_options.package].package
self._activity = constants.PACKAGE_INFO[test_options.package].activity
#override
def InstallTestPackage(self):
@ -54,10 +56,15 @@ class TestRunner(instr_test_runner.TestRunner):
#override
def _RunTest(self, test, timeout):
self.adb.ClearApplicationState(self.package_name)
self.adb.ClearApplicationState(self._package)
if 'Feature:FirstRunExperience' in self.test_pkg.GetTestAnnotations(test):
self.flags.RemoveFlags(['--disable-fre'])
else:
self.flags.AddFlags(['--disable-fre'])
self.adb.StartActivity(self._package,
self._activity,
wait_for_completion=True,
action='android.intent.action.MAIN',
force_stop=True)
return self.adb.RunUIAutomatorTest(
test, self.test_pkg.GetPackageName(), timeout)

Просмотреть файл

@ -300,10 +300,11 @@ def AddUIAutomatorTestOptions(option_parser):
option_parser.commands_dict = {}
option_parser.example = (
'%prog uiautomator --test-jar=chromium_testshell_uiautomator_tests'
' --package-name=org.chromium.chrome.testshell')
' --package=chromium_test_shell')
option_parser.add_option(
'--package-name',
help='The package name used by the apk containing the application.')
'--package',
help=('Package under test. Possible values: %s' %
constants.PACKAGE_INFO.keys()))
option_parser.add_option(
'--test-jar', dest='test_jar',
help=('The name of the dexed jar containing the tests (without the '
@ -328,8 +329,11 @@ def ProcessUIAutomatorOptions(options, error_func):
ProcessJavaTestOptions(options, error_func)
if not options.package_name:
error_func('--package-name must be specified.')
if not options.package:
error_func('--package is required.')
if options.package not in constants.PACKAGE_INFO:
error_func('Invalid package.')
if not options.test_jar:
error_func('--test-jar must be specified.')
@ -358,7 +362,7 @@ def ProcessUIAutomatorOptions(options, error_func):
options.screenshot_failures,
options.uiautomator_jar,
options.uiautomator_info_jar,
options.package_name)
options.package)
def AddMonkeyTestOptions(option_parser):
@ -367,12 +371,12 @@ def AddMonkeyTestOptions(option_parser):
option_parser.usage = '%prog monkey [options]'
option_parser.commands_dict = {}
option_parser.example = (
'%prog monkey --package-name=org.chromium.content_shell_apk'
' --activity-name=.ContentShellActivity')
'%prog monkey --package=chromium_test_shell')
option_parser.add_option('--package-name', help='Allowed package.')
option_parser.add_option(
'--activity-name', help='Name of the activity to start.')
'--package',
help=('Package under test. Possible values: %s' %
constants.PACKAGE_INFO.keys()))
option_parser.add_option(
'--event-count', default=10000, type='int',
help='Number of events to generate [default: %default].')
@ -405,8 +409,11 @@ def ProcessMonkeyTestOptions(options, error_func):
A MonkeyOptions named tuple which contains all options relevant to
monkey tests.
"""
if not options.package_name:
error_func('Package name is required.')
if not options.package:
error_func('--package is required.')
if options.package not in constants.PACKAGE_INFO:
error_func('Invalid package.')
category = options.category
if category:
@ -414,8 +421,7 @@ def ProcessMonkeyTestOptions(options, error_func):
return monkey_test_options.MonkeyOptions(
options.verbose_count,
options.package_name,
options.activity_name,
options.package,
options.event_count,
category,
options.throttle,