blob: f78da17b1d448f87bf5cbd610df18684a1b38309 [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
7from os import path
8from test_case import TestCase
Dennis Kempin31c0fbd2012-07-30 13:20:37 -07009from test_factory import TestFactory
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070010from test_runner import TestRunner
Dennis Kempin1479e742012-07-31 17:31:01 -070011from test_verifier import TestVerifier
12import json
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070013import os
14import pprint
15import sys
16
17_help_text = """\
18Available Commands:
19-------------------
20$ touchtests run [all|testname]
21Executes tests. Either all of them and presents a resulting overall score.
22Or just a single named test.
23
24$ touchtests debug [testname]
25Same as run, but additionally displays the gestures library log.
26
27$ touchtests log [testname]
28Executes a test and prints the activity_log to stdout. Useful to view the
29results in tp_view.
30
31$ touchtests add [platform] [activity_log] [cmt_log]
32Adds a new test case. Each test belongs to a platform "lumpy", "cr48", etc.
33activity_log and cmt_log should point to the file names of log files collected
34from the device.
35You can trim activity_log in tp_view, touchtests will automatically trim the
36cmt_log accordingly.
37The test will be named after the filename of the activity_log and a dummy
38validation script is automatically generated.
39
40General Info:
41-------------
42testname arguments:
43Tests are always names as [platform]/[name of test case]. You can find the tests
44available in the tests folder.
45For example: lumpy/scroll_test
46
47Tests Folder:
48The tests folder contains a folder for each platform and all the test cases.
49Each test case is made up of 3 files:
50[testcase].py which contains the validation script
51[testcase].log which contains the input_event log
52[testcase].props which contains user_defined properties passed to gestures lib.
53
54Platform folders:
55To add a new platform you need to add a new folder to the Tests folder, and
56generate a platform.dat file. This can be done using the evemu-describe tool
57on the target platform:
58
59$ gmerge utouch-evemu
60$ evemu-describe /path/to/device > platform.dat
61"""
62
63
64def HelpExit():
65 """
66 Print help text and exit
67 """
68 print _help_text
69 exit()
70
Dennis Kempin1479e742012-07-31 17:31:01 -070071def Verify(device, glob):
72 verifier = TestVerifier(os.environ["TESTS_DIR"], os.environ["REPLAY_TOOL"],
73 device)
74 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["REPLAY_TOOL"])
75 cases = runner.DiscoverTestCases(glob)
76
77 for case in cases:
78 print "###", case.name
79 report = verifier.Verify(case)
80 print report
81
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070082
83def Log(glob, debug=False):
84 """
85 Print only the activity log. The user can pipe the output of this command
86 to be used in other tools like tpview.
87 """
88 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["REPLAY_TOOL"])
89 cases = runner.DiscoverTestCases(glob)
90
91 # check number of test cases
92 if len(cases) > 1:
93 print "Multiple test cases match '" + str(glob) + "':"
94 for case in cases:
95 print " -", case.name
96 return
97
98 if len(cases) < 1:
99 print "Cannot find test " + glob
100 return
101
102 # run test and print results
103 results = runner.RunTest(cases[0])
104 if results["error"]:
105 print results["error"]
106 return
107
108 print results["logs"]["activity"]
109
110
111def Run(glob, debug=False):
112 """
113 Run tests. For now just print all of the results as a data structure.
114 TODO(denniskempin): Pretty formating of results
115 """
116 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["REPLAY_TOOL"])
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700117 print "Running tests..."
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700118 results = runner.RunAll(glob)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700119
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700120 for key, value in results.items():
121 print "### Validation report for", key
122 print value["logs"]["validation"]
123
Dennis Kempin8cb1a892012-08-06 15:19:17 -0700124 print "Test Results:"
125 for key, value in results.items():
126 print " ", key + ":", value["result"], "(" + str(value["score"]) + ")"
127 if value["result"] == "error":
128 print " ", value["error"]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700129
130def Add(platform, activity_log, event_log):
131 """
132 Adds a new test case.
133 """
134 # determine test name from activity_log name
135 testname = path.splitext(path.basename(activity_log))[0]
136 testname = path.join(platform, testname)
Dennis Kempin31c0fbd2012-07-30 13:20:37 -0700137 factory = TestFactory(os.environ["TESTS_DIR"], os.environ["REPLAY_TOOL"])
138 case = factory.CreateTest(testname, activity_log, event_log)
139
Dennis Kempin49fe8a52012-08-03 11:27:40 -0700140 print "Test \"" + case.name + "\" created"
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700141
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700142def Main():
143 """
144 Main entry point for the console interface
145 """
146
147 # setup paths from environment variables
148 if "TESTS_DIR" not in os.environ:
149 print "Require TESTS_DIR environment variable"
150 exit(-1)
151
152 if "REPLAY_TOOL" not in os.environ:
153 print "Require REPLAY_TOOL environment variable"
154 exit(-1)
155
156 TestCase.tests_path = os.environ["TESTS_DIR"]
157 TestCase.replay_tool = os.environ["REPLAY_TOOL"]
158
159 # parse arguments and call command methods
160 if len(sys.argv) < 2:
161 HelpExit()
162
163 # call command method according to command line arguments
164 cmd = sys.argv[1]
165
166 if cmd == "run" or cmd == "debug":
167 if len(sys.argv) < 3:
168 HelpExit()
169 test_name = sys.argv[2]
170 Run(test_name, (cmd == "debug"))
171
172 elif cmd == "log":
173 if len(sys.argv) < 3:
174 HelpExit()
175 test_name = sys.argv[2]
176 Log(test_name)
177
Dennis Kempin1479e742012-07-31 17:31:01 -0700178 elif cmd == "verify":
179 if len(sys.argv) < 4:
180 HelpExit()
181 device_name = sys.argv[2]
182 test_name = sys.argv[3]
183 Verify(device_name, test_name)
184
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700185 elif cmd == "add":
186 if len(sys.argv) < 5:
187 HelpExit()
188 platform = sys.argv[2]
189 activity_log = sys.argv[3]
190 event_log = sys.argv[4]
191 Add(platform, activity_log, event_log)
192
193 else:
194 HelpExit()
195
196if __name__ == "__main__":
197 Main()