1# Copyright 2016 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 string
6import time
7
8from telemetry.testing import serially_executed_browser_test_case
9
10
11_prev_test_name = None
12
13class SimpleTest(
14    serially_executed_browser_test_case.SeriallyExecutedBrowserTestCase):
15
16  @classmethod
17  def AddCommandlineArgs(cls, parser):
18    parser.add_option('--adder-sum', type=int, default=5)
19
20  def setUp(self):
21    self.extra = 5
22
23  @classmethod
24  def GenerateTestCases_AdderTest(cls, options):
25    yield 'add_1_and_2', (1, 2, options.adder_sum)
26    yield 'add_2_and_3', (2, 3, options.adder_sum)
27    yield 'add_7_and_3', (7, 3, options.adder_sum)
28    # Filtered out in browser_test_runner_unittest.py
29    yield 'dontrun_add_1_and_2', (1, 2, options.adder_sum)
30
31  @classmethod
32  def GenerateTestCases_AlphabeticalTest(cls, options):
33    del options  # unused
34    prefix = 'Alphabetical_'
35    test_names = []
36    for character in string.lowercase[:26]:
37      test_names.append(prefix + character)
38    for character in string.uppercase[:26]:
39      test_names.append(prefix + character)
40    for num in xrange(20):
41      test_names.append(prefix + str(num))
42
43    # Shuffle |test_names| so the tests will be generated in a random order.
44    test_names = (test_names[25:40] + test_names[40:70] + test_names[:25] +
45                  test_names[70:])
46    for t in test_names:
47      yield t, ()
48
49  def AlphabeticalTest(self):
50    test_name = self.id()
51    global _prev_test_name
52    self.assertLess(_prev_test_name, test_name)
53    _prev_test_name = test_name
54
55  def AdderTest(self, a, b, partial_sum):
56    self.assertEqual(a + b, partial_sum)
57
58  @classmethod
59  def GenerateTestCases_MultiplierTest(cls, options):
60    del options  # unused
61    yield 'multiplier_simple', (10, 2, 4)
62    yield 'multiplier_simple_2', (2, 3, 5)
63    yield 'multiplier_simple_3', (10, 3, 6)
64    # Filtered out in browser_test_runner_unittest.py
65    yield 'dontrun_multiplier_simple', (10, 2, 4)
66
67  def MultiplierTest(self, a, b, partial_sum):
68    self.assertEqual(a * b, partial_sum * self.extra)
69
70  def TestSimple(self):
71    time.sleep(0.5)
72    self.assertEqual(1, self.extra)
73
74  def TestException(self):
75    raise Exception('Expected exception')
76