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.
8 from telemetry import benchmark
9 from telemetry.core import util
10 from telemetry.core import wpr_modes
11 from telemetry.page import page as page_module
12 from telemetry.page import page_set as page_set_module
13 from telemetry.page import page_test
14 from telemetry.page import record_wpr
15 from telemetry.unittest import tab_test_case
18 class MockPage(page_module.Page):
19 def __init__(self, page_set, url):
20 super(MockPage, self).__init__(url=url,
22 base_dir=util.GetUnittestDataDir())
25 def RunNavigateSteps(self, action_runner):
26 self.func_calls.append('RunNavigateSteps')
27 super(MockPage, self).RunNavigateSteps(action_runner)
30 self.func_calls.append('RunFoo')
33 self.func_calls.append('RunBar')
36 self.func_calls.append('RunBaz')
39 class MockPageSet(page_set_module.PageSet):
40 def __init__(self, url=''):
41 super(MockPageSet, self).__init__(archive_data_file='data/test.json')
42 self.AddPage(MockPage(self, url))
45 class MockPageTest(page_test.PageTest):
47 super(MockPageTest, self).__init__()
48 self._action_name_to_run = "RunBaz"
52 def AddCommandLineArgs(cls, parser):
53 parser.add_option('--mock-page-test-option', action="store_true")
55 def WillNavigateToPage(self, page, tab):
56 self.func_calls.append('WillNavigateToPage')
58 def DidNavigateToPage(self, page, tab):
59 self.func_calls.append('DidNavigateToPage')
61 def WillRunActions(self, page, tab):
62 self.func_calls.append('WillRunActions')
64 def DidRunActions(self, page, tab):
65 self.func_calls.append('DidRunActions')
67 def ValidatePage(self, page, tab, results):
68 self.func_calls.append('ValidatePage')
71 class MockBenchmark(benchmark.Benchmark):
76 def AddTestCommandLineArgs(cls, group):
77 group.add_option('', '--mock-benchmark-url', action='store', type='string')
79 def CreatePageSet(self, options):
81 if (options.mock_benchmark_url):
82 kwargs['url'] = options.mock_benchmark_url
83 self.mock_page_set = MockPageSet(**kwargs)
84 return self.mock_page_set
87 class RecordWprUnitTests(tab_test_case.TabTestCase):
89 _base_dir = util.GetUnittestDataDir()
90 _test_data_dir = os.path.join(util.GetUnittestDataDir(), 'page_tests')
94 sys.path.extend([cls._base_dir, cls._test_data_dir])
95 super(RecordWprUnitTests, cls).setUpClass()
96 cls._url = cls.UrlOfUnittestFile('blank.html')
98 # When the RecorderPageTest is created from a PageSet, we do not have a
99 # PageTest to use. In this case, we will record every available action.
100 def testRunPage_AllActions(self):
101 record_page_test = record_wpr.RecorderPageTest(["RunFoo", "RunBar"])
102 page = MockPage(page_set=MockPageSet(url=self._url), url=self._url)
103 record_page_test.RunPage(page, self._tab, results=None)
104 self.assertTrue('RunFoo' in page.func_calls)
105 self.assertTrue('RunBar' in page.func_calls)
106 self.assertFalse('RunBaz' in page.func_calls)
108 def testRunPage_DontReloadSingleActions(self):
109 record_page_test = record_wpr.RecorderPageTest(["RunFoo"])
110 page = MockPage(page_set=MockPageSet(url=self._url), url=self._url)
111 record_page_test.RunPage(page, self._tab, results=None)
112 self.assertFalse('RunNavigateSteps' in page.func_calls)
114 def testRunPage_ReloadPageBetweenActions(self):
115 record_page_test = record_wpr.RecorderPageTest(["RunFoo", "RunBar"])
116 page = MockPage(page_set=MockPageSet(url=self._url), url=self._url)
117 record_page_test.RunPage(page, self._tab, results=None)
118 self.assertTrue('RunNavigateSteps' in page.func_calls)
120 # When the RecorderPageTest is created from a Benchmark, the benchmark will
121 # have a PageTest, specified by its test attribute.
122 def testRunPage_OnlyRunBenchmarkAction(self):
123 record_page_test = record_wpr.RecorderPageTest(["RunFoo"])
124 record_page_test.page_test = MockBenchmark().test()
125 page = MockPage(page_set=MockPageSet(url=self._url), url=self._url)
126 record_page_test.RunPage(page, self._tab, results=None)
127 self.assertFalse('RunFoo' in page.func_calls)
128 self.assertTrue('RunBaz' in page.func_calls)
130 def testRunPage_CallBenchmarksPageTestsFunctions(self):
131 record_page_test = record_wpr.RecorderPageTest([])
132 record_page_test.page_test = MockBenchmark().test()
133 page = MockPage(page_set=MockPageSet(url=self._url), url=self._url)
134 record_page_test.RunPage(page, self._tab, results=None)
135 self.assertEqual(3, len(record_page_test.page_test.func_calls))
136 self.assertEqual('WillRunActions', record_page_test.page_test.func_calls[0])
137 self.assertEqual('DidRunActions', record_page_test.page_test.func_calls[1])
138 self.assertEqual('ValidatePage', record_page_test.page_test.func_calls[2])
140 def testWprRecorderWithPageSet(self):
142 mock_page_set = MockPageSet(url=self._url)
143 wpr_recorder = record_wpr.WprRecorder(self._test_data_dir,
144 mock_page_set, flags)
145 results = wpr_recorder.CreateResults()
146 wpr_recorder.Record(results)
147 self.assertEqual(set(mock_page_set.pages), results.pages_that_succeeded)
149 def testWprRecorderWithBenchmark(self):
150 flags = ['--mock-benchmark-url', self._url]
151 mock_benchmark = MockBenchmark()
152 wpr_recorder = record_wpr.WprRecorder(self._test_data_dir, mock_benchmark,
154 results = wpr_recorder.CreateResults()
155 wpr_recorder.Record(results)
156 self.assertEqual(set(mock_benchmark.mock_page_set.pages),
157 results.pages_that_succeeded)
159 def testCommandLineFlags(self):
161 '--page-repeat', '2',
162 '--mock-benchmark-url', self._url,
163 '--mock-page-test-option',
165 wpr_recorder = record_wpr.WprRecorder(self._test_data_dir, MockBenchmark(),
167 # page_runner command-line args
168 self.assertEquals(2, wpr_recorder.options.page_repeat)
169 # benchmark command-line args
170 self.assertEquals(self._url, wpr_recorder.options.mock_benchmark_url)
171 # benchmark's page_test command-line args
172 self.assertTrue(wpr_recorder.options.mock_page_test_option)
173 # invalid command-line args
174 self.assertFalse(hasattr(wpr_recorder.options, 'not_a_real_option'))
176 def testRecordingEnabled(self):
177 flags = ['--mock-benchmark-url', self._url]
178 wpr_recorder = record_wpr.WprRecorder(self._test_data_dir, MockBenchmark(),
180 self.assertEqual(wpr_modes.WPR_RECORD,
181 wpr_recorder.options.browser_options.wpr_mode)
183 def testFindAllActionNames(self):
184 # The src/tools/telemetry/unittest_data/page_tests/ has been
185 # populated with three simple Page Measurement classes, the first two of
186 # which have action_name_to_run defined.
187 action_names_to_run = record_wpr.FindAllActionNames(self._test_data_dir)
188 self.assertTrue('RunFoo' in action_names_to_run)
189 self.assertTrue('RunBar' in action_names_to_run)
190 self.assertFalse('RunBaz' in action_names_to_run)