blob: 38822d1edb48c54f8c528b646fca4c7005aa2d50 [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 Mooneye0d81aa2014-12-03 14:16:27 -08007import mtb
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 Mooneyaf9d5122014-12-04 15:15:52 -080036 tests.validator.BaseValidator._device = self.touch_dev
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080037
38 # Compute the list of tests to run
39 self.tests = tests.generate_test_list(options)
40 self.curr_test = 0
41 self.curr_variation = 0
Charlie Mooneyaf9d5122014-12-04 15:15:52 -080042 self.curr_iteration = 1
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080043
Charlie Mooney985cf402015-01-08 15:15:59 -080044 # Create a new Report that will store all the test Results
45 self.report = Report()
46
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080047 def RunNextTestAndVariation(self):
48 """ Run the next test.
49
50 This function runs the next test/variation combination in the test suite
51 and advances the internal state to the next one automatically. When
52 finished, this function return True if there are more tests to run, and
53 False if the whole test suite is done.
54
55 After a TestSuite is instantiated, this function should be called
56 repeatedly until it returns False to go through all tests, variations,
57 and iterations.
58 """
59
60 test = self.tests[self.curr_test]
61
62 # Print the header for this new test and variation
63 prompt = test.PromptForVariation(self.curr_variation)
64 print color.Fore.WHITE + '-' * 80
65 print color.Fore.BLUE + test.name
66 print color.Fore.GREEN + prompt
67
68 # Start collecting data
69 print 'Opening connection with DUT... ',
70 self.touch_dev.BeginEventStream()
71 print 'Connection established!'
72
73 # Wait a long time for the first event, then have a much shorter
74 # timeout on subsequent incoming events
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080075 events = []
Charlie Mooney350fbc32015-01-07 13:27:09 -080076 plotter = TouchPlotter(self.touch_dev.x_min, self.touch_dev.x_max,
77 self.touch_dev.y_min, self.touch_dev.y_max)
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080078 print 'Waiting for 1st event...',
79 event = self.touch_dev.NextEvent(TestSuite.NO_EVENTS_DETECTED_TIMEOUT_S)
80 if not event:
81 print ('\rNo MTB events collected before timeout (%d seconds)!' %
82 TestSuite.NO_EVENTS_DETECTED_TIMEOUT_S),
83 while event:
Charlie Mooney86b5cf12014-12-04 09:34:09 -080084 plotter.add_event(event)
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080085 print '\rCollected %d MTB events' % len(events),
86 events.append(event)
87 event = self.touch_dev.NextEvent(test.timeout)
88 print
Charlie Mooney97d73c12015-01-09 08:57:03 -080089 plot_image_png = plotter.end()
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080090
91 # Run the validators on these events
Charlie Mooney16fc0f12014-12-29 14:10:48 -080092 snapshots = mtb.process(events)
Charlie Mooneyaf9d5122014-12-04 15:15:52 -080093 results = test.RunAllValidators(snapshots)
Charlie Mooney985cf402015-01-08 15:15:59 -080094
Charlie Mooney5e9a10f2015-01-13 14:54:27 -080095 # Bundle the Validator results with some details of which gesture was used
96 # during the test for easier debugging.
97 test_result = tests.TestResult(results, prompt, plot_image_png)
98
Charlie Mooney985cf402015-01-08 15:15:59 -080099 # Add the results into our report (And have it print them to stdout, too)
Charlie Mooney5e9a10f2015-01-13 14:54:27 -0800100 self.report.AddTestResult(test_result, verbose=True)
Charlie Mooney3cca6ba2014-11-19 16:15:28 -0800101
102 # Advance the test suite to the next test and variation and return an
103 # indicator as to whether this was the last thing to do or not.
104 next_test, next_var = self._Advance()
105 return (next_test is not None)
106
107 def _Advance(self):
108 """ Move on to the next test/variation pair
109
110 This function increments all the interal counters, according to the
111 number of tests, their variations, and the selected number of iterations
112 and returns the test object and the variation number that should be
113 done next.
114
115 When the TestSuite is complete, this function will return None, None
116 otherwise it will return the next Test object and the variation number
117 the test suite is on.
118 """
119 if self.curr_test >= len(self.tests):
120 return None, None
121 test = self.tests[self.curr_test]
122
123 if self.curr_variation >= len(test.variations):
124 self.curr_test += 1
125 self.curr_variation = 0
126 self.curr_iteration = 0
127 return self._Advance()
128
129 if self.curr_iteration >= self.options.num_iterations:
130 self.curr_variation += 1
131 self.curr_iteration = 0
132 return self._Advance()
133
134 self.curr_iteration += 1
135 return test, self.curr_variation