1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch# Copyright 2014 The Chromium Authors. All rights reserved.
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch# Use of this source code is governed by a BSD-style license that can be
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch# found in the LICENSE file.
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdochimport unittest
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdochfrom telemetry.core import util
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdochfrom telemetry.unittest import options_for_unittests
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class ProgressReporter(object):
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def __init__(self, output_stream):
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    self._output_stream = output_stream
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def StartTest(self, test):
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    pass
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def StartTestSuite(self, suite):
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    pass
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def StartTestRun(self):
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    pass
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def StopTest(self, test):
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    pass
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def StopTestSuite(self, suite):
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    pass
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def StopTestRun(self, result):
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    pass
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def Error(self, test, err):
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    pass
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def Failure(self, test, err):
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    pass
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def Success(self, test):
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    pass
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def Skip(self, test, reason):
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    pass
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass TestSuite(unittest.TestSuite):
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  """TestSuite that can delegate start and stop calls to a TestResult object."""
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def run(self, result):  # pylint: disable=W0221
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if hasattr(result, 'startTestSuite'):
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      result.startTestSuite(self)
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    result = super(TestSuite, self).run(result)
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if hasattr(result, 'stopTestSuite'):
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      result.stopTestSuite(self)
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return result
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass TestRunner(object):
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  def run(self, test, progress_reporters, repeat_count, args):
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    util.AddDirToPythonPath(util.GetUnittestDataDir())
606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    result = TestResult(progress_reporters)
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    result.startTestRun()
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    try:
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      options_for_unittests.Push(args)
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      for _ in xrange(repeat_count):
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        test(result)
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    finally:
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      options_for_unittests.Pop()
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      result.stopTestRun()
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return result
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass TestResult(unittest.TestResult):
746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  def __init__(self, progress_reporters):
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    super(TestResult, self).__init__()
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    self.successes = []
776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    self._progress_reporters = progress_reporters
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  @property
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def failures_and_errors(self):
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return self.failures + self.errors
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def startTest(self, test):
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    super(TestResult, self).startTest(test)
856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    for progress_reporter in self._progress_reporters:
866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      progress_reporter.StartTest(test)
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def startTestSuite(self, suite):
896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    for progress_reporter in self._progress_reporters:
906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      progress_reporter.StartTestSuite(suite)
91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def startTestRun(self):
93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    super(TestResult, self).startTestRun()
946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    for progress_reporter in self._progress_reporters:
956e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      progress_reporter.StartTestRun()
96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def stopTest(self, test):
98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    super(TestResult, self).stopTest(test)
996e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    for progress_reporter in self._progress_reporters:
1006e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      progress_reporter.StopTest(test)
101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def stopTestSuite(self, suite):
1036e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    for progress_reporter in self._progress_reporters:
1046e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      progress_reporter.StopTestSuite(suite)
105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
106116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def stopTestRun(self):
107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    super(TestResult, self).stopTestRun()
1086e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    for progress_reporter in self._progress_reporters:
1096e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      progress_reporter.StopTestRun(self)
110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def addError(self, test, err):
112116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    super(TestResult, self).addError(test, err)
1136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    for progress_reporter in self._progress_reporters:
1146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      progress_reporter.Error(test, err)
115116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def addFailure(self, test, err):
117116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    super(TestResult, self).addFailure(test, err)
1186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    for progress_reporter in self._progress_reporters:
1196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      progress_reporter.Failure(test, err)
120116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
121116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def addSuccess(self, test):
122116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    super(TestResult, self).addSuccess(test)
123116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    self.successes.append(test)
1246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    for progress_reporter in self._progress_reporters:
1256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      progress_reporter.Success(test)
126116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
127116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  def addSkip(self, test, reason):
128116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    super(TestResult, self).addSkip(test, reason)
1296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    for progress_reporter in self._progress_reporters:
1306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      progress_reporter.Skip(test, reason)
131