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 remoting.settings = new remoting.Settings();
38 onStateChange = sinon.spy();
39 onIncomingStanzaCallback = sinon.spy();
40 signalStrategy = new remoting.MockSignalStrategy();
41 sinon.stub(signalStrategy, 'connect', base.doNothing);
42 checker = new remoting.DnsBlackholeChecker(signalStrategy);
44 checker.setStateChangedCallback(onStateChange);
45 checker.setIncomingStanzaCallback(onIncomingStanzaCallback);
47 sinon.assert.notCalled(onStateChange);
48 sinon.assert.notCalled(signalStrategy.connect);
49 checker.connect('server', 'username', 'authToken');
50 sinon.assert.calledWith(signalStrategy.connect, 'server', 'username',
54 fakeXhr.url, checker.url_,
55 'the correct URL is requested');
57 afterEach: function() {
58 base.dispose(checker);
59 sinon.assert.calledWith(onStateChange,
60 remoting.SignalStrategy.State.CLOSED);
61 remoting.settings = null;
63 onIncomingStanzaCallback = null;
71 function checkState(state) {
72 signalStrategy.setStateForTesting(state);
73 sinon.assert.calledWith(onStateChange, state);
74 assert.equal(checker.getState(), state);
77 return base.SpyPromise.run(function() {
80 sinon.assert.notCalled(onStateChange);
81 checkState(remoting.SignalStrategy.State.CONNECTING);
82 checkState(remoting.SignalStrategy.State.HANDSHAKE);
83 checkState(remoting.SignalStrategy.State.CONNECTED);
87 QUnit.test('http response after connected',
89 function checkState(state) {
90 signalStrategy.setStateForTesting(state);
91 sinon.assert.calledWith(onStateChange, state);
92 assert.equal(checker.getState(), state);
95 checkState(remoting.SignalStrategy.State.CONNECTING);
96 checkState(remoting.SignalStrategy.State.HANDSHAKE);
97 onStateChange.reset();
99 // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the
100 // signal strategy has connected.
101 return base.SpyPromise.run(function() {
102 signalStrategy.setStateForTesting(
103 remoting.SignalStrategy.State.CONNECTED);
105 sinon.assert.notCalled(onStateChange);
106 assert.equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE);
108 // Verify that DnsBlackholeChecker goes to CONNECTED state after the
109 // the HTTP request has succeeded.
110 return base.SpyPromise.run(function() {
111 fakeXhr.respond(200);
114 sinon.assert.calledWith(onStateChange,
115 remoting.SignalStrategy.State.CONNECTED);
119 QUnit.test('connect failed',
121 function checkState(state) {
122 signalStrategy.setStateForTesting(state);
123 sinon.assert.calledWith(onStateChange, state);
126 return base.SpyPromise.run(function() {
127 fakeXhr.respond(200);
129 sinon.assert.notCalled(onStateChange);
130 checkState(remoting.SignalStrategy.State.CONNECTING);
131 checkState(remoting.SignalStrategy.State.FAILED);
135 QUnit.test('blocked',
137 function checkState(state) {
138 assert.equal(checker.getError().getTag(),
139 remoting.Error.Tag.NOT_AUTHORIZED);
140 onStateChange.reset();
141 signalStrategy.setStateForTesting(state);
142 sinon.assert.notCalled(onStateChange);
143 assert.equal(checker.getState(),
145 remoting.SignalStrategy.State.FAILED,
146 'checker state is still FAILED');
149 return base.SpyPromise.run(function() {
150 fakeXhr.respond(400);
152 sinon.assert.calledWith(
153 onStateChange, remoting.SignalStrategy.State.FAILED);
155 checker.getError().getTag(),
156 remoting.Error.Tag.NOT_AUTHORIZED,
157 'checker error is NOT_AUTHORIZED');
158 checkState(remoting.SignalStrategy.State.CONNECTING);
159 checkState(remoting.SignalStrategy.State.HANDSHAKE);
160 checkState(remoting.SignalStrategy.State.FAILED);
164 QUnit.test('blocked after connected',
166 function checkState(state) {
167 signalStrategy.setStateForTesting(state);
168 sinon.assert.calledWith(onStateChange, state);
169 assert.equal(checker.getState(), state);
172 checkState(remoting.SignalStrategy.State.CONNECTING);
173 checkState(remoting.SignalStrategy.State.HANDSHAKE);
174 onStateChange.reset();
176 // Verify that DnsBlackholeChecker stays in HANDSHAKE state even
177 // if the signal strategy has connected.
178 return base.SpyPromise.run(function() {
179 signalStrategy.setStateForTesting(
180 remoting.SignalStrategy.State.CONNECTED);
182 sinon.assert.notCalled(onStateChange);
183 assert.equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE);
185 // Verify that DnsBlackholeChecker goes to FAILED state after it
186 // gets the blocked HTTP response.
187 return base.SpyPromise.run(function() {
188 fakeXhr.respond(400);
191 sinon.assert.calledWith(onStateChange,
192 remoting.SignalStrategy.State.FAILED);
193 assert.ok(checker.getError().hasTag(remoting.Error.Tag.NOT_AUTHORIZED));