Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import re | |
| 6 import time | 7 import time |
| 7 | 8 |
| 8 import pyauto_functional | 9 import pyauto_functional |
| 9 import pyauto | 10 import pyauto |
| 10 | 11 |
| 11 | 12 |
| 12 class YoutubeTestHelper(): | 13 class YoutubeTestHelper(): |
| 13 """Helper functions for Youtube tests. | 14 """Helper functions for Youtube tests. |
| 14 | 15 |
| 15 For sample usage, look at class YoutubeTest. | 16 For sample usage, look at class YoutubeTest. |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 38 """), expect_retval='player ready') | 39 """), expect_retval='player ready') |
| 39 | 40 |
| 40 def GetPlayerState(self): | 41 def GetPlayerState(self): |
| 41 """Returns a player state""" | 42 """Returns a player state""" |
| 42 js = """ | 43 js = """ |
| 43 var val = ytplayer.getPlayerState(); | 44 var val = ytplayer.getPlayerState(); |
| 44 window.domAutomationController.send(val + ''); | 45 window.domAutomationController.send(val + ''); |
| 45 """ | 46 """ |
| 46 return self._pyauto.ExecuteJavascript(js) | 47 return self._pyauto.ExecuteJavascript(js) |
| 47 | 48 |
| 49 def GetVideoInfo(self): | |
| 50 """Returns video info""" | |
|
Nirnimesh
2011/11/15 02:13:50
What info? Include sample.
rohitbm
2011/11/15 03:10:50
This API is undocumented, so do we want to include
| |
| 51 youtube_apis = self._pyauto.GetPrivateInfo()['youtube_api'] | |
| 52 youtube_debug_text = youtube_apis['GetDebugText'] | |
| 53 video_data = self._pyauto.ExecuteJavascript( | |
| 54 'window.domAutomationController.send(%s);' % youtube_debug_text) | |
| 55 return video_data | |
|
Nirnimesh
2011/11/15 02:13:50
merge with previous line
rohitbm
2011/11/15 03:10:50
Done.
| |
| 56 | |
| 57 def GetVideoDroppedFrames(self): | |
| 58 """Returns total Youtube video dropped frames. | |
| 59 | |
| 60 Returns: | |
| 61 -1 if failed to get video frames from the video data | |
| 62 """ | |
| 63 video_data = self._pyauto.GetVideoInfo() | |
| 64 matched = re.search('droppedFrames=([\d\.]+)', video_data) | |
| 65 if matched: | |
| 66 return int(matched.group(1)) | |
| 67 else: | |
| 68 return -1 | |
| 69 | |
| 70 def GetVideoFrames(self): | |
| 71 """Returns Youtube video frames/second | |
|
Nirnimesh
2011/11/15 02:13:50
end with . for consistency
rohitbm
2011/11/15 03:10:50
Done.
| |
| 72 | |
| 73 Returns: | |
| 74 -1 if failed to get droppd frames from the video data. | |
| 75 """ | |
| 76 video_data = self._pyauto.GetVideoInfo() | |
| 77 matched = re.search('videoFps=([\d\.]+)', video_data) | |
| 78 if matched: | |
| 79 return int(matched.group(1)) | |
| 80 else: | |
| 81 return -1 | |
| 82 | |
| 48 def GetVideoTotalBytes(self): | 83 def GetVideoTotalBytes(self): |
| 49 """Returns video size in bytes | 84 """Returns video total size in bytes |
| 50 | 85 |
| 51 To call this function, video must be in the paying state, | 86 To call this function, video must be in the paying state, |
| 52 or this returns 0. | 87 or this returns 0. |
| 53 """ | 88 """ |
| 54 total_bytes = 0 | 89 total_bytes = 0 |
| 55 total_bytes = self._pyauto.ExecuteJavascript(""" | 90 total_bytes = self._pyauto.ExecuteJavascript(""" |
| 56 bytes = ytplayer.getVideoBytesTotal(); | 91 bytes = ytplayer.getVideoBytesTotal(); |
| 57 window.domAutomationController.send(bytes + ''); | 92 window.domAutomationController.send(bytes + ''); |
| 58 """) | 93 """) |
| 59 return int(total_bytes) | 94 return int(total_bytes) |
| 60 | 95 |
| 96 def GetVideoLoadedBytes(self): | |
| 97 """Returns video size in bytes.""" | |
| 98 loaded_bytes = 0 | |
| 99 loaded_bytes = self.ExecuteJavascript(""" | |
| 100 bytes = ytplayer.getVideoBytesLoaded(); | |
| 101 window.domAutomationController.send(bytes + ''); | |
| 102 """) | |
| 103 return int(loaded_bytes) | |
| 104 | |
| 61 def PlayVideo(self): | 105 def PlayVideo(self): |
| 62 """Plays the loaded video""" | 106 """Plays the loaded video""" |
| 63 self._pyauto.ExecuteJavascript(""" | 107 self._pyauto.ExecuteJavascript(""" |
| 64 ytplayer.playVideo(); | 108 ytplayer.playVideo(); |
| 65 window.domAutomationController.send(''); | 109 window.domAutomationController.send(''); |
| 66 """) | 110 """) |
| 67 | 111 |
| 112 def PauseVideo(self): | |
| 113 """Pause the video""" | |
| 114 self.ExecuteJavascript(""" | |
| 115 ytplayer.pauseVideo(); | |
| 116 window.domAutomationController.send(''); | |
| 117 """) | |
| 118 | |
| 68 def AssertPlayingState(self): | 119 def AssertPlayingState(self): |
| 69 """Assert player's playing state""" | 120 """Assert player's playing state""" |
| 70 self._pyauto.assertTrue(self._pyauto.WaitUntil(self._pyauto.GetPlayerState, | 121 self._pyauto.assertTrue(self._pyauto.WaitUntil(self._pyauto.GetPlayerState, |
| 71 expect_retval=self._pyauto.is_playing), | 122 expect_retval=self._pyauto.is_playing), |
| 72 msg='Player did not enter the playing state') | 123 msg='Player did not enter the playing state') |
| 73 | 124 |
| 74 def PlayVideoAndAssert(self): | 125 def PlayVideoAndAssert(self): |
| 75 """Start video and assert the playing state""" | 126 """Start video and assert the playing state""" |
| 76 self._pyauto.assertTrue(self._pyauto.IsFlashPluginEnabled(), | 127 self._pyauto.assertTrue(self._pyauto.IsFlashPluginEnabled(), |
| 77 msg='From here Flash plugin is disabled or not available') | 128 msg='From here Flash plugin is disabled or not available') |
| 78 url = self._pyauto.GetHttpURLForDataPath('media', 'youtube.html') | 129 url = self._pyauto.GetHttpURLForDataPath('media', 'youtube.html') |
| 79 self._pyauto.NavigateToURL(url) | 130 self._pyauto.NavigateToURL(url) |
| 80 self._pyauto.assertTrue(self._pyauto.WaitUntilPlayerReady(), | 131 self._pyauto.assertTrue(self._pyauto.WaitUntilPlayerReady(), |
| 81 msg='Failed to load YouTube player') | 132 msg='Failed to load YouTube player') |
| 82 self._pyauto.PlayVideo() | 133 self._pyauto.PlayVideo() |
| 83 self._pyauto.AssertPlayingState() | 134 self._pyauto.AssertPlayingState() |
| 84 | 135 |
| 85 | 136 |
| 86 class YoutubeTest(pyauto.PyUITest, YoutubeTestHelper): | 137 class YoutubeTest(pyauto.PyUITest, YoutubeTestHelper): |
| 87 """Test case for Youtube videos.""" | 138 """Test case for Youtube videos.""" |
| 88 | 139 |
| 89 def __init__(self, methodName='runTest', **kwargs): | 140 def __init__(self, methodName='runTest', **kwargs): |
| 90 pyauto.PyUITest.__init__(self, methodName, **kwargs) | 141 pyauto.PyUITest.__init__(self, methodName, **kwargs) |
| 91 YoutubeTestHelper.__init__(self, self) | 142 YoutubeTestHelper.__init__(self, self) |
| 92 | 143 |
| 93 def testPlayerStatus(self): | 144 def testPlayerStatus(self): |
| 94 """Test that YouTube loads a player and changes player states | 145 """Test that YouTube loads a player and changes player states |
| 95 | 146 |
| 96 Test verifies various player states like unstarted, playing, paused | 147 Test verifies various player states like unstarted, playing, paused |
| 97 and ended. | 148 and ended. |
| 98 """ | 149 """ |
| 99 # Navigating to Youtube video. This video is 122 seconds long. | 150 # Navigating to Youtube video. This video is 122 seconds long. |
| 100 # During tests, we are not goinig to play this video full. | 151 # During tests, we are not goinig to play this video full. |
| 101 self.PlayVideoAndAssert() | 152 self.PlayVideoAndAssert() |
| 102 # Pause the playing video | 153 self.PauseVideo() |
| 103 self.ExecuteJavascript(""" | |
| 104 ytplayer.pauseVideo(); | |
| 105 window.domAutomationController.send(''); | |
| 106 """) | |
| 107 self.assertEqual(self.GetPlayerState(), self.is_paused, | 154 self.assertEqual(self.GetPlayerState(), self.is_paused, |
| 108 msg='Player did not enter the paused state') | 155 msg='Player did not enter the paused state') |
| 109 # Seek to the end of video | 156 # Seek to the end of video |
| 110 self.ExecuteJavascript(""" | 157 self.ExecuteJavascript(""" |
| 111 val = ytplayer.getDuration(); | 158 val = ytplayer.getDuration(); |
| 112 ytplayer.seekTo(val, true); | 159 ytplayer.seekTo(val, true); |
| 113 window.domAutomationController.send(''); | 160 window.domAutomationController.send(''); |
| 114 """) | 161 """) |
| 115 self.PlayVideo() | 162 self.PlayVideo() |
| 116 # We've seeked to almost the end of the video but not quite. | 163 # We've seeked to almost the end of the video but not quite. |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 143 self.PlayVideoAndAssert() | 190 self.PlayVideoAndAssert() |
| 144 total_bytes = self.GetVideoTotalBytes() | 191 total_bytes = self.GetVideoTotalBytes() |
| 145 prev_loaded_bytes = 0 | 192 prev_loaded_bytes = 0 |
| 146 loaded_bytes = 0 | 193 loaded_bytes = 0 |
| 147 count = 0 | 194 count = 0 |
| 148 while loaded_bytes < total_bytes: | 195 while loaded_bytes < total_bytes: |
| 149 # We want to test bytes loading only twice | 196 # We want to test bytes loading only twice |
| 150 count = count + 1 | 197 count = count + 1 |
| 151 if count == 2: | 198 if count == 2: |
| 152 break | 199 break |
| 153 loaded_bytes = self.ExecuteJavascript(""" | 200 loaded_bytes = self.GetVideoLoadedBytes() |
| 154 bytes = ytplayer.getVideoBytesLoaded(); | |
| 155 window.domAutomationController.send(bytes + ''); | |
| 156 """) | |
| 157 loaded_bytes = int(loaded_bytes) | |
| 158 self.assertTrue(prev_loaded_bytes <= loaded_bytes) | 201 self.assertTrue(prev_loaded_bytes <= loaded_bytes) |
| 159 prev_loaded_bytes = loaded_bytes | 202 prev_loaded_bytes = loaded_bytes |
| 160 # Give some time to load a video | 203 # Give some time to load a video |
| 161 time.sleep(1) | 204 time.sleep(1) |
| 162 | 205 |
| 163 | 206 |
| 164 if __name__ == '__main__': | 207 if __name__ == '__main__': |
| 165 pyauto_functional.Main() | 208 pyauto_functional.Main() |
| OLD | NEW |