Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS 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 | # This module is the main module for the console interface. It takes care |
| 6 | # of parsing the command line arguments and formating the output |
| 7 | from os import path |
| 8 | from test_case import TestCase |
Dennis Kempin | 31c0fbd | 2012-07-30 13:20:37 -0700 | [diff] [blame] | 9 | from test_factory import TestFactory |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 10 | from test_runner import TestRunner |
| 11 | import os |
| 12 | import pprint |
| 13 | import sys |
| 14 | |
| 15 | _help_text = """\ |
| 16 | Available Commands: |
| 17 | ------------------- |
| 18 | $ touchtests run [all|testname] |
| 19 | Executes tests. Either all of them and presents a resulting overall score. |
| 20 | Or just a single named test. |
| 21 | |
| 22 | $ touchtests debug [testname] |
| 23 | Same as run, but additionally displays the gestures library log. |
| 24 | |
| 25 | $ touchtests log [testname] |
| 26 | Executes a test and prints the activity_log to stdout. Useful to view the |
| 27 | results in tp_view. |
| 28 | |
| 29 | $ touchtests add [platform] [activity_log] [cmt_log] |
| 30 | Adds a new test case. Each test belongs to a platform "lumpy", "cr48", etc. |
| 31 | activity_log and cmt_log should point to the file names of log files collected |
| 32 | from the device. |
| 33 | You can trim activity_log in tp_view, touchtests will automatically trim the |
| 34 | cmt_log accordingly. |
| 35 | The test will be named after the filename of the activity_log and a dummy |
| 36 | validation script is automatically generated. |
| 37 | |
| 38 | General Info: |
| 39 | ------------- |
| 40 | testname arguments: |
| 41 | Tests are always names as [platform]/[name of test case]. You can find the tests |
| 42 | available in the tests folder. |
| 43 | For example: lumpy/scroll_test |
| 44 | |
| 45 | Tests Folder: |
| 46 | The tests folder contains a folder for each platform and all the test cases. |
| 47 | Each test case is made up of 3 files: |
| 48 | [testcase].py which contains the validation script |
| 49 | [testcase].log which contains the input_event log |
| 50 | [testcase].props which contains user_defined properties passed to gestures lib. |
| 51 | |
| 52 | Platform folders: |
| 53 | To add a new platform you need to add a new folder to the Tests folder, and |
| 54 | generate a platform.dat file. This can be done using the evemu-describe tool |
| 55 | on the target platform: |
| 56 | |
| 57 | $ gmerge utouch-evemu |
| 58 | $ evemu-describe /path/to/device > platform.dat |
| 59 | """ |
| 60 | |
| 61 | |
| 62 | def HelpExit(): |
| 63 | """ |
| 64 | Print help text and exit |
| 65 | """ |
| 66 | print _help_text |
| 67 | exit() |
| 68 | |
| 69 | |
| 70 | def Log(glob, debug=False): |
| 71 | """ |
| 72 | Print only the activity log. The user can pipe the output of this command |
| 73 | to be used in other tools like tpview. |
| 74 | """ |
| 75 | runner = TestRunner(os.environ["TESTS_DIR"], os.environ["REPLAY_TOOL"]) |
| 76 | cases = runner.DiscoverTestCases(glob) |
| 77 | |
| 78 | # check number of test cases |
| 79 | if len(cases) > 1: |
| 80 | print "Multiple test cases match '" + str(glob) + "':" |
| 81 | for case in cases: |
| 82 | print " -", case.name |
| 83 | return |
| 84 | |
| 85 | if len(cases) < 1: |
| 86 | print "Cannot find test " + glob |
| 87 | return |
| 88 | |
| 89 | # run test and print results |
| 90 | results = runner.RunTest(cases[0]) |
| 91 | if results["error"]: |
| 92 | print results["error"] |
| 93 | return |
| 94 | |
| 95 | print results["logs"]["activity"] |
| 96 | |
| 97 | |
| 98 | def Run(glob, debug=False): |
| 99 | """ |
| 100 | Run tests. For now just print all of the results as a data structure. |
| 101 | TODO(denniskempin): Pretty formating of results |
| 102 | """ |
| 103 | runner = TestRunner(os.environ["TESTS_DIR"], os.environ["REPLAY_TOOL"]) |
| 104 | results = runner.RunAll(glob) |
| 105 | pprint.pprint(results) |
| 106 | |
| 107 | |
| 108 | def Add(platform, activity_log, event_log): |
| 109 | """ |
| 110 | Adds a new test case. |
| 111 | """ |
| 112 | # determine test name from activity_log name |
| 113 | testname = path.splitext(path.basename(activity_log))[0] |
| 114 | testname = path.join(platform, testname) |
Dennis Kempin | 31c0fbd | 2012-07-30 13:20:37 -0700 | [diff] [blame] | 115 | factory = TestFactory(os.environ["TESTS_DIR"], os.environ["REPLAY_TOOL"]) |
| 116 | case = factory.CreateTest(testname, activity_log, event_log) |
| 117 | |
| 118 | runner = TestRunner(os.environ["TESTS_DIR"], os.environ["REPLAY_TOOL"]) |
| 119 | result = runner.RunTest(case) |
| 120 | print result["gestures"] |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 121 | |
| 122 | |
| 123 | def Main(): |
| 124 | """ |
| 125 | Main entry point for the console interface |
| 126 | """ |
| 127 | |
| 128 | # setup paths from environment variables |
| 129 | if "TESTS_DIR" not in os.environ: |
| 130 | print "Require TESTS_DIR environment variable" |
| 131 | exit(-1) |
| 132 | |
| 133 | if "REPLAY_TOOL" not in os.environ: |
| 134 | print "Require REPLAY_TOOL environment variable" |
| 135 | exit(-1) |
| 136 | |
| 137 | TestCase.tests_path = os.environ["TESTS_DIR"] |
| 138 | TestCase.replay_tool = os.environ["REPLAY_TOOL"] |
| 139 | |
| 140 | # parse arguments and call command methods |
| 141 | if len(sys.argv) < 2: |
| 142 | HelpExit() |
| 143 | |
| 144 | # call command method according to command line arguments |
| 145 | cmd = sys.argv[1] |
| 146 | |
| 147 | if cmd == "run" or cmd == "debug": |
| 148 | if len(sys.argv) < 3: |
| 149 | HelpExit() |
| 150 | test_name = sys.argv[2] |
| 151 | Run(test_name, (cmd == "debug")) |
| 152 | |
| 153 | elif cmd == "log": |
| 154 | if len(sys.argv) < 3: |
| 155 | HelpExit() |
| 156 | test_name = sys.argv[2] |
| 157 | Log(test_name) |
| 158 | |
| 159 | elif cmd == "add": |
| 160 | if len(sys.argv) < 5: |
| 161 | HelpExit() |
| 162 | platform = sys.argv[2] |
| 163 | activity_log = sys.argv[3] |
| 164 | event_log = sys.argv[4] |
| 165 | Add(platform, activity_log, event_log) |
| 166 | |
| 167 | else: |
| 168 | HelpExit() |
| 169 | |
| 170 | if __name__ == "__main__": |
| 171 | Main() |