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.
9 /** @type {remoting.XhrEventWriter} */
10 var eventWriter = null;
11 /** @type {!chromeMocks.StorageArea} */
13 /** @type {sinon.TestStub} */
17 * Flush the writer and ensure no outgoing requests is made.
19 * @param {QUnit.Assert} assert
20 * @param {remoting.XhrEventWriter} writer
23 function flushAndEnsureNoRequests(assert, writer) {
24 var requestReceived = false;
25 remoting.MockXhr.setResponseFor(
26 'POST', 'fake_url', function(/** remoting.MockXhr */ xhr) {
27 xhr.setTextResponse(200, '');
28 requestReceived = true;
30 return writer.flush().then(function(){
31 assert.ok(!requestReceived);
36 * Flush the writer and ensure no outgoing requests is made.
38 * @param {QUnit.Assert} assert
39 * @param {Array<string>} requestIds
40 * @return {Map<string, boolean>}
42 function expectRequests(assert, requestIds) {
43 var expected = new Map();
44 requestIds.forEach(function(/** string */ id){
45 expected.set(id, true);
48 remoting.MockXhr.setResponseFor(
49 'POST', 'fake_url', function(/** remoting.MockXhr */ xhr) {
50 var jsonContent = /** @type {Object} */ (xhr.params.jsonContent);
51 var requests = /** @type {Array} */ (jsonContent['event']);
52 requests.forEach(function(/** Object */ request){
53 var id = /** @type {string} */ (request['id']);
54 assert.ok(expected.has(id), 'Unexpected request :=' + id);
57 xhr.setTextResponse(200, '');
63 QUnit.module('XhrEventWriter', {
64 beforeEach: function() {
65 remoting.MockXhr.activate();
66 mockStorage = new chromeMocks.StorageArea();
67 eventWriter = new remoting.XhrEventWriter(
68 'fake_url', mockStorage, 'fake-storage-key');
69 isOnlineStub = sinon.stub(base, 'isOnline');
71 afterEach: function() {
72 isOnlineStub.restore();
73 remoting.MockXhr.restore();
77 QUnit.test('loadPendingRequests() handles empty storage.', function(assert){
78 return eventWriter.loadPendingRequests().then(function(){
79 return flushAndEnsureNoRequests(assert, eventWriter);
83 QUnit.test('loadPendingRequests() handles corrupted data.', function(assert){
84 var storage = mockStorage.mock$getStorage();
85 storage['fake-storage-key'] = 'corrupted_data';
86 return eventWriter.loadPendingRequests().then(function(){
87 return flushAndEnsureNoRequests(assert, eventWriter);
91 QUnit.test('write() should post XHR to server.', function(assert){
92 isOnlineStub.returns(true);
93 var outstanding = expectRequests(assert, ['1']);
94 return eventWriter.write({ id: '1'}).then(function(){
95 assert.equal(outstanding.size, 0, outstanding.toString());
99 QUnit.test('flush() should retry requests if OFFLINE.', function(assert){
100 isOnlineStub.returns(false);
101 /** @type {Map<string, boolean>} */
102 var outstanding = null;
104 return eventWriter.write({ id: '1'}).then(function(){
105 assert.ok(false, 'Expect to fail.');
106 }).catch(function() {
107 isOnlineStub.returns(true);
108 outstanding = expectRequests(assert, ['1']);
109 return eventWriter.flush();
111 assert.equal(outstanding.size, 0, outstanding.toString());
115 QUnit.test('flush() should handle batch requests.', function(assert){
116 /** @type {Map<string, boolean>} */
117 var outstanding = null;
118 isOnlineStub.returns(false);
121 eventWriter.write({id: '1'}),
122 eventWriter.write({id: '2'}),
123 eventWriter.write({id: '3'})
125 assert.ok(false, 'Expect to fail.');
126 }).catch(function() {
127 isOnlineStub.returns(true);
128 outstanding = expectRequests(assert, ['1', '2', '3']);
129 return eventWriter.flush();
131 assert.equal(outstanding.size, 0, outstanding.toString());
135 QUnit.test('flush() should not send duplicate requests.', function(assert){
136 isOnlineStub.returns(true);
137 var outstanding = expectRequests(assert, ['1', '2', '3', '4', '5']);
140 eventWriter.write({ id: '1'}),
141 eventWriter.write({ id: '2'}),
142 eventWriter.write({ id: '3'}),
143 eventWriter.write({ id: '4'}),
144 eventWriter.write({ id: '5'})
146 assert.equal(outstanding.size, 0, outstanding.toString());
150 QUnit.test('flush() should not retry on server error.', function(assert){
151 isOnlineStub.returns(true);
152 remoting.MockXhr.setResponseFor(
153 'POST', 'fake_url', function(/** remoting.MockXhr */ xhr) {
154 assert.deepEqual(xhr.params.jsonContent, {event: [{hello: 'world'}]});
155 xhr.setTextResponse(500, '');
158 return eventWriter.write({ hello: 'world'}).then(function(){
159 assert.ok(false, 'Expect to fail.');
160 }).catch(function() {
161 return flushAndEnsureNoRequests(assert, eventWriter);
165 QUnit.test('writeToStorage() should save pending requests.', function(assert){
166 var requestReceived = false;
167 var newEventWriter = new remoting.XhrEventWriter(
168 'fake_url', mockStorage, 'fake-storage-key');
169 isOnlineStub.returns(false);
171 return eventWriter.write({ hello: 'world'}).then(function(){
172 assert.ok(false, 'Expect to fail.');
174 return eventWriter.writeToStorage();
176 return newEventWriter.loadPendingRequests();
178 isOnlineStub.returns(true);
179 remoting.MockXhr.setResponseFor(
180 'POST', 'fake_url', function(/** remoting.MockXhr */ xhr) {
181 assert.deepEqual(xhr.params.jsonContent, {event: [{hello: 'world'}]});
182 requestReceived = true;
183 xhr.setTextResponse(200, '');
185 return newEventWriter.flush();
187 assert.ok(requestReceived);