blob: 8eb1225cddddc6910462cb98e1bf365a25cb71ac [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"])
117 results = runner.RunAll(glob)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700118
Dennis Kempin8cb1a892012-08-06 15:19:17 -0700119 print "Test Results:"
120 for key, value in results.items():
121 print " ", key + ":", value["result"], "(" + str(value["score"]) + ")"
122 if value["result"] == "error":
123 print " ", value["error"]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700124
125def Add(platform, activity_log, event_log):
126 """
127 Adds a new test case.
128 """
129 # determine test name from activity_log name
130 testname = path.splitext(path.basename(activity_log))[0]
131 testname = path.join(platform, testname)
Dennis Kempin31c0fbd2012-07-30 13:20:37 -0700132 factory = TestFactory(os.environ["TESTS_DIR"], os.environ["REPLAY_TOOL"])
133 case = factory.CreateTest(testname, activity_log, event_log)
134
Dennis Kempin49fe8a52012-08-03 11:27:40 -0700135 print "Test \"" + case.name + "\" created"
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700136
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700137def Main():
138 """
139 Main entry point for the console interface
140 """
141
142 # setup paths from environment variables
143 if "TESTS_DIR" not in os.environ:
144 print "Require TESTS_DIR environment variable"
145 exit(-1)
146
147 if "REPLAY_TOOL" not in os.environ:
148 print "Require REPLAY_TOOL environment variable"
149 exit(-1)
150
151 TestCase.tests_path = os.environ["TESTS_DIR"]
152 TestCase.replay_tool = os.environ["REPLAY_TOOL"]
153
154 # parse arguments and call command methods
155 if len(sys.argv) < 2:
156 HelpExit()
157
158 # call command method according to command line arguments
159 cmd = sys.argv[1]
160
161 if cmd == "run" or cmd == "debug":
162 if len(sys.argv) < 3:
163 HelpExit()
164 test_name = sys.argv[2]
165 Run(test_name, (cmd == "debug"))
166
167 elif cmd == "log":
168 if len(sys.argv) < 3:
169 HelpExit()
170 test_name = sys.argv[2]
171 Log(test_name)
172
Dennis Kempin1479e742012-07-31 17:31:01 -0700173 elif cmd == "verify":
174 if len(sys.argv) < 4:
175 HelpExit()
176 device_name = sys.argv[2]
177 test_name = sys.argv[3]
178 Verify(device_name, test_name)
179
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700180 elif cmd == "add":
181 if len(sys.argv) < 5:
182 HelpExit()
183 platform = sys.argv[2]
184 activity_log = sys.argv[3]
185 event_log = sys.argv[4]
186 Add(platform, activity_log, event_log)
187
188 else:
189 HelpExit()
190
191if __name__ == "__main__":
192 Main()