- add sources.
[platform/framework/web/crosswalk.git] / src / build / android / pylib / monkey / test_runner.py
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 """Runs a monkey test on a single device."""
6
7 import random
8
9 from pylib import constants
10 from pylib.base import base_test_result
11 from pylib.base import base_test_runner
12
13
14 class TestRunner(base_test_runner.BaseTestRunner):
15   """A TestRunner instance runs a monkey test on a single device."""
16
17   def __init__(self, test_options, device, _):
18     super(TestRunner, self).__init__(device, None)
19     self._options = test_options
20     self._package = constants.PACKAGE_INFO[self._options.package].package
21     self._activity = constants.PACKAGE_INFO[self._options.package].activity
22
23   def _LaunchMonkeyTest(self):
24     """Runs monkey test for a given package.
25
26     Returns:
27       Output from the monkey command on the device.
28     """
29
30     timeout_ms = self._options.event_count * self._options.throttle * 1.5
31
32     cmd = ['monkey',
33            '-p %s' % self._package,
34            ' '.join(['-c %s' % c for c in self._options.category]),
35            '--throttle %d' % self._options.throttle,
36            '-s %d' % (self._options.seed or random.randint(1, 100)),
37            '-v ' * self._options.verbose_count,
38            '--monitor-native-crashes',
39            '--kill-process-after-error',
40            self._options.extra_args,
41            '%d' % self._options.event_count]
42     return self.adb.RunShellCommand(' '.join(cmd), timeout_time=timeout_ms)
43
44   def RunTest(self, test_name):
45     """Run a Monkey test on the device.
46
47     Args:
48       test_name: String to use for logging the test result.
49
50     Returns:
51       A tuple of (TestRunResults, retry).
52     """
53     self.adb.StartActivity(self._package,
54                            self._activity,
55                            wait_for_completion=True,
56                            action='android.intent.action.MAIN',
57                            force_stop=True)
58
59     # Chrome crashes are not always caught by Monkey test runner.
60     # Verify Chrome has the same PID before and after the test.
61     before_pids = self.adb.ExtractPid(self._package)
62
63     # Run the test.
64     output = ''
65     if before_pids:
66       output = '\n'.join(self._LaunchMonkeyTest())
67       after_pids = self.adb.ExtractPid(self._package)
68
69     crashed = (not before_pids or not after_pids
70                or after_pids[0] != before_pids[0])
71
72     results = base_test_result.TestRunResults()
73     success_pattern = 'Events injected: %d' % self._options.event_count
74     if success_pattern in output and not crashed:
75       result = base_test_result.BaseTestResult(
76           test_name, base_test_result.ResultType.PASS, log=output)
77     else:
78       result = base_test_result.BaseTestResult(
79           test_name, base_test_result.ResultType.FAIL, log=output)
80     results.AddResult(result)
81     return results, False