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