1# Copyright 2015 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 unittest
6
7from telemetry import benchmark
8from telemetry import benchmark_runner
9from telemetry.testing import stream
10import mock
11
12
13class BenchmarkFoo(benchmark.Benchmark):
14  """ Benchmark Foo for testing."""
15
16  @classmethod
17  def Name(cls):
18    return 'FooBenchmark'
19
20
21class BenchmarkBar(benchmark.Benchmark):
22  """ Benchmark Bar for testing long description line."""
23
24  @classmethod
25  def Name(cls):
26    return 'BarBenchmarkkkkk'
27
28class UnusualBenchmark(benchmark.Benchmark):
29  @classmethod
30  def Name(cls):
31    return 'I have a very unusual name'
32
33
34class BenchmarkRunnerUnittest(unittest.TestCase):
35  def setUp(self):
36    self._stream = stream.TestOutputStream()
37    self._mock_possible_browser = mock.MagicMock()
38    self._mock_possible_browser.browser_type = 'TestBrowser'
39
40  def testPrintBenchmarkListWithNoDisabledBenchmark(self):
41    expected_printed_stream = (
42        'Available benchmarks for TestBrowser are:\n'
43        '  BarBenchmarkkkkk  Benchmark Bar for testing long description line.\n'
44        '  FooBenchmark      Benchmark Foo for testing.\n'
45        'Pass --browser to list benchmarks for another browser.\n\n')
46    with mock.patch('telemetry.benchmark_runner.decorators') as mock_module:
47      mock_module.IsEnabled.return_value = (True, None)
48      benchmark_runner.PrintBenchmarkList(
49        [BenchmarkFoo, BenchmarkBar], self._mock_possible_browser, self._stream)
50      self.assertEquals(expected_printed_stream, self._stream.output_data)
51
52  def testPrintBenchmarkListWithOneDisabledBenchmark(self):
53    expected_printed_stream = (
54        'Available benchmarks for TestBrowser are:\n'
55        '  FooBenchmark      Benchmark Foo for testing.\n'
56        '\n'
57        'Disabled benchmarks for TestBrowser are (force run with -d):\n'
58        '  BarBenchmarkkkkk  Benchmark Bar for testing long description line.\n'
59        'Pass --browser to list benchmarks for another browser.\n\n')
60    with mock.patch('telemetry.benchmark_runner.decorators') as mock_module:
61      def FakeIsEnabled(benchmark_class, _):
62        if benchmark_class is BenchmarkFoo:
63          return True, None
64        else:
65          return False, 'Only supported BenchmarkFoo'
66      mock_module.IsEnabled = FakeIsEnabled
67      benchmark_runner.PrintBenchmarkList(
68        [BenchmarkFoo, BenchmarkBar], self._mock_possible_browser, self._stream)
69      self.assertEquals(expected_printed_stream, self._stream.output_data)
70
71  def testShouldDisable(self):
72    """Ensure that overridden ShouldDisable class methods are respected."""
73    expected_printed_stream = (
74        'Available benchmarks for TestBrowser are:\n'
75        '  BarBenchmarkkkkk  Benchmark Bar for testing long description line.\n'
76        '\n'
77        'Disabled benchmarks for TestBrowser are (force run with -d):\n'
78        '  FooBenchmark      Benchmark Foo for testing.\n'
79        'Pass --browser to list benchmarks for another browser.\n\n')
80    @classmethod
81    def FakeShouldDisable(cls, possible_browser):
82      del possible_browser  # unused
83      return cls is BenchmarkFoo
84    BenchmarkFoo.ShouldDisable = FakeShouldDisable
85    BenchmarkBar.ShouldDisable = FakeShouldDisable
86    benchmark_runner.PrintBenchmarkList(
87      [BenchmarkFoo, BenchmarkBar], self._mock_possible_browser, self._stream)
88    self.assertEquals(expected_printed_stream, self._stream.output_data)
89
90  def testShouldDisableComplex(self):
91    """Ensure that browser-dependent ShouldDisable overrides are respected."""
92    expected_printed_stream = (
93        # Expected output for 'TestBrowser':
94        'Available benchmarks for TestBrowser are:\n'
95        '  FooBenchmark      Benchmark Foo for testing.\n'
96        '\n'
97        'Disabled benchmarks for TestBrowser are (force run with -d):\n'
98        '  BarBenchmarkkkkk  Benchmark Bar for testing long description line.\n'
99        'Pass --browser to list benchmarks for another browser.\n\n'
100        # Expected output for 'MockBrowser':
101        'Available benchmarks for MockBrowser are:\n'
102        '  BarBenchmarkkkkk  Benchmark Bar for testing long description line.\n'
103        '  FooBenchmark      Benchmark Foo for testing.\n'
104        'Pass --browser to list benchmarks for another browser.\n\n')
105    @classmethod
106    def FakeShouldDisable(cls, possible_browser):
107      return cls is BenchmarkBar and not 'Mock' in possible_browser.browser_type
108    BenchmarkFoo.ShouldDisable = FakeShouldDisable
109    BenchmarkBar.ShouldDisable = FakeShouldDisable
110    benchmark_runner.PrintBenchmarkList(
111      [BenchmarkFoo, BenchmarkBar], self._mock_possible_browser, self._stream)
112    self._mock_possible_browser.browser_type = 'MockBrowser'
113    benchmark_runner.PrintBenchmarkList(
114      [BenchmarkFoo, BenchmarkBar], self._mock_possible_browser, self._stream)
115    self.assertEquals(expected_printed_stream, self._stream.output_data)
116