e1ae62bd2efb5409a053c3ccaf91e786f162b91c
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / page / page_measurement_unittest.py
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
5 import json
6 import os
7
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
18
19
20 class MeasurementThatFails(page_measurement.PageMeasurement):
21   def MeasurePage(self, page, tab, results):
22     raise page_measurement.MeasurementFailure('Intentional failure.')
23
24 class MeasurementThatHasDefaults(page_measurement.PageMeasurement):
25   def AddCommandLineOptions(self, parser):
26     parser.add_option('-x', dest='x', default=3)
27
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)
35
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)
42
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.')
49
50 class MeasurementQueryParams(page_measurement.PageMeasurement):
51   def MeasurePage(self, page, tab, results):
52     query = tab.EvaluateJavaScript('window.location.search')
53     expected = '?foo=1'
54     if query.strip() != expected:
55       raise page_measurement.MeasurementFailure(
56           'query was %s, not %s.' % (query, expected))
57
58 class MeasurementWithAction(page_measurement.PageMeasurement):
59   def __init__(self):
60     super(MeasurementWithAction, self).__init__('test_action')
61
62   def MeasurePage(self, page, tab, results):
63     pass
64
65 class PageMeasurementUnitTest(
66   page_measurement_unittest_base.PageMeasurementUnitTestBase):
67
68   def setUp(self):
69     self._options = options_for_unittests.GetCopy()
70     self._options.browser_options.wpr_mode = wpr_modes.WPR_OFF
71
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))
77
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))
83
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))
89
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)
95     self.assertEquals(
96       all_results.all_page_specific_values[0].value, 7)
97
98   # This test is disabled because it runs against live sites, and needs to be
99   # fixed. crbug.com/179038
100   @test.Disabled
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 = ("""
106 {
107 "archives": {
108   "%s": ["%s"]
109 }
110 }
111 """)
112     try:
113       ps = page_set.PageSet()
114       measurement = MeasurementForReplay()
115
116       # First record an archive with only www.google.com.
117       self._options.browser_options.wpr_mode = wpr_modes.WPR_RECORD
118
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))
125
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
128
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))
134
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))
141
142       self.assertTrue(os.path.isfile(test_archive))
143
144     finally:
145       if os.path.isfile(test_archive):
146         os.remove(test_archive)
147
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)
154
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])