blob: 20517266c5bde1744b241906f4700485b53afccc [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 Mooney86b5cf12014-12-04 09:34:09 -080076 plotter = TouchPlotter(self.touch_dev.width, self.touch_dev.height)
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080077 print 'Waiting for 1st event...',
78 event = self.touch_dev.NextEvent(TestSuite.NO_EVENTS_DETECTED_TIMEOUT_S)
79 if not event:
80 print ('\rNo MTB events collected before timeout (%d seconds)!' %
81 TestSuite.NO_EVENTS_DETECTED_TIMEOUT_S),
82 while event:
Charlie Mooney86b5cf12014-12-04 09:34:09 -080083 plotter.add_event(event)
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080084 print '\rCollected %d MTB events' % len(events),
85 events.append(event)
86 event = self.touch_dev.NextEvent(test.timeout)
87 print
Charlie Mooney86b5cf12014-12-04 09:34:09 -080088 plotter.end()
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080089
90 # Run the validators on these events
Charlie Mooney16fc0f12014-12-29 14:10:48 -080091 snapshots = mtb.process(events)
Charlie Mooneyaf9d5122014-12-04 15:15:52 -080092 results = test.RunAllValidators(snapshots)
Charlie Mooney985cf402015-01-08 15:15:59 -080093
94 # Add the results into our report (And have it print them to stdout, too)
95 self.report.AddResults(results, verbose=True)
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080096
97 # Advance the test suite to the next test and variation and return an
98 # indicator as to whether this was the last thing to do or not.
99 next_test, next_var = self._Advance()
100 return (next_test is not None)
101
102 def _Advance(self):
103 """ Move on to the next test/variation pair
104
105 This function increments all the interal counters, according to the
106 number of tests, their variations, and the selected number of iterations
107 and returns the test object and the variation number that should be
108 done next.
109
110 When the TestSuite is complete, this function will return None, None
111 otherwise it will return the next Test object and the variation number
112 the test suite is on.
113 """
114 if self.curr_test >= len(self.tests):
115 return None, None
116 test = self.tests[self.curr_test]
117
118 if self.curr_variation >= len(test.variations):
119 self.curr_test += 1
120 self.curr_variation = 0
121 self.curr_iteration = 0
122 return self._Advance()
123
124 if self.curr_iteration >= self.options.num_iterations:
125 self.curr_variation += 1
126 self.curr_iteration = 0
127 return self._Advance()
128
129 self.curr_iteration += 1
130 return test, self.curr_variation