Debian Codegraph @

closure-library/closure/goog/net/bulkloader_test.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.net.BulkLoaderTest');
16 goog.setTestOnly('goog.net.BulkLoaderTest');
17
18 goog.require('goog.events.Event');
19 goog.require('goog.events.EventHandler');
20 goog.require('goog.net.BulkLoader');
21 goog.require('goog.net.EventType');
22 goog.require('goog.testing.MockClock');
23 goog.require('goog.testing.jsunit');
24
25
26 /**
27 * Test interval between sending uri requests to the server.
28 */
29 var DELAY_INTERVAL_BETWEEN_URI_REQUESTS = 5;
30
31
32 /**
33 * Test interval before a response is received for a URI request.
34 */
35 var DELAY_INTERVAL_FOR_URI_LOAD = 15;
36
37 var clock;
38 var loadSuccess, loadError;
39 var successResponseTexts;
40
41 function setUpPage() {
42 clock = new goog.testing.MockClock(true);
43 }
44
45 function tearDownPage() {
46 clock.dispose();
47 }
48
49 function setUp() {
50 loadSuccess = false;
51 loadError = false;
52 successResponseTexts = [];
53 }
54
55
56 /**
57 * Gets the successful bulkloader for the specified uris with some
58 * modifications for testability.
59 * <ul>
60 * <li> Added onSuccess methods to simulate success while loading uris.
61 * <li> The send function of the XhrManager used by the bulkloader
62 * calls the onSuccess function after a specified time interval.
63 * </ul>
64 * @param {Array<string>} uris The URIs.
65 */
66 function getSuccessfulBulkLoader(uris) {
67 var bulkLoader = new goog.net.BulkLoader(uris);
68 bulkLoader.load = function() {
69 var uris = this.helper_.getUris();
70 for (var i = 0; i < uris.length; i++) {
71 // This clock tick simulates a delay for processing every URI.
72 clock.tick(DELAY_INTERVAL_BETWEEN_URI_REQUESTS);
73 // This timeout determines how many ticks after the send request
74 // all the URIs will complete loading. This delays the load of
75 // the first uri and every subsequent uri by 15 ticks.
76 setTimeout(
77 goog.bind(this.onSuccess, this, i, uris[i]),
78 DELAY_INTERVAL_FOR_URI_LOAD);
79 }
80 };
81
82 bulkLoader.onSuccess = function(id, uri) {
83 var xhrIo = {
84 getResponseText: function() { return uri; },
85 isSuccess: function() { return true; },
86 dispose: function() {}
87 };
88 this.handleEvent_(
89 id, new goog.events.Event(goog.net.EventType.COMPLETE, xhrIo));
90 };
91
92 var eventHandler = new goog.events.EventHandler();
93 eventHandler.listen(bulkLoader, goog.net.EventType.SUCCESS, handleSuccess);
94 eventHandler.listen(bulkLoader, goog.net.EventType.ERROR, handleError);
95
96 return bulkLoader;
97 }
98
99
100 /**
101 * Gets the non-successful bulkloader for the specified uris with some
102 * modifications for testability.
103 * <ul>
104 * <li> Added onSuccess and onError methods to simulate success and error
105 * while loading uris.
106 * <li> The send function of the XhrManager used by the bulkloader
107 * calls the onSuccess or onError function after a specified time
108 * interval.
109 * </ul>
110 * @param {Array<string>} uris The URIs.
111 */
112 function getNonSuccessfulBulkLoader(uris) {
113 var bulkLoader = new goog.net.BulkLoader(uris);
114 bulkLoader.load = function() {
115 var uris = this.helper_.getUris();
116 for (var i = 0; i < uris.length; i++) {
117 // This clock tick simulates a delay for processing every URI.
118 clock.tick(DELAY_INTERVAL_BETWEEN_URI_REQUESTS);
119
120 // This timeout determines how many ticks after the send request
121 // all the URIs will complete loading in error. This delays the load
122 // of the first uri and every subsequent uri by 15 ticks. The URI
123 // with id == 2 is in error.
124 if (i != 2) {
125 setTimeout(
126 goog.bind(this.onSuccess, this, i, uris[i]),
127 DELAY_INTERVAL_FOR_URI_LOAD);
128 } else {
129 setTimeout(
130 goog.bind(this.onError, this, i, uris[i]),
131 DELAY_INTERVAL_FOR_URI_LOAD);
132 }
133 }
134 };
135
136 bulkLoader.onSuccess = function(id, uri) {
137 var xhrIo = {
138 getResponseText: function() { return uri; },
139 isSuccess: function() { return true; },
140 dispose: function() {}
141 };
142 this.handleEvent_(
143 id, new goog.events.Event(goog.net.EventType.COMPLETE, xhrIo));
144 };
145
146 bulkLoader.onError = function(id) {
147 var xhrIo = {
148 getResponseText: function() { return null; },
149 isSuccess: function() { return false; },
150 dispose: function() {}
151 };
152 this.handleEvent_(
153 id, new goog.events.Event(goog.net.EventType.ERROR, xhrIo));
154 };
155
156 var eventHandler = new goog.events.EventHandler();
157 eventHandler.listen(bulkLoader, goog.net.EventType.SUCCESS, handleSuccess);
158 eventHandler.listen(bulkLoader, goog.net.EventType.ERROR, handleError);
159
160 return bulkLoader;
161 }
162
163 function handleSuccess(e) {
164 loadSuccess = true;
165 successResponseTexts = e.target.getResponseTexts();
166 }
167
168 function handleError(e) {
169 loadError = true;
170 }
171
172
173 /**
174 * Test successful loading of URIs using the bulkloader.
175 */
176 function testBulkLoaderLoadSuccess() {
177 var uris = ['a', 'b', 'c'];
178 var bulkLoader = getSuccessfulBulkLoader(uris);
179 assertArrayEquals(uris, bulkLoader.getRequestUris());
180
181 bulkLoader.load();
182
183 clock.tick(2);
184 assertFalse('The bulk loader is not yet loaded (after 2 ticks)', loadSuccess);
185
186 clock.tick(3);
187 assertFalse('The bulk loader is not yet loaded (after 5 ticks)', loadSuccess);
188
189 clock.tick(5);
190 assertFalse(
191 'The bulk loader is not yet loaded (after 10 ticks)', loadSuccess);
192
193 clock.tick(5);
194 assertTrue('The bulk loader is loaded (after 15 ticks)', loadSuccess);
195
196 assertArrayEquals(
197 'Ensure that the response texts are present', successResponseTexts, uris);
198 }
199
200
201 /**
202 * Test error loading URIs using the bulkloader.
203 */
204 function testBulkLoaderLoadError() {
205 var uris = ['a', 'b', 'c'];
206 var bulkLoader = getNonSuccessfulBulkLoader(uris);
207
208 bulkLoader.load();
209
210 clock.tick(2);
211 assertFalse('The bulk loader is not yet loaded (after 2 ticks)', loadError);
212
213 clock.tick(3);
214 assertFalse('The bulk loader is not yet loaded (after 5 ticks)', loadError);
215
216 clock.tick(5);
217 assertFalse('The bulk loader is not yet loaded (after 10 ticks)', loadError);
218
219 clock.tick(5);
220 assertFalse(
221 'The bulk loader is not loaded successfully (after 15 ticks)',
222 loadSuccess);
223 assertTrue('The bulk loader is loaded in error (after 15 ticks)', loadError);
224 }