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