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.core import util
8 from telemetry.core import exceptions
9 from telemetry.unittest import tab_test_case
12 def _IsDocumentVisible(tab):
13 return not tab.EvaluateJavaScript('document.hidden || document.webkitHidden')
16 class FakePlatform(object):
18 self._is_video_capture_running = False
20 #pylint: disable=W0613
21 def StartVideoCapture(self, min_bitrate_mbps):
22 self._is_video_capture_running = True
24 def StopVideoCapture(self):
25 self._is_video_capture_running = False
28 def SetFullPerformanceModeEnabled(self, enabled):
32 def is_video_capture_running(self):
33 return self._is_video_capture_running
36 class TabTest(tab_test_case.TabTestCase):
37 def testNavigateAndWaitToForCompleteState(self):
38 self._browser.SetHTTPServerDirectories(util.GetUnittestDataDir())
39 self._tab.Navigate(self._browser.http_server.UrlOf('blank.html'))
40 self._tab.WaitForDocumentReadyStateToBeComplete()
42 def testNavigateAndWaitToForInteractiveState(self):
43 self._browser.SetHTTPServerDirectories(util.GetUnittestDataDir())
44 self._tab.Navigate(self._browser.http_server.UrlOf('blank.html'))
45 self._tab.WaitForDocumentReadyStateToBeInteractiveOrBetter()
47 def testTabBrowserIsRightBrowser(self):
48 self.assertEquals(self._tab.browser, self._browser)
50 def testRendererCrash(self):
51 self.assertRaises(exceptions.TabCrashException,
52 lambda: self._tab.Navigate('chrome://crash',
55 def testActivateTab(self):
56 if not self._browser.supports_tab_control:
57 logging.warning('Browser does not support tab control, skipping test.')
60 self.assertTrue(_IsDocumentVisible(self._tab))
61 new_tab = self._browser.tabs.New()
62 new_tab.Navigate('about:blank')
63 util.WaitFor(lambda: _IsDocumentVisible(new_tab), timeout=5)
64 self.assertFalse(_IsDocumentVisible(self._tab))
66 util.WaitFor(lambda: _IsDocumentVisible(self._tab), timeout=5)
67 self.assertFalse(_IsDocumentVisible(new_tab))
69 def testIsTimelineRecordingRunningTab(self):
70 self.assertFalse(self._tab.is_timeline_recording_running)
71 self._tab.StartTimelineRecording()
72 self.assertTrue(self._tab.is_timeline_recording_running)
73 self._tab.StopTimelineRecording()
74 self.assertFalse(self._tab.is_timeline_recording_running)
76 #pylint: disable=W0212
77 def testIsVideoCaptureRunning(self):
78 original_platform = self._tab.browser._platform
79 self._tab.browser._platform = FakePlatform()
80 self.assertFalse(self._tab.is_video_capture_running)
81 self._tab.StartVideoCapture(min_bitrate_mbps=2)
82 self.assertTrue(self._tab.is_video_capture_running)
84 self._tab.StopVideoCapture().next()
87 self.assertFalse(self._tab.is_video_capture_running)
88 self._tab.browser._platform = original_platform
91 class GpuTabTest(tab_test_case.TabTestCase):
93 self._extra_browser_args = ['--enable-gpu-benchmarking']
94 super(GpuTabTest, self).setUp()
96 def testScreenshot(self):
97 if not self._tab.screenshot_supported:
98 logging.warning('Browser does not support screenshots, skipping test.')
101 self.Navigate('green_rect.html')
102 pixel_ratio = self._tab.EvaluateJavaScript('window.devicePixelRatio || 1')
104 screenshot = self._tab.Screenshot(5)
106 screenshot.GetPixelColor(0 * pixel_ratio, 0 * pixel_ratio).AssertIsRGB(
107 0, 255, 0, tolerance=2)
108 screenshot.GetPixelColor(31 * pixel_ratio, 31 * pixel_ratio).AssertIsRGB(
109 0, 255, 0, tolerance=2)
110 screenshot.GetPixelColor(32 * pixel_ratio, 32 * pixel_ratio).AssertIsRGB(
111 255, 255, 255, tolerance=2)