| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2012 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 import json | |
| 5 import os | |
| 6 | |
| 7 from telemetry.core import wpr_modes | |
| 8 from telemetry.page import multi_page_benchmark | |
| 9 from telemetry.page import multi_page_benchmark_unittest_base | |
| 10 from telemetry.page import page as page_module | |
| 11 from telemetry.page import page_action | |
| 12 from telemetry.page import page_set | |
| 13 from telemetry.page import page_set_archive_info | |
| 14 from telemetry.test import options_for_unittests | |
| 15 | |
| 16 class BenchThatFails(multi_page_benchmark.MultiPageBenchmark): | |
| 17 def MeasurePage(self, page, tab, results): | |
| 18 raise multi_page_benchmark.MeasurementFailure('Intentional failure.') | |
| 19 | |
| 20 class BenchThatHasDefaults(multi_page_benchmark.MultiPageBenchmark): | |
| 21 def AddCommandLineOptions(self, parser): | |
| 22 parser.add_option('-x', dest='x', default=3) | |
| 23 | |
| 24 def MeasurePage(self, page, tab, results): | |
| 25 assert self.options.x == 3 | |
| 26 results.Add('x', 'ms', 7) | |
| 27 | |
| 28 class BenchForBlank(multi_page_benchmark.MultiPageBenchmark): | |
| 29 def MeasurePage(self, page, tab, results): | |
| 30 contents = tab.EvaluateJavaScript('document.body.textContent') | |
| 31 assert contents.strip() == 'Hello world' | |
| 32 | |
| 33 class BenchForReplay(multi_page_benchmark.MultiPageBenchmark): | |
| 34 def MeasurePage(self, page, tab, results): | |
| 35 # Web Page Replay returns '404 Not found' if a page is not in the archive. | |
| 36 contents = tab.EvaluateJavaScript('document.body.textContent') | |
| 37 if '404 Not Found' in contents.strip(): | |
| 38 raise multi_page_benchmark.MeasurementFailure('Page not in archive.') | |
| 39 | |
| 40 class BenchQueryParams(multi_page_benchmark.MultiPageBenchmark): | |
| 41 def MeasurePage(self, page, tab, results): | |
| 42 query = tab.EvaluateJavaScript('window.location.search') | |
| 43 assert query.strip() == '?foo=1' | |
| 44 | |
| 45 class BenchWithAction(multi_page_benchmark.MultiPageBenchmark): | |
| 46 def __init__(self): | |
| 47 super(BenchWithAction, self).__init__('test_action') | |
| 48 | |
| 49 def MeasurePage(self, page, tab, results): | |
| 50 pass | |
| 51 | |
| 52 class MultiPageBenchmarkUnitTest( | |
| 53 multi_page_benchmark_unittest_base.MultiPageBenchmarkUnitTestBase): | |
| 54 | |
| 55 def setUp(self): | |
| 56 self._options = options_for_unittests.GetCopy() | |
| 57 self._options.wpr_mode = wpr_modes.WPR_OFF | |
| 58 | |
| 59 def testGotToBlank(self): | |
| 60 ps = self.CreatePageSetFromFileInUnittestDataDir('blank.html') | |
| 61 benchmark = BenchForBlank() | |
| 62 all_results = self.RunBenchmark(benchmark, ps, options=self._options) | |
| 63 self.assertEquals(0, len(all_results.page_failures)) | |
| 64 | |
| 65 def testGotQueryParams(self): | |
| 66 ps = self.CreatePageSet('file:///../../unittest_data/blank.html?foo=1') | |
| 67 benchmark = BenchQueryParams() | |
| 68 ps.pages[-1].query_params = '?foo=1' | |
| 69 all_results = self.RunBenchmark(benchmark, ps, options=self._options) | |
| 70 self.assertEquals(0, len(all_results.page_failures)) | |
| 71 | |
| 72 def testFailure(self): | |
| 73 ps = self.CreatePageSetFromFileInUnittestDataDir('blank.html') | |
| 74 benchmark = BenchThatFails() | |
| 75 all_results = self.RunBenchmark(benchmark, ps, options=self._options) | |
| 76 self.assertEquals(1, len(all_results.page_failures)) | |
| 77 | |
| 78 def testDefaults(self): | |
| 79 ps = self.CreatePageSetFromFileInUnittestDataDir('blank.html') | |
| 80 benchmark = BenchThatHasDefaults() | |
| 81 all_results = self.RunBenchmark(benchmark, ps, options=self._options) | |
| 82 self.assertEquals(len(all_results.page_results), 1) | |
| 83 self.assertEquals( | |
| 84 all_results.page_results[0].FindValueByTraceName('x').value, 7) | |
| 85 | |
| 86 def testRecordAndReplay(self): | |
| 87 test_archive = '/tmp/google.wpr' | |
| 88 google_url = 'http://www.google.com/' | |
| 89 foo_url = 'http://www.foo.com/' | |
| 90 archive_info_template = (""" | |
| 91 { | |
| 92 "archives": { | |
| 93 "%s": ["%s"] | |
| 94 } | |
| 95 } | |
| 96 """) | |
| 97 try: | |
| 98 ps = page_set.PageSet() | |
| 99 benchmark = BenchForReplay() | |
| 100 | |
| 101 # First record an archive with only www.google.com. | |
| 102 self._options.wpr_mode = wpr_modes.WPR_RECORD | |
| 103 | |
| 104 ps.wpr_archive_info = page_set_archive_info.PageSetArchiveInfo( | |
| 105 '', '', json.loads(archive_info_template % | |
| 106 (test_archive, google_url))) | |
| 107 ps.pages = [page_module.Page(google_url, ps)] | |
| 108 all_results = self.RunBenchmark(benchmark, ps, options=self._options) | |
| 109 self.assertEquals(0, len(all_results.page_failures)) | |
| 110 | |
| 111 # Now replay it and verify that google.com is found but foo.com is not. | |
| 112 self._options.wpr_mode = wpr_modes.WPR_REPLAY | |
| 113 | |
| 114 ps.wpr_archive_info = page_set_archive_info.PageSetArchiveInfo( | |
| 115 '', '', json.loads(archive_info_template % (test_archive, foo_url))) | |
| 116 ps.pages = [page_module.Page(foo_url, ps)] | |
| 117 all_results = self.RunBenchmark(benchmark, ps, options=self._options) | |
| 118 self.assertEquals(1, len(all_results.page_failures)) | |
| 119 | |
| 120 ps.wpr_archive_info = page_set_archive_info.PageSetArchiveInfo( | |
| 121 '', '', json.loads(archive_info_template % | |
| 122 (test_archive, google_url))) | |
| 123 ps.pages = [page_module.Page(google_url, ps)] | |
| 124 all_results = self.RunBenchmark(benchmark, ps, options=self._options) | |
| 125 self.assertEquals(0, len(all_results.page_failures)) | |
| 126 | |
| 127 self.assertTrue(os.path.isfile(test_archive)) | |
| 128 | |
| 129 finally: | |
| 130 if os.path.isfile(test_archive): | |
| 131 os.remove(test_archive) | |
| 132 | |
| 133 def testActions(self): | |
| 134 action_called = [False] | |
| 135 class MockAction(page_action.PageAction): | |
| 136 def RunAction(self, page, tab, previous_action): | |
| 137 action_called[0] = True | |
| 138 from telemetry.page import all_page_actions | |
| 139 all_page_actions.RegisterClassForTest('mock', MockAction) | |
| 140 | |
| 141 ps = self.CreatePageSetFromFileInUnittestDataDir('blank.html') | |
| 142 setattr(ps.pages[0], 'test_action', {'action': 'mock'}) | |
| 143 benchmark = BenchWithAction() | |
| 144 self.RunBenchmark(benchmark, ps, options=self._options) | |
| 145 self.assertTrue(action_called[0]) | |
| OLD | NEW |