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