blob: a22ea9cb6dc7c237be891a2e8efcbdca53c1c89b [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
9from test_runner import TestRunner
10import os
11import pprint
12import sys
13
14_help_text = """\
15Available Commands:
16-------------------
17$ touchtests run [all|testname]
18Executes tests. Either all of them and presents a resulting overall score.
19Or just a single named test.
20
21$ touchtests debug [testname]
22Same as run, but additionally displays the gestures library log.
23
24$ touchtests log [testname]
25Executes a test and prints the activity_log to stdout. Useful to view the
26results in tp_view.
27
28$ touchtests add [platform] [activity_log] [cmt_log]
29Adds a new test case. Each test belongs to a platform "lumpy", "cr48", etc.
30activity_log and cmt_log should point to the file names of log files collected
31from the device.
32You can trim activity_log in tp_view, touchtests will automatically trim the
33cmt_log accordingly.
34The test will be named after the filename of the activity_log and a dummy
35validation script is automatically generated.
36
37General Info:
38-------------
39testname arguments:
40Tests are always names as [platform]/[name of test case]. You can find the tests
41available in the tests folder.
42For example: lumpy/scroll_test
43
44Tests Folder:
45The tests folder contains a folder for each platform and all the test cases.
46Each test case is made up of 3 files:
47[testcase].py which contains the validation script
48[testcase].log which contains the input_event log
49[testcase].props which contains user_defined properties passed to gestures lib.
50
51Platform folders:
52To add a new platform you need to add a new folder to the Tests folder, and
53generate a platform.dat file. This can be done using the evemu-describe tool
54on the target platform:
55
56$ gmerge utouch-evemu
57$ evemu-describe /path/to/device > platform.dat
58"""
59
60
61def HelpExit():
62 """
63 Print help text and exit
64 """
65 print _help_text
66 exit()
67
68
69def Log(glob, debug=False):
70 """
71 Print only the activity log. The user can pipe the output of this command
72 to be used in other tools like tpview.
73 """
74 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["REPLAY_TOOL"])
75 cases = runner.DiscoverTestCases(glob)
76
77 # check number of test cases
78 if len(cases) > 1:
79 print "Multiple test cases match '" + str(glob) + "':"
80 for case in cases:
81 print " -", case.name
82 return
83
84 if len(cases) < 1:
85 print "Cannot find test " + glob
86 return
87
88 # run test and print results
89 results = runner.RunTest(cases[0])
90 if results["error"]:
91 print results["error"]
92 return
93
94 print results["logs"]["activity"]
95
96
97def Run(glob, debug=False):
98 """
99 Run tests. For now just print all of the results as a data structure.
100 TODO(denniskempin): Pretty formating of results
101 """
102 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["REPLAY_TOOL"])
103 results = runner.RunAll(glob)
104 pprint.pprint(results)
105
106
107def Add(platform, activity_log, event_log):
108 """
109 Adds a new test case.
110 """
111 # determine test name from activity_log name
112 testname = path.splitext(path.basename(activity_log))[0]
113 testname = path.join(platform, testname)
114 case = TestCase.add_test_case(testname, activity_log, event_log)
115 case.run(True)
116
117
118def Main():
119 """
120 Main entry point for the console interface
121 """
122
123 # setup paths from environment variables
124 if "TESTS_DIR" not in os.environ:
125 print "Require TESTS_DIR environment variable"
126 exit(-1)
127
128 if "REPLAY_TOOL" not in os.environ:
129 print "Require REPLAY_TOOL environment variable"
130 exit(-1)
131
132 TestCase.tests_path = os.environ["TESTS_DIR"]
133 TestCase.replay_tool = os.environ["REPLAY_TOOL"]
134
135 # parse arguments and call command methods
136 if len(sys.argv) < 2:
137 HelpExit()
138
139 # call command method according to command line arguments
140 cmd = sys.argv[1]
141
142 if cmd == "run" or cmd == "debug":
143 if len(sys.argv) < 3:
144 HelpExit()
145 test_name = sys.argv[2]
146 Run(test_name, (cmd == "debug"))
147
148 elif cmd == "log":
149 if len(sys.argv) < 3:
150 HelpExit()
151 test_name = sys.argv[2]
152 Log(test_name)
153
154 elif cmd == "add":
155 if len(sys.argv) < 5:
156 HelpExit()
157 platform = sys.argv[2]
158 activity_log = sys.argv[3]
159 event_log = sys.argv[4]
160 Add(platform, activity_log, event_log)
161
162 else:
163 HelpExit()
164
165if __name__ == "__main__":
166 Main()