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.
8 from telemetry import test
9 from telemetry.core import wpr_modes
10 from telemetry.page import page as page_module
11 from telemetry.page import page_measurement
12 from telemetry.page import page_measurement_unittest_base
13 from telemetry.page import page_set
14 from telemetry.page import page_set_archive_info
15 from telemetry.page.actions import all_page_actions
16 from telemetry.page.actions import page_action
17 from telemetry.unittest import options_for_unittests
20 class MeasurementThatFails(page_measurement.PageMeasurement):
21 def MeasurePage(self, page, tab, results):
22 raise page_measurement.MeasurementFailure('Intentional failure.')
24 class MeasurementThatHasDefaults(page_measurement.PageMeasurement):
25 def AddCommandLineOptions(self, parser):
26 parser.add_option('-x', dest='x', default=3)
28 def MeasurePage(self, page, tab, results):
29 if not hasattr(self.options, 'x'):
30 raise page_measurement.MeasurementFailure('Default option was not set.')
31 if self.options.x != 3:
32 raise page_measurement.MeasurementFailure(
33 'Expected x == 3, got x == ' + self.options.x)
34 results.Add('x', 'ms', 7)
36 class MeasurementForBlank(page_measurement.PageMeasurement):
37 def MeasurePage(self, page, tab, results):
38 contents = tab.EvaluateJavaScript('document.body.textContent')
39 if contents.strip() != 'Hello world':
40 raise page_measurement.MeasurementFailure(
41 'Page contents were: ' + contents)
43 class MeasurementForReplay(page_measurement.PageMeasurement):
44 def MeasurePage(self, page, tab, results):
45 # Web Page Replay returns '404 Not found' if a page is not in the archive.
46 contents = tab.EvaluateJavaScript('document.body.textContent')
47 if '404 Not Found' in contents.strip():
48 raise page_measurement.MeasurementFailure('Page not in archive.')
50 class MeasurementQueryParams(page_measurement.PageMeasurement):
51 def MeasurePage(self, page, tab, results):
52 query = tab.EvaluateJavaScript('window.location.search')
54 if query.strip() != expected:
55 raise page_measurement.MeasurementFailure(
56 'query was %s, not %s.' % (query, expected))
58 class MeasurementWithAction(page_measurement.PageMeasurement):
60 super(MeasurementWithAction, self).__init__('test_action')
62 def MeasurePage(self, page, tab, results):
65 class PageMeasurementUnitTest(
66 page_measurement_unittest_base.PageMeasurementUnitTestBase):
69 self._options = options_for_unittests.GetCopy()
70 self._options.browser_options.wpr_mode = wpr_modes.WPR_OFF
72 def testGotToBlank(self):
73 ps = self.CreatePageSetFromFileInUnittestDataDir('blank.html')
74 measurement = MeasurementForBlank()
75 all_results = self.RunMeasurement(measurement, ps, options=self._options)
76 self.assertEquals(0, len(all_results.failures))
78 def testGotQueryParams(self):
79 ps = self.CreatePageSet('file://blank.html?foo=1')
80 measurement = MeasurementQueryParams()
81 all_results = self.RunMeasurement(measurement, ps, options=self._options)
82 self.assertEquals(0, len(all_results.failures))
84 def testFailure(self):
85 ps = self.CreatePageSetFromFileInUnittestDataDir('blank.html')
86 measurement = MeasurementThatFails()
87 all_results = self.RunMeasurement(measurement, ps, options=self._options)
88 self.assertEquals(1, len(all_results.failures))
90 def testDefaults(self):
91 ps = self.CreatePageSetFromFileInUnittestDataDir('blank.html')
92 measurement = MeasurementThatHasDefaults()
93 all_results = self.RunMeasurement(measurement, ps, options=self._options)
94 self.assertEquals(len(all_results.all_page_specific_values), 1)
96 all_results.all_page_specific_values[0].value, 7)
98 # This test is disabled because it runs against live sites, and needs to be
99 # fixed. crbug.com/179038
101 def testRecordAndReplay(self):
102 test_archive = '/tmp/google.wpr'
103 google_url = 'http://www.google.com/'
104 foo_url = 'http://www.foo.com/'
105 archive_info_template = ("""
113 ps = page_set.PageSet()
114 measurement = MeasurementForReplay()
116 # First record an archive with only www.google.com.
117 self._options.browser_options.wpr_mode = wpr_modes.WPR_RECORD
119 ps.wpr_archive_info = page_set_archive_info.PageSetArchiveInfo(
120 '', '', json.loads(archive_info_template %
121 (test_archive, google_url)))
122 ps.pages = [page_module.Page(google_url, ps)]
123 all_results = self.RunMeasurement(measurement, ps, options=self._options)
124 self.assertEquals(0, len(all_results.failures))
126 # Now replay it and verify that google.com is found but foo.com is not.
127 self._options.browser_options.wpr_mode = wpr_modes.WPR_REPLAY
129 ps.wpr_archive_info = page_set_archive_info.PageSetArchiveInfo(
130 '', '', json.loads(archive_info_template % (test_archive, foo_url)))
131 ps.pages = [page_module.Page(foo_url, ps)]
132 all_results = self.RunMeasurement(measurement, ps, options=self._options)
133 self.assertEquals(1, len(all_results.failures))
135 ps.wpr_archive_info = page_set_archive_info.PageSetArchiveInfo(
136 '', '', json.loads(archive_info_template %
137 (test_archive, google_url)))
138 ps.pages = [page_module.Page(google_url, ps)]
139 all_results = self.RunMeasurement(measurement, ps, options=self._options)
140 self.assertEquals(0, len(all_results.failures))
142 self.assertTrue(os.path.isfile(test_archive))
145 if os.path.isfile(test_archive):
146 os.remove(test_archive)
148 def testActions(self):
149 action_called = [False]
150 class MockAction(page_action.PageAction):
151 def RunAction(self, page, tab, previous_action):
152 action_called[0] = True
153 all_page_actions.RegisterClassForTest('mock', MockAction)
155 ps = self.CreatePageSetFromFileInUnittestDataDir('blank.html')
156 setattr(ps.pages[0], 'test_action', {'action': 'mock'})
157 measurement = MeasurementWithAction()
158 self.RunMeasurement(measurement, ps, options=self._options)
159 self.assertTrue(action_called[0])