• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 os
6import sys
7
8from telemetry import benchmark
9from telemetry import story
10from telemetry.core import util
11from telemetry import decorators
12from telemetry.page import page as page_module
13from telemetry.page import legacy_page_test
14from telemetry import record_wpr
15from telemetry.testing import options_for_unittests
16from telemetry.testing import tab_test_case
17from telemetry.util import wpr_modes
18
19
20class MockPage(page_module.Page):
21  def __init__(self, story_set, url):
22    super(MockPage, self).__init__(url=url,
23                                   page_set=story_set,
24                                   base_dir=util.GetUnittestDataDir())
25    self.func_calls = []
26
27  def RunNavigateSteps(self, action_runner):
28    self.func_calls.append('RunNavigateSteps')
29    super(MockPage, self).RunNavigateSteps(action_runner)
30
31  def RunPageInteractions(self, _):
32    self.func_calls.append('RunPageInteractions')
33
34  def RunSmoothness(self, _):
35    self.func_calls.append('RunSmoothness')
36
37class MockStorySet(story.StorySet):
38  def __init__(self, url=''):
39    super(MockStorySet, self).__init__(
40        archive_data_file='data/archive_files/test.json')
41    self.AddStory(MockPage(self, url))
42
43
44class MockPageTest(legacy_page_test.LegacyPageTest):
45  def __init__(self):
46    super(MockPageTest, self).__init__()
47    self._action_name_to_run = "RunPageInteractions"
48    self.func_calls = []
49
50  def CustomizeBrowserOptions(self, options):
51    self.func_calls.append('CustomizeBrowserOptions')
52
53  def WillNavigateToPage(self, page, tab):
54    self.func_calls.append('WillNavigateToPage')
55
56  def DidNavigateToPage(self, page, tab):
57    self.func_calls.append('DidNavigateToPage')
58
59  def ValidateAndMeasurePage(self, page, tab, results):
60    self.func_calls.append('ValidateAndMeasurePage')
61
62  def WillStartBrowser(self, platform):
63    self.func_calls.append('WillStartBrowser')
64
65  def DidStartBrowser(self, browser):
66    self.func_calls.append('DidStartBrowser')
67
68class MockBenchmark(benchmark.Benchmark):
69  test = MockPageTest
70
71  def __init__(self):
72    super(MockBenchmark, self).__init__()
73    self.mock_story_set = None
74
75  @classmethod
76  def AddBenchmarkCommandLineArgs(cls, group):
77    group.add_option('', '--mock-benchmark-url', action='store', type='string')
78
79  def CreateStorySet(self, options):
80    kwargs = {}
81    if options.mock_benchmark_url:
82      kwargs['url'] = options.mock_benchmark_url
83    self.mock_story_set = MockStorySet(**kwargs)
84    return self.mock_story_set
85
86
87class MockTimelineBasedMeasurementBenchmark(benchmark.Benchmark):
88
89  def __init__(self):
90    super(MockTimelineBasedMeasurementBenchmark, self).__init__()
91    self.mock_story_set = None
92
93  @classmethod
94  def AddBenchmarkCommandLineArgs(cls, group):
95    group.add_option('', '--mock-benchmark-url', action='store', type='string')
96
97  def CreateStorySet(self, options):
98    kwargs = {}
99    if options.mock_benchmark_url:
100      kwargs['url'] = options.mock_benchmark_url
101    self.mock_story_set = MockStorySet(**kwargs)
102    return self.mock_story_set
103
104
105class RecordWprUnitTests(tab_test_case.TabTestCase):
106
107  _base_dir = util.GetUnittestDataDir()
108  _test_data_dir = os.path.join(util.GetUnittestDataDir(), 'page_tests')
109
110  @classmethod
111  def setUpClass(cls):
112    sys.path.extend([cls._base_dir, cls._test_data_dir])
113    super(RecordWprUnitTests, cls).setUpClass()
114    cls._url = cls.UrlOfUnittestFile('blank.html')
115    cls._test_options = options_for_unittests.GetCopy()
116
117  # When the RecorderPageTest is created from a PageSet, we do not have a
118  # PageTest to use. In this case, we will record every available action.
119  def testRunPage_AllActions(self):
120    record_page_test = record_wpr.RecorderPageTest()
121    page = MockPage(story_set=MockStorySet(url=self._url), url=self._url)
122
123    record_page_test.RunNavigateSteps(page, self._tab)
124    self.assertTrue('RunNavigateSteps' in page.func_calls)
125
126  # When the RecorderPageTest is created from a Benchmark, the benchmark will
127  # have a PageTest, specified by its test attribute.
128  def testRunPage_OnlyRunBenchmarkAction(self):
129    record_page_test = record_wpr.RecorderPageTest()
130    record_page_test.page_test = MockBenchmark().test()
131    page = MockPage(story_set=MockStorySet(url=self._url), url=self._url)
132    record_page_test.ValidateAndMeasurePage(page, self._tab, results=None)
133
134  def testRunPage_CallBenchmarksPageTestsFunctions(self):
135    record_page_test = record_wpr.RecorderPageTest()
136    record_page_test.page_test = MockBenchmark().test()
137    page = MockPage(story_set=MockStorySet(url=self._url), url=self._url)
138    record_page_test.ValidateAndMeasurePage(page, self._tab, results=None)
139    self.assertEqual(1, len(record_page_test.page_test.func_calls))
140    self.assertEqual('ValidateAndMeasurePage',
141                     record_page_test.page_test.func_calls[0])
142
143  def GetBrowserDeviceFlags(self):
144    flags = ['--browser', self._browser.browser_type,
145             '--remote', self._test_options.cros_remote,
146             '--device', self._device]
147    if self._test_options.chrome_root:
148      flags += ['--chrome-root', self._test_options.chrome_root]
149    return flags
150
151  @decorators.Disabled('chromeos') # crbug.com/404868.
152  def testWprRecorderWithPageSet(self):
153    flags = self.GetBrowserDeviceFlags()
154    mock_story_set = MockStorySet(url=self._url)
155    wpr_recorder = record_wpr.WprRecorder(self._test_data_dir,
156                                          mock_story_set, flags)
157    results = wpr_recorder.CreateResults()
158    wpr_recorder.Record(results)
159    self.assertEqual(set(mock_story_set.stories), results.pages_that_succeeded)
160
161  def testWprRecorderWithBenchmark(self):
162    flags = self.GetBrowserDeviceFlags()
163    flags.extend(['--mock-benchmark-url', self._url])
164    mock_benchmark = MockBenchmark()
165    wpr_recorder = record_wpr.WprRecorder(self._test_data_dir, mock_benchmark,
166                                          flags)
167    results = wpr_recorder.CreateResults()
168    wpr_recorder.Record(results)
169    self.assertEqual(set(mock_benchmark.mock_story_set.stories),
170                     results.pages_that_succeeded)
171
172  def testWprRecorderWithTimelineBasedMeasurementBenchmark(self):
173    flags = self.GetBrowserDeviceFlags()
174    flags.extend(['--mock-benchmark-url', self._url])
175    mock_benchmark = MockTimelineBasedMeasurementBenchmark()
176    wpr_recorder = record_wpr.WprRecorder(self._test_data_dir, mock_benchmark,
177                                          flags)
178    results = wpr_recorder.CreateResults()
179    wpr_recorder.Record(results)
180    self.assertEqual(set(mock_benchmark.mock_story_set.stories),
181                     results.pages_that_succeeded)
182
183  def testPageSetBaseDirFlag(self):
184    flags = self.GetBrowserDeviceFlags()
185    flags.extend(['--page-set-base-dir', self._test_data_dir,
186                  '--mock-benchmark-url', self._url])
187    mock_benchmark = MockBenchmark()
188    wpr_recorder = record_wpr.WprRecorder(
189        'non-existent-dummy-dir', mock_benchmark, flags)
190    results = wpr_recorder.CreateResults()
191    wpr_recorder.Record(results)
192    self.assertEqual(set(mock_benchmark.mock_story_set.stories),
193                     results.pages_that_succeeded)
194
195  def testCommandLineFlags(self):
196    flags = [
197        '--page-repeat', '2',
198        '--mock-benchmark-url', self._url,
199        '--upload',
200    ]
201    wpr_recorder = record_wpr.WprRecorder(self._test_data_dir, MockBenchmark(),
202                                          flags)
203    # page_runner command-line args
204    self.assertEquals(2, wpr_recorder.options.page_repeat)
205    # benchmark command-line args
206    self.assertEquals(self._url, wpr_recorder.options.mock_benchmark_url)
207    # record_wpr command-line arg to upload to cloud-storage.
208    self.assertTrue(wpr_recorder.options.upload)
209    # invalid command-line args
210    self.assertFalse(hasattr(wpr_recorder.options, 'not_a_real_option'))
211
212  def testRecordingEnabled(self):
213    flags = ['--mock-benchmark-url', self._url]
214    wpr_recorder = record_wpr.WprRecorder(self._test_data_dir, MockBenchmark(),
215                                          flags)
216    self.assertEqual(wpr_modes.WPR_RECORD,
217                     wpr_recorder.options.browser_options.wpr_mode)
218
219  # When the RecorderPageTest CustomizeBrowserOptions/WillStartBrowser/
220  # DidStartBrowser function is called, it forwards the call to the PageTest
221  def testRecorderPageTest_BrowserMethods(self):
222    flags = ['--mock-benchmark-url', self._url]
223    record_page_test = record_wpr.RecorderPageTest()
224    record_page_test.page_test = MockBenchmark().test()
225    wpr_recorder = record_wpr.WprRecorder(self._test_data_dir, MockBenchmark(),
226                                          flags)
227    record_page_test.CustomizeBrowserOptions(wpr_recorder.options)
228    record_page_test.WillStartBrowser(self._tab.browser.platform)
229    record_page_test.DidStartBrowser(self._tab.browser)
230    self.assertTrue(
231        'CustomizeBrowserOptions' in record_page_test.page_test.func_calls)
232    self.assertTrue('WillStartBrowser' in record_page_test.page_test.func_calls)
233    self.assertTrue('DidStartBrowser' in record_page_test.page_test.func_calls)
234
235  def testUseLiveSitesUnsupported(self):
236    flags = ['--use-live-sites']
237    with self.assertRaises(SystemExit):
238      record_wpr.WprRecorder(self._test_data_dir, MockBenchmark(), flags)
239