1 #!/usr/bin/env python 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 6 import pyauto_functional 7 import pyauto 8 import webrtc_test_base 9 10 11 class MediaStreamInfobarTest(webrtc_test_base.WebrtcTestBase): 12 """Performs basic tests on the media stream infobar. 13 14 This infobar is used to grant or deny access to WebRTC capabilities for a 15 webpage. If a page calls the getUserMedia function the infobar will ask the 16 user if it is OK for the webpage to use the webcam or microphone on the user's 17 machine. These tests ensure that the infobar works as intended. 18 """ 19 20 def ExtraChromeFlags(self): 21 """Adds flags to the Chrome command line.""" 22 extra_flags = ['--enable-media-stream'] 23 return pyauto.PyUITest.ExtraChromeFlags(self) + extra_flags 24 25 def testAllowingUserMedia(self): 26 """Test that selecting 'accept' gives us a media stream. 27 28 When the user clicks allow, the javascript should have the success callback 29 called with a media stream. 30 """ 31 self.assertEquals('ok-got-stream', 32 self._TestGetUserMedia(with_action='accept')) 33 34 def testDenyingUserMedia(self): 35 """Tests that selecting 'cancel' actually denies access to user media. 36 37 When the user clicks deny in the user media bar, the javascript should have 38 the error callback called with an error specification instead of the success 39 callback with a media stream. This is important since the user should be 40 able to deny the javascript to access the webcam. 41 """ 42 # Error 1 = Permission denied 43 self.assertEquals('failed-with-error-PERMISSION_DENIED', 44 self._TestGetUserMedia(with_action='cancel')) 45 46 def testDismissingUserMedia(self): 47 """Dismiss should be treated just like deny, which is described above.""" 48 # Error 1 = Permission denied 49 self.assertEquals('failed-with-error-PERMISSION_DENIED', 50 self._TestGetUserMedia(with_action='dismiss')) 51 52 def testConsecutiveGetUserMediaCalls(self): 53 """Ensures we deal appropriately with several consecutive requests.""" 54 self.assertEquals('failed-with-error-PERMISSION_DENIED', 55 self._TestGetUserMedia(with_action='dismiss')) 56 self.assertEquals('failed-with-error-PERMISSION_DENIED', 57 self._TestGetUserMedia(with_action='cancel')) 58 self.assertEquals('ok-got-stream', 59 self._TestGetUserMedia(with_action='accept')) 60 self.assertEquals('failed-with-error-PERMISSION_DENIED', 61 self._TestGetUserMedia(with_action='cancel')) 62 self.assertEquals('ok-got-stream', 63 self._TestGetUserMedia(with_action='accept')) 64 self.assertEquals('failed-with-error-PERMISSION_DENIED', 65 self._TestGetUserMedia(with_action='dismiss')) 66 67 def _TestGetUserMedia(self, with_action): 68 """Runs getUserMedia in the test page and returns the result.""" 69 url = self.GetFileURLForDataPath('webrtc', 'webrtc_jsep01_test.html') 70 self.NavigateToURL(url) 71 72 self.assertEquals('ok-requested', self.ExecuteJavascript( 73 'getUserMedia("{ audio: true, video: true, }")')) 74 75 self.WaitForInfobarCount(1) 76 self.PerformActionOnInfobar(with_action, infobar_index=0) 77 self.WaitForGetUserMediaResult(tab_index=0) 78 79 return self.GetUserMediaResult(tab_index=0) 80 81 82 if __name__ == '__main__': 83 pyauto_functional.Main() 84