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.
12 class MissingRequiredBinaryException(Exception):
16 class WebrtcTestBase(pyauto
.PyUITest
):
17 """This base class provides helpers for WebRTC calls."""
19 def ExtraChromeFlags(self
):
20 """Adds flags to the Chrome command line."""
21 extra_flags
= ['--enable-media-stream', '--enable-peer-connection']
22 return pyauto
.PyUITest
.ExtraChromeFlags(self
) + extra_flags
24 def GetUserMedia(self
, tab_index
, action
='allow'):
25 """Acquires webcam or mic for one tab and returns the result.
28 tab_index: The tab to request user media on.
29 action: The action to take on the info bar. Can be 'allow', 'deny' or
33 A string as specified by the getUserMedia javascript function.
35 self
.assertEquals('ok-requested', self
.ExecuteJavascript(
36 'getUserMedia("{ audio: true, video: true, }")', tab_index
=tab_index
))
38 self
.WaitForInfobarCount(1, tab_index
=tab_index
)
39 self
.PerformActionOnInfobar(action
, infobar_index
=0, tab_index
=tab_index
)
40 self
.WaitForGetUserMediaResult(tab_index
=0)
42 result
= self
.GetUserMediaResult(tab_index
=0)
43 self
.AssertNoFailures(tab_index
)
46 def WaitForGetUserMediaResult(self
, tab_index
):
47 """Waits until WebRTC has responded to a getUserMedia query.
49 Fails an assert if WebRTC doesn't respond within the default timeout.
52 tab_index: the tab to query.
55 return self
.GetUserMediaResult(tab_index
) != 'not-called-yet'
56 self
.assertTrue(self
.WaitUntil(HasResult
),
57 msg
='Timed out while waiting for getUserMedia callback.')
59 def GetUserMediaResult(self
, tab_index
):
60 """Retrieves WebRTC's answer to a user media query.
63 tab_index: the tab to query.
66 Specified in obtainGetUserMediaResult() in getusermedia.js.
68 return self
.ExecuteJavascript(
69 'obtainGetUserMediaResult()', tab_index
=tab_index
)
71 def AssertNoFailures(self
, tab_index
):
72 """Ensures the javascript hasn't registered any asynchronous errors.
75 tab_index: The tab to check.
77 self
.assertEquals('ok-no-errors', self
.ExecuteJavascript(
78 'getAnyTestFailures()', tab_index
=tab_index
))
80 def Connect(self
, user_name
, tab_index
):
81 self
.assertEquals('ok-connected', self
.ExecuteJavascript(
82 'connect("http://localhost:8888", "%s")' % user_name
,
84 self
.AssertNoFailures(tab_index
)
86 def EstablishCall(self
, from_tab_with_index
):
87 self
.WaitUntilPeerConnects(tab_index
=from_tab_with_index
)
89 self
.assertEquals('ok-got-remote-stream', self
.ExecuteJavascript(
90 'call()', tab_index
=from_tab_with_index
))
91 self
.AssertNoFailures(from_tab_with_index
)
93 self
.assertEquals('ok-local-stream-sent', self
.ExecuteJavascript(
94 'sendLocalStreamOverPeerConnection()', tab_index
=from_tab_with_index
))
95 self
.AssertNoFailures(from_tab_with_index
)
97 # Double-check the call reached the other side.
98 self
.assertEquals('yes', self
.ExecuteJavascript(
99 'isCallActive()', tab_index
=from_tab_with_index
))
101 def HangUp(self
, from_tab_with_index
):
102 self
.assertEquals('ok-call-hung-up', self
.ExecuteJavascript(
103 'hangUp()', tab_index
=from_tab_with_index
))
104 self
.WaitUntilHangUpVerified(tab_index
=from_tab_with_index
)
105 self
.AssertNoFailures(tab_index
=from_tab_with_index
)
107 def WaitUntilPeerConnects(self
, tab_index
):
108 peer_connected
= self
.WaitUntil(
109 function
=lambda: self
.ExecuteJavascript('remotePeerIsConnected()',
110 tab_index
=tab_index
),
111 expect_retval
='peer-connected')
112 self
.assertTrue(peer_connected
,
113 msg
='Timed out while waiting for peer to connect.')
115 def WaitUntilHangUpVerified(self
, tab_index
):
116 hung_up
= self
.WaitUntil(
117 function
=lambda: self
.ExecuteJavascript('isCallActive()',
118 tab_index
=tab_index
),
120 self
.assertTrue(hung_up
,
121 msg
='Timed out while waiting for hang-up to be confirmed.')
123 def Disconnect(self
, tab_index
):
124 self
.assertEquals('ok-disconnected', self
.ExecuteJavascript(
125 'disconnect()', tab_index
=tab_index
))
127 def BinPathForPlatform(self
, path
):
128 """Form a platform specific path to a binary.
131 path(string): The path to the binary without an extension.
133 (string): The platform-specific bin path.
139 def StartPeerConnectionServer(self
):
140 """Starts peerconnection_server.
142 Peerconnection_server is a custom binary allowing two WebRTC clients to find
143 each other. For more details, see the source code which is available at the
144 site http://code.google.com/p/libjingle/source/browse/ (make sure to browse
145 to trunk/talk/examples/peerconnection/server).
147 # Start the peerconnection_server. It should be next to chrome.
148 binary_path
= os
.path
.join(self
.BrowserPath(), 'peerconnection_server')
149 binary_path
= self
.BinPathForPlatform(binary_path
)
151 if not os
.path
.exists(binary_path
):
152 raise MissingRequiredBinaryException(
153 'Could not locate peerconnection_server. Have you built the '
154 'peerconnection_server target? We expect to have a '
155 'peerconnection_server binary next to the chrome binary.')
157 self
._server
_process
= subprocess
.Popen(binary_path
)
159 def StopPeerConnectionServer(self
):
160 """Stops the peerconnection_server."""
161 assert self
._server
_process
162 self
._server
_process
.kill()