1# Copyright 2014 The Chromium 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 sys
6import unittest
7
8from telemetry.core import exceptions
9from telemetry.unittest import gtest_progress_reporter
10from telemetry.unittest import simple_mock
11
12
13try:
14  raise exceptions.IntentionalException()
15except exceptions.IntentionalException:
16  INTENTIONAL_EXCEPTION = sys.exc_info()
17
18
19class TestFoo(unittest.TestCase):
20  # Test method doesn't have test- prefix intentionally. This is so that
21  # run_test script won't run this test.
22  def runTezt(self):
23    pass
24
25
26class TestOutputStream(object):
27  def __init__(self):
28    self._output_data = []
29
30  @property
31  def output_data(self):
32    return ''.join(self._output_data)
33
34  def write(self, data):
35    self._output_data.append(data)
36
37  def flush(self):
38    pass
39
40
41class TestResultWithSuccesses(unittest.TestResult):
42  def __init__(self):
43    super(TestResultWithSuccesses, self).__init__()
44    self.successes = []
45
46  def addSuccess(self, test):
47    super(TestResultWithSuccesses, self).addSuccess(test)
48    self.successes.append(test)
49
50
51class GTestProgressReporterTest(unittest.TestCase):
52  def setUp(self):
53    super(GTestProgressReporterTest, self).setUp()
54    self._stream = TestOutputStream()
55    self._formatter = gtest_progress_reporter.GTestProgressReporter(
56        self._stream)
57
58    self._mock_timer = simple_mock.MockTimer(gtest_progress_reporter)
59
60  def tearDown(self):
61    self._mock_timer.Restore()
62
63  def testTestSuiteWithWrapperSuite(self):
64    suite = unittest.TestSuite()
65    suite.addTest(unittest.TestSuite())
66    self._formatter.StartTestSuite(suite)
67    self._formatter.StopTestSuite(suite)
68
69    self.assertEqual(self._stream.output_data, '')
70
71  def testTestSuiteWithTestCase(self):
72    suite = unittest.TestSuite()
73    suite.addTest(TestFoo(methodName='runTezt'))
74    self._formatter.StartTestSuite(suite)
75    self._mock_timer.SetTime(0.042)
76    self._formatter.StopTestSuite(suite)
77
78    expected = ('[----------] 1 test\n'
79                '[----------] 1 test (42 ms total)\n\n')
80    self.assertEqual(self._stream.output_data, expected)
81
82  def testCaseFailure(self):
83    test = TestFoo(methodName='runTezt')
84    self._formatter.StartTest(test)
85    self._mock_timer.SetTime(0.042)
86    self._formatter.Failure(test, INTENTIONAL_EXCEPTION)
87
88    expected = (
89        '[ RUN      ] gtest_progress_reporter_unittest.TestFoo.runTezt\n'
90        '[  FAILED  ] gtest_progress_reporter_unittest.TestFoo.runTezt '
91        '(42 ms)\n')
92    self.assertEqual(self._stream.output_data, expected)
93
94  def testCaseSuccess(self):
95    test = TestFoo(methodName='runTezt')
96    self._formatter.StartTest(test)
97    self._mock_timer.SetTime(0.042)
98    self._formatter.Success(test)
99
100    expected = (
101        '[ RUN      ] gtest_progress_reporter_unittest.TestFoo.runTezt\n'
102        '[       OK ] gtest_progress_reporter_unittest.TestFoo.runTezt '
103        '(42 ms)\n')
104    self.assertEqual(self._stream.output_data, expected)
105
106  def testStopTestRun(self):
107    result = TestResultWithSuccesses()
108    self._formatter.StopTestRun(result)
109
110    expected = '[  PASSED  ] 0 tests.\n\n'
111    self.assertEqual(self._stream.output_data, expected)
112
113  def testStopTestRunWithFailureAndSuccess(self):
114    test = TestFoo(methodName='runTezt')
115    result = TestResultWithSuccesses()
116    result.addSuccess(test)
117    result.addFailure(test, INTENTIONAL_EXCEPTION)
118    self._formatter.StopTestRun(result)
119
120    expected = (
121        '[  PASSED  ] 1 test.\n'
122        '[  FAILED  ] 1 test, listed below:\n'
123        '[  FAILED  ]  gtest_progress_reporter_unittest.TestFoo.runTezt\n\n'
124        '1 FAILED TEST\n\n')
125    self.assertEqual(self._stream.output_data, expected)
126