blob: 95d4eb86eefa709bc6c0adc13847f9cda564be73 [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
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 Kempin1c7ffab2013-02-28 11:43:45 -080078
79def Compile():
80 if "SRC_DIR" not in os.environ:
81 print "Requires SRC_DIR env-var. Re-run $ sudo make setup-in-place"
82 sys.exit(-1)
83
84 dir = os.environ["SRC_DIR"]
85 print "Recompiling gestures/libevdev/replay..."
86 process = Popen(["make", "in-place"], cwd=dir,
87 stdout=PIPE, stderr=STDOUT)
88 ret = process.wait()
89 if ret != 0:
90 print process.stdout.read()
91 sys.exit(-1)
92
93
Dennis Kempin1479e742012-07-31 17:31:01 -070094def Verify(device, glob):
Dennis Kempin774b0622013-02-08 13:14:54 -080095 verifier = TestVerifier(os.environ["TESTS_DIR"], device)
96 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
Dennis Kempin1479e742012-07-31 17:31:01 -070097 cases = runner.DiscoverTestCases(glob)
98
99 for case in cases:
100 print "###", case.name
101 report = verifier.Verify(case)
102 print report
103
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800104
105def Run(glob, out_file=None, ref_file=None, autotest=False):
106 if not autotest:
107 Compile()
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700108 print "Running tests..."
Dennis Kempin774b0622013-02-08 13:14:54 -0800109 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700110 results = runner.RunAll(glob)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700111
Dennis Kempin97397c62013-02-28 10:49:59 -0800112 # load reference
113 ref = {}
114 if ref_file:
115 ref = json.load(open(ref_file))
116
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700117 # print reports
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700118 sorted_results_items = sorted(results.items())
119 for key, value in sorted_results_items:
Dennis Kempin97397c62013-02-28 10:49:59 -0800120 if len(results) > 1:
121 # only print reports for regressions or failed tests
122 if key in ref:
123 delta = value["score"] - ref[key]["score"]
124 if math.fabs(delta) < 1e-10:
125 continue
126 elif value["result"] == "success":
127 continue
128
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700129 print "### Validation report for", key
Dennis Kempin9cfc4842013-02-28 10:39:24 -0800130 print value["description"]
131 if value["disabled"]:
132 print "DISABLED"
133 else:
134 print value["logs"]["validation"]
135 print value["error"]
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700136
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700137 # format result table
138 table = Table()
139 table.title = "Test Results"
140 table.header("Test", "reference score", "new score", "delta")
141
Chung-yih Wangd5aa52f2013-01-22 15:45:26 +0800142 regression = False
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700143 for key, value in sorted_results_items:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700144 def ResultStr(value):
145 # format result to string
146 if value["result"] == "success":
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700147 msg = "success" if value["score"] >= 0.5 else "bad"
148 return "%s (%.4f)" % (msg, value["score"])
Dennis Kempin9cfc4842013-02-28 10:39:24 -0800149 elif value["disabled"]:
150 return "disabled"
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700151 else:
152 return value["result"]
153
154 # format reference and delta column
155 ref_score = ""
156 delta_str = ""
Dennis Kempin12968302012-08-09 15:37:09 -0700157 if key in ref:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700158 ref_score = ResultStr(ref[key])
159 delta = value["score"] - ref[key]["score"]
160 if math.fabs(delta) < 1e-10:
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700161 # default color
162 delta_str = "\x1b[0m%.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700163 elif delta < 0:
164 regression = True
165 # color red
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700166 delta_str = "\x1b[31m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700167 else:
168 # color green
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700169 delta_str = "\x1b[32m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700170 table.row(key, ref_score, ResultStr(value), delta_str)
171
172 print table
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700173
Dennis Kempin12968302012-08-09 15:37:09 -0700174 if out_file:
175 json.dump(results, open(out_file, "w"), indent=2)
176 print "results stored in:", out_file
177
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700178 if regression:
179 print "\x1b[91mThere are regressions present in this test run!\x1b[0m"
180 exit(-1)
181
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800182
Dennis Kempin64f513f2012-08-10 15:09:18 -0700183def Get(test_name, what, file=None):
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800184 Compile()
Dennis Kempin64f513f2012-08-10 15:09:18 -0700185 if file:
186 data = json.load(open(file))
187 results = data[test_name]
188 else:
Dennis Kempin774b0622013-02-08 13:14:54 -0800189 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700190 data = runner.RunAll(test_name)
191 results = data[test_name]
192
193 if what == "gestures-log":
194 print results["logs"]["gestures"]
195 elif what == "evdev-log":
196 print results["logs"]["evdev"]
197 elif what == "activity-log":
198 print results["logs"]["activity"]
199 elif what == "gestures":
200 print results["gestures"]
201 elif what == "events":
202 print results["events"]
Dennis Kempinc2b59862013-02-20 14:02:25 -0800203 elif what == "return-activity":
204 return results["logs"]["activity"]
Dennis Kempin64f513f2012-08-10 15:09:18 -0700205
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800206
Dennis Kempin7a2f40b2012-08-10 15:13:54 -0700207def Add(testname, activity_log, event_log):
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700208 """
209 Adds a new test case.
210 """
211 # determine test name from activity_log name
Dennis Kempin774b0622013-02-08 13:14:54 -0800212 factory = TestFactory(os.environ["TESTS_DIR"])
Dennis Kempin31c0fbd2012-07-30 13:20:37 -0700213 case = factory.CreateTest(testname, activity_log, event_log)
Dennis Kempin15967a42013-02-25 12:41:54 -0800214 if case:
215 print "Test \"" + case.name + "\" created"
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700216
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800217
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700218def Main():
219 """
220 Main entry point for the console interface
221 """
222
223 # setup paths from environment variables
224 if "TESTS_DIR" not in os.environ:
225 print "Require TESTS_DIR environment variable"
226 exit(-1)
227
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700228 TestCase.tests_path = os.environ["TESTS_DIR"]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700229
Dennis Kempinc2b59862013-02-20 14:02:25 -0800230 parser = OptionParser(usage=_help_text)
231 parser.add_option("-c", "--create",
232 dest="create", default=None,
233 help="create new test case from URL/IP or log file")
234 parser.add_option("-e", "--evdev",
235 dest="evdev", default=None,
236 help="path to evdev log for creating a new test")
237 parser.add_option("-v", "--view",
238 dest="view", default=None,
239 help="view generated gestures(g), activity in mtedit(a) " +
240 "gestures-log(gl), evdev-log(el) or activity-log(al)")
241 parser.add_option("-r", "--ref",
242 dest="ref", default=None,
243 help="reference test results for detecting regressions")
244 parser.add_option("-o", "--out",
245 dest="out", default=None,
246 help="output test results to file.")
247 parser.add_option("-n", "--new",
248 dest="new", action="store_true", default=False,
249 help="Create new device logs before downloading. " +
250 "[Default: False]")
251 parser.add_option("--no-edit",
252 dest="noedit", action="store_true", default=False,
253 help="Skip editing when creating tests. Add original log " +
254 "[Default: False]")
Dennis Kempin8e3201c2013-03-14 13:49:01 -0700255 parser.add_option("--autotest",
256 dest="autotest", action="store_true", default=False,
257 help="Run in autotest mode. Skips recompilation.")
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700258
Dennis Kempinc2b59862013-02-20 14:02:25 -0800259 (options, args) = parser.parse_args()
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700260
Dennis Kempinc2b59862013-02-20 14:02:25 -0800261 if len(args) == 0:
262 test_name = "all"
263 elif len(args) == 1:
264 test_name = args[0]
265 else:
266 parser.print_help()
267 exit(-1)
268
269 if options.create:
270 # create temporary files for storing activity and evdev log
271 tmp = NamedTemporaryFile("w")
272 tmp_evdev = NamedTemporaryFile("w")
273
274 # obtain trimmed log data
275 original_log = Log(options.create, options)
276 if options.noedit:
277 log = original_log
Dennis Kempin12968302012-08-09 15:37:09 -0700278 else:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800279 editor = LogEditor(persistent=False)
280 log = editor.Edit(original_log)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700281
Dennis Kempinc2b59862013-02-20 14:02:25 -0800282 # write to temporary files
283 tmp.write(log.activity)
284 tmp.flush()
285 tmp_evdev.write(log.evdev)
286 tmp_evdev.flush()
287
288 # pass to touchtests
289 Add(test_name, tmp.name, tmp_evdev.name)
290
291 # cleanup
292 tmp.close()
293 tmp_evdev.close()
294
295 elif options.view:
296 view = options.view
297 if view == "g":
298 view = "gestures"
299 elif view == "gl":
300 view = "gestures-log"
301 elif view == "el":
302 view = "evdev-log"
303 elif view == "al":
304 view = "activity-log"
305 elif view == "a":
306 view = "activity"
307
308 if view == "activity":
309 activity = Get(test_name, "return-activity")
310 tmp = NamedTemporaryFile("w")
311 tmp.write(activity)
312 tmp.flush()
313 log = Log(tmp.name, options)
314 editor = LogEditor(persistent=False)
315 editor.View(log)
Dennis Kempin64f513f2012-08-10 15:09:18 -0700316 else:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800317 Get(test_name, view)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700318
319 else:
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800320 Run(test_name, options.out, options.ref, options.autotest)
321
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700322
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700323if __name__ == "__main__":
324 Main()