blob: f3d6dfb291b569228954e09314b096e45005b18c [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 Kempinaf6791a2013-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 Kempinaf6791a2013-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 Kempinaf6791a2013-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 Kempinaf6791a2013-02-28 11:43:45 -0800104
Dennis Kempin12968302012-08-09 15:37:09 -0700105def Run(glob, out_file=None, ref_file=None):
Dennis Kempinaf6791a2013-02-28 11:43:45 -0800106 Compile()
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700107 print "Running tests..."
Dennis Kempin774b0622013-02-08 13:14:54 -0800108 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700109 results = runner.RunAll(glob)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700110
Dennis Kempin0559fa12013-02-28 10:49:59 -0800111 # load reference
112 ref = {}
113 if ref_file:
114 ref = json.load(open(ref_file))
115
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700116 # print reports
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700117 sorted_results_items = sorted(results.items())
118 for key, value in sorted_results_items:
Dennis Kempin0559fa12013-02-28 10:49:59 -0800119 if len(results) > 1:
120 # only print reports for regressions or failed tests
121 if key in ref:
122 delta = value["score"] - ref[key]["score"]
123 if math.fabs(delta) < 1e-10:
124 continue
125 elif value["result"] == "success":
126 continue
127
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700128 print "### Validation report for", key
Dennis Kempin2cae4432013-02-28 10:39:24 -0800129 print value["description"]
130 if value["disabled"]:
131 print "DISABLED"
132 else:
133 print value["logs"]["validation"]
134 print value["error"]
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700135
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700136 # format result table
137 table = Table()
138 table.title = "Test Results"
139 table.header("Test", "reference score", "new score", "delta")
140
Chung-yih Wangd5aa52f2013-01-22 15:45:26 +0800141 regression = False
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700142 for key, value in sorted_results_items:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700143 def ResultStr(value):
144 # format result to string
145 if value["result"] == "success":
146 return "%s (%.4f)" % (value["result"], value["score"])
Dennis Kempin2cae4432013-02-28 10:39:24 -0800147 elif value["disabled"]:
148 return "disabled"
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700149 else:
150 return value["result"]
151
152 # format reference and delta column
153 ref_score = ""
154 delta_str = ""
Dennis Kempin12968302012-08-09 15:37:09 -0700155 if key in ref:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700156 ref_score = ResultStr(ref[key])
157 delta = value["score"] - ref[key]["score"]
158 if math.fabs(delta) < 1e-10:
159 # don't color, but line up with other values
160 delta_str = " %.4f " % delta
161 elif delta < 0:
162 regression = True
163 # color red
164 delta_str = "\x1b[91m%+.4f\x1b[0m" % delta
165 else:
166 # color green
167 delta_str = "\x1b[92m%+.4f\x1b[0m" % delta
168 table.row(key, ref_score, ResultStr(value), delta_str)
169
170 print table
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700171
Dennis Kempin12968302012-08-09 15:37:09 -0700172 if out_file:
173 json.dump(results, open(out_file, "w"), indent=2)
174 print "results stored in:", out_file
175
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700176 if regression:
177 print "\x1b[91mThere are regressions present in this test run!\x1b[0m"
178 exit(-1)
179
Dennis Kempinaf6791a2013-02-28 11:43:45 -0800180
Dennis Kempin64f513f2012-08-10 15:09:18 -0700181def Get(test_name, what, file=None):
Dennis Kempinaf6791a2013-02-28 11:43:45 -0800182 Compile()
Dennis Kempin64f513f2012-08-10 15:09:18 -0700183 if file:
184 data = json.load(open(file))
185 results = data[test_name]
186 else:
Dennis Kempin774b0622013-02-08 13:14:54 -0800187 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700188 data = runner.RunAll(test_name)
189 results = data[test_name]
190
191 if what == "gestures-log":
192 print results["logs"]["gestures"]
193 elif what == "evdev-log":
194 print results["logs"]["evdev"]
195 elif what == "activity-log":
196 print results["logs"]["activity"]
197 elif what == "gestures":
198 print results["gestures"]
199 elif what == "events":
200 print results["events"]
Dennis Kempinc2b59862013-02-20 14:02:25 -0800201 elif what == "return-activity":
202 return results["logs"]["activity"]
Dennis Kempin64f513f2012-08-10 15:09:18 -0700203
Dennis Kempinaf6791a2013-02-28 11:43:45 -0800204
Dennis Kempin7a2f40b2012-08-10 15:13:54 -0700205def Add(testname, activity_log, event_log):
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700206 """
207 Adds a new test case.
208 """
209 # determine test name from activity_log name
Dennis Kempin774b0622013-02-08 13:14:54 -0800210 factory = TestFactory(os.environ["TESTS_DIR"])
Dennis Kempin31c0fbd2012-07-30 13:20:37 -0700211 case = factory.CreateTest(testname, activity_log, event_log)
Dennis Kempin15967a42013-02-25 12:41:54 -0800212 if case:
213 print "Test \"" + case.name + "\" created"
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700214
Dennis Kempinaf6791a2013-02-28 11:43:45 -0800215
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700216def Main():
217 """
218 Main entry point for the console interface
219 """
220
221 # setup paths from environment variables
222 if "TESTS_DIR" not in os.environ:
223 print "Require TESTS_DIR environment variable"
224 exit(-1)
225
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700226 TestCase.tests_path = os.environ["TESTS_DIR"]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700227
Dennis Kempinc2b59862013-02-20 14:02:25 -0800228 parser = OptionParser(usage=_help_text)
229 parser.add_option("-c", "--create",
230 dest="create", default=None,
231 help="create new test case from URL/IP or log file")
232 parser.add_option("-e", "--evdev",
233 dest="evdev", default=None,
234 help="path to evdev log for creating a new test")
235 parser.add_option("-v", "--view",
236 dest="view", default=None,
237 help="view generated gestures(g), activity in mtedit(a) " +
238 "gestures-log(gl), evdev-log(el) or activity-log(al)")
239 parser.add_option("-r", "--ref",
240 dest="ref", default=None,
241 help="reference test results for detecting regressions")
242 parser.add_option("-o", "--out",
243 dest="out", default=None,
244 help="output test results to file.")
245 parser.add_option("-n", "--new",
246 dest="new", action="store_true", default=False,
247 help="Create new device logs before downloading. " +
248 "[Default: False]")
249 parser.add_option("--no-edit",
250 dest="noedit", action="store_true", default=False,
251 help="Skip editing when creating tests. Add original log " +
252 "[Default: False]")
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700253
Dennis Kempinc2b59862013-02-20 14:02:25 -0800254 (options, args) = parser.parse_args()
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700255
Dennis Kempinc2b59862013-02-20 14:02:25 -0800256 if len(args) == 0:
257 test_name = "all"
258 elif len(args) == 1:
259 test_name = args[0]
260 else:
261 parser.print_help()
262 exit(-1)
263
264 if options.create:
265 # create temporary files for storing activity and evdev log
266 tmp = NamedTemporaryFile("w")
267 tmp_evdev = NamedTemporaryFile("w")
268
269 # obtain trimmed log data
270 original_log = Log(options.create, options)
271 if options.noedit:
272 log = original_log
Dennis Kempin12968302012-08-09 15:37:09 -0700273 else:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800274 editor = LogEditor(persistent=False)
275 log = editor.Edit(original_log)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700276
Dennis Kempinc2b59862013-02-20 14:02:25 -0800277 # write to temporary files
278 tmp.write(log.activity)
279 tmp.flush()
280 tmp_evdev.write(log.evdev)
281 tmp_evdev.flush()
282
283 # pass to touchtests
284 Add(test_name, tmp.name, tmp_evdev.name)
285
286 # cleanup
287 tmp.close()
288 tmp_evdev.close()
289
290 elif options.view:
291 view = options.view
292 if view == "g":
293 view = "gestures"
294 elif view == "gl":
295 view = "gestures-log"
296 elif view == "el":
297 view = "evdev-log"
298 elif view == "al":
299 view = "activity-log"
300 elif view == "a":
301 view = "activity"
302
303 if view == "activity":
304 activity = Get(test_name, "return-activity")
305 tmp = NamedTemporaryFile("w")
306 tmp.write(activity)
307 tmp.flush()
308 log = Log(tmp.name, options)
309 editor = LogEditor(persistent=False)
310 editor.View(log)
Dennis Kempin64f513f2012-08-10 15:09:18 -0700311 else:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800312 Get(test_name, view)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700313
314 else:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800315 Run(test_name, options.out, options.ref)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700316
Dennis Kempinaf6791a2013-02-28 11:43:45 -0800317
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700318if __name__ == "__main__":
319 Main()