OLD | NEW |
(Empty) | |
| 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 unittest |
| 6 |
| 7 import pyauto_tracing |
| 8 import pyauto |
| 9 import tab_tracker |
| 10 from timeline_model import TimelineModel |
| 11 |
| 12 import os |
| 13 |
| 14 |
| 15 class Tracer: |
| 16 """Controlls chrome and system tracing, returning a TimelineModel.""" |
| 17 |
| 18 def __init__(self, browser, tab_tracker): |
| 19 """ |
| 20 Args: |
| 21 browser: an instance of a PyUITest |
| 22 tab_tracker: an instance of a TabTracker |
| 23 """ |
| 24 self._browser = browser |
| 25 self._tab_tracker = tab_tracker |
| 26 self._tab_uuid = tab_tracker.CreateTab('chrome://tracing') |
| 27 |
| 28 # TODO(eatnumber): Find a way to import timeline_model_shim.js from within |
| 29 # TimelineModelProxy |
| 30 # I'm not happy with importing timeline_model_shim.js |
| 31 # here. I'd rather pull it in from within TimelineModelProxy. |
| 32 # tracing_test.js depends on timeline_model_shim.js however. |
| 33 files = ['timeline_model_shim.js', 'tracer.js'] |
| 34 for fileName in files: |
| 35 file_js = None |
| 36 with open(os.path.join(os.path.dirname(__file__), fileName), 'r') as fd: |
| 37 file_js = fd.read() |
| 38 self._ExecuteJavascript(file_js); |
| 39 |
| 40 def __del__(self): |
| 41 self._tab_tracker.ReleaseTab(self._tab_uuid) |
| 42 |
| 43 def _ExecuteJavascript(self, js): |
| 44 return self._browser.ExecuteJavascript( |
| 45 js = js, |
| 46 windex = self._tab_tracker.GetWindowIndex(), |
| 47 tab_index = self._tab_tracker.GetTabIndex(self._tab_uuid) |
| 48 ) |
| 49 |
| 50 def BeginTracing(self, system_tracing=True): |
| 51 """Start tracing. |
| 52 |
| 53 Args: |
| 54 system_tracing: whether or not to gather system traces along with chrome |
| 55 traces. |
| 56 """ |
| 57 self._ExecuteJavascript( |
| 58 """ |
| 59 window.domAutomationController.send( |
| 60 window.pyautoRecordTrace(%s) |
| 61 ); |
| 62 """ % ( |
| 63 'true' if system_tracing else 'false' |
| 64 ) |
| 65 ) |
| 66 |
| 67 def EndTracing(self): |
| 68 """End tracing and return a TimelineModel. |
| 69 |
| 70 Returns: |
| 71 an instance of a TimelineModel which contains the results of the trace |
| 72 """ |
| 73 return TimelineModel( |
| 74 js_executor = self._ExecuteJavascript.__get__(self, Tracer), |
| 75 shim_id = self._ExecuteJavascript( |
| 76 """tracingController.endTracing();""" |
| 77 ) |
| 78 ) |
| 79 |
| 80 |
| 81 class TracerFactory: |
| 82 """A TracerFactory is used to produce a Tracer. |
| 83 |
| 84 It's recommended to use the same TracerFactory to produce all Tracers so that |
| 85 the same TabTracker can be used throughout |
| 86 |
| 87 Args: |
| 88 browser: an instance of a PyUITest |
| 89 """ |
| 90 def __init__(self, browser): |
| 91 self._tab_tracker = tab_tracker.TabTracker(browser) |
| 92 self._browser = browser |
| 93 |
| 94 def Produce(self): |
| 95 """Produce an instance of a Tracer""" |
| 96 return Tracer(self._browser, self._tab_tracker) |
OLD | NEW |