| OLD | NEW |
| (Empty) |
| 1 # Copyright 2015 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 """Apptest is a Mojo application that interacts with another Mojo application | |
| 6 and verifies assumptions about behavior of the app being tested. | |
| 7 """ | |
| 8 | |
| 9 import logging | |
| 10 import time | |
| 11 | |
| 12 | |
| 13 _logger = logging.getLogger() | |
| 14 | |
| 15 | |
| 16 def _build_shell_arguments(shell_args, apptest_url, apptest_args): | |
| 17 """Builds the list of arguments for the shell. | |
| 18 | |
| 19 Args: | |
| 20 shell_args: List of arguments for the shell run. | |
| 21 apptest_url: Url of the apptest app to run. | |
| 22 apptest_args: Parameters to be passed to the apptest app. | |
| 23 | |
| 24 Returns: | |
| 25 Single list of shell arguments. | |
| 26 """ | |
| 27 result = list(shell_args) | |
| 28 if apptest_args: | |
| 29 result.append("--args-for=%s %s" % (apptest_url, " ".join(apptest_args))) | |
| 30 result.append(apptest_url) | |
| 31 return result | |
| 32 | |
| 33 | |
| 34 def run_apptest(shell, shell_args, apptest_url, apptest_args, output_test): | |
| 35 """Runs shell with the given arguments, retrieves the output and applies | |
| 36 |output_test| to determine if the run was successful. | |
| 37 | |
| 38 Args: | |
| 39 shell: Wrapper around concrete Mojo shell, implementing devtools Shell | |
| 40 interface. | |
| 41 shell_args: List of arguments for the shell run. | |
| 42 apptest_url: Url of the apptest app to run. | |
| 43 apptest_args: Parameters to be passed to the apptest app. | |
| 44 output_test: Function accepting the shell output and returning True iff | |
| 45 the output indicates a successful run. | |
| 46 | |
| 47 Returns: | |
| 48 True iff the test succeeded, False otherwise. | |
| 49 """ | |
| 50 arguments = _build_shell_arguments(shell_args, apptest_url, apptest_args) | |
| 51 command_line = "mojo_shell " + " ".join(["%r" % x for x in arguments]) | |
| 52 | |
| 53 _logger.debug("Starting: " + command_line) | |
| 54 start_time = time.time() | |
| 55 (exit_code, output) = shell.RunAndGetOutput(arguments) | |
| 56 run_time = time.time() - start_time | |
| 57 _logger.debug("Completed: " + command_line) | |
| 58 | |
| 59 # Only log if it took more than 3 second. | |
| 60 if run_time >= 3: | |
| 61 _logger.info("Test took %.3f seconds: %s" % (run_time, command_line)) | |
| 62 | |
| 63 if exit_code or not output_test(output): | |
| 64 print 'Failed test: %r' % command_line | |
| 65 if exit_code: | |
| 66 print ' due to shell exit code %d' % exit_code | |
| 67 else: | |
| 68 print ' due to test results' | |
| 69 print 72 * '-' | |
| 70 print output | |
| 71 print 72 * '-' | |
| 72 return False | |
| 73 return True | |
| OLD | NEW |