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