blob: cdb4bd6612e6c95e10c8891c5346d9e19f9a8a38 [file] [log] [blame]
Charlie Mooney3cca6ba2014-11-19 16:15:28 -08001# Copyright (c) 2014 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
5import colorama as color
6
Charlie Mooneye0d06c42015-01-27 11:32:51 -08007import mt
Charlie Mooney3cca6ba2014-11-19 16:15:28 -08008import tests
Charlie Mooney86b5cf12014-12-04 09:34:09 -08009from plotter import TouchPlotter
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080010from remote import ChromeOSTouchDevice, AndroidTouchDevice
Charlie Mooney985cf402015-01-08 15:15:59 -080011from report import Report
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080012
13
14class TestSuite:
15 """ This class represents a collection of tests and is used to run them
16
17 A TestSuite object will set up a connection to the DUT, robot, etc, and
18 determine which tests can be run. Once the object is instantiated,
19 RunNextTestAndVariation() can be run repeatedly to work your way through
20 the entire suite.
21 """
22
23 NO_EVENTS_DETECTED_TIMEOUT_S = 5
24
25 def __init__(self, options, args):
26 color.init(autoreset=True)
27
28 self.options = options
29
30 # Open a connection to the device specified
31 if options.dut_type == 'chromeos':
32 self.touch_dev = ChromeOSTouchDevice(self.options.addr,
33 self.options.is_touchscreen)
34 else:
35 self.touch_dev = AndroidTouchDevice(self.options.addr, True)
Charlie Mooneye0d06c42015-01-27 11:32:51 -080036
37 # If the user specified the device's mt protocol override it now
38 if options.protocol != 'auto':
39 self.touch_dev.protocol = options.protocol
40
Charlie Mooneyaf9d5122014-12-04 15:15:52 -080041 tests.validator.BaseValidator._device = self.touch_dev
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080042
43 # Compute the list of tests to run
44 self.tests = tests.generate_test_list(options)
45 self.curr_test = 0
46 self.curr_variation = 0
Charlie Mooneyaf9d5122014-12-04 15:15:52 -080047 self.curr_iteration = 1
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080048
Charlie Mooney985cf402015-01-08 15:15:59 -080049 # Create a new Report that will store all the test Results
50 self.report = Report()
51
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080052 def RunNextTestAndVariation(self):
53 """ Run the next test.
54
55 This function runs the next test/variation combination in the test suite
56 and advances the internal state to the next one automatically. When
57 finished, this function return True if there are more tests to run, and
58 False if the whole test suite is done.
59
60 After a TestSuite is instantiated, this function should be called
61 repeatedly until it returns False to go through all tests, variations,
62 and iterations.
63 """
64
65 test = self.tests[self.curr_test]
66
67 # Print the header for this new test and variation
68 prompt = test.PromptForVariation(self.curr_variation)
69 print color.Fore.WHITE + '-' * 80
70 print color.Fore.BLUE + test.name
71 print color.Fore.GREEN + prompt
72
73 # Start collecting data
74 print 'Opening connection with DUT... ',
75 self.touch_dev.BeginEventStream()
76 print 'Connection established!'
77
78 # Wait a long time for the first event, then have a much shorter
79 # timeout on subsequent incoming events
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080080 events = []
Charlie Mooney350fbc32015-01-07 13:27:09 -080081 plotter = TouchPlotter(self.touch_dev.x_min, self.touch_dev.x_max,
Charlie Mooneye0d06c42015-01-27 11:32:51 -080082 self.touch_dev.y_min, self.touch_dev.y_max,
Charlie Mooney8dc2aef2015-02-02 14:20:34 -080083 self.touch_dev.p_min, self.touch_dev.p_max,
Charlie Mooneye0d06c42015-01-27 11:32:51 -080084 self.touch_dev.protocol)
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080085 print 'Waiting for 1st event...',
86 event = self.touch_dev.NextEvent(TestSuite.NO_EVENTS_DETECTED_TIMEOUT_S)
87 if not event:
Charlie Mooneye0d06c42015-01-27 11:32:51 -080088 print ('\rNo MT events collected before timeout (%d seconds)!' %
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080089 TestSuite.NO_EVENTS_DETECTED_TIMEOUT_S),
90 while event:
Charlie Mooney86b5cf12014-12-04 09:34:09 -080091 plotter.add_event(event)
Charlie Mooneye0d06c42015-01-27 11:32:51 -080092 print '\rCollected %d MT events' % len(events),
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080093 events.append(event)
94 event = self.touch_dev.NextEvent(test.timeout)
95 print
Charlie Mooney97d73c12015-01-09 08:57:03 -080096 plot_image_png = plotter.end()
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080097
98 # Run the validators on these events
Charlie Mooneye0d06c42015-01-27 11:32:51 -080099 snapshots = mt.process(events, protocol=self.touch_dev.protocol)
Charlie Mooneyaf9d5122014-12-04 15:15:52 -0800100 results = test.RunAllValidators(snapshots)
Charlie Mooney985cf402015-01-08 15:15:59 -0800101
Charlie Mooney5e9a10f2015-01-13 14:54:27 -0800102 # Bundle the Validator results with some details of which gesture was used
103 # during the test for easier debugging.
104 test_result = tests.TestResult(results, prompt, plot_image_png)
105
Charlie Mooney985cf402015-01-08 15:15:59 -0800106 # Add the results into our report (And have it print them to stdout, too)
Charlie Mooney5e9a10f2015-01-13 14:54:27 -0800107 self.report.AddTestResult(test_result, verbose=True)
Charlie Mooney3cca6ba2014-11-19 16:15:28 -0800108
109 # Advance the test suite to the next test and variation and return an
110 # indicator as to whether this was the last thing to do or not.
111 next_test, next_var = self._Advance()
112 return (next_test is not None)
113
114 def _Advance(self):
115 """ Move on to the next test/variation pair
116
117 This function increments all the interal counters, according to the
118 number of tests, their variations, and the selected number of iterations
119 and returns the test object and the variation number that should be
120 done next.
121
122 When the TestSuite is complete, this function will return None, None
123 otherwise it will return the next Test object and the variation number
124 the test suite is on.
125 """
126 if self.curr_test >= len(self.tests):
127 return None, None
128 test = self.tests[self.curr_test]
129
130 if self.curr_variation >= len(test.variations):
131 self.curr_test += 1
132 self.curr_variation = 0
133 self.curr_iteration = 0
134 return self._Advance()
135
136 if self.curr_iteration >= self.options.num_iterations:
137 self.curr_variation += 1
138 self.curr_iteration = 0
139 return self._Advance()
140
141 self.curr_iteration += 1
142 return test, self.curr_variation