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 import decorators
6from telemetry.testing import browser_test_case
7from telemetry.timeline import model
8from telemetry.timeline import trace_data
9from telemetry.timeline import tracing_config
10
11
12class DevToolsClientBackendTest(browser_test_case.BrowserTestCase):
13  @property
14  def _browser_backend(self):
15    return self._browser._browser_backend
16
17  @property
18  def _devtools_client(self):
19    return self._browser_backend.devtools_client
20
21  def testGetChromeBranchNumber(self):
22    branch_num = self._devtools_client.GetChromeBranchNumber()
23    self.assertIsInstance(branch_num, int)
24    self.assertGreater(branch_num, 0)
25
26  def testIsAlive(self):
27    self.assertTrue(self._devtools_client.IsAlive())
28
29  @decorators.Enabled('has tabs')
30  @decorators.Disabled('chromeos')  # crbug.com/483212
31  def testGetUpdatedInspectableContexts(self):
32    self._browser.tabs.New()
33    c1 = self._devtools_client.GetUpdatedInspectableContexts()
34    self.assertEqual(len(c1.contexts), 2)
35    backends1 = [c1.GetInspectorBackend(c['id']) for c in c1.contexts]
36    tabs1 = list(self._browser.tabs)
37
38    c2 = self._devtools_client.GetUpdatedInspectableContexts()
39    self.assertEqual(len(c2.contexts), 2)
40    backends2 = [c2.GetInspectorBackend(c['id']) for c in c2.contexts]
41    tabs2 = list(self._browser.tabs)
42    self.assertEqual(backends2, backends1)
43    self.assertEqual(tabs2, tabs1)
44
45    self._browser.tabs.New()
46    c3 = self._devtools_client.GetUpdatedInspectableContexts()
47    self.assertEqual(len(c3.contexts), 3)
48    backends3 = [c3.GetInspectorBackend(c['id']) for c in c3.contexts]
49    tabs3 = list(self._browser.tabs)
50    self.assertEqual(backends3[1], backends1[0])
51    self.assertEqual(backends3[2], backends1[1])
52    self.assertEqual(tabs3[0], tabs1[0])
53    self.assertEqual(tabs3[1], tabs1[1])
54
55    self._browser.tabs[1].Close()
56    c4 = self._devtools_client.GetUpdatedInspectableContexts()
57    self.assertEqual(len(c4.contexts), 2)
58    backends4 = [c4.GetInspectorBackend(c['id']) for c in c4.contexts]
59    tabs4 = list(self._browser.tabs)
60    self.assertEqual(backends4[0], backends3[0])
61    self.assertEqual(backends4[1], backends3[1])
62    self.assertEqual(tabs4[0], tabs3[0])
63    self.assertEqual(tabs4[1], tabs3[2])
64
65  @decorators.Disabled('chromeos')  # crbug.com/483212
66  def testGetUpdatedInspectableContextsUpdateContextsData(self):
67    c1 = self._devtools_client.GetUpdatedInspectableContexts()
68    self.assertEqual(len(c1.contexts), 1)
69    self.assertEqual(c1.contexts[0]['url'], 'about:blank')
70
71    context_id = c1.contexts[0]['id']
72    backend = c1.GetInspectorBackend(context_id)
73    backend.Navigate(self.UrlOfUnittestFile('blank.html'), None, 10)
74    c2 = self._devtools_client.GetUpdatedInspectableContexts()
75    self.assertEqual(len(c2.contexts), 1)
76    self.assertTrue('blank.html' in c2.contexts[0]['url'])
77    self.assertEqual(c2.GetInspectorBackend(context_id), backend)
78
79  def testTracing(self):
80    devtools_client = self._devtools_client
81    if not devtools_client.IsChromeTracingSupported():
82      self.skipTest('Browser does not support tracing, skipping test.')
83
84    # Start Chrome tracing.
85    config = tracing_config.TracingConfig()
86    config.enable_chrome_trace = True
87    devtools_client.StartChromeTracing(config)
88
89    # Stop Chrome tracing and check that the resulting data is valid.
90    builder = trace_data.TraceDataBuilder()
91    devtools_client.StopChromeTracing(builder)
92    model.TimelineModel(builder.AsData())
93