Skip to content

Commit

Permalink
[Android] Launch chrome using an intent before running uiautomator te…
Browse files Browse the repository at this point in the history
…sts.

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: svn://svn.chromium.org/chrome/trunk/src@227535 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
frankf@chromium.org committed Oct 8, 2013
1 parent 3118d34 commit a8886c8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 34 deletions.
3 changes: 1 addition & 2 deletions build/android/pylib/monkey/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

MonkeyOptions = collections.namedtuple('MonkeyOptions', [
'verbose_count',
'package_name',
'activity_name',
'package',
'event_count',
'category',
'throttle',
Expand Down
33 changes: 18 additions & 15 deletions build/android/pylib/monkey/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@

import random

from pylib import constants
from pylib.base import base_test_result
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.
Expand All @@ -24,18 +27,18 @@ def _LaunchMonkeyTest(self):
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):
Expand All @@ -47,27 +50,27 @@ def RunTest(self, test_name):
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)
Expand Down
2 changes: 1 addition & 1 deletion build/android/pylib/uiautomator/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
'screenshot_failures',
'uiautomator_jar',
'uiautomator_info_jar',
'package_name'])
'package'])
11 changes: 9 additions & 2 deletions build/android/pylib/uiautomator/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -42,7 +43,8 @@ def __init__(self, test_options, device, shard_index, test_pkg,
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):
Expand All @@ -54,10 +56,15 @@ def PushDataDeps(self):

#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)
34 changes: 20 additions & 14 deletions build/android/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '
Expand All @@ -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.')
Expand Down Expand Up @@ -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):
Expand All @@ -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].')
Expand Down Expand Up @@ -405,17 +409,19 @@ 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:
category = options.category.split(',')

return monkey_test_options.MonkeyOptions(
options.verbose_count,
options.package_name,
options.activity_name,
options.package,
options.event_count,
category,
options.throttle,
Expand Down

0 comments on commit a8886c8

Please sign in to comment.