blob: f25a89906c52b1a5c7562d4d752302ea415b4583 [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)
118 pprint.pprint(results)
119
120
121def Add(platform, activity_log, event_log):
122 """
123 Adds a new test case.
124 """
125 # determine test name from activity_log name
126 testname = path.splitext(path.basename(activity_log))[0]
127 testname = path.join(platform, testname)
Dennis Kempin31c0fbd2012-07-30 13:20:37 -0700128 factory = TestFactory(os.environ["TESTS_DIR"], os.environ["REPLAY_TOOL"])
129 case = factory.CreateTest(testname, activity_log, event_log)
130
Dennis Kempin49fe8a52012-08-03 11:27:40 -0700131 print "Test \"" + case.name + "\" created"
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700132
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700133def Main():
134 """
135 Main entry point for the console interface
136 """
137
138 # setup paths from environment variables
139 if "TESTS_DIR" not in os.environ:
140 print "Require TESTS_DIR environment variable"
141 exit(-1)
142
143 if "REPLAY_TOOL" not in os.environ:
144 print "Require REPLAY_TOOL environment variable"
145 exit(-1)
146
147 TestCase.tests_path = os.environ["TESTS_DIR"]
148 TestCase.replay_tool = os.environ["REPLAY_TOOL"]
149
150 # parse arguments and call command methods
151 if len(sys.argv) < 2:
152 HelpExit()
153
154 # call command method according to command line arguments
155 cmd = sys.argv[1]
156
157 if cmd == "run" or cmd == "debug":
158 if len(sys.argv) < 3:
159 HelpExit()
160 test_name = sys.argv[2]
161 Run(test_name, (cmd == "debug"))
162
163 elif cmd == "log":
164 if len(sys.argv) < 3:
165 HelpExit()
166 test_name = sys.argv[2]
167 Log(test_name)
168
Dennis Kempin1479e742012-07-31 17:31:01 -0700169 elif cmd == "verify":
170 if len(sys.argv) < 4:
171 HelpExit()
172 device_name = sys.argv[2]
173 test_name = sys.argv[3]
174 Verify(device_name, test_name)
175
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700176 elif cmd == "add":
177 if len(sys.argv) < 5:
178 HelpExit()
179 platform = sys.argv[2]
180 activity_log = sys.argv[3]
181 event_log = sys.argv[4]
182 Add(platform, activity_log, event_log)
183
184 else:
185 HelpExit()
186
187if __name__ == "__main__":
188 Main()