Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 1 | # 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 | |
| 5 | import colorama as color |
| 6 | |
Charlie Mooney | e0d06c4 | 2015-01-27 11:32:51 -0800 | [diff] [blame] | 7 | import mt |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 8 | import tests |
Charlie Mooney | 86b5cf1 | 2014-12-04 09:34:09 -0800 | [diff] [blame] | 9 | from plotter import TouchPlotter |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 10 | from remote import ChromeOSTouchDevice, AndroidTouchDevice |
Charlie Mooney | 985cf40 | 2015-01-08 15:15:59 -0800 | [diff] [blame] | 11 | from report import Report |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 12 | |
| 13 | |
| 14 | class 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 Mooney | e0d06c4 | 2015-01-27 11:32:51 -0800 | [diff] [blame] | 36 | |
| 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 Mooney | af9d512 | 2014-12-04 15:15:52 -0800 | [diff] [blame] | 41 | tests.validator.BaseValidator._device = self.touch_dev |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 42 | |
Charlie Mooney | daaaa24 | 2015-02-03 14:20:23 -0800 | [diff] [blame^] | 43 | # 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 Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 52 | # 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 Mooney | af9d512 | 2014-12-04 15:15:52 -0800 | [diff] [blame] | 56 | self.curr_iteration = 1 |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 57 | |
Charlie Mooney | 985cf40 | 2015-01-08 15:15:59 -0800 | [diff] [blame] | 58 | # Create a new Report that will store all the test Results |
| 59 | self.report = Report() |
| 60 | |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 61 | 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 Mooney | daaaa24 | 2015-02-03 14:20:23 -0800 | [diff] [blame^] | 82 | # 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 Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 90 | # 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 Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 97 | events = [] |
Charlie Mooney | 350fbc3 | 2015-01-07 13:27:09 -0800 | [diff] [blame] | 98 | plotter = TouchPlotter(self.touch_dev.x_min, self.touch_dev.x_max, |
Charlie Mooney | e0d06c4 | 2015-01-27 11:32:51 -0800 | [diff] [blame] | 99 | self.touch_dev.y_min, self.touch_dev.y_max, |
Charlie Mooney | 8dc2aef | 2015-02-02 14:20:34 -0800 | [diff] [blame] | 100 | self.touch_dev.p_min, self.touch_dev.p_max, |
Charlie Mooney | e0d06c4 | 2015-01-27 11:32:51 -0800 | [diff] [blame] | 101 | self.touch_dev.protocol) |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 102 | print 'Waiting for 1st event...', |
| 103 | event = self.touch_dev.NextEvent(TestSuite.NO_EVENTS_DETECTED_TIMEOUT_S) |
| 104 | if not event: |
Charlie Mooney | e0d06c4 | 2015-01-27 11:32:51 -0800 | [diff] [blame] | 105 | print ('\rNo MT events collected before timeout (%d seconds)!' % |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 106 | TestSuite.NO_EVENTS_DETECTED_TIMEOUT_S), |
| 107 | while event: |
Charlie Mooney | 86b5cf1 | 2014-12-04 09:34:09 -0800 | [diff] [blame] | 108 | plotter.add_event(event) |
Charlie Mooney | e0d06c4 | 2015-01-27 11:32:51 -0800 | [diff] [blame] | 109 | print '\rCollected %d MT events' % len(events), |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 110 | events.append(event) |
| 111 | event = self.touch_dev.NextEvent(test.timeout) |
| 112 | print |
Charlie Mooney | 97d73c1 | 2015-01-09 08:57:03 -0800 | [diff] [blame] | 113 | plot_image_png = plotter.end() |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 114 | |
| 115 | # Run the validators on these events |
Charlie Mooney | e0d06c4 | 2015-01-27 11:32:51 -0800 | [diff] [blame] | 116 | snapshots = mt.process(events, protocol=self.touch_dev.protocol) |
Charlie Mooney | af9d512 | 2014-12-04 15:15:52 -0800 | [diff] [blame] | 117 | results = test.RunAllValidators(snapshots) |
Charlie Mooney | 985cf40 | 2015-01-08 15:15:59 -0800 | [diff] [blame] | 118 | |
Charlie Mooney | 5e9a10f | 2015-01-13 14:54:27 -0800 | [diff] [blame] | 119 | # 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 Mooney | 985cf40 | 2015-01-08 15:15:59 -0800 | [diff] [blame] | 123 | # Add the results into our report (And have it print them to stdout, too) |
Charlie Mooney | 5e9a10f | 2015-01-13 14:54:27 -0800 | [diff] [blame] | 124 | self.report.AddTestResult(test_result, verbose=True) |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 125 | |
| 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 |