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.
7 from telemetry import test
8 from telemetry.core import util
9 from telemetry.core import exceptions
10 from telemetry.unittest import tab_test_case
13 def _IsDocumentVisible(tab):
14 return not tab.EvaluateJavaScript('document.hidden || document.webkitHidden')
17 class FakePlatform(object):
19 self._is_video_capture_running = False
21 #pylint: disable=W0613
22 def StartVideoCapture(self, min_bitrate_mbps):
23 self._is_video_capture_running = True
25 def StopVideoCapture(self):
26 self._is_video_capture_running = False
29 def SetFullPerformanceModeEnabled(self, enabled):
33 def is_video_capture_running(self):
34 return self._is_video_capture_running
37 class TabTest(tab_test_case.TabTestCase):
38 def testNavigateAndWaitToForCompleteState(self):
39 self._browser.SetHTTPServerDirectories(util.GetUnittestDataDir())
40 self._tab.Navigate(self._browser.http_server.UrlOf('blank.html'))
41 self._tab.WaitForDocumentReadyStateToBeComplete()
43 def testNavigateAndWaitToForInteractiveState(self):
44 self._browser.SetHTTPServerDirectories(util.GetUnittestDataDir())
45 self._tab.Navigate(self._browser.http_server.UrlOf('blank.html'))
46 self._tab.WaitForDocumentReadyStateToBeInteractiveOrBetter()
48 def testTabBrowserIsRightBrowser(self):
49 self.assertEquals(self._tab.browser, self._browser)
51 def testRendererCrash(self):
52 self.assertRaises(exceptions.TabCrashException,
53 lambda: self._tab.Navigate('chrome://crash',
56 def testActivateTab(self):
57 if not self._browser.supports_tab_control:
58 logging.warning('Browser does not support tab control, skipping test.')
61 util.WaitFor(lambda: _IsDocumentVisible(self._tab), timeout=5)
62 new_tab = self._browser.tabs.New()
63 new_tab.Navigate('about:blank')
64 util.WaitFor(lambda: _IsDocumentVisible(new_tab), timeout=5)
65 self.assertFalse(_IsDocumentVisible(self._tab))
67 util.WaitFor(lambda: _IsDocumentVisible(self._tab), timeout=5)
68 self.assertFalse(_IsDocumentVisible(new_tab))
71 self.assertEquals(self._tab.url, 'about:blank')
72 self.Navigate('blank.html')
73 self.assertEquals(self._tab.url, self.test_url)
75 def testIsTimelineRecordingRunningTab(self):
76 self.assertFalse(self._tab.is_timeline_recording_running)
77 self._tab.StartTimelineRecording()
78 self.assertTrue(self._tab.is_timeline_recording_running)
79 self._tab.StopTimelineRecording()
80 self.assertFalse(self._tab.is_timeline_recording_running)
82 #pylint: disable=W0212
83 def testIsVideoCaptureRunning(self):
84 original_platform = self._tab.browser._platform
85 self._tab.browser._platform = FakePlatform()
86 self.assertFalse(self._tab.is_video_capture_running)
87 self._tab.StartVideoCapture(min_bitrate_mbps=2)
88 self.assertTrue(self._tab.is_video_capture_running)
90 self._tab.StopVideoCapture().next()
93 self.assertFalse(self._tab.is_video_capture_running)
94 self._tab.browser._platform = original_platform
97 class GpuTabTest(tab_test_case.TabTestCase):
99 self._extra_browser_args = ['--enable-gpu-benchmarking']
100 super(GpuTabTest, self).setUp()
102 # Test flaky on mac: http://crbug.com/358664
103 @test.Disabled('android', 'mac')
104 def testScreenshot(self):
105 if not self._tab.screenshot_supported:
106 logging.warning('Browser does not support screenshots, skipping test.')
109 self.Navigate('green_rect.html')
110 pixel_ratio = self._tab.EvaluateJavaScript('window.devicePixelRatio || 1')
112 screenshot = self._tab.Screenshot(5)
114 screenshot.GetPixelColor(0 * pixel_ratio, 0 * pixel_ratio).AssertIsRGB(
115 0, 255, 0, tolerance=2)
116 screenshot.GetPixelColor(31 * pixel_ratio, 31 * pixel_ratio).AssertIsRGB(
117 0, 255, 0, tolerance=2)
118 screenshot.GetPixelColor(32 * pixel_ratio, 32 * pixel_ratio).AssertIsRGB(
119 255, 255, 255, tolerance=2)