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
3 // found in the LICENSE file.
7 * TODO(garykac): Create interface for SignalStrategy.
8 * @suppress {checkTypes|checkVars|reportUnknownTypes|visibility}
15 /** @type {(sinon.Spy|function(remoting.SignalStrategy.State))} */
16 var onStateChange
= null;
18 /** @type {(sinon.Spy|function(Element):void)} */
19 var onIncomingStanzaCallback
= null;
21 /** @type {remoting.DnsBlackholeChecker} */
24 /** @type {remoting.MockSignalStrategy} */
25 var signalStrategy
= null;
27 /** @type {sinon.FakeXhr} */
30 QUnit
.module('dns_blackhole_checker', {
31 beforeEach: function(assert
) {
32 sinon
.useFakeXMLHttpRequest().onCreate = function(xhr
) {
33 QUnit
.equal(fakeXhr
, null, 'exactly one XHR is issued');
37 onStateChange
= sinon
.spy();
38 onIncomingStanzaCallback
= sinon
.spy();
39 signalStrategy
= new remoting
.MockSignalStrategy();
40 sinon
.stub(signalStrategy
, 'connect', base
.doNothing
);
41 checker
= new remoting
.DnsBlackholeChecker(signalStrategy
);
43 checker
.setStateChangedCallback(onStateChange
);
44 checker
.setIncomingStanzaCallback(onIncomingStanzaCallback
);
46 sinon
.assert
.notCalled(onStateChange
);
47 sinon
.assert
.notCalled(signalStrategy
.connect
);
48 checker
.connect('server', 'username', 'authToken');
49 sinon
.assert
.calledWith(signalStrategy
.connect
, 'server', 'username',
53 fakeXhr
.url
, remoting
.DnsBlackholeChecker
.URL_TO_REQUEST_
,
54 'the correct URL is requested');
56 afterEach: function() {
57 base
.dispose(checker
);
58 sinon
.assert
.calledWith(onStateChange
,
59 remoting
.SignalStrategy
.State
.CLOSED
);
62 onIncomingStanzaCallback
= null;
70 function checkState(state
) {
71 signalStrategy
.setStateForTesting(state
);
72 sinon
.assert
.calledWith(onStateChange
, state
);
73 assert
.equal(checker
.getState(), state
);
76 return base
.SpyPromise
.run(function() {
79 sinon
.assert
.notCalled(onStateChange
);
80 checkState(remoting
.SignalStrategy
.State
.CONNECTING
);
81 checkState(remoting
.SignalStrategy
.State
.HANDSHAKE
);
82 checkState(remoting
.SignalStrategy
.State
.CONNECTED
);
86 QUnit
.test('http response after connected',
88 function checkState(state
) {
89 signalStrategy
.setStateForTesting(state
);
90 sinon
.assert
.calledWith(onStateChange
, state
);
91 assert
.equal(checker
.getState(), state
);
94 checkState(remoting
.SignalStrategy
.State
.CONNECTING
);
95 checkState(remoting
.SignalStrategy
.State
.HANDSHAKE
);
96 onStateChange
.reset();
98 // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the
99 // signal strategy has connected.
100 return base
.SpyPromise
.run(function() {
101 signalStrategy
.setStateForTesting(
102 remoting
.SignalStrategy
.State
.CONNECTED
);
104 sinon
.assert
.notCalled(onStateChange
);
105 assert
.equal(checker
.getState(), remoting
.SignalStrategy
.State
.HANDSHAKE
);
107 // Verify that DnsBlackholeChecker goes to CONNECTED state after the
108 // the HTTP request has succeeded.
109 return base
.SpyPromise
.run(function() {
110 fakeXhr
.respond(200);
113 sinon
.assert
.calledWith(onStateChange
,
114 remoting
.SignalStrategy
.State
.CONNECTED
);
118 QUnit
.test('connect failed',
120 function checkState(state
) {
121 signalStrategy
.setStateForTesting(state
);
122 sinon
.assert
.calledWith(onStateChange
, state
);
125 return base
.SpyPromise
.run(function() {
126 fakeXhr
.respond(200);
128 sinon
.assert
.notCalled(onStateChange
);
129 checkState(remoting
.SignalStrategy
.State
.CONNECTING
);
130 checkState(remoting
.SignalStrategy
.State
.FAILED
);
134 QUnit
.test('blocked',
136 function checkState(state
) {
137 assert
.equal(checker
.getError().getTag(),
138 remoting
.Error
.Tag
.NOT_AUTHORIZED
);
139 onStateChange
.reset();
140 signalStrategy
.setStateForTesting(state
);
141 sinon
.assert
.notCalled(onStateChange
);
142 assert
.equal(checker
.getState(),
144 remoting
.SignalStrategy
.State
.FAILED
,
145 'checker state is still FAILED');
148 return base
.SpyPromise
.run(function() {
149 fakeXhr
.respond(400);
151 sinon
.assert
.calledWith(
152 onStateChange
, remoting
.SignalStrategy
.State
.FAILED
);
154 checker
.getError().getTag(),
155 remoting
.Error
.Tag
.NOT_AUTHORIZED
,
156 'checker error is NOT_AUTHORIZED');
157 checkState(remoting
.SignalStrategy
.State
.CONNECTING
);
158 checkState(remoting
.SignalStrategy
.State
.HANDSHAKE
);
159 checkState(remoting
.SignalStrategy
.State
.FAILED
);
163 QUnit
.test('blocked after connected',
165 function checkState(state
) {
166 signalStrategy
.setStateForTesting(state
);
167 sinon
.assert
.calledWith(onStateChange
, state
);
168 assert
.equal(checker
.getState(), state
);
171 checkState(remoting
.SignalStrategy
.State
.CONNECTING
);
172 checkState(remoting
.SignalStrategy
.State
.HANDSHAKE
);
173 onStateChange
.reset();
175 // Verify that DnsBlackholeChecker stays in HANDSHAKE state even
176 // if the signal strategy has connected.
177 return base
.SpyPromise
.run(function() {
178 signalStrategy
.setStateForTesting(
179 remoting
.SignalStrategy
.State
.CONNECTED
);
181 sinon
.assert
.notCalled(onStateChange
);
182 assert
.equal(checker
.getState(), remoting
.SignalStrategy
.State
.HANDSHAKE
);
184 // Verify that DnsBlackholeChecker goes to FAILED state after it
185 // gets the blocked HTTP response.
186 return base
.SpyPromise
.run(function() {
187 fakeXhr
.respond(400);
190 sinon
.assert
.calledWith(onStateChange
,
191 remoting
.SignalStrategy
.State
.FAILED
);
192 assert
.ok(checker
.getError().hasTag(remoting
.Error
.Tag
.NOT_AUTHORIZED
));