| Index: remoting/webapp/unittests/dns_blackhole_checker_unittest.js
|
| diff --git a/remoting/webapp/unittests/dns_blackhole_checker_unittest.js b/remoting/webapp/unittests/dns_blackhole_checker_unittest.js
|
| index dafa2c2bf997bf3d6c8b4c235ed7123a94928983..fd5f3bf4b8765ffc53d66221b63c9e01841c3552 100644
|
| --- a/remoting/webapp/unittests/dns_blackhole_checker_unittest.js
|
| +++ b/remoting/webapp/unittests/dns_blackhole_checker_unittest.js
|
| @@ -25,7 +25,7 @@ var checker = null;
|
| var signalStrategy = null;
|
| var fakeXhrs;
|
|
|
| -module('dns_blackhole_checker', {
|
| +QUnit.module('dns_blackhole_checker', {
|
| setup: function() {
|
| fakeXhrs = [];
|
| sinon.useFakeXMLHttpRequest().onCreate = function(xhr) {
|
| @@ -59,111 +59,169 @@ module('dns_blackhole_checker', {
|
| onStateChange = null;
|
| onIncomingStanzaCallback = null;
|
| checker = null;
|
| - },
|
| + }
|
| });
|
|
|
| +function await(condition) {
|
| + function loop(count) {
|
| + if (condition()) {
|
| + return Promise.resolve();
|
| + } else if (count > 1000) {
|
| + return Promise.reject('condition never became true');
|
| + } else {
|
| + return Promise.resolve().then(loop.bind(null, count + 1));
|
| + }
|
| + };
|
| + return loop(0);
|
| +};
|
| +
|
| test('success',
|
| function() {
|
| - fakeXhrs[0].respond(200);
|
| - sinon.assert.notCalled(onStateChange);
|
| + function checkState(state) {
|
| + return SpyPromise.run(function() {
|
| + signalStrategy.setStateForTesting(state);
|
| + }).then(function() {
|
| + sinon.assert.calledWith(onStateChange, state);
|
| + equal(checker.getState(), state);
|
| + });
|
| + };
|
|
|
| - [
|
| - remoting.SignalStrategy.State.CONNECTING,
|
| - remoting.SignalStrategy.State.HANDSHAKE,
|
| - remoting.SignalStrategy.State.CONNECTED
|
| - ].forEach(function(state) {
|
| - signalStrategy.setStateForTesting(state);
|
| - sinon.assert.calledWith(onStateChange, state);
|
| - equal(checker.getState(), state);
|
| - });
|
| - }
|
| -);
|
| + return SpyPromise.run(function() {
|
| + fakeXhrs[0].respond(200);
|
| + }).then(function() {
|
| + sinon.assert.notCalled(onStateChange);
|
| + }).
|
| + then(checkState.bind(null, remoting.SignalStrategy.State.CONNECTING)).
|
| + then(checkState.bind(null, remoting.SignalStrategy.State.HANDSHAKE)).
|
| + then(checkState.bind(null, remoting.SignalStrategy.State.CONNECTED));
|
| + });
|
|
|
| test('http response after connected',
|
| function() {
|
| - [
|
| - remoting.SignalStrategy.State.CONNECTING,
|
| - remoting.SignalStrategy.State.HANDSHAKE,
|
| - ].forEach(function(state) {
|
| - signalStrategy.setStateForTesting(state);
|
| - sinon.assert.calledWith(onStateChange, state);
|
| - equal(checker.getState(), state);
|
| + function checkState(state) {
|
| + return SpyPromise.run(function() {
|
| + signalStrategy.setStateForTesting(state);
|
| + }).then(function() {
|
| + sinon.assert.calledWith(onStateChange, state);
|
| + equal(checker.getState(), state);
|
| + });
|
| + }
|
| +
|
| + return Promise.resolve().then(function() {
|
| + return checkState(remoting.SignalStrategy.State.CONNECTING);
|
| + }).then(function() {
|
| + return checkState(remoting.SignalStrategy.State.HANDSHAKE);
|
| + }).then(function() {
|
| + onStateChange.reset();
|
| +
|
| + // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the
|
| + // signal strategy has connected.
|
| + return SpyPromise.run(function() {
|
| + signalStrategy.setStateForTesting(
|
| + remoting.SignalStrategy.State.CONNECTED);
|
| + });
|
| + }).then(function() {
|
| + sinon.assert.notCalled(onStateChange);
|
| + equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE);
|
| +
|
| + // Verify that DnsBlackholeChecker goes to CONNECTED state after the
|
| + // the HTTP request has succeeded.
|
| + return SpyPromise.run(function() {
|
| + fakeXhrs[0].respond(200);
|
| + });
|
| + }).then(function() {
|
| + sinon.assert.calledWith(onStateChange,
|
| + remoting.SignalStrategy.State.CONNECTED);
|
| });
|
| - onStateChange.reset();
|
| -
|
| - // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the
|
| - // signal strategy has connected.
|
| - signalStrategy.setStateForTesting(remoting.SignalStrategy.State.CONNECTED);
|
| - sinon.assert.notCalled(onStateChange);
|
| - equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE);
|
| -
|
| - // Verify that DnsBlackholeChecker goes to CONNECTED state after the
|
| - // the HTTP request has succeeded.
|
| - fakeXhrs[0].respond(200);
|
| - sinon.assert.calledWith(onStateChange,
|
| - remoting.SignalStrategy.State.CONNECTED);
|
| - }
|
| -);
|
| + });
|
|
|
| -test('connect failed',
|
| +QUnit.test('connect failed',
|
| function() {
|
| - fakeXhrs[0].respond(200);
|
| - sinon.assert.notCalled(onStateChange);
|
| -
|
| - [
|
| - remoting.SignalStrategy.State.CONNECTING,
|
| - remoting.SignalStrategy.State.FAILED
|
| - ].forEach(function(state) {
|
| - signalStrategy.setStateForTesting(state);
|
| - sinon.assert.calledWith(onStateChange, state);
|
| - });
|
| -}
|
| -);
|
| + function checkState(state) {
|
| + return SpyPromise.run(function() {
|
| + signalStrategy.setStateForTesting(state);
|
| + }).then(function() {
|
| + sinon.assert.calledWith(onStateChange, state);
|
| + });
|
| + };
|
|
|
| -test('blocked',
|
| - function() {
|
| - fakeXhrs[0].respond(400);
|
| - sinon.assert.calledWith(onStateChange,
|
| - remoting.SignalStrategy.State.FAILED);
|
| - equal(checker.getError().tag, remoting.Error.Tag.NOT_AUTHORIZED);
|
| - onStateChange.reset();
|
| -
|
| - [
|
| - remoting.SignalStrategy.State.CONNECTING,
|
| - remoting.SignalStrategy.State.HANDSHAKE,
|
| - remoting.SignalStrategy.State.CONNECTED
|
| - ].forEach(function(state) {
|
| - signalStrategy.setStateForTesting(state);
|
| + return SpyPromise.run(function() {
|
| + fakeXhrs[0].respond(200);
|
| + }).then(function() {
|
| sinon.assert.notCalled(onStateChange);
|
| - equal(checker.getState(), remoting.SignalStrategy.State.FAILED);
|
| - });
|
| - }
|
| -);
|
| + }).
|
| + then(checkState.bind(null, remoting.SignalStrategy.State.CONNECTING)).
|
| + then(checkState.bind(null, remoting.SignalStrategy.State.FAILED));
|
| + });
|
| +
|
| +QUnit.test('blocked',
|
| + function(assert) {
|
| + function checkState(state) {
|
| + onStateChange.reset();
|
| + return SpyPromise.run(function() {
|
| + signalStrategy.setStateForTesting(state);
|
| + }).then(function() {
|
| + sinon.assert.notCalled(onStateChange);
|
| + assert.equal(
|
| + checker.getState(),
|
| + remoting.SignalStrategy.State.FAILED,
|
| + 'checker state is still FAILED');
|
| + });
|
| + };
|
|
|
| -test('blocked after connected',
|
| + return SpyPromise.run(function() {
|
| + fakeXhrs[0].respond(400);
|
| + }).then(function() {
|
| + sinon.assert.calledWith(
|
| + onStateChange, remoting.SignalStrategy.State.FAILED);
|
| + assert.equal(
|
| + checker.getError().tag,
|
| + remoting.Error.Tag.NOT_AUTHORIZED,
|
| + 'checker error is NOT_AUTHORIZED');
|
| + }).
|
| + then(checkState.bind(null, remoting.SignalStrategy.State.CONNECTING)).
|
| + then(checkState.bind(null, remoting.SignalStrategy.State.HANDSHAKE)).
|
| + then(checkState.bind(null, remoting.SignalStrategy.State.CONNECTED));
|
| + });
|
| +
|
| +QUnit.test('blocked after connected',
|
| function() {
|
| - [
|
| - remoting.SignalStrategy.State.CONNECTING,
|
| - remoting.SignalStrategy.State.HANDSHAKE,
|
| - ].forEach(function(state) {
|
| - signalStrategy.setStateForTesting(state);
|
| - sinon.assert.calledWith(onStateChange, state);
|
| - equal(checker.getState(), state);
|
| - });
|
| - onStateChange.reset();
|
| -
|
| - // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the
|
| - // signal strategy has connected.
|
| - signalStrategy.setStateForTesting(remoting.SignalStrategy.State.CONNECTED);
|
| - sinon.assert.notCalled(onStateChange);
|
| - equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE);
|
| + function checkState(state) {
|
| + return SpyPromise.run(function() {
|
| + signalStrategy.setStateForTesting(state);
|
| + }).then(function() {
|
| + sinon.assert.calledWith(onStateChange, state);
|
| + equal(checker.getState(), state);
|
| + });
|
| + };
|
|
|
| - // Verify that DnsBlackholeChecker goes to FAILED state after it gets the
|
| - // blocked HTTP response.
|
| - fakeXhrs[0].respond(400);
|
| - sinon.assert.calledWith(onStateChange,
|
| - remoting.SignalStrategy.State.FAILED);
|
| - equal(checker.getError().tag, remoting.Error.Tag.NOT_AUTHORIZED);
|
| + return Promise.resolve().then(function() {
|
| + return checkState(remoting.SignalStrategy.State.CONNECTING);
|
| + }).then(function() {
|
| + return checkState(remoting.SignalStrategy.State.HANDSHAKE);
|
| + }).then(function() {
|
| + onStateChange.reset();
|
| +
|
| + // Verify that DnsBlackholeChecker stays in HANDSHAKE state even
|
| + // if the signal strategy has connected.
|
| + return SpyPromise.run(function() {
|
| + signalStrategy.setStateForTesting(
|
| + remoting.SignalStrategy.State.CONNECTED);
|
| + });
|
| + }).then(function() {
|
| + sinon.assert.notCalled(onStateChange);
|
| + equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE);
|
| +
|
| + // Verify that DnsBlackholeChecker goes to FAILED state after it
|
| + // gets the blocked HTTP response.
|
| + return SpyPromise.run(function() {
|
| + fakeXhrs[0].respond(400);
|
| + });
|
| + }).then(function() {
|
| + sinon.assert.calledWith(onStateChange,
|
| + remoting.SignalStrategy.State.FAILED);
|
| + equal(checker.getError().tag, remoting.Error.Tag.NOT_AUTHORIZED);
|
| + });
|
| }
|
| );
|
|
|
|
|