blob: 58caf100647b94a1d594901ce27e7ba1abde5991 [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
Dennis Kempinc2b59862013-02-20 14:02:25 -08007from optparse import OptionParser
Ilja H. Friedela35b0132013-03-13 21:48:14 -07008from os import path
Dennis Kempinc2b59862013-02-20 14:02:25 -08009from tempfile import NamedTemporaryFile
Dennis Kempin725ee5b2012-08-10 14:17:15 -070010import json
11import math
12import os
Ilja H. Friedela35b0132013-03-13 21:48:14 -070013import pprint
Dennis Kempin725ee5b2012-08-10 14:17:15 -070014import sys
15
Dennis Kempinc2b59862013-02-20 14:02:25 -080016from mtedit.editor import LogEditor
17from mtedit.log import Log
18
Dennis Kempin725ee5b2012-08-10 14:17:15 -070019from table import Table
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070020from test_case import TestCase
Dennis Kempin31c0fbd2012-07-30 13:20:37 -070021from test_factory import TestFactory
Dennis Kempin963fb152013-01-11 15:40:19 -080022from test_runner import ParallelTestRunner as TestRunner
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070023
24_help_text = """\
Dennis Kempinc2b59862013-02-20 14:02:25 -080025Multitouch Regression Test Suite:
26---------------------------------
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070027
Dennis Kempinc2b59862013-02-20 14:02:25 -080028$ %prog [all|glob]
29Executes tests. Either all of them or selected tests by providing a glob match
30In order to test for regressions use:
31$ %prog all --out filename
32make a change
33$ %prog all --ref filename
34Which will display the changes in test results compared to before the change
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070035
Dennis Kempinc2b59862013-02-20 14:02:25 -080036$ %prog testname -v %info%
37Run test and display information. %info% can be:
38- a or activity: to view the touchpad activity in mtedit
39- g or gestures: to view the generated gestures
40- al or activity-log: to view the generated activity log
41- gl or gestures-log: to view the generated gestures log
42- el or evdev-log: to view the generated evdev log
43
44$ %prog test_name -c %source%
45Create a new test case from %source%. Source can be:
46- A feedback URL
47- A device IP
48- The path to an activity log file
49When using a file name test.log, %prog will look at test.log.evdev
50for the evdev log file. You can optionally supply -e to override this path.
51%prog will display an URL where you can trim the log file, the trimmed
52log file will then be used to create the new test case. Specify --no-edit in
53case you do not want to use the original files without trimming.
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070054
55General Info:
56-------------
57testname arguments:
58Tests are always names as [platform]/[name of test case]. You can find the tests
59available in the tests folder.
60For example: lumpy/scroll_test
61
62Tests Folder:
63The tests folder contains a folder for each platform and all the test cases.
64Each test case is made up of 3 files:
65[testcase].py which contains the validation script
66[testcase].log which contains the input_event log
67[testcase].props which contains user_defined properties passed to gestures lib.
68
69Platform folders:
70To add a new platform you need to add a new folder to the Tests folder, and
71generate a platform.dat file. This can be done using the evemu-describe tool
72on the target platform:
73
74$ gmerge utouch-evemu
75$ evemu-describe /path/to/device > platform.dat
76"""
77
Dennis Kempin1479e742012-07-31 17:31:01 -070078def Verify(device, glob):
Dennis Kempin774b0622013-02-08 13:14:54 -080079 verifier = TestVerifier(os.environ["TESTS_DIR"], device)
80 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
Dennis Kempin1479e742012-07-31 17:31:01 -070081 cases = runner.DiscoverTestCases(glob)
82
83 for case in cases:
84 print "###", case.name
85 report = verifier.Verify(case)
86 print report
87
Dennis Kempin12968302012-08-09 15:37:09 -070088def Run(glob, out_file=None, ref_file=None):
Dennis Kempin4bc397b2012-08-08 16:51:47 -070089 print "Running tests..."
Dennis Kempin774b0622013-02-08 13:14:54 -080090 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070091 results = runner.RunAll(glob)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070092
Dennis Kempin725ee5b2012-08-10 14:17:15 -070093 # print reports
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -070094 sorted_results_items = sorted(results.items())
95 for key, value in sorted_results_items:
Dennis Kempin4bc397b2012-08-08 16:51:47 -070096 print "### Validation report for", key
Dennis Kempin2cae4432013-02-28 10:39:24 -080097 print value["description"]
98 if value["disabled"]:
99 print "DISABLED"
100 else:
101 print value["logs"]["validation"]
102 print value["error"]
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700103
Ilja H. Friedelb8e567c2013-03-13 21:54:57 -0700104 # load reference
105 ref = {}
106 if ref_file:
107 ref = json.load(open(ref_file))
108
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700109 # format result table
110 table = Table()
111 table.title = "Test Results"
112 table.header("Test", "reference score", "new score", "delta")
113
Chung-yih Wangd5aa52f2013-01-22 15:45:26 +0800114 regression = False
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700115 for key, value in sorted_results_items:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700116 def ResultStr(value):
117 # format result to string
118 if value["result"] == "success":
119 return "%s (%.4f)" % (value["result"], value["score"])
Dennis Kempin2cae4432013-02-28 10:39:24 -0800120 elif value["disabled"]:
121 return "disabled"
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700122 else:
123 return value["result"]
124
125 # format reference and delta column
126 ref_score = ""
127 delta_str = ""
Dennis Kempin12968302012-08-09 15:37:09 -0700128 if key in ref:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700129 ref_score = ResultStr(ref[key])
130 delta = value["score"] - ref[key]["score"]
131 if math.fabs(delta) < 1e-10:
132 # don't color, but line up with other values
133 delta_str = " %.4f " % delta
134 elif delta < 0:
135 regression = True
136 # color red
137 delta_str = "\x1b[91m%+.4f\x1b[0m" % delta
138 else:
139 # color green
140 delta_str = "\x1b[92m%+.4f\x1b[0m" % delta
141 table.row(key, ref_score, ResultStr(value), delta_str)
142
143 print table
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700144
Dennis Kempin12968302012-08-09 15:37:09 -0700145 if out_file:
146 json.dump(results, open(out_file, "w"), indent=2)
147 print "results stored in:", out_file
148
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700149 if regression:
150 print "\x1b[91mThere are regressions present in this test run!\x1b[0m"
151 exit(-1)
152
Dennis Kempin64f513f2012-08-10 15:09:18 -0700153def Get(test_name, what, file=None):
154 if file:
155 data = json.load(open(file))
156 results = data[test_name]
157 else:
Dennis Kempin774b0622013-02-08 13:14:54 -0800158 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700159 data = runner.RunAll(test_name)
160 results = data[test_name]
161
162 if what == "gestures-log":
163 print results["logs"]["gestures"]
164 elif what == "evdev-log":
165 print results["logs"]["evdev"]
166 elif what == "activity-log":
167 print results["logs"]["activity"]
168 elif what == "gestures":
169 print results["gestures"]
170 elif what == "events":
171 print results["events"]
Dennis Kempinc2b59862013-02-20 14:02:25 -0800172 elif what == "return-activity":
173 return results["logs"]["activity"]
Dennis Kempin64f513f2012-08-10 15:09:18 -0700174
Dennis Kempin7a2f40b2012-08-10 15:13:54 -0700175def Add(testname, activity_log, event_log):
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700176 """
177 Adds a new test case.
178 """
179 # determine test name from activity_log name
Dennis Kempin774b0622013-02-08 13:14:54 -0800180 factory = TestFactory(os.environ["TESTS_DIR"])
Dennis Kempin31c0fbd2012-07-30 13:20:37 -0700181 case = factory.CreateTest(testname, activity_log, event_log)
Dennis Kempin15967a42013-02-25 12:41:54 -0800182 if case:
183 print "Test \"" + case.name + "\" created"
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700184
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700185def Main():
186 """
187 Main entry point for the console interface
188 """
189
190 # setup paths from environment variables
191 if "TESTS_DIR" not in os.environ:
192 print "Require TESTS_DIR environment variable"
193 exit(-1)
194
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700195 TestCase.tests_path = os.environ["TESTS_DIR"]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700196
Dennis Kempinc2b59862013-02-20 14:02:25 -0800197 parser = OptionParser(usage=_help_text)
198 parser.add_option("-c", "--create",
199 dest="create", default=None,
200 help="create new test case from URL/IP or log file")
201 parser.add_option("-e", "--evdev",
202 dest="evdev", default=None,
203 help="path to evdev log for creating a new test")
204 parser.add_option("-v", "--view",
205 dest="view", default=None,
206 help="view generated gestures(g), activity in mtedit(a) " +
207 "gestures-log(gl), evdev-log(el) or activity-log(al)")
208 parser.add_option("-r", "--ref",
209 dest="ref", default=None,
210 help="reference test results for detecting regressions")
211 parser.add_option("-o", "--out",
212 dest="out", default=None,
213 help="output test results to file.")
214 parser.add_option("-n", "--new",
215 dest="new", action="store_true", default=False,
216 help="Create new device logs before downloading. " +
217 "[Default: False]")
218 parser.add_option("--no-edit",
219 dest="noedit", action="store_true", default=False,
220 help="Skip editing when creating tests. Add original log " +
221 "[Default: False]")
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700222
Dennis Kempinc2b59862013-02-20 14:02:25 -0800223 (options, args) = parser.parse_args()
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700224
Dennis Kempinc2b59862013-02-20 14:02:25 -0800225 if len(args) == 0:
226 test_name = "all"
227 elif len(args) == 1:
228 test_name = args[0]
229 else:
230 parser.print_help()
231 exit(-1)
232
233 if options.create:
234 # create temporary files for storing activity and evdev log
235 tmp = NamedTemporaryFile("w")
236 tmp_evdev = NamedTemporaryFile("w")
237
238 # obtain trimmed log data
239 original_log = Log(options.create, options)
240 if options.noedit:
241 log = original_log
Dennis Kempin12968302012-08-09 15:37:09 -0700242 else:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800243 editor = LogEditor(persistent=False)
244 log = editor.Edit(original_log)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700245
Dennis Kempinc2b59862013-02-20 14:02:25 -0800246 # write to temporary files
247 tmp.write(log.activity)
248 tmp.flush()
249 tmp_evdev.write(log.evdev)
250 tmp_evdev.flush()
251
252 # pass to touchtests
253 Add(test_name, tmp.name, tmp_evdev.name)
254
255 # cleanup
256 tmp.close()
257 tmp_evdev.close()
258
259 elif options.view:
260 view = options.view
261 if view == "g":
262 view = "gestures"
263 elif view == "gl":
264 view = "gestures-log"
265 elif view == "el":
266 view = "evdev-log"
267 elif view == "al":
268 view = "activity-log"
269 elif view == "a":
270 view = "activity"
271
272 if view == "activity":
273 activity = Get(test_name, "return-activity")
274 tmp = NamedTemporaryFile("w")
275 tmp.write(activity)
276 tmp.flush()
277 log = Log(tmp.name, options)
278 editor = LogEditor(persistent=False)
279 editor.View(log)
Dennis Kempin64f513f2012-08-10 15:09:18 -0700280 else:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800281 Get(test_name, view)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700282
283 else:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800284 Run(test_name, options.out, options.ref)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700285
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700286if __name__ == "__main__":
287 Main()