1# Copyright 2013 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.
4from metrics import timeline
5from metrics import loading
6from telemetry.core import util
7from telemetry.page import page_measurement
8
9class LoadingTimeline(page_measurement.PageMeasurement):
10  def __init__(self, *args, **kwargs):
11    super(LoadingTimeline, self).__init__(*args, **kwargs)
12    self._metrics = None
13
14  @property
15  def results_are_the_same_on_every_page(self):
16    return False
17
18  def WillNavigateToPage(self, page, tab):
19    self._metrics = timeline.TimelineMetrics(timeline.TIMELINE_MODE)
20    self._metrics.Start(tab)
21
22  def MeasurePage(self, page, tab, results):
23    # In current telemetry tests, all tests wait for DocumentComplete state,
24    # but we need to wait for the load event.
25    def IsLoaded():
26      return bool(tab.EvaluateJavaScript('performance.timing.loadEventStart'))
27    util.WaitFor(IsLoaded, 300)
28
29    # TODO(nduca): when crbug.com/168431 is fixed, modify the page sets to
30    # recognize loading as a toplevel action.
31    self._metrics.Stop(tab)
32
33    loading.LoadingMetric().AddResults(tab, results)
34    self._metrics.AddResults(results)
35