blob: c22e70d4339567f78c9dfc1a98c42b95c9bd14ca [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
Charlie Mooneydaaaa242015-02-03 14:20:23 -080043 # Connect to the function generator if the operator says they have one
44 self.fn_generator = None
45 if options.has_fn_gen:
46 self.fn_generator = tests.noise.HP33120A()
47 if not self.fn_generator.IsValid():
48 self.fn_generator = None
49 options.has_fn_gen = False
50 print 'Error: Unable to connect to function generator!'
51
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080052 # Compute the list of tests to run
53 self.tests = tests.generate_test_list(options)
54 self.curr_test = 0
55 self.curr_variation = 0
Charlie Mooneyaf9d5122014-12-04 15:15:52 -080056 self.curr_iteration = 1
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080057
Charlie Mooney985cf402015-01-08 15:15:59 -080058 # Create a new Report that will store all the test Results
59 self.report = Report()
60
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080061 def RunNextTestAndVariation(self):
62 """ Run the next test.
63
64 This function runs the next test/variation combination in the test suite
65 and advances the internal state to the next one automatically. When
66 finished, this function return True if there are more tests to run, and
67 False if the whole test suite is done.
68
69 After a TestSuite is instantiated, this function should be called
70 repeatedly until it returns False to go through all tests, variations,
71 and iterations.
72 """
73
74 test = self.tests[self.curr_test]
75
76 # Print the header for this new test and variation
77 prompt = test.PromptForVariation(self.curr_variation)
78 print color.Fore.WHITE + '-' * 80
79 print color.Fore.BLUE + test.name
80 print color.Fore.GREEN + prompt
81
Charlie Mooneydaaaa242015-02-03 14:20:23 -080082 # Start the function generator (if applicable)
83 if self.fn_generator:
84 waveform = test.WaveformForVariation(self.curr_variation)
85 if waveform:
86 self.fn_generator.GenerateFunction(*waveform)
87 else:
88 self.fn_generator.Off()
89
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080090 # Start collecting data
91 print 'Opening connection with DUT... ',
92 self.touch_dev.BeginEventStream()
93 print 'Connection established!'
94
95 # Wait a long time for the first event, then have a much shorter
96 # timeout on subsequent incoming events
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080097 events = []
Charlie Mooney350fbc32015-01-07 13:27:09 -080098 plotter = TouchPlotter(self.touch_dev.x_min, self.touch_dev.x_max,
Charlie Mooneye0d06c42015-01-27 11:32:51 -080099 self.touch_dev.y_min, self.touch_dev.y_max,
Charlie Mooney8dc2aef2015-02-02 14:20:34 -0800100 self.touch_dev.p_min, self.touch_dev.p_max,
Charlie Mooneye0d06c42015-01-27 11:32:51 -0800101 self.touch_dev.protocol)
Charlie Mooney3cca6ba2014-11-19 16:15:28 -0800102 print 'Waiting for 1st event...',
103 event = self.touch_dev.NextEvent(TestSuite.NO_EVENTS_DETECTED_TIMEOUT_S)
104 if not event:
Charlie Mooneye0d06c42015-01-27 11:32:51 -0800105 print ('\rNo MT events collected before timeout (%d seconds)!' %
Charlie Mooney3cca6ba2014-11-19 16:15:28 -0800106 TestSuite.NO_EVENTS_DETECTED_TIMEOUT_S),
107 while event:
Charlie Mooney86b5cf12014-12-04 09:34:09 -0800108 plotter.add_event(event)
Charlie Mooneye0d06c42015-01-27 11:32:51 -0800109 print '\rCollected %d MT events' % len(events),
Charlie Mooney3cca6ba2014-11-19 16:15:28 -0800110 events.append(event)
111 event = self.touch_dev.NextEvent(test.timeout)
112 print
Charlie Mooney97d73c12015-01-09 08:57:03 -0800113 plot_image_png = plotter.end()
Charlie Mooney3cca6ba2014-11-19 16:15:28 -0800114
115 # Run the validators on these events
Charlie Mooneye0d06c42015-01-27 11:32:51 -0800116 snapshots = mt.process(events, protocol=self.touch_dev.protocol)
Charlie Mooneyaf9d5122014-12-04 15:15:52 -0800117 results = test.RunAllValidators(snapshots)
Charlie Mooney985cf402015-01-08 15:15:59 -0800118
Charlie Mooney5e9a10f2015-01-13 14:54:27 -0800119 # Bundle the Validator results with some details of which gesture was used
120 # during the test for easier debugging.
121 test_result = tests.TestResult(results, prompt, plot_image_png)
122
Charlie Mooney985cf402015-01-08 15:15:59 -0800123 # Add the results into our report (And have it print them to stdout, too)
Charlie Mooney5e9a10f2015-01-13 14:54:27 -0800124 self.report.AddTestResult(test_result, verbose=True)
Charlie Mooney3cca6ba2014-11-19 16:15:28 -0800125
126 # Advance the test suite to the next test and variation and return an
127 # indicator as to whether this was the last thing to do or not.
128 next_test, next_var = self._Advance()
129 return (next_test is not None)
130
131 def _Advance(self):
132 """ Move on to the next test/variation pair
133
134 This function increments all the interal counters, according to the
135 number of tests, their variations, and the selected number of iterations
136 and returns the test object and the variation number that should be
137 done next.
138
139 When the TestSuite is complete, this function will return None, None
140 otherwise it will return the next Test object and the variation number
141 the test suite is on.
142 """
143 if self.curr_test >= len(self.tests):
144 return None, None
145 test = self.tests[self.curr_test]
146
147 if self.curr_variation >= len(test.variations):
148 self.curr_test += 1
149 self.curr_variation = 0
150 self.curr_iteration = 0
151 return self._Advance()
152
153 if self.curr_iteration >= self.options.num_iterations:
154 self.curr_variation += 1
155 self.curr_iteration = 0
156 return self._Advance()
157
158 self.curr_iteration += 1
159 return test, self.curr_variation