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