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