Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / webapp / base / js / dns_blackhole_checker_unittest.js
blobfc6ce8f025b88d574e79cf441fe7c2c35319c4f5
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.
5 /**
6  * @fileoverview
7  * TODO(garykac): Create interface for SignalStrategy.
8  * @suppress {checkTypes|checkVars|reportUnknownTypes|visibility}
9  */
11 (function() {
13 'use strict';
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} */
22 var checker = null;
24 /** @type {remoting.MockSignalStrategy} */
25 var signalStrategy = null;
27 /** @type {sinon.FakeXhr} */
28 var fakeXhr = null;
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');
34       fakeXhr = xhr;
35     };
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',
51                             'authToken');
53     assert.equal(
54         fakeXhr.url, checker.url_,
55         'the correct URL is requested');
56   },
57   afterEach: function() {
58     base.dispose(checker);
59     sinon.assert.calledWith(onStateChange,
60                             remoting.SignalStrategy.State.CLOSED);
61     remoting.settings = null;
62     onStateChange = null;
63     onIncomingStanzaCallback = null;
64     checker = null;
65     fakeXhr = null;
66   }
67 });
69 QUnit.test('success',
70   function(assert) {
71     function checkState(state) {
72       signalStrategy.setStateForTesting(state);
73       sinon.assert.calledWith(onStateChange, state);
74       assert.equal(checker.getState(), state);
75     }
77     return base.SpyPromise.run(function() {
78       fakeXhr.respond(200);
79     }).then(function() {
80       sinon.assert.notCalled(onStateChange);
81       checkState(remoting.SignalStrategy.State.CONNECTING);
82       checkState(remoting.SignalStrategy.State.HANDSHAKE);
83       checkState(remoting.SignalStrategy.State.CONNECTED);
84     });
85   });
87 QUnit.test('http response after connected',
88   function(assert) {
89     function checkState(state) {
90       signalStrategy.setStateForTesting(state);
91       sinon.assert.calledWith(onStateChange, state);
92       assert.equal(checker.getState(), state);
93     }
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);
104     }).then(function() {
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);
112       });
113     }).then(function() {
114       sinon.assert.calledWith(onStateChange,
115                               remoting.SignalStrategy.State.CONNECTED);
116     });
117   });
119 QUnit.test('connect failed',
120   function(assert) {
121     function checkState(state) {
122       signalStrategy.setStateForTesting(state);
123       sinon.assert.calledWith(onStateChange, state);
124     };
126     return base.SpyPromise.run(function() {
127       fakeXhr.respond(200);
128     }).then(function() {
129       sinon.assert.notCalled(onStateChange);
130       checkState(remoting.SignalStrategy.State.CONNECTING);
131       checkState(remoting.SignalStrategy.State.FAILED);
132     });
133   });
135 QUnit.test('blocked',
136   function(assert) {
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(),
144           checker.getState(),
145           remoting.SignalStrategy.State.FAILED,
146           'checker state is still FAILED');
147     };
149     return base.SpyPromise.run(function() {
150       fakeXhr.respond(400);
151     }).then(function() {
152       sinon.assert.calledWith(
153           onStateChange, remoting.SignalStrategy.State.FAILED);
154       assert.equal(
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);
161     });
162   });
164 QUnit.test('blocked after connected',
165   function(assert) {
166     function checkState(state) {
167       signalStrategy.setStateForTesting(state);
168       sinon.assert.calledWith(onStateChange, state);
169       assert.equal(checker.getState(), state);
170     };
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);
181     }).then(function() {
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);
189       });
190     }).then(function() {
191       sinon.assert.calledWith(onStateChange,
192                               remoting.SignalStrategy.State.FAILED);
193     assert.ok(checker.getError().hasTag(remoting.Error.Tag.NOT_AUTHORIZED));
194     });
195   }
198 })();