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 tracing_config
9from tracing.trace_data import trace_data
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  # https://github.com/catapult-project/catapult/issues/3099 (Android)
30  # crbug.com/483212 (CrOS)
31  @decorators.Enabled('has tabs')
32  @decorators.Disabled('android', 'chromeos')
33  def testGetUpdatedInspectableContexts(self):
34    self._browser.tabs.New()
35    c1 = self._devtools_client.GetUpdatedInspectableContexts()
36    self.assertEqual(len(c1.contexts), 2)
37    backends1 = [c1.GetInspectorBackend(c['id']) for c in c1.contexts]
38    tabs1 = list(self._browser.tabs)
39
40    c2 = self._devtools_client.GetUpdatedInspectableContexts()
41    self.assertEqual(len(c2.contexts), 2)
42    backends2 = [c2.GetInspectorBackend(c['id']) for c in c2.contexts]
43    tabs2 = list(self._browser.tabs)
44    self.assertEqual(backends2, backends1)
45    self.assertEqual(tabs2, tabs1)
46
47    self._browser.tabs.New()
48    c3 = self._devtools_client.GetUpdatedInspectableContexts()
49    self.assertEqual(len(c3.contexts), 3)
50    backends3 = [c3.GetInspectorBackend(c['id']) for c in c3.contexts]
51    tabs3 = list(self._browser.tabs)
52    self.assertEqual(backends3[1], backends1[0])
53    self.assertEqual(backends3[2], backends1[1])
54    self.assertEqual(tabs3[0], tabs1[0])
55    self.assertEqual(tabs3[1], tabs1[1])
56
57    self._browser.tabs[1].Close()
58    c4 = self._devtools_client.GetUpdatedInspectableContexts()
59    self.assertEqual(len(c4.contexts), 2)
60    backends4 = [c4.GetInspectorBackend(c['id']) for c in c4.contexts]
61    tabs4 = list(self._browser.tabs)
62    self.assertEqual(backends4[0], backends3[0])
63    self.assertEqual(backends4[1], backends3[1])
64    self.assertEqual(tabs4[0], tabs3[0])
65    self.assertEqual(tabs4[1], tabs3[2])
66
67  # https://github.com/catapult-project/catapult/issues/3099 (Android)
68  # crbug.com/483212 (CrOS)
69  @decorators.Disabled('android', 'chromeos')
70  def testGetUpdatedInspectableContextsUpdateContextsData(self):
71    c1 = self._devtools_client.GetUpdatedInspectableContexts()
72    self.assertEqual(len(c1.contexts), 1)
73    self.assertEqual(c1.contexts[0]['url'], 'about:blank')
74
75    context_id = c1.contexts[0]['id']
76    backend = c1.GetInspectorBackend(context_id)
77    backend.Navigate(self.UrlOfUnittestFile('blank.html'), None, 10)
78    c2 = self._devtools_client.GetUpdatedInspectableContexts()
79    self.assertEqual(len(c2.contexts), 1)
80    self.assertTrue('blank.html' in c2.contexts[0]['url'])
81    self.assertEqual(c2.GetInspectorBackend(context_id), backend)
82
83  def testTracing(self):
84    devtools_client = self._devtools_client
85    if not devtools_client.IsChromeTracingSupported():
86      self.skipTest('Browser does not support tracing, skipping test.')
87
88    # Start Chrome tracing.
89    config = tracing_config.TracingConfig()
90    config.enable_chrome_trace = True
91    devtools_client.StartChromeTracing(config)
92
93    # Stop Chrome tracing and check that the resulting data is valid.
94    builder = trace_data.TraceDataBuilder()
95    devtools_client.StopChromeTracing()
96    devtools_client.CollectChromeTracingData(builder)
97    model.TimelineModel(builder.AsData())
98