1# Copyright 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
5import logging
6
7from telemetry import benchmark
8from telemetry.core import gpu_device
9from telemetry.core import gpu_info
10from telemetry.core import system_info
11from telemetry.core.platform import tracing_category_filter
12from telemetry.core.platform import tracing_options
13from telemetry.unittest import browser_test_case
14
15
16class BrowserTest(browser_test_case.BrowserTestCase):
17  def testBrowserCreation(self):
18    self.assertEquals(1, len(self._browser.tabs))
19
20    # Different browsers boot up to different things.
21    assert self._browser.tabs[0].url
22
23  def testVersionDetection(self):
24    # pylint: disable=W0212
25    v = self._browser._browser_backend.chrome_branch_number
26    self.assertTrue(v > 0)
27
28  @benchmark.Enabled('has tabs')
29  def testNewCloseTab(self):
30    existing_tab = self._browser.tabs[0]
31    self.assertEquals(1, len(self._browser.tabs))
32    existing_tab_url = existing_tab.url
33
34    new_tab = self._browser.tabs.New()
35    self.assertEquals(2, len(self._browser.tabs))
36    self.assertEquals(existing_tab.url, existing_tab_url)
37    self.assertEquals(new_tab.url, 'about:blank')
38
39    new_tab.Close()
40    self.assertEquals(1, len(self._browser.tabs))
41    self.assertEquals(existing_tab.url, existing_tab_url)
42
43  def testMultipleTabCalls(self):
44    self._browser.tabs[0].Navigate(self.UrlOfUnittestFile('blank.html'))
45    self._browser.tabs[0].WaitForDocumentReadyStateToBeInteractiveOrBetter()
46
47  def testTabCallByReference(self):
48    tab = self._browser.tabs[0]
49    tab.Navigate(self.UrlOfUnittestFile('blank.html'))
50    self._browser.tabs[0].WaitForDocumentReadyStateToBeInteractiveOrBetter()
51
52  @benchmark.Enabled('has tabs')
53  @benchmark.Disabled('win')  # crbug.com/321527
54  def testCloseReferencedTab(self):
55    self._browser.tabs.New()
56    tab = self._browser.tabs[0]
57    tab.Navigate(self.UrlOfUnittestFile('blank.html'))
58    tab.Close()
59    self.assertEquals(1, len(self._browser.tabs))
60
61  @benchmark.Enabled('has tabs')
62  def testForegroundTab(self):
63    # Should be only one tab at this stage, so that must be the foreground tab
64    original_tab = self._browser.tabs[0]
65    self.assertEqual(self._browser.foreground_tab, original_tab)
66    new_tab = self._browser.tabs.New()
67    # New tab shouls be foreground tab
68    self.assertEqual(self._browser.foreground_tab, new_tab)
69    # Make sure that activating the background tab makes it the foreground tab
70    original_tab.Activate()
71    self.assertEqual(self._browser.foreground_tab, original_tab)
72    # Closing the current foreground tab should switch the foreground tab to the
73    # other tab
74    original_tab.Close()
75    self.assertEqual(self._browser.foreground_tab, new_tab)
76
77  def testGetSystemInfo(self):
78    if not self._browser.supports_system_info:
79      logging.warning(
80          'Browser does not support getting system info, skipping test.')
81      return
82
83    info = self._browser.GetSystemInfo()
84
85    self.assertTrue(isinstance(info, system_info.SystemInfo))
86    self.assertTrue(hasattr(info, 'model_name'))
87    self.assertTrue(hasattr(info, 'gpu'))
88    self.assertTrue(isinstance(info.gpu, gpu_info.GPUInfo))
89    self.assertTrue(hasattr(info.gpu, 'devices'))
90    self.assertTrue(len(info.gpu.devices) > 0)
91    for g in info.gpu.devices:
92      self.assertTrue(isinstance(g, gpu_device.GPUDevice))
93
94  def testGetSystemInfoNotCachedObject(self):
95    if not self._browser.supports_system_info:
96      logging.warning(
97          'Browser does not support getting system info, skipping test.')
98      return
99
100    info_a = self._browser.GetSystemInfo()
101    info_b = self._browser.GetSystemInfo()
102    self.assertFalse(info_a is info_b)
103
104  def testGetSystemTotalMemory(self):
105    self.assertTrue(self._browser.memory_stats['SystemTotalPhysicalMemory'] > 0)
106
107  @benchmark.Disabled('chromeos') # crbug.com/412713.
108  def testIsTracingRunning(self):
109    tracing_controller = self._browser.platform.tracing_controller
110    if not tracing_controller.IsChromeTracingSupported(self._browser):
111      return
112    self.assertFalse(tracing_controller.is_tracing_running)
113    options = tracing_options.TracingOptions()
114    options.enable_chrome_trace = True
115    category_filter = tracing_category_filter.TracingCategoryFilter()
116    tracing_controller.Start(options, category_filter)
117    self.assertTrue(tracing_controller.is_tracing_running)
118    tracing_controller.Stop()
119    self.assertFalse(tracing_controller.is_tracing_running)
120
121
122class CommandLineBrowserTest(browser_test_case.BrowserTestCase):
123  @classmethod
124  def CustomizeBrowserOptions(cls, options):
125    options.AppendExtraBrowserArgs('--user-agent=telemetry')
126
127  def testCommandLineOverriding(self):
128    # This test starts the browser with --user-agent=telemetry. This tests
129    # whether the user agent is then set.
130    t = self._browser.tabs[0]
131    t.Navigate(self.UrlOfUnittestFile('blank.html'))
132    t.WaitForDocumentReadyStateToBeInteractiveOrBetter()
133    self.assertEquals(t.EvaluateJavaScript('navigator.userAgent'),
134                      'telemetry')
135
136class DirtyProfileBrowserTest(browser_test_case.BrowserTestCase):
137  @classmethod
138  def CustomizeBrowserOptions(cls, options):
139    options.profile_type = 'small_profile'
140
141  @benchmark.Disabled('chromeos')  # crbug.com/243912
142  def testDirtyProfileCreation(self):
143    self.assertEquals(1, len(self._browser.tabs))
144