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. |
16 """ | 17 """ |
17 | 18 |
18 # YouTube player states | 19 # YouTube player states |
19 is_unstarted = '-1' | 20 is_unstarted = '-1' |
20 is_playing = '1' | 21 is_playing = '1' |
21 is_paused = '2' | 22 is_paused = '2' |
22 has_ended = '0' | 23 has_ended = '0' |
23 _pyauto = None | 24 _pyauto = None |
24 | 25 |
25 def __init__(self, pyauto): | 26 def __init__(self, pyauto): |
26 self._pyauto = pyauto | 27 self._pyauto = pyauto |
27 | 28 |
28 def IsFlashPluginEnabled(self): | 29 def IsFlashPluginEnabled(self): |
29 """Verify flash plugin availability and its state""" | 30 """Verify flash plugin availability and its state.""" |
30 return [x for x in self._pyauto.GetPluginsInfo().Plugins() \ | 31 return [x for x in self._pyauto.GetPluginsInfo().Plugins() \ |
31 if x['name'] == 'Shockwave Flash' and x['enabled']] | 32 if x['name'] == 'Shockwave Flash' and x['enabled']] |
32 | 33 |
33 def WaitUntilPlayerReady(self): | 34 def WaitUntilPlayerReady(self): |
34 """Verify that player is ready""" | 35 """Verify that player is ready.""" |
35 return self._pyauto.WaitUntil(lambda: self._pyauto.ExecuteJavascript(""" | 36 return self._pyauto.WaitUntil(lambda: self._pyauto.ExecuteJavascript(""" |
36 player_status = document.getElementById("player_status"); | 37 player_status = document.getElementById("player_status"); |
37 window.domAutomationController.send(player_status.innerHTML); | 38 window.domAutomationController.send(player_status.innerHTML); |
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 Youtube video info.""" |
| 51 youtube_apis = self._pyauto.GetPrivateInfo()['youtube_api'] |
| 52 youtube_debug_text = youtube_apis['GetDebugText'] |
| 53 return self._pyauto.ExecuteJavascript( |
| 54 'window.domAutomationController.send(%s);' % youtube_debug_text) |
| 55 |
| 56 def GetVideoDroppedFrames(self): |
| 57 """Returns total Youtube video dropped frames. |
| 58 |
| 59 Returns: |
| 60 -1 if failed to get video frames from the video data |
| 61 """ |
| 62 video_data = self._pyauto.GetVideoInfo() |
| 63 matched = re.search('droppedFrames=([\d\.]+)', video_data) |
| 64 if matched: |
| 65 return int(matched.group(1)) |
| 66 else: |
| 67 return -1 |
| 68 |
| 69 def GetVideoFrames(self): |
| 70 """Returns Youtube video frames/second. |
| 71 |
| 72 Returns: |
| 73 -1 if failed to get droppd frames from the video data. |
| 74 """ |
| 75 video_data = self._pyauto.GetVideoInfo() |
| 76 matched = re.search('videoFps=([\d\.]+)', video_data) |
| 77 if matched: |
| 78 return int(matched.group(1)) |
| 79 else: |
| 80 return -1 |
| 81 |
48 def GetVideoTotalBytes(self): | 82 def GetVideoTotalBytes(self): |
49 """Returns video size in bytes | 83 """Returns video total size in bytes. |
50 | 84 |
51 To call this function, video must be in the paying state, | 85 To call this function, video must be in the paying state, |
52 or this returns 0. | 86 or this returns 0. |
53 """ | 87 """ |
54 total_bytes = 0 | 88 total_bytes = 0 |
55 total_bytes = self._pyauto.ExecuteJavascript(""" | 89 total_bytes = self._pyauto.ExecuteJavascript(""" |
56 bytes = ytplayer.getVideoBytesTotal(); | 90 bytes = ytplayer.getVideoBytesTotal(); |
57 window.domAutomationController.send(bytes + ''); | 91 window.domAutomationController.send(bytes + ''); |
58 """) | 92 """) |
59 return int(total_bytes) | 93 return int(total_bytes) |
60 | 94 |
| 95 def GetVideoLoadedBytes(self): |
| 96 """Returns video size in bytes.""" |
| 97 loaded_bytes = 0 |
| 98 loaded_bytes = self.ExecuteJavascript(""" |
| 99 bytes = ytplayer.getVideoBytesLoaded(); |
| 100 window.domAutomationController.send(bytes + ''); |
| 101 """) |
| 102 return int(loaded_bytes) |
| 103 |
61 def PlayVideo(self): | 104 def PlayVideo(self): |
62 """Plays the loaded video""" | 105 """Plays the loaded video.""" |
63 self._pyauto.ExecuteJavascript(""" | 106 self._pyauto.ExecuteJavascript(""" |
64 ytplayer.playVideo(); | 107 ytplayer.playVideo(); |
65 window.domAutomationController.send(''); | 108 window.domAutomationController.send(''); |
66 """) | 109 """) |
67 | 110 |
| 111 def PauseVideo(self): |
| 112 """Pause the video.""" |
| 113 self.ExecuteJavascript(""" |
| 114 ytplayer.pauseVideo(); |
| 115 window.domAutomationController.send(''); |
| 116 """) |
| 117 |
68 def AssertPlayingState(self): | 118 def AssertPlayingState(self): |
69 """Assert player's playing state""" | 119 """Assert player's playing state.""" |
70 self._pyauto.assertTrue(self._pyauto.WaitUntil(self._pyauto.GetPlayerState, | 120 self._pyauto.assertTrue(self._pyauto.WaitUntil(self._pyauto.GetPlayerState, |
71 expect_retval=self._pyauto.is_playing), | 121 expect_retval=self._pyauto.is_playing), |
72 msg='Player did not enter the playing state') | 122 msg='Player did not enter the playing state') |
73 | 123 |
74 def PlayVideoAndAssert(self): | 124 def PlayVideoAndAssert(self): |
75 """Start video and assert the playing state""" | 125 """Start video and assert the playing state.""" |
76 self._pyauto.assertTrue(self._pyauto.IsFlashPluginEnabled(), | 126 self._pyauto.assertTrue(self._pyauto.IsFlashPluginEnabled(), |
77 msg='From here Flash plugin is disabled or not available') | 127 msg='From here Flash plugin is disabled or not available') |
78 url = self._pyauto.GetHttpURLForDataPath('media', 'youtube.html') | 128 url = self._pyauto.GetHttpURLForDataPath('media', 'youtube.html') |
79 self._pyauto.NavigateToURL(url) | 129 self._pyauto.NavigateToURL(url) |
80 self._pyauto.assertTrue(self._pyauto.WaitUntilPlayerReady(), | 130 self._pyauto.assertTrue(self._pyauto.WaitUntilPlayerReady(), |
81 msg='Failed to load YouTube player') | 131 msg='Failed to load YouTube player') |
82 self._pyauto.PlayVideo() | 132 self._pyauto.PlayVideo() |
83 self._pyauto.AssertPlayingState() | 133 self._pyauto.AssertPlayingState() |
84 | 134 |
85 | 135 |
86 class YoutubeTest(pyauto.PyUITest, YoutubeTestHelper): | 136 class YoutubeTest(pyauto.PyUITest, YoutubeTestHelper): |
87 """Test case for Youtube videos.""" | 137 """Test case for Youtube videos.""" |
88 | 138 |
89 def __init__(self, methodName='runTest', **kwargs): | 139 def __init__(self, methodName='runTest', **kwargs): |
90 pyauto.PyUITest.__init__(self, methodName, **kwargs) | 140 pyauto.PyUITest.__init__(self, methodName, **kwargs) |
91 YoutubeTestHelper.__init__(self, self) | 141 YoutubeTestHelper.__init__(self, self) |
92 | 142 |
93 def testPlayerStatus(self): | 143 def testPlayerStatus(self): |
94 """Test that YouTube loads a player and changes player states | 144 """Test that YouTube loads a player and changes player states |
95 | 145 |
96 Test verifies various player states like unstarted, playing, paused | 146 Test verifies various player states like unstarted, playing, paused |
97 and ended. | 147 and ended. |
98 """ | 148 """ |
99 # Navigating to Youtube video. This video is 122 seconds long. | 149 # Navigating to Youtube video. This video is 122 seconds long. |
100 # During tests, we are not goinig to play this video full. | 150 # During tests, we are not goinig to play this video full. |
101 self.PlayVideoAndAssert() | 151 self.PlayVideoAndAssert() |
102 # Pause the playing video | 152 self.PauseVideo() |
103 self.ExecuteJavascript(""" | |
104 ytplayer.pauseVideo(); | |
105 window.domAutomationController.send(''); | |
106 """) | |
107 self.assertEqual(self.GetPlayerState(), self.is_paused, | 153 self.assertEqual(self.GetPlayerState(), self.is_paused, |
108 msg='Player did not enter the paused state') | 154 msg='Player did not enter the paused state') |
109 # Seek to the end of video | 155 # Seek to the end of video |
110 self.ExecuteJavascript(""" | 156 self.ExecuteJavascript(""" |
111 val = ytplayer.getDuration(); | 157 val = ytplayer.getDuration(); |
112 ytplayer.seekTo(val, true); | 158 ytplayer.seekTo(val, true); |
113 window.domAutomationController.send(''); | 159 window.domAutomationController.send(''); |
114 """) | 160 """) |
115 self.PlayVideo() | 161 self.PlayVideo() |
116 # We've seeked to almost the end of the video but not quite. | 162 # We've seeked to almost the end of the video but not quite. |
117 # Wait until the end. | 163 # Wait until the end. |
118 self.assertTrue(self.WaitUntil(self.GetPlayerState, | 164 self.assertTrue(self.WaitUntil(self.GetPlayerState, |
119 expect_retval=self.has_ended), | 165 expect_retval=self.has_ended), |
120 msg='Player did not reach the stopped state') | 166 msg='Player did not reach the stopped state') |
121 | 167 |
122 def testPlayerResolution(self): | 168 def testPlayerResolution(self): |
123 """Test various video resolutions""" | 169 """Test various video resolutions.""" |
124 self.PlayVideoAndAssert() | 170 self.PlayVideoAndAssert() |
125 resolutions = self.ExecuteJavascript(""" | 171 resolutions = self.ExecuteJavascript(""" |
126 res = ytplayer.getAvailableQualityLevels(); | 172 res = ytplayer.getAvailableQualityLevels(); |
127 window.domAutomationController.send(res.toString()); | 173 window.domAutomationController.send(res.toString()); |
128 """) | 174 """) |
129 resolutions = resolutions.split(',') | 175 resolutions = resolutions.split(',') |
130 for res in resolutions: | 176 for res in resolutions: |
131 self.ExecuteJavascript(""" | 177 self.ExecuteJavascript(""" |
132 ytplayer.setPlaybackQuality('%s'); | 178 ytplayer.setPlaybackQuality('%s'); |
133 window.domAutomationController.send(''); | 179 window.domAutomationController.send(''); |
134 """ % res) | 180 """ % res) |
135 curr_res = self.ExecuteJavascript(""" | 181 curr_res = self.ExecuteJavascript(""" |
136 res = ytplayer.getPlaybackQuality(); | 182 res = ytplayer.getPlaybackQuality(); |
137 window.domAutomationController.send(res + ''); | 183 window.domAutomationController.send(res + ''); |
138 """) | 184 """) |
139 self.assertEqual(res, curr_res, msg='Resolution is not set to %s.' % res) | 185 self.assertEqual(res, curr_res, msg='Resolution is not set to %s.' % res) |
140 | 186 |
141 def testPlayerBytes(self): | 187 def testPlayerBytes(self): |
142 """Test that player downloads video bytes""" | 188 """Test that player downloads video bytes.""" |
143 self.PlayVideoAndAssert() | 189 self.PlayVideoAndAssert() |
144 total_bytes = self.GetVideoTotalBytes() | 190 total_bytes = self.GetVideoTotalBytes() |
145 prev_loaded_bytes = 0 | 191 prev_loaded_bytes = 0 |
146 loaded_bytes = 0 | 192 loaded_bytes = 0 |
147 count = 0 | 193 count = 0 |
148 while loaded_bytes < total_bytes: | 194 while loaded_bytes < total_bytes: |
149 # We want to test bytes loading only twice | 195 # We want to test bytes loading only twice |
150 count = count + 1 | 196 count = count + 1 |
151 if count == 2: | 197 if count == 2: |
152 break | 198 break |
153 loaded_bytes = self.ExecuteJavascript(""" | 199 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) | 200 self.assertTrue(prev_loaded_bytes <= loaded_bytes) |
159 prev_loaded_bytes = loaded_bytes | 201 prev_loaded_bytes = loaded_bytes |
160 # Give some time to load a video | 202 # Give some time to load a video |
161 time.sleep(1) | 203 time.sleep(1) |
162 | 204 |
163 | 205 |
164 if __name__ == '__main__': | 206 if __name__ == '__main__': |
165 pyauto_functional.Main() | 207 pyauto_functional.Main() |
OLD | NEW |