| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview | 6 * @fileoverview |
| 7 * TODO(garykac): Create interface for SignalStrategy. | 7 * TODO(garykac): Create interface for SignalStrategy. |
| 8 * @suppress {checkTypes|checkVars|reportUnknownTypes|visibility} | 8 * @suppress {checkTypes|checkVars|reportUnknownTypes|visibility} |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 (function() { | 11 (function() { |
| 12 | 12 |
| 13 'use strict'; | 13 'use strict'; |
| 14 | 14 |
| 15 /** @type {(sinon.Spy|function(remoting.SignalStrategy.State))} */ | 15 /** @type {(sinon.Spy|function(remoting.SignalStrategy.State))} */ |
| 16 var onStateChange = null; | 16 var onStateChange = null; |
| 17 | 17 |
| 18 /** @type {(sinon.Spy|function(Element):void)} */ | 18 /** @type {(sinon.Spy|function(Element):void)} */ |
| 19 var onIncomingStanzaCallback = null; | 19 var onIncomingStanzaCallback = null; |
| 20 | 20 |
| 21 /** @type {remoting.DnsBlackholeChecker} */ | 21 /** @type {remoting.DnsBlackholeChecker} */ |
| 22 var checker = null; | 22 var checker = null; |
| 23 | 23 |
| 24 /** @type {remoting.MockSignalStrategy} */ | 24 /** @type {remoting.MockSignalStrategy} */ |
| 25 var signalStrategy = null; | 25 var signalStrategy = null; |
| 26 var fakeXhrs; | 26 var fakeXhrs; |
| 27 | 27 |
| 28 module('dns_blackhole_checker', { | 28 QUnit.module('dns_blackhole_checker', { |
| 29 setup: function() { | 29 beforeEach: function() { |
| 30 fakeXhrs = []; | 30 fakeXhrs = []; |
| 31 sinon.useFakeXMLHttpRequest().onCreate = function(xhr) { | 31 sinon.useFakeXMLHttpRequest().onCreate = function(xhr) { |
| 32 fakeXhrs.push(xhr); | 32 fakeXhrs.push(xhr); |
| 33 }; | 33 }; |
| 34 | 34 |
| 35 onStateChange = sinon.spy(); | 35 onStateChange = sinon.spy(); |
| 36 onIncomingStanzaCallback = sinon.spy(); | 36 onIncomingStanzaCallback = sinon.spy(); |
| 37 signalStrategy = new remoting.MockSignalStrategy(); | 37 signalStrategy = new remoting.MockSignalStrategy(); |
| 38 checker = new remoting.DnsBlackholeChecker(signalStrategy); | 38 checker = new remoting.DnsBlackholeChecker(signalStrategy); |
| 39 | 39 |
| 40 checker.setStateChangedCallback(onStateChange); | 40 checker.setStateChangedCallback(onStateChange); |
| 41 checker.setIncomingStanzaCallback(onIncomingStanzaCallback); | 41 checker.setIncomingStanzaCallback(onIncomingStanzaCallback); |
| 42 | 42 |
| 43 sinon.assert.notCalled(onStateChange); | 43 sinon.assert.notCalled(onStateChange); |
| 44 sinon.assert.notCalled(signalStrategy.connect); | 44 sinon.assert.notCalled(signalStrategy.connect); |
| 45 checker.connect('server', 'username', 'authToken'); | 45 checker.connect('server', 'username', 'authToken'); |
| 46 sinon.assert.calledWith(signalStrategy.connect, 'server', 'username', | 46 sinon.assert.calledWith(signalStrategy.connect, 'server', 'username', |
| 47 'authToken'); | 47 'authToken'); |
| 48 | 48 |
| 49 QUnit.equal(fakeXhrs.length, 1, 'exactly one XHR is issued'); | 49 QUnit.equal(fakeXhrs.length, 1, 'exactly one XHR is issued'); |
| 50 QUnit.equal( | 50 QUnit.equal( |
| 51 fakeXhrs[0].url, remoting.DnsBlackholeChecker.URL_TO_REQUEST_, | 51 fakeXhrs[0].url, remoting.DnsBlackholeChecker.URL_TO_REQUEST_, |
| 52 'the correct URL is requested'); | 52 'the correct URL is requested'); |
| 53 }, | 53 }, |
| 54 teardown: function() { | 54 afterEach: function() { |
| 55 base.dispose(checker); | 55 base.dispose(checker); |
| 56 sinon.assert.calledWith(onStateChange, | 56 sinon.assert.calledWith(onStateChange, |
| 57 remoting.SignalStrategy.State.CLOSED); | 57 remoting.SignalStrategy.State.CLOSED); |
| 58 | 58 |
| 59 onStateChange = null; | 59 onStateChange = null; |
| 60 onIncomingStanzaCallback = null; | 60 onIncomingStanzaCallback = null; |
| 61 checker = null; | 61 checker = null; |
| 62 }, | 62 }, |
| 63 }); | 63 }); |
| 64 | 64 |
| 65 test('success', | 65 QUnit.test('success', |
| 66 function() { | 66 function() { |
| 67 fakeXhrs[0].respond(200); | 67 fakeXhrs[0].respond(200); |
| 68 sinon.assert.notCalled(onStateChange); | 68 sinon.assert.notCalled(onStateChange); |
| 69 | 69 |
| 70 [ | 70 [ |
| 71 remoting.SignalStrategy.State.CONNECTING, | 71 remoting.SignalStrategy.State.CONNECTING, |
| 72 remoting.SignalStrategy.State.HANDSHAKE, | 72 remoting.SignalStrategy.State.HANDSHAKE, |
| 73 remoting.SignalStrategy.State.CONNECTED | 73 remoting.SignalStrategy.State.CONNECTED |
| 74 ].forEach(function(state) { | 74 ].forEach(function(state) { |
| 75 signalStrategy.setStateForTesting(state); | 75 signalStrategy.setStateForTesting(state); |
| 76 sinon.assert.calledWith(onStateChange, state); | 76 sinon.assert.calledWith(onStateChange, state); |
| 77 equal(checker.getState(), state); | 77 QUnit.equal(checker.getState(), state); |
| 78 }); | 78 }); |
| 79 } | 79 } |
| 80 ); | 80 ); |
| 81 | 81 |
| 82 test('http response after connected', | 82 QUnit.test('http response after connected', |
| 83 function() { | 83 function() { |
| 84 [ | 84 [ |
| 85 remoting.SignalStrategy.State.CONNECTING, | 85 remoting.SignalStrategy.State.CONNECTING, |
| 86 remoting.SignalStrategy.State.HANDSHAKE, | 86 remoting.SignalStrategy.State.HANDSHAKE, |
| 87 ].forEach(function(state) { | 87 ].forEach(function(state) { |
| 88 signalStrategy.setStateForTesting(state); | 88 signalStrategy.setStateForTesting(state); |
| 89 sinon.assert.calledWith(onStateChange, state); | 89 sinon.assert.calledWith(onStateChange, state); |
| 90 equal(checker.getState(), state); | 90 QUnit.equal(checker.getState(), state); |
| 91 }); | 91 }); |
| 92 onStateChange.reset(); | 92 onStateChange.reset(); |
| 93 | 93 |
| 94 // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the | 94 // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the |
| 95 // signal strategy has connected. | 95 // signal strategy has connected. |
| 96 signalStrategy.setStateForTesting(remoting.SignalStrategy.State.CONNECTED); | 96 signalStrategy.setStateForTesting(remoting.SignalStrategy.State.CONNECTED); |
| 97 sinon.assert.notCalled(onStateChange); | 97 sinon.assert.notCalled(onStateChange); |
| 98 equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE); | 98 QUnit.equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE); |
| 99 | 99 |
| 100 // Verify that DnsBlackholeChecker goes to CONNECTED state after the | 100 // Verify that DnsBlackholeChecker goes to CONNECTED state after the |
| 101 // the HTTP request has succeeded. | 101 // the HTTP request has succeeded. |
| 102 fakeXhrs[0].respond(200); | 102 fakeXhrs[0].respond(200); |
| 103 sinon.assert.calledWith(onStateChange, | 103 sinon.assert.calledWith(onStateChange, |
| 104 remoting.SignalStrategy.State.CONNECTED); | 104 remoting.SignalStrategy.State.CONNECTED); |
| 105 } | 105 } |
| 106 ); | 106 ); |
| 107 | 107 |
| 108 test('connect failed', | 108 QUnit.test('connect failed', |
| 109 function() { | 109 function() { |
| 110 fakeXhrs[0].respond(200); | 110 fakeXhrs[0].respond(200); |
| 111 sinon.assert.notCalled(onStateChange); | 111 sinon.assert.notCalled(onStateChange); |
| 112 | 112 |
| 113 [ | 113 [ |
| 114 remoting.SignalStrategy.State.CONNECTING, | 114 remoting.SignalStrategy.State.CONNECTING, |
| 115 remoting.SignalStrategy.State.FAILED | 115 remoting.SignalStrategy.State.FAILED |
| 116 ].forEach(function(state) { | 116 ].forEach(function(state) { |
| 117 signalStrategy.setStateForTesting(state); | 117 signalStrategy.setStateForTesting(state); |
| 118 sinon.assert.calledWith(onStateChange, state); | 118 sinon.assert.calledWith(onStateChange, state); |
| 119 }); | 119 }); |
| 120 } | 120 } |
| 121 ); | 121 ); |
| 122 | 122 |
| 123 test('blocked', | 123 QUnit.test('blocked', |
| 124 function() { | 124 function() { |
| 125 fakeXhrs[0].respond(400); | 125 fakeXhrs[0].respond(400); |
| 126 sinon.assert.calledWith(onStateChange, | 126 sinon.assert.calledWith(onStateChange, |
| 127 remoting.SignalStrategy.State.FAILED); | 127 remoting.SignalStrategy.State.FAILED); |
| 128 equal(checker.getError().getTag(), remoting.Error.Tag.NOT_AUTHORIZED); | 128 QUnit.equal(checker.getError().getTag(), remoting.Error.Tag.NOT_AUTHORIZED); |
| 129 onStateChange.reset(); | 129 onStateChange.reset(); |
| 130 | 130 |
| 131 [ | 131 [ |
| 132 remoting.SignalStrategy.State.CONNECTING, | 132 remoting.SignalStrategy.State.CONNECTING, |
| 133 remoting.SignalStrategy.State.HANDSHAKE, | 133 remoting.SignalStrategy.State.HANDSHAKE, |
| 134 remoting.SignalStrategy.State.CONNECTED | 134 remoting.SignalStrategy.State.CONNECTED |
| 135 ].forEach(function(state) { | 135 ].forEach(function(state) { |
| 136 signalStrategy.setStateForTesting(state); | 136 signalStrategy.setStateForTesting(state); |
| 137 sinon.assert.notCalled(onStateChange); | 137 sinon.assert.notCalled(onStateChange); |
| 138 equal(checker.getState(), remoting.SignalStrategy.State.FAILED); | 138 QUnit.equal(checker.getState(), remoting.SignalStrategy.State.FAILED); |
| 139 }); | 139 }); |
| 140 } | 140 } |
| 141 ); | 141 ); |
| 142 | 142 |
| 143 test('blocked after connected', | 143 QUnit.test('blocked after connected', |
| 144 function() { | 144 function() { |
| 145 [ | 145 [ |
| 146 remoting.SignalStrategy.State.CONNECTING, | 146 remoting.SignalStrategy.State.CONNECTING, |
| 147 remoting.SignalStrategy.State.HANDSHAKE, | 147 remoting.SignalStrategy.State.HANDSHAKE, |
| 148 ].forEach(function(state) { | 148 ].forEach(function(state) { |
| 149 signalStrategy.setStateForTesting(state); | 149 signalStrategy.setStateForTesting(state); |
| 150 sinon.assert.calledWith(onStateChange, state); | 150 sinon.assert.calledWith(onStateChange, state); |
| 151 equal(checker.getState(), state); | 151 QUnit.equal(checker.getState(), state); |
| 152 }); | 152 }); |
| 153 onStateChange.reset(); | 153 onStateChange.reset(); |
| 154 | 154 |
| 155 // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the | 155 // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the |
| 156 // signal strategy has connected. | 156 // signal strategy has connected. |
| 157 signalStrategy.setStateForTesting(remoting.SignalStrategy.State.CONNECTED); | 157 signalStrategy.setStateForTesting(remoting.SignalStrategy.State.CONNECTED); |
| 158 sinon.assert.notCalled(onStateChange); | 158 sinon.assert.notCalled(onStateChange); |
| 159 equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE); | 159 QUnit.equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE); |
| 160 | 160 |
| 161 // Verify that DnsBlackholeChecker goes to FAILED state after it gets the | 161 // Verify that DnsBlackholeChecker goes to FAILED state after it gets the |
| 162 // blocked HTTP response. | 162 // blocked HTTP response. |
| 163 fakeXhrs[0].respond(400); | 163 fakeXhrs[0].respond(400); |
| 164 sinon.assert.calledWith(onStateChange, | 164 sinon.assert.calledWith(onStateChange, |
| 165 remoting.SignalStrategy.State.FAILED); | 165 remoting.SignalStrategy.State.FAILED); |
| 166 ok(checker.getError().hasTag(remoting.Error.Tag.NOT_AUTHORIZED)); | 166 QUnit.ok(checker.getError().hasTag(remoting.Error.Tag.NOT_AUTHORIZED)); |
| 167 } | 167 } |
| 168 ); | 168 ); |
| 169 | 169 |
| 170 })(); | 170 })(); |
| OLD | NEW |