blob: 11a09a5d0bd02303c673823675b6131fc29aeaa0 [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 Kempin725ee5b2012-08-10 14:17:15 -0700112 # print reports
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700113 sorted_results_items = sorted(results.items())
114 for key, value in sorted_results_items:
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700115 print "### Validation report for", key
Ilja H. Friedeld88f14f2013-03-13 21:58:37 -0700116 print value["logs"]["validation"]
117 print value["error"]
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700118
Ilja H. Friedelb8e567c2013-03-13 21:54:57 -0700119 # load reference
120 ref = {}
121 if ref_file:
122 ref = json.load(open(ref_file))
123
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700124 # format result table
125 table = Table()
126 table.title = "Test Results"
127 table.header("Test", "reference score", "new score", "delta")
128
Chung-yih Wangd5aa52f2013-01-22 15:45:26 +0800129 regression = False
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700130 for key, value in sorted_results_items:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700131 def ResultStr(value):
132 # format result to string
133 if value["result"] == "success":
134 return "%s (%.4f)" % (value["result"], value["score"])
135 else:
136 return value["result"]
137
138 # format reference and delta column
139 ref_score = ""
140 delta_str = ""
Dennis Kempin12968302012-08-09 15:37:09 -0700141 if key in ref:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700142 ref_score = ResultStr(ref[key])
143 delta = value["score"] - ref[key]["score"]
144 if math.fabs(delta) < 1e-10:
145 # don't color, but line up with other values
146 delta_str = " %.4f " % delta
147 elif delta < 0:
148 regression = True
149 # color red
150 delta_str = "\x1b[91m%+.4f\x1b[0m" % delta
151 else:
152 # color green
153 delta_str = "\x1b[92m%+.4f\x1b[0m" % delta
154 table.row(key, ref_score, ResultStr(value), delta_str)
155
156 print table
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700157
Dennis Kempin12968302012-08-09 15:37:09 -0700158 if out_file:
159 json.dump(results, open(out_file, "w"), indent=2)
160 print "results stored in:", out_file
161
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700162 if regression:
163 print "\x1b[91mThere are regressions present in this test run!\x1b[0m"
164 exit(-1)
165
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800166
Dennis Kempin64f513f2012-08-10 15:09:18 -0700167def Get(test_name, what, file=None):
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800168 Compile()
Dennis Kempin64f513f2012-08-10 15:09:18 -0700169 if file:
170 data = json.load(open(file))
171 results = data[test_name]
172 else:
Dennis Kempin774b0622013-02-08 13:14:54 -0800173 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700174 data = runner.RunAll(test_name)
175 results = data[test_name]
176
177 if what == "gestures-log":
178 print results["logs"]["gestures"]
179 elif what == "evdev-log":
180 print results["logs"]["evdev"]
181 elif what == "activity-log":
182 print results["logs"]["activity"]
183 elif what == "gestures":
184 print results["gestures"]
185 elif what == "events":
186 print results["events"]
Dennis Kempinc2b59862013-02-20 14:02:25 -0800187 elif what == "return-activity":
188 return results["logs"]["activity"]
Dennis Kempin64f513f2012-08-10 15:09:18 -0700189
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800190
Dennis Kempin7a2f40b2012-08-10 15:13:54 -0700191def Add(testname, activity_log, event_log):
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700192 """
193 Adds a new test case.
194 """
195 # determine test name from activity_log name
Dennis Kempin774b0622013-02-08 13:14:54 -0800196 factory = TestFactory(os.environ["TESTS_DIR"])
Dennis Kempin31c0fbd2012-07-30 13:20:37 -0700197 case = factory.CreateTest(testname, activity_log, event_log)
Dennis Kempin15967a42013-02-25 12:41:54 -0800198 if case:
199 print "Test \"" + case.name + "\" created"
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700200
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800201
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700202def Main():
203 """
204 Main entry point for the console interface
205 """
206
207 # setup paths from environment variables
208 if "TESTS_DIR" not in os.environ:
209 print "Require TESTS_DIR environment variable"
210 exit(-1)
211
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700212 TestCase.tests_path = os.environ["TESTS_DIR"]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700213
Dennis Kempinc2b59862013-02-20 14:02:25 -0800214 parser = OptionParser(usage=_help_text)
215 parser.add_option("-c", "--create",
216 dest="create", default=None,
217 help="create new test case from URL/IP or log file")
218 parser.add_option("-e", "--evdev",
219 dest="evdev", default=None,
220 help="path to evdev log for creating a new test")
221 parser.add_option("-v", "--view",
222 dest="view", default=None,
223 help="view generated gestures(g), activity in mtedit(a) " +
224 "gestures-log(gl), evdev-log(el) or activity-log(al)")
225 parser.add_option("-r", "--ref",
226 dest="ref", default=None,
227 help="reference test results for detecting regressions")
228 parser.add_option("-o", "--out",
229 dest="out", default=None,
230 help="output test results to file.")
231 parser.add_option("-n", "--new",
232 dest="new", action="store_true", default=False,
233 help="Create new device logs before downloading. " +
234 "[Default: False]")
235 parser.add_option("--no-edit",
236 dest="noedit", action="store_true", default=False,
237 help="Skip editing when creating tests. Add original log " +
238 "[Default: False]")
Dennis Kempin8e3201c2013-03-14 13:49:01 -0700239 parser.add_option("--autotest",
240 dest="autotest", action="store_true", default=False,
241 help="Run in autotest mode. Skips recompilation.")
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700242
Dennis Kempinc2b59862013-02-20 14:02:25 -0800243 (options, args) = parser.parse_args()
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700244
Dennis Kempinc2b59862013-02-20 14:02:25 -0800245 if len(args) == 0:
246 test_name = "all"
247 elif len(args) == 1:
248 test_name = args[0]
249 else:
250 parser.print_help()
251 exit(-1)
252
253 if options.create:
254 # create temporary files for storing activity and evdev log
255 tmp = NamedTemporaryFile("w")
256 tmp_evdev = NamedTemporaryFile("w")
257
258 # obtain trimmed log data
259 original_log = Log(options.create, options)
260 if options.noedit:
261 log = original_log
Dennis Kempin12968302012-08-09 15:37:09 -0700262 else:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800263 editor = LogEditor(persistent=False)
264 log = editor.Edit(original_log)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700265
Dennis Kempinc2b59862013-02-20 14:02:25 -0800266 # write to temporary files
267 tmp.write(log.activity)
268 tmp.flush()
269 tmp_evdev.write(log.evdev)
270 tmp_evdev.flush()
271
272 # pass to touchtests
273 Add(test_name, tmp.name, tmp_evdev.name)
274
275 # cleanup
276 tmp.close()
277 tmp_evdev.close()
278
279 elif options.view:
280 view = options.view
281 if view == "g":
282 view = "gestures"
283 elif view == "gl":
284 view = "gestures-log"
285 elif view == "el":
286 view = "evdev-log"
287 elif view == "al":
288 view = "activity-log"
289 elif view == "a":
290 view = "activity"
291
292 if view == "activity":
293 activity = Get(test_name, "return-activity")
294 tmp = NamedTemporaryFile("w")
295 tmp.write(activity)
296 tmp.flush()
297 log = Log(tmp.name, options)
298 editor = LogEditor(persistent=False)
299 editor.View(log)
Dennis Kempin64f513f2012-08-10 15:09:18 -0700300 else:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800301 Get(test_name, view)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700302
303 else:
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800304 Run(test_name, options.out, options.ref, options.autotest)
305
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700306
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700307if __name__ == "__main__":
308 Main()