blob: 30fc1398fda55b28eaf391a4089c25e377d54d18 [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
Dennis Kempin1c7ffab2013-02-28 11:43:45 -08008from subprocess import Popen, PIPE, STDOUT
Dennis Kempinc2b59862013-02-20 14:02:25 -08009from tempfile import NamedTemporaryFile
Dennis Kempin725ee5b2012-08-10 14:17:15 -070010import json
11import math
12import os
Dennis Kempin725ee5b2012-08-10 14:17:15 -070013import sys
14
Dennis Kempinc2b59862013-02-20 14:02:25 -080015from mtedit.editor import LogEditor
16from mtedit.log import Log
17
Dennis Kempin725ee5b2012-08-10 14:17:15 -070018from table import Table
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070019from test_case import TestCase
Dennis Kempin31c0fbd2012-07-30 13:20:37 -070020from test_factory import TestFactory
Dennis Kempin963fb152013-01-11 15:40:19 -080021from test_runner import ParallelTestRunner as TestRunner
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070022
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080023
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070024_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
Dennis Kempin462c8752013-04-04 16:01:54 -070055$ %prog test_name --gdb
56Run the test using gdb for debugging the gestures library
57
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070058General Info:
59-------------
60testname arguments:
61Tests are always names as [platform]/[name of test case]. You can find the tests
62available in the tests folder.
63For example: lumpy/scroll_test
64
65Tests Folder:
66The tests folder contains a folder for each platform and all the test cases.
67Each test case is made up of 3 files:
68[testcase].py which contains the validation script
69[testcase].log which contains the input_event log
70[testcase].props which contains user_defined properties passed to gestures lib.
71
72Platform folders:
73To add a new platform you need to add a new folder to the Tests folder, and
74generate a platform.dat file. This can be done using the evemu-describe tool
75on the target platform:
76
77$ gmerge utouch-evemu
78$ evemu-describe /path/to/device > platform.dat
79"""
80
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080081
82def Compile():
83 if "SRC_DIR" not in os.environ:
84 print "Requires SRC_DIR env-var. Re-run $ sudo make setup-in-place"
85 sys.exit(-1)
86
87 dir = os.environ["SRC_DIR"]
88 print "Recompiling gestures/libevdev/replay..."
89 process = Popen(["make", "in-place"], cwd=dir,
90 stdout=PIPE, stderr=STDOUT)
91 ret = process.wait()
92 if ret != 0:
93 print process.stdout.read()
94 sys.exit(-1)
95
96
Dennis Kempin1479e742012-07-31 17:31:01 -070097def Verify(device, glob):
Dennis Kempin774b0622013-02-08 13:14:54 -080098 verifier = TestVerifier(os.environ["TESTS_DIR"], device)
99 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
Dennis Kempin1479e742012-07-31 17:31:01 -0700100 cases = runner.DiscoverTestCases(glob)
101
102 for case in cases:
103 print "###", case.name
104 report = verifier.Verify(case)
105 print report
106
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800107
108def Run(glob, out_file=None, ref_file=None, autotest=False):
109 if not autotest:
110 Compile()
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700111 print "Running tests..."
Dennis Kempin774b0622013-02-08 13:14:54 -0800112 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700113 results = runner.RunAll(glob)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700114
Dennis Kempin97397c62013-02-28 10:49:59 -0800115 # load reference
116 ref = {}
117 if ref_file:
118 ref = json.load(open(ref_file))
119
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700120 # print reports
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700121 sorted_results_items = sorted(results.items())
122 for key, value in sorted_results_items:
Dennis Kempin97397c62013-02-28 10:49:59 -0800123 if len(results) > 1:
124 # only print reports for regressions or failed tests
125 if key in ref:
126 delta = value["score"] - ref[key]["score"]
127 if math.fabs(delta) < 1e-10:
128 continue
129 elif value["result"] == "success":
130 continue
131
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700132 print "### Validation report for", key
Dennis Kempin9cfc4842013-02-28 10:39:24 -0800133 print value["description"]
134 if value["disabled"]:
135 print "DISABLED"
136 else:
137 print value["logs"]["validation"]
138 print value["error"]
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700139
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700140 # format result table
141 table = Table()
142 table.title = "Test Results"
143 table.header("Test", "reference score", "new score", "delta")
144
Chung-yih Wangd5aa52f2013-01-22 15:45:26 +0800145 regression = False
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700146 for key, value in sorted_results_items:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700147 def ResultStr(value):
148 # format result to string
149 if value["result"] == "success":
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700150 msg = "success" if value["score"] >= 0.5 else "bad"
151 return "%s (%.4f)" % (msg, value["score"])
Dennis Kempin9cfc4842013-02-28 10:39:24 -0800152 elif value["disabled"]:
153 return "disabled"
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700154 else:
155 return value["result"]
156
157 # format reference and delta column
158 ref_score = ""
159 delta_str = ""
Dennis Kempin12968302012-08-09 15:37:09 -0700160 if key in ref:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700161 ref_score = ResultStr(ref[key])
162 delta = value["score"] - ref[key]["score"]
163 if math.fabs(delta) < 1e-10:
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700164 # default color
165 delta_str = "\x1b[0m%.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700166 elif delta < 0:
167 regression = True
168 # color red
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700169 delta_str = "\x1b[31m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700170 else:
171 # color green
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700172 delta_str = "\x1b[32m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700173 table.row(key, ref_score, ResultStr(value), delta_str)
174
175 print table
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700176
Dennis Kempin12968302012-08-09 15:37:09 -0700177 if out_file:
178 json.dump(results, open(out_file, "w"), indent=2)
179 print "results stored in:", out_file
180
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700181 if regression:
182 print "\x1b[91mThere are regressions present in this test run!\x1b[0m"
183 exit(-1)
184
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800185
Dennis Kempin64f513f2012-08-10 15:09:18 -0700186def Get(test_name, what, file=None):
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800187 Compile()
Dennis Kempin64f513f2012-08-10 15:09:18 -0700188 if file:
189 data = json.load(open(file))
190 results = data[test_name]
191 else:
Dennis Kempin774b0622013-02-08 13:14:54 -0800192 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700193 data = runner.RunAll(test_name)
194 results = data[test_name]
195
196 if what == "gestures-log":
197 print results["logs"]["gestures"]
198 elif what == "evdev-log":
199 print results["logs"]["evdev"]
200 elif what == "activity-log":
201 print results["logs"]["activity"]
202 elif what == "gestures":
203 print results["gestures"]
204 elif what == "events":
205 print results["events"]
Dennis Kempinc2b59862013-02-20 14:02:25 -0800206 elif what == "return-activity":
207 return results["logs"]["activity"]
Dennis Kempin64f513f2012-08-10 15:09:18 -0700208
Dennis Kempin462c8752013-04-04 16:01:54 -0700209def GDB(test_name):
210 Compile()
211 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
212 data = runner.RunGDB(test_name)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800213
Dennis Kempin4e2e6892013-04-19 17:04:00 -0700214def Add(testname, activity_log, event_log, gdb):
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700215 """
216 Adds a new test case.
217 """
218 # determine test name from activity_log name
Dennis Kempin2eb653b2013-04-03 14:58:32 -0700219 factory = TestFactory(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
Dennis Kempin4e2e6892013-04-19 17:04:00 -0700220 case = factory.CreateTest(testname, activity_log, event_log, gdb)
Dennis Kempin15967a42013-02-25 12:41:54 -0800221 if case:
222 print "Test \"" + case.name + "\" created"
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700223
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800224
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700225def Main():
226 """
227 Main entry point for the console interface
228 """
229
230 # setup paths from environment variables
231 if "TESTS_DIR" not in os.environ:
232 print "Require TESTS_DIR environment variable"
233 exit(-1)
234
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700235 TestCase.tests_path = os.environ["TESTS_DIR"]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700236
Dennis Kempinc2b59862013-02-20 14:02:25 -0800237 parser = OptionParser(usage=_help_text)
238 parser.add_option("-c", "--create",
239 dest="create", default=None,
240 help="create new test case from URL/IP or log file")
241 parser.add_option("-e", "--evdev",
242 dest="evdev", default=None,
243 help="path to evdev log for creating a new test")
244 parser.add_option("-v", "--view",
245 dest="view", default=None,
246 help="view generated gestures(g), activity in mtedit(a) " +
247 "gestures-log(gl), evdev-log(el) or activity-log(al)")
248 parser.add_option("-r", "--ref",
249 dest="ref", default=None,
250 help="reference test results for detecting regressions")
251 parser.add_option("-o", "--out",
252 dest="out", default=None,
253 help="output test results to file.")
254 parser.add_option("-n", "--new",
255 dest="new", action="store_true", default=False,
256 help="Create new device logs before downloading. " +
257 "[Default: False]")
258 parser.add_option("--no-edit",
259 dest="noedit", action="store_true", default=False,
260 help="Skip editing when creating tests. Add original log " +
261 "[Default: False]")
Dennis Kempin8e3201c2013-03-14 13:49:01 -0700262 parser.add_option("--autotest",
263 dest="autotest", action="store_true", default=False,
264 help="Run in autotest mode. Skips recompilation.")
Dennis Kempin462c8752013-04-04 16:01:54 -0700265 parser.add_option("--gdb",
266 dest="gdb", action="store_true", default=False,
267 help="Run the test case in GDB")
Dennis Kempinc2b59862013-02-20 14:02:25 -0800268 (options, args) = parser.parse_args()
Andrew de los Reyes8c5585b2013-05-20 15:32:05 -0700269 options.download = False # For compatibility with mtedit
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700270
Dennis Kempinc2b59862013-02-20 14:02:25 -0800271 if len(args) == 0:
272 test_name = "all"
273 elif len(args) == 1:
274 test_name = args[0]
275 else:
276 parser.print_help()
277 exit(-1)
278
279 if options.create:
280 # create temporary files for storing activity and evdev log
281 tmp = NamedTemporaryFile("w")
282 tmp_evdev = NamedTemporaryFile("w")
283
284 # obtain trimmed log data
285 original_log = Log(options.create, options)
286 if options.noedit:
287 log = original_log
Dennis Kempin12968302012-08-09 15:37:09 -0700288 else:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800289 editor = LogEditor(persistent=False)
290 log = editor.Edit(original_log)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700291
Dennis Kempinc2b59862013-02-20 14:02:25 -0800292 # write to temporary files
293 tmp.write(log.activity)
294 tmp.flush()
295 tmp_evdev.write(log.evdev)
296 tmp_evdev.flush()
297
298 # pass to touchtests
Dennis Kempin4e2e6892013-04-19 17:04:00 -0700299 Add(test_name, tmp.name, tmp_evdev.name, options.gdb)
Dennis Kempinc2b59862013-02-20 14:02:25 -0800300
301 # cleanup
302 tmp.close()
303 tmp_evdev.close()
304
305 elif options.view:
306 view = options.view
307 if view == "g":
308 view = "gestures"
309 elif view == "gl":
310 view = "gestures-log"
311 elif view == "el":
312 view = "evdev-log"
313 elif view == "al":
314 view = "activity-log"
315 elif view == "a":
316 view = "activity"
317
318 if view == "activity":
319 activity = Get(test_name, "return-activity")
320 tmp = NamedTemporaryFile("w")
321 tmp.write(activity)
322 tmp.flush()
323 log = Log(tmp.name, options)
324 editor = LogEditor(persistent=False)
325 editor.View(log)
Dennis Kempin64f513f2012-08-10 15:09:18 -0700326 else:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800327 Get(test_name, view)
Dennis Kempin462c8752013-04-04 16:01:54 -0700328 elif options.gdb:
329 GDB(test_name)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700330 else:
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800331 Run(test_name, options.out, options.ref, options.autotest)
332
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700333
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700334if __name__ == "__main__":
335 Main()