Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / tab_unittest.py
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.
4
5 import logging
6
7 from telemetry.core import util
8 from telemetry.core import exceptions
9 from telemetry.unittest import tab_test_case
10
11
12 def _IsDocumentVisible(tab):
13   return not tab.EvaluateJavaScript('document.hidden || document.webkitHidden')
14
15
16 class FakePlatform(object):
17   def __init__(self):
18     self._is_video_capture_running = False
19
20   #pylint: disable=W0613
21   def StartVideoCapture(self, min_bitrate_mbps):
22     self._is_video_capture_running = True
23
24   def StopVideoCapture(self):
25     self._is_video_capture_running = False
26     return []
27
28   def SetFullPerformanceModeEnabled(self, enabled):
29     pass
30
31   @property
32   def is_video_capture_running(self):
33     return self._is_video_capture_running
34
35
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()
41
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()
46
47   def testTabBrowserIsRightBrowser(self):
48     self.assertEquals(self._tab.browser, self._browser)
49
50   def testRendererCrash(self):
51     self.assertRaises(exceptions.TabCrashException,
52                       lambda: self._tab.Navigate('chrome://crash',
53                                                  timeout=5))
54
55   def testActivateTab(self):
56     if not self._browser.supports_tab_control:
57       logging.warning('Browser does not support tab control, skipping test.')
58       return
59
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))
65     self._tab.Activate()
66     util.WaitFor(lambda: _IsDocumentVisible(self._tab), timeout=5)
67     self.assertFalse(_IsDocumentVisible(new_tab))
68
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)
75
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)
83     try:
84       self._tab.StopVideoCapture().next()
85     except Exception:
86       pass
87     self.assertFalse(self._tab.is_video_capture_running)
88     self._tab.browser._platform = original_platform
89
90
91 class GpuTabTest(tab_test_case.TabTestCase):
92   def setUp(self):
93     self._extra_browser_args = ['--enable-gpu-benchmarking']
94     super(GpuTabTest, self).setUp()
95
96   def testScreenshot(self):
97     if not self._tab.screenshot_supported:
98       logging.warning('Browser does not support screenshots, skipping test.')
99       return
100
101     self.Navigate('green_rect.html')
102     pixel_ratio = self._tab.EvaluateJavaScript('window.devicePixelRatio || 1')
103
104     screenshot = self._tab.Screenshot(5)
105     assert screenshot
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)