blob: 54eda97dbc86632a032a2956d86d86f9ac9b749d [file] [log] [blame]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -07001# 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
7from os import path
8from test_case import TestCase
Dennis Kempin31c0fbd2012-07-30 13:20:37 -07009from test_factory import TestFactory
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070010from test_runner import TestRunner
Dennis Kempin1479e742012-07-31 17:31:01 -070011from test_verifier import TestVerifier
12import json
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070013import os
14import pprint
15import sys
16
17_help_text = """\
18Available Commands:
19-------------------
20$ touchtests run [all|testname]
21Executes tests. Either all of them and presents a resulting overall score.
22Or just a single named test.
23
24$ touchtests debug [testname]
25Same as run, but additionally displays the gestures library log.
26
27$ touchtests log [testname]
28Executes a test and prints the activity_log to stdout. Useful to view the
29results in tp_view.
30
31$ touchtests add [platform] [activity_log] [cmt_log]
32Adds a new test case. Each test belongs to a platform "lumpy", "cr48", etc.
33activity_log and cmt_log should point to the file names of log files collected
34from the device.
35You can trim activity_log in tp_view, touchtests will automatically trim the
36cmt_log accordingly.
37The test will be named after the filename of the activity_log and a dummy
38validation script is automatically generated.
39
40General Info:
41-------------
42testname arguments:
43Tests are always names as [platform]/[name of test case]. You can find the tests
44available in the tests folder.
45For example: lumpy/scroll_test
46
47Tests Folder:
48The tests folder contains a folder for each platform and all the test cases.
49Each 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
54Platform folders:
55To add a new platform you need to add a new folder to the Tests folder, and
56generate a platform.dat file. This can be done using the evemu-describe tool
57on the target platform:
58
59$ gmerge utouch-evemu
60$ evemu-describe /path/to/device > platform.dat
61"""
62
63
64def HelpExit():
65 """
66 Print help text and exit
67 """
68 print _help_text
69 exit()
70
Dennis Kempin1479e742012-07-31 17:31:01 -070071def 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 Kempin22c7c4d2012-07-26 13:04:24 -070082
83def 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
Dennis Kempin12968302012-08-09 15:37:09 -0700111def Run(glob, out_file=None, ref_file=None):
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700112 """
Dennis Kempin12968302012-08-09 15:37:09 -0700113 Run tests.
114 TODO(denniskempin): Pretty formatting with code extracted from fuzzy_check
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700115 """
116 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["REPLAY_TOOL"])
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700117 print "Running tests..."
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700118 results = runner.RunAll(glob)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700119
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700120 for key, value in results.items():
121 print "### Validation report for", key
122 print value["logs"]["validation"]
123
Dennis Kempin12968302012-08-09 15:37:09 -0700124 ref = {}
125 if ref_file:
126 ref = json.load(open(ref_file))
127
Dennis Kempin8cb1a892012-08-06 15:19:17 -0700128 print "Test Results:"
129 for key, value in results.items():
Dennis Kempin12968302012-08-09 15:37:09 -0700130 res = " " + key + ": " + value["result"] + " (" + str(value["score"]) + ")"
131 if key in ref:
132 ref_value = ref[key]
133 res = (res + " vs " + ref_value["result"] + " (" +
134 str(ref_value["score"]) + ")")
135 print res
Dennis Kempin8cb1a892012-08-06 15:19:17 -0700136 if value["result"] == "error":
137 print " ", value["error"]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700138
Dennis Kempin12968302012-08-09 15:37:09 -0700139 if out_file:
140 json.dump(results, open(out_file, "w"), indent=2)
141 print "results stored in:", out_file
142
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700143def Add(platform, activity_log, event_log):
144 """
145 Adds a new test case.
146 """
147 # determine test name from activity_log name
148 testname = path.splitext(path.basename(activity_log))[0]
149 testname = path.join(platform, testname)
Dennis Kempin31c0fbd2012-07-30 13:20:37 -0700150 factory = TestFactory(os.environ["TESTS_DIR"], os.environ["REPLAY_TOOL"])
151 case = factory.CreateTest(testname, activity_log, event_log)
152
Dennis Kempin49fe8a52012-08-03 11:27:40 -0700153 print "Test \"" + case.name + "\" created"
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700154
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700155def Main():
156 """
157 Main entry point for the console interface
158 """
159
160 # setup paths from environment variables
161 if "TESTS_DIR" not in os.environ:
162 print "Require TESTS_DIR environment variable"
163 exit(-1)
164
165 if "REPLAY_TOOL" not in os.environ:
166 print "Require REPLAY_TOOL environment variable"
167 exit(-1)
168
169 TestCase.tests_path = os.environ["TESTS_DIR"]
170 TestCase.replay_tool = os.environ["REPLAY_TOOL"]
171
172 # parse arguments and call command methods
173 if len(sys.argv) < 2:
174 HelpExit()
175
176 # call command method according to command line arguments
Dennis Kempin12968302012-08-09 15:37:09 -0700177 # todo: use an option parser library
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700178 cmd = sys.argv[1]
179
Dennis Kempin12968302012-08-09 15:37:09 -0700180 if cmd == "run":
181 if len(sys.argv) == 3:
182 test_name = sys.argv[2]
183 Run(test_name)
184 elif len(sys.argv) == 5 and sys.argv[2] == "--out":
185 test_name = sys.argv[4]
186 out = sys.argv[3]
187 Run(test_name, out)
188 elif len(sys.argv) == 5 and sys.argv[2] == "--ref":
189 test_name = sys.argv[4]
190 ref_file = sys.argv[3]
191 Run(test_name, None, ref_file)
192 else:
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700193 HelpExit()
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700194
195 elif cmd == "log":
196 if len(sys.argv) < 3:
197 HelpExit()
198 test_name = sys.argv[2]
199 Log(test_name)
200
Dennis Kempin1479e742012-07-31 17:31:01 -0700201 elif cmd == "verify":
202 if len(sys.argv) < 4:
203 HelpExit()
204 device_name = sys.argv[2]
205 test_name = sys.argv[3]
206 Verify(device_name, test_name)
207
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700208 elif cmd == "add":
209 if len(sys.argv) < 5:
210 HelpExit()
211 platform = sys.argv[2]
212 activity_log = sys.argv[3]
213 event_log = sys.argv[4]
214 Add(platform, activity_log, event_log)
215
216 else:
217 HelpExit()
218
219if __name__ == "__main__":
220 Main()