Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / remoting / webapp / unittests / dns_blackhole_checker_unittest.js
blobd6e7d224c5f92428a5b3c849c1cd609389585a04
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 (function() {
7 'use strict';
9 var onStateChange = null;
10 var onIncomingStanzaCallback = null;
11 var checker = null;
12 var signalStrategy = null;
13 var fakeXhrs;
15 module('dns_blackhole_checker', {
16   setup: function() {
17     fakeXhrs = [];
18     sinon.useFakeXMLHttpRequest().onCreate = function(xhr) {
19       fakeXhrs.push(xhr);
20     };
23     onStateChange = sinon.spy();
24     onIncomingStanzaCallback = sinon.spy();
25     signalStrategy = new remoting.MockSignalStrategy();
26     checker = new remoting.DnsBlackholeChecker(signalStrategy);
28     checker.setStateChangedCallback(onStateChange);
29     checker.setIncomingStanzaCallback(onIncomingStanzaCallback);
31     sinon.assert.notCalled(onStateChange);
32     sinon.assert.notCalled(signalStrategy.connect);
33     checker.connect('server', 'username', 'authToken');
34     sinon.assert.calledWith(signalStrategy.connect, 'server', 'username',
35                             'authToken');
37     QUnit.equal(fakeXhrs.length, 1, 'exactly one XHR is issued');
38     QUnit.equal(
39         fakeXhrs[0].url, remoting.DnsBlackholeChecker.URL_TO_REQUEST_,
40         'the correct URL is requested');
41   },
42   teardown: function() {
43     base.dispose(checker);
44     sinon.assert.calledWith(onStateChange,
45                             remoting.SignalStrategy.State.CLOSED);
47     onStateChange = null;
48     onIncomingStanzaCallback = null;
49     checker = null;
50   },
51 });
53 test('success',
54   function() {
55     fakeXhrs[0].respond(200);
56     sinon.assert.notCalled(onStateChange);
58     [
59       remoting.SignalStrategy.State.CONNECTING,
60       remoting.SignalStrategy.State.HANDSHAKE,
61       remoting.SignalStrategy.State.CONNECTED
62     ].forEach(function(state) {
63       signalStrategy.setStateForTesting(state);
64       sinon.assert.calledWith(onStateChange, state);
65       equal(checker.getState(), state);
66     });
67   }
70 test('http response after connected',
71   function() {
72     [
73       remoting.SignalStrategy.State.CONNECTING,
74       remoting.SignalStrategy.State.HANDSHAKE,
75     ].forEach(function(state) {
76       signalStrategy.setStateForTesting(state);
77       sinon.assert.calledWith(onStateChange, state);
78       equal(checker.getState(), state);
79     });
80     onStateChange.reset();
82     // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the
83     // signal strategy has connected.
84     signalStrategy.setStateForTesting(remoting.SignalStrategy.State.CONNECTED);
85     sinon.assert.notCalled(onStateChange);
86     equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE);
88     // Verify that DnsBlackholeChecker goes to CONNECTED state after the
89     // the HTTP request has succeeded.
90     fakeXhrs[0].respond(200);
91     sinon.assert.calledWith(onStateChange,
92                             remoting.SignalStrategy.State.CONNECTED);
93   }
96 test('connect failed',
97   function() {
98     fakeXhrs[0].respond(200);
99     sinon.assert.notCalled(onStateChange);
101     [
102       remoting.SignalStrategy.State.CONNECTING,
103       remoting.SignalStrategy.State.FAILED
104     ].forEach(function(state) {
105       signalStrategy.setStateForTesting(state);
106       sinon.assert.calledWith(onStateChange, state);
107     });
111 test('blocked',
112   function() {
113     fakeXhrs[0].respond(400);
114     sinon.assert.calledWith(onStateChange,
115                             remoting.SignalStrategy.State.FAILED);
116     equal(checker.getError(), remoting.Error.NOT_AUTHORIZED);
117     onStateChange.reset();
119     [
120       remoting.SignalStrategy.State.CONNECTING,
121       remoting.SignalStrategy.State.HANDSHAKE,
122       remoting.SignalStrategy.State.CONNECTED
123     ].forEach(function(state) {
124       signalStrategy.setStateForTesting(state);
125       sinon.assert.notCalled(onStateChange);
126       equal(checker.getState(), remoting.SignalStrategy.State.FAILED);
127     });
128   }
131 test('blocked after connected',
132   function() {
133     [
134       remoting.SignalStrategy.State.CONNECTING,
135       remoting.SignalStrategy.State.HANDSHAKE,
136     ].forEach(function(state) {
137       signalStrategy.setStateForTesting(state);
138       sinon.assert.calledWith(onStateChange, state);
139       equal(checker.getState(), state);
140     });
141     onStateChange.reset();
143     // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the
144     // signal strategy has connected.
145     signalStrategy.setStateForTesting(remoting.SignalStrategy.State.CONNECTED);
146     sinon.assert.notCalled(onStateChange);
147     equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE);
149     // Verify that DnsBlackholeChecker goes to FAILED state after it gets the
150     // blocked HTTP response.
151     fakeXhrs[0].respond(400);
152     sinon.assert.calledWith(onStateChange,
153                             remoting.SignalStrategy.State.FAILED);
154     equal(checker.getError(), remoting.Error.NOT_AUTHORIZED);
155   }
158 })();