Verify the page has begun loading before doing a DRP check.
[chromium-blink-merge.git] / tools / chrome_proxy / common / chrome_proxy_measurements.py
blob7cdef14d65300030fab093a7a4c394487293728a
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.
5 import base64
6 import logging
8 from common import chrome_proxy_metrics as metrics
9 from telemetry.core import exceptions
10 from telemetry.page import page_test
13 def WaitForViaHeader(tab, url="http://check.googlezip.net/test.html"):
14 """Wait until responses start coming back with the Chrome Proxy via header.
16 Poll |url| in |tab| until the Chrome Proxy via header is present in a
17 response.
19 This function is useful when testing with the Data Saver API, since Chrome
20 won't actually start sending requests to the Data Reduction Proxy until the
21 Data Saver API fetch completes. This function can be used to wait for the Data
22 Saver API fetch to complete.
23 """
25 tab.Navigate('data:text/html;base64,%s' % base64.b64encode(
26 '<html><body><script>'
27 'function ProbeViaHeader(url, wanted_via) {'
28 'var xmlhttp = new XMLHttpRequest();'
29 'xmlhttp.open("HEAD",url,false);'
30 'xmlhttp.send();'
31 'var via=xmlhttp.getResponseHeader("via");'
32 'return (via && via.indexOf(wanted_via) != -1);'
33 '}'
34 '</script>'
35 'Waiting for Chrome to start using the DRP...'
36 '</body></html>'))
38 # Ensure the page has started loading before attempting the DRP check.
39 tab.WaitForJavaScriptExpression('performance.timing.loadEventStart', 300)
40 tab.WaitForJavaScriptExpression(
41 'ProbeViaHeader("%s", "%s")' % (url, metrics.CHROME_PROXY_VIA_HEADER), 300)
44 class ChromeProxyValidation(page_test.PageTest):
45 """Base class for all chrome proxy correctness measurements."""
47 # Value of the extra via header. |None| if no extra via header is expected.
48 extra_via_header = None
50 def __init__(self, restart_after_each_page=False, metrics=None):
51 super(ChromeProxyValidation, self).__init__(
52 needs_browser_restart_after_each_page=restart_after_each_page)
53 self._metrics = metrics
54 self._page = None
56 def CustomizeBrowserOptions(self, options):
57 # Enable the chrome proxy (data reduction proxy).
58 options.AppendExtraBrowserArgs('--enable-spdy-proxy-auth')
60 def WillNavigateToPage(self, page, tab):
61 WaitForViaHeader(tab)
63 tab.ClearCache(force=True)
64 assert self._metrics
65 self._metrics.Start(page, tab)
67 def ValidateAndMeasurePage(self, page, tab, results):
68 self._page = page
69 # Wait for the load event.
70 tab.WaitForJavaScriptExpression('performance.timing.loadEventStart', 300)
71 assert self._metrics
72 self._metrics.Stop(page, tab)
73 if ChromeProxyValidation.extra_via_header:
74 self._metrics.AddResultsForExtraViaHeader(
75 tab, results, ChromeProxyValidation.extra_via_header)
76 self.AddResults(tab, results)
78 def AddResults(self, tab, results):
79 raise NotImplementedError
81 def StopBrowserAfterPage(self, browser, page): # pylint: disable=W0613
82 if hasattr(page, 'restart_after') and page.restart_after:
83 return True
84 return False