blob: 2a8fbb0833c7684115809ad4f2f2b5ce2b8c621b [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
11
12
13class 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)
35
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
41 self.curr_iteration = 0
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 Mooney3cca6ba2014-11-19 16:15:28 -080071 events = []
Charlie Mooney86b5cf12014-12-04 09:34:09 -080072 plotter = TouchPlotter(self.touch_dev.width, self.touch_dev.height)
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080073 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 Mooney86b5cf12014-12-04 09:34:09 -080079 plotter.add_event(event)
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080080 print '\rCollected %d MTB events' % len(events),
81 events.append(event)
82 event = self.touch_dev.NextEvent(test.timeout)
83 print
Charlie Mooney86b5cf12014-12-04 09:34:09 -080084 plotter.end()
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080085
86 # Run the validators on these events
Charlie Mooneye0d81aa2014-12-03 14:16:27 -080087 # TODO (charliemooney): Run the validators on these MtbPackets here
88 snapshots = mtb.packetize(events)
89 for snapshot in snapshots:
90 for packet in snapshot:
91 print packet
92 print '--'
Charlie Mooney3cca6ba2014-11-19 16:15:28 -080093
94 # Advance the test suite to the next test and variation and return an
95 # indicator as to whether this was the last thing to do or not.
96 next_test, next_var = self._Advance()
97 return (next_test is not None)
98
99 def _Advance(self):
100 """ Move on to the next test/variation pair
101
102 This function increments all the interal counters, according to the
103 number of tests, their variations, and the selected number of iterations
104 and returns the test object and the variation number that should be
105 done next.
106
107 When the TestSuite is complete, this function will return None, None
108 otherwise it will return the next Test object and the variation number
109 the test suite is on.
110 """
111 if self.curr_test >= len(self.tests):
112 return None, None
113 test = self.tests[self.curr_test]
114
115 if self.curr_variation >= len(test.variations):
116 self.curr_test += 1
117 self.curr_variation = 0
118 self.curr_iteration = 0
119 return self._Advance()
120
121 if self.curr_iteration >= self.options.num_iterations:
122 self.curr_variation += 1
123 self.curr_iteration = 0
124 return self._Advance()
125
126 self.curr_iteration += 1
127 return test, self.curr_variation