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.
4
5from telemetry.page import page
6from telemetry.page import page_test
7from telemetry.value import scalar
8
9
10class PageForPolymerLoad(page.Page):
11
12  def __init__(self, url, page_set, ready_event='polymer-ready'):
13    super(PageForPolymerLoad, self).__init__(
14      url=url,
15      page_set=page_set)
16    self.script_to_evaluate_on_commit = '''
17      document.addEventListener("%s", function() {
18        var unused = document.body.offsetHeight;
19        window.__polymer_ready_time = performance.now();
20        setTimeout(function() {
21          window.__polymer_ready = true;
22        }, 1000);
23      })
24    ''' % ready_event
25
26  def RunNavigateSteps(self, action_runner):
27    action_runner.NavigateToPage(self)
28    action_runner.WaitForJavaScriptCondition('window.__polymer_ready')
29
30
31class PolymerLoadMeasurement(page_test.PageTest):
32  def ValidateAndMeasurePage(self, _, tab, results):
33    result = int(tab.EvaluateJavaScript('__polymer_ready_time'))
34    results.AddValue(scalar.ScalarValue(
35        results.current_page, 'Total', 'ms', result))
36