Chromium Code Reviews| Index: chrome/test/media_router/resources/common.js |
| diff --git a/chrome/test/media_router/resources/common.js b/chrome/test/media_router/resources/common.js |
| index 6740342957cb0e845a3068bb7a20ba9fc224733e..73593fb7476f457bb5ba596874dc3f240304f235 100644 |
| --- a/chrome/test/media_router/resources/common.js |
| +++ b/chrome/test/media_router/resources/common.js |
| @@ -131,6 +131,82 @@ function terminateSessionAndWaitForStateChange() { |
| } |
| } |
| +/** |
| + * Closes |startedConnection| and waits for its onclose event. |
| + */ |
| +function closeSessionAndWaitForStateChange() { |
| + if (startedConnection) { |
| + startedConnection.onclose = function() { |
| + sendResult(true, ''); |
| + }; |
| + startedConnection.close(); |
| + } else { |
| + sendResult(false, 'startedConnection does not exist.'); |
| + } |
| +} |
| + |
| +/** |
| + * Reconnects to |startedConnection| and waits for its state to change to |
| + * "connected." Requires |startedConnection| to be closed initially. |
| + */ |
| +function reconnectToClosedSessionAndWaitForStateChange() { |
| + if (startedConnection) { |
| + if (startedConnection.state != 'closed') { |
| + sendResult(false, 'startedConnection is not closed.'); |
| + return; |
| + } |
| + startSessionRequest.reconnect(startedConnection.id).then(function(session) { |
| + if(session !== startedConnection) { |
| + sendResult(false, 'Failed to reconnect to the closed session'); |
| + } else { |
| + console.log('connection state is "' + startedConnection.state + '"'); |
| + if (startedConnection.state == 'connected') { |
| + sendResult(true, ''); |
| + } else if (startedConnection.state == 'connecting') { |
| + startedConnection.onconnect = () => { |
| + sendResult(true, ''); |
| + }; |
| + } else { |
| + sendResult(false, |
| + 'Expect connection state to be "connecting" or "connected", ' + |
| + 'actual "' + startedConnection.state + '"'); |
| + } |
| + } |
| + }).catch(function(e) { |
| + // terminate old session if exists |
| + startedConnection && startedConnection.terminate(); |
| + sendResult(false, 'Failed to start session: encountered exception ' + e); |
| + }); |
| + } else { |
| + sendResult(false, 'startedConnection does not exist.'); |
| + } |
| +} |
| + |
| +/** |
| + * Sends a message to |startedConnection| and expects InvalidStateError to be |
| + * thrown. Requires |startedConnection.state| to not be "connected." |
|
mark a. foltz
2017/01/20 19:00:09
Can you pass in an expectation of the connection s
takumif
2017/02/07 22:17:36
Done.
|
| + */ |
| +function checkSendMessageFailed() { |
| + if (!startedConnection) { |
| + sendResult(false, 'startedConnection does not exist.'); |
| + return; |
| + } |
| + if (startedConnection.state == 'connected') { |
| + sendResult(false, 'startedConnection is connected, which is not expected.'); |
| + return; |
| + } |
| + |
| + try { |
| + startedConnection.send('test message'); |
| + } catch (e) { |
| + if (e.name == 'InvalidStateError') { |
| + sendResult(true, ''); |
| + } else { |
| + sendResult(false, 'Got an unexpected error: ' + e.name); |
| + } |
| + } |
| + sendResult(false, 'Expected InvalidStateError but it was never thrown.'); |
| +} |
| /** |
| * Sends a message, and expects the connection to close on error. |