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 | |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 5 | import os |
| 6 | import sys |
Charlie Mooney | c68f9c3 | 2015-04-16 15:23:22 -0700 | [diff] [blame] | 7 | import webbrowser |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 8 | |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 9 | import colorama as color |
| 10 | |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 11 | import gesture_interpreter |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 12 | import tests |
Charlie Mooney | c68f9c3 | 2015-04-16 15:23:22 -0700 | [diff] [blame] | 13 | from webplot import Webplot |
Charlie Mooney | 985cf40 | 2015-01-08 15:15:59 -0800 | [diff] [blame] | 14 | from report import Report |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 15 | from touchbot import Touchbot, DeviceSpec |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 16 | |
| 17 | |
| 18 | class TestSuite: |
| 19 | """ This class represents a collection of tests and is used to run them |
| 20 | |
| 21 | A TestSuite object will set up a connection to the DUT, robot, etc, and |
| 22 | determine which tests can be run. Once the object is instantiated, |
| 23 | RunNextTestAndVariation() can be run repeatedly to work your way through |
| 24 | the entire suite. |
| 25 | """ |
Charlie Mooney | 1144c9b | 2015-04-16 13:59:56 -0700 | [diff] [blame] | 26 | FIRST_SNAPSHOT_TIMEOUT_S = 60 |
Charlie Mooney | c68f9c3 | 2015-04-16 15:23:22 -0700 | [diff] [blame] | 27 | WEBPLOT_ADDR = '127.0.0.1' |
| 28 | WEBPLOT_PORT = 8080 |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 29 | |
Charlie Mooney | d5712b8 | 2015-02-23 12:06:59 -0800 | [diff] [blame] | 30 | def __init__(self, touch_dev, options, args): |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 31 | color.init(autoreset=True) |
| 32 | |
| 33 | self.options = options |
Charlie Mooney | d5712b8 | 2015-02-23 12:06:59 -0800 | [diff] [blame] | 34 | self.touch_dev = touch_dev |
Charlie Mooney | af9d512 | 2014-12-04 15:15:52 -0800 | [diff] [blame] | 35 | tests.validator.BaseValidator._device = self.touch_dev |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 36 | |
Charlie Mooney | daaaa24 | 2015-02-03 14:20:23 -0800 | [diff] [blame] | 37 | # Connect to the function generator if the operator says they have one |
| 38 | self.fn_generator = None |
| 39 | if options.has_fn_gen: |
| 40 | self.fn_generator = tests.noise.HP33120A() |
| 41 | if not self.fn_generator.IsValid(): |
| 42 | self.fn_generator = None |
| 43 | options.has_fn_gen = False |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 44 | print color.Fore.RED + 'Error: Unable to connect to function generator!' |
| 45 | sys.exit(1) |
| 46 | |
| 47 | # Connect to the robot if the operator says they have it |
| 48 | self.robot = self.device_spec = None |
| 49 | if options.has_robot: |
| 50 | self.robot = Touchbot() |
| 51 | if not self.robot.comm: |
| 52 | print color.Fore.RED + 'Error: Unable to connect to robot!' |
| 53 | sys.exit(1) |
| 54 | # Load a copy of the device spec if it already exists or calibrate now |
| 55 | device_spec_filename = './%s.p' % options.name |
| 56 | if os.path.isfile(device_spec_filename): |
| 57 | self.device_spec = DeviceSpec.LoadFromDisk(device_spec_filename) |
| 58 | else: |
| 59 | print color.Fore.YELLOW + 'No spec found (%s)' % device_spec_filename |
| 60 | print (color.Fore.YELLOW + 'Please follow these instructions to ' |
| 61 | 'calibrate for your "%s" device' % options.name) |
| 62 | self.device_spec = self.robot.CalibrateDevice(device_spec_filename) |
Charlie Mooney | daaaa24 | 2015-02-03 14:20:23 -0800 | [diff] [blame] | 63 | |
Charlie Mooney | c68f9c3 | 2015-04-16 15:23:22 -0700 | [diff] [blame] | 64 | # Start up the plotter |
| 65 | self.plotter = Webplot(TestSuite.WEBPLOT_ADDR, TestSuite.WEBPLOT_PORT, |
| 66 | self.touch_dev) |
| 67 | self.plotter.start() |
| 68 | print color.Fore.YELLOW + 'Plots visible at "%s"' % self.plotter.Url() |
| 69 | webbrowser.open(self.plotter.Url()) |
| 70 | |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 71 | # Compute the list of tests to run |
| 72 | self.tests = tests.generate_test_list(options) |
Charlie Mooney | 7305176 | 2015-02-06 11:52:38 -0800 | [diff] [blame] | 73 | if not self.tests: |
| 74 | print color.Fore.RED + 'Warning: No tests selected!' |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 75 | self.curr_test = 0 |
| 76 | self.curr_variation = 0 |
Charlie Mooney | af9d512 | 2014-12-04 15:15:52 -0800 | [diff] [blame] | 77 | self.curr_iteration = 1 |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 78 | |
Charlie Mooney | 985cf40 | 2015-01-08 15:15:59 -0800 | [diff] [blame] | 79 | # Create a new Report that will store all the test Results |
| 80 | self.report = Report() |
Charlie Mooney | 3d00bdc | 2015-05-08 12:58:35 -0700 | [diff] [blame] | 81 | self.report.title = self.options.title |
Charlie Mooney | 985cf40 | 2015-01-08 15:15:59 -0800 | [diff] [blame] | 82 | |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 83 | def RunNextTestAndVariation(self): |
| 84 | """ Run the next test. |
| 85 | |
| 86 | This function runs the next test/variation combination in the test suite |
| 87 | and advances the internal state to the next one automatically. When |
| 88 | finished, this function return True if there are more tests to run, and |
| 89 | False if the whole test suite is done. |
| 90 | |
| 91 | After a TestSuite is instantiated, this function should be called |
| 92 | repeatedly until it returns False to go through all tests, variations, |
| 93 | and iterations. |
| 94 | """ |
Charlie Mooney | 7305176 | 2015-02-06 11:52:38 -0800 | [diff] [blame] | 95 | if self.curr_test >= len(self.tests): |
| 96 | return False |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 97 | test = self.tests[self.curr_test] |
| 98 | |
| 99 | # Print the header for this new test and variation |
| 100 | prompt = test.PromptForVariation(self.curr_variation) |
| 101 | print color.Fore.WHITE + '-' * 80 |
| 102 | print color.Fore.BLUE + test.name |
| 103 | print color.Fore.GREEN + prompt |
| 104 | |
Charlie Mooney | daaaa24 | 2015-02-03 14:20:23 -0800 | [diff] [blame] | 105 | # Start the function generator (if applicable) |
| 106 | if self.fn_generator: |
| 107 | waveform = test.WaveformForVariation(self.curr_variation) |
| 108 | if waveform: |
| 109 | self.fn_generator.GenerateFunction(*waveform) |
| 110 | else: |
| 111 | self.fn_generator.Off() |
| 112 | |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 113 | # Start a new thread to perform the robot gesture (if applicable) |
| 114 | robot_thread = None |
| 115 | if self.robot: |
| 116 | robot_thread = gesture_interpreter.PerformCorrespondingGesture( |
| 117 | test, self.curr_variation, |
| 118 | self.robot, self.device_spec) |
| 119 | if robot_thread: |
| 120 | robot_thread.start() |
Charlie Mooney | ca3fc20 | 2015-05-21 11:16:53 -0700 | [diff] [blame^] | 121 | else: |
| 122 | next_test, next_var = self._Advance() |
| 123 | return (next_test is not None) |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 124 | |
Charlie Mooney | 389e1f3 | 2015-03-06 13:33:20 -0800 | [diff] [blame] | 125 | # Consume any stray events that happened since the last gesture |
| 126 | print 'Waiting for all contacts to leave before recording the gesture...' |
| 127 | self.touch_dev.FlushSnapshotBuffer() |
Charlie Mooney | c68f9c3 | 2015-04-16 15:23:22 -0700 | [diff] [blame] | 128 | self.plotter.Clear() |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 129 | |
| 130 | # Wait a long time for the first event, then have a much shorter |
| 131 | # timeout on subsequent incoming events |
Charlie Mooney | 389e1f3 | 2015-03-06 13:33:20 -0800 | [diff] [blame] | 132 | snapshots = [] |
Charlie Mooney | 389e1f3 | 2015-03-06 13:33:20 -0800 | [diff] [blame] | 133 | print 'Waiting for 1st snapshot...', |
Charlie Mooney | 1144c9b | 2015-04-16 13:59:56 -0700 | [diff] [blame] | 134 | snapshot = None |
| 135 | if not robot_thread or not robot_thread.isAlive(): |
| 136 | snapshot = self.touch_dev.NextSnapshot(TestSuite.FIRST_SNAPSHOT_TIMEOUT_S) |
| 137 | if not snapshot: |
| 138 | print '\rNo MT snapshots collected before timeout!' |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 139 | while snapshot or (robot_thread and robot_thread.isAlive()): |
| 140 | if snapshot: |
Charlie Mooney | c68f9c3 | 2015-04-16 15:23:22 -0700 | [diff] [blame] | 141 | self.plotter.AddSnapshot(snapshot) |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 142 | snapshots.append(snapshot) |
| 143 | print '\rCollected %d MT snapshots' % len(snapshots), |
Charlie Mooney | 389e1f3 | 2015-03-06 13:33:20 -0800 | [diff] [blame] | 144 | snapshot = self.touch_dev.NextSnapshot(test.timeout) |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 145 | print |
Charlie Mooney | 173396c | 2015-03-19 15:25:41 -0700 | [diff] [blame] | 146 | |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 147 | # Cleanup the robot gesture thread, if it existed |
| 148 | if robot_thread: |
| 149 | robot_thread.join() |
| 150 | |
Charlie Mooney | 76c871f | 2015-05-21 12:01:52 -0700 | [diff] [blame] | 151 | # Run the validators on these events |
| 152 | results = test.RunAllValidators(snapshots) |
| 153 | for result in results: |
| 154 | print result |
| 155 | |
Charlie Mooney | 173396c | 2015-03-19 15:25:41 -0700 | [diff] [blame] | 156 | # Prompt to see if the gesture was performed acceptibly by the user before |
| 157 | # continuing. If the user is unsatisfied abort before advancing on to the |
| 158 | # next test/variation, allowing the test suite to retry this variation. |
| 159 | if not self.options.has_robot: |
| 160 | CONFIRMATION_PROMPT = 'Accept Gesture? ([y]/n) ' |
| 161 | yorn = raw_input(CONFIRMATION_PROMPT).lower() |
| 162 | while yorn not in ['y', 'n', 'yes', 'no', '']: |
| 163 | print color.Fore.RED + 'Error: please enter "y" or "n" only' |
| 164 | yorn = raw_input(CONFIRMATION_PROMPT).lower() |
Charlie Mooney | 173396c | 2015-03-19 15:25:41 -0700 | [diff] [blame] | 165 | if yorn in ['n', 'no']: |
| 166 | print color.Fore.RED + 'Operator rejected the gesture. Please retry...' |
| 167 | return True |
Charlie Mooney | c68f9c3 | 2015-04-16 15:23:22 -0700 | [diff] [blame] | 168 | |
Charlie Mooney | 76c871f | 2015-05-21 12:01:52 -0700 | [diff] [blame] | 169 | # Save the image displayed by the plotter |
Charlie Mooney | c68f9c3 | 2015-04-16 15:23:22 -0700 | [diff] [blame] | 170 | plot_image_png = self.plotter.Save(wait_for_image=True) |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 171 | |
Charlie Mooney | 5e9a10f | 2015-01-13 14:54:27 -0800 | [diff] [blame] | 172 | # Bundle the Validator results with some details of which gesture was used |
| 173 | # during the test for easier debugging. |
Charlie Mooney | 2c270d1 | 2015-04-16 13:38:03 -0700 | [diff] [blame] | 174 | test_result = tests.TestResult(test.variations[self.curr_variation], |
| 175 | results, prompt, plot_image_png) |
Charlie Mooney | 5e9a10f | 2015-01-13 14:54:27 -0800 | [diff] [blame] | 176 | |
Charlie Mooney | 985cf40 | 2015-01-08 15:15:59 -0800 | [diff] [blame] | 177 | # Add the results into our report (And have it print them to stdout, too) |
Charlie Mooney | 76c871f | 2015-05-21 12:01:52 -0700 | [diff] [blame] | 178 | self.report.AddTestResult(test_result) |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 179 | |
| 180 | # Advance the test suite to the next test and variation and return an |
| 181 | # indicator as to whether this was the last thing to do or not. |
| 182 | next_test, next_var = self._Advance() |
| 183 | return (next_test is not None) |
| 184 | |
Charlie Mooney | c68f9c3 | 2015-04-16 15:23:22 -0700 | [diff] [blame] | 185 | def StopPlotter(self): |
| 186 | self.plotter.Quit() |
| 187 | |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 188 | def _Advance(self): |
| 189 | """ Move on to the next test/variation pair |
| 190 | |
| 191 | This function increments all the interal counters, according to the |
| 192 | number of tests, their variations, and the selected number of iterations |
| 193 | and returns the test object and the variation number that should be |
| 194 | done next. |
| 195 | |
| 196 | When the TestSuite is complete, this function will return None, None |
| 197 | otherwise it will return the next Test object and the variation number |
| 198 | the test suite is on. |
| 199 | """ |
| 200 | if self.curr_test >= len(self.tests): |
| 201 | return None, None |
| 202 | test = self.tests[self.curr_test] |
| 203 | |
| 204 | if self.curr_variation >= len(test.variations): |
| 205 | self.curr_test += 1 |
| 206 | self.curr_variation = 0 |
| 207 | self.curr_iteration = 0 |
| 208 | return self._Advance() |
| 209 | |
| 210 | if self.curr_iteration >= self.options.num_iterations: |
| 211 | self.curr_variation += 1 |
| 212 | self.curr_iteration = 0 |
| 213 | return self._Advance() |
| 214 | |
| 215 | self.curr_iteration += 1 |
| 216 | return test, self.curr_variation |