blob: 2b9cdf29d761ea78f181751e89280f8cca6ceb21 [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,
83 self.touch_dev.protocol)
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080084 print 'Waiting for 1st event...',
85 event = self.touch_dev.NextEvent(TestSuite.NO_EVENTS_DETECTED_TIMEOUT_S)
86 if not event:
Charlie Mooneye0d06c42015-01-27 11:32:51 -080087 print ('\rNo MT events collected before timeout (%d seconds)!' %
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080088 TestSuite.NO_EVENTS_DETECTED_TIMEOUT_S),
89 while event:
Charlie Mooney86b5cf12014-12-04 09:34:09 -080090 plotter.add_event(event)
Charlie Mooneye0d06c42015-01-27 11:32:51 -080091 print '\rCollected %d MT events' % len(events),
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080092 events.append(event)
93 event = self.touch_dev.NextEvent(test.timeout)
94 print
Charlie Mooney97d73c12015-01-09 08:57:03 -080095 plot_image_png = plotter.end()
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080096
97 # Run the validators on these events
Charlie Mooneye0d06c42015-01-27 11:32:51 -080098 snapshots = mt.process(events, protocol=self.touch_dev.protocol)
Charlie Mooneyaf9d5122014-12-04 15:15:52 -080099 results = test.RunAllValidators(snapshots)
Charlie Mooney985cf402015-01-08 15:15:59 -0800100
Charlie Mooney5e9a10f2015-01-13 14:54:27 -0800101 # Bundle the Validator results with some details of which gesture was used
102 # during the test for easier debugging.
103 test_result = tests.TestResult(results, prompt, plot_image_png)
104
Charlie Mooney985cf402015-01-08 15:15:59 -0800105 # Add the results into our report (And have it print them to stdout, too)
Charlie Mooney5e9a10f2015-01-13 14:54:27 -0800106 self.report.AddTestResult(test_result, verbose=True)
Charlie Mooney3cca6ba2014-11-19 16:15:28 -0800107
108 # Advance the test suite to the next test and variation and return an
109 # indicator as to whether this was the last thing to do or not.
110 next_test, next_var = self._Advance()
111 return (next_test is not None)
112
113 def _Advance(self):
114 """ Move on to the next test/variation pair
115
116 This function increments all the interal counters, according to the
117 number of tests, their variations, and the selected number of iterations
118 and returns the test object and the variation number that should be
119 done next.
120
121 When the TestSuite is complete, this function will return None, None
122 otherwise it will return the next Test object and the variation number
123 the test suite is on.
124 """
125 if self.curr_test >= len(self.tests):
126 return None, None
127 test = self.tests[self.curr_test]
128
129 if self.curr_variation >= len(test.variations):
130 self.curr_test += 1
131 self.curr_variation = 0
132 self.curr_iteration = 0
133 return self._Advance()
134
135 if self.curr_iteration >= self.options.num_iterations:
136 self.curr_variation += 1
137 self.curr_iteration = 0
138 return self._Advance()
139
140 self.curr_iteration += 1
141 return test, self.curr_variation