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