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