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 | e0d81aa | 2014-12-03 14:16:27 -0800 | [diff] [blame] | 7 | import mtb |
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 |
| 11 | |
| 12 | |
| 13 | class TestSuite: |
| 14 | """ This class represents a collection of tests and is used to run them |
| 15 | |
| 16 | A TestSuite object will set up a connection to the DUT, robot, etc, and |
| 17 | determine which tests can be run. Once the object is instantiated, |
| 18 | RunNextTestAndVariation() can be run repeatedly to work your way through |
| 19 | the entire suite. |
| 20 | """ |
| 21 | |
| 22 | NO_EVENTS_DETECTED_TIMEOUT_S = 5 |
| 23 | |
| 24 | def __init__(self, options, args): |
| 25 | color.init(autoreset=True) |
| 26 | |
| 27 | self.options = options |
| 28 | |
| 29 | # Open a connection to the device specified |
| 30 | if options.dut_type == 'chromeos': |
| 31 | self.touch_dev = ChromeOSTouchDevice(self.options.addr, |
| 32 | self.options.is_touchscreen) |
| 33 | else: |
| 34 | self.touch_dev = AndroidTouchDevice(self.options.addr, True) |
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 | |
| 37 | # Compute the list of tests to run |
| 38 | self.tests = tests.generate_test_list(options) |
| 39 | self.curr_test = 0 |
| 40 | self.curr_variation = 0 |
Charlie Mooney | af9d512 | 2014-12-04 15:15:52 -0800 | [diff] [blame^] | 41 | self.curr_iteration = 1 |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 42 | |
| 43 | def RunNextTestAndVariation(self): |
| 44 | """ Run the next test. |
| 45 | |
| 46 | This function runs the next test/variation combination in the test suite |
| 47 | and advances the internal state to the next one automatically. When |
| 48 | finished, this function return True if there are more tests to run, and |
| 49 | False if the whole test suite is done. |
| 50 | |
| 51 | After a TestSuite is instantiated, this function should be called |
| 52 | repeatedly until it returns False to go through all tests, variations, |
| 53 | and iterations. |
| 54 | """ |
| 55 | |
| 56 | test = self.tests[self.curr_test] |
| 57 | |
| 58 | # Print the header for this new test and variation |
| 59 | prompt = test.PromptForVariation(self.curr_variation) |
| 60 | print color.Fore.WHITE + '-' * 80 |
| 61 | print color.Fore.BLUE + test.name |
| 62 | print color.Fore.GREEN + prompt |
| 63 | |
| 64 | # Start collecting data |
| 65 | print 'Opening connection with DUT... ', |
| 66 | self.touch_dev.BeginEventStream() |
| 67 | print 'Connection established!' |
| 68 | |
| 69 | # Wait a long time for the first event, then have a much shorter |
| 70 | # timeout on subsequent incoming events |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 71 | events = [] |
Charlie Mooney | 86b5cf1 | 2014-12-04 09:34:09 -0800 | [diff] [blame] | 72 | plotter = TouchPlotter(self.touch_dev.width, self.touch_dev.height) |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 73 | print 'Waiting for 1st event...', |
| 74 | event = self.touch_dev.NextEvent(TestSuite.NO_EVENTS_DETECTED_TIMEOUT_S) |
| 75 | if not event: |
| 76 | print ('\rNo MTB events collected before timeout (%d seconds)!' % |
| 77 | TestSuite.NO_EVENTS_DETECTED_TIMEOUT_S), |
| 78 | while event: |
Charlie Mooney | 86b5cf1 | 2014-12-04 09:34:09 -0800 | [diff] [blame] | 79 | plotter.add_event(event) |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 80 | print '\rCollected %d MTB events' % len(events), |
| 81 | events.append(event) |
| 82 | event = self.touch_dev.NextEvent(test.timeout) |
| 83 | print |
Charlie Mooney | 86b5cf1 | 2014-12-04 09:34:09 -0800 | [diff] [blame] | 84 | plotter.end() |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 85 | |
| 86 | # Run the validators on these events |
Charlie Mooney | e0d81aa | 2014-12-03 14:16:27 -0800 | [diff] [blame] | 87 | snapshots = mtb.packetize(events) |
Charlie Mooney | af9d512 | 2014-12-04 15:15:52 -0800 | [diff] [blame^] | 88 | results = test.RunAllValidators(snapshots) |
| 89 | # TODO (charliemooney): Save these Results and build a report out of them |
| 90 | for result in results: |
| 91 | print result |
Charlie Mooney | 3cca6ba | 2014-11-19 16:15:28 -0800 | [diff] [blame] | 92 | |
| 93 | # Advance the test suite to the next test and variation and return an |
| 94 | # indicator as to whether this was the last thing to do or not. |
| 95 | next_test, next_var = self._Advance() |
| 96 | return (next_test is not None) |
| 97 | |
| 98 | def _Advance(self): |
| 99 | """ Move on to the next test/variation pair |
| 100 | |
| 101 | This function increments all the interal counters, according to the |
| 102 | number of tests, their variations, and the selected number of iterations |
| 103 | and returns the test object and the variation number that should be |
| 104 | done next. |
| 105 | |
| 106 | When the TestSuite is complete, this function will return None, None |
| 107 | otherwise it will return the next Test object and the variation number |
| 108 | the test suite is on. |
| 109 | """ |
| 110 | if self.curr_test >= len(self.tests): |
| 111 | return None, None |
| 112 | test = self.tests[self.curr_test] |
| 113 | |
| 114 | if self.curr_variation >= len(test.variations): |
| 115 | self.curr_test += 1 |
| 116 | self.curr_variation = 0 |
| 117 | self.curr_iteration = 0 |
| 118 | return self._Advance() |
| 119 | |
| 120 | if self.curr_iteration >= self.options.num_iterations: |
| 121 | self.curr_variation += 1 |
| 122 | self.curr_iteration = 0 |
| 123 | return self._Advance() |
| 124 | |
| 125 | self.curr_iteration += 1 |
| 126 | return test, self.curr_variation |