aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/gears/urlcapture_test.html
blob: 9a1f2c4f1ea600b5851d77da00d7c131124c9a9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<!DOCTYPE html>
<html>
<!--
Copyright 2010 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>UrlCapture tests</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.gears.UrlCapture');
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.LooseMock');
</script>
</head>
<body>
<script>

/**
 * @return {GearsLocalServer} Stub local server.
 */
function newStubLocalServer() {
  return {
    createStore: function() {},
    openStore: function() {},
    removeStore: function() {}
  };
}

/**
 * @return {GearsResourceStore} Stub resource store.
 */
function newStubResourceStore() {
  var nextCaptureId = 0;
  var capturedUrls = [];

  var result = {};
  result.callbacks = {};

  result.capture = function(urls, callback) {
    var id = nextCaptureId++;
    this.callbacks[id] = function(url, success, captureId) {
      if (url && success) {
        capturedUrls.push(url);
      }
      callback(url, success, captureId);
    }
    return id;
  };

  result.rename = function(oldUrl, newUrl) {
    goog.array.remove(capturedUrls, oldUrl);
    capturedUrls.push(newUrl);
  };

  result.copy = function(oldUrl, newUrl) {
    capturedUrls.push(newUrl);
  };

  result.remove = function(url) {
    goog.array.remove(capturedUrls, url);
  };

  result.isCaptured = function(url) {
    return goog.array.contains(capturedUrls, url);
  };

  result.abortCapture = function(id) {
    delete this.callbacks[id];
  };

  return result;
}

/**
 * @param {GearsResourceStore} ResourceStore to be returned by #createStore.
 * @return {Mock.<GearsLocalServer>} Mock local server expecting a single call
 *     to #createStore returning #stubResourceStore.
 */
function newMockLocalServerCreateStore(stubResourceStore) {
  var result = new goog.testing.LooseMock(newStubLocalServer());
  result.createStore(STORE_NAME, COOKIE_NAME).$returns(
      stubResourceStore);
  result.$replay();
  return result;
}

/**
 * @return {Object} Event listener for UrlCapture.
 */
function newListener(uut) {
  var result = {
      url_success: [],
      url_error: [],
      complete: [],
      abort: []
  };
  goog.events.listen(uut, goog.gears.UrlCapture.EventType.URL_SUCCESS,
      function(e) { result.url_success.push(e); });
  goog.events.listen(uut, goog.gears.UrlCapture.EventType.URL_ERROR,
      function(e) { result.url_error.push(e); });
  goog.events.listen(uut, goog.gears.UrlCapture.EventType.COMPLETE,
      function(e) { result.complete.push(e); });
  goog.events.listen(uut, goog.gears.UrlCapture.EventType.ABORT,
      function(e) { result.abort.push(e); });
  return result;
}

function assertEventEquals(expectedType, expectedCaptureId, expectedUri,
    expectedErrorUris, actualEvent) {
  assertEquals('wrong type', expectedType, actualEvent.type);
  assertEquals('wrong capture ID', expectedCaptureId, actualEvent.captureId);
  assertEquals('wrong URL', expectedUri, actualEvent.uri);
  assertArrayEquals('wrong error URIs', expectedErrorUris,
      actualEvent.errorUris);
}

function assertIsCaptured(expectedCapturedUrls, expectedUncapturedUrls, uut) {
  for (var i = 0; i < expectedCapturedUrls.length; ++i) {
    var url = expectedCapturedUrls[i];
    assertTrue('not captured: ' + url, uut.isCaptured(url));
  }
  for (var i = 0; i < expectedUncapturedUrls.length; ++i) {
    var url = expectedUncapturedUrls[i];
    assertFalse('captured: ' + url, uut.isCaptured(url));
  }
}

var STORE_NAME = 'TestCapture';
var COOKIE_NAME = 'TestCookie';

/**
 * Tests resource store exists.
 */
function testExists() {
  var mockLocalServer = new goog.testing.LooseMock(newStubLocalServer());
  mockLocalServer.openStore(STORE_NAME, COOKIE_NAME).$returns(true);
  mockLocalServer.$replay();

  var uut = new goog.gears.UrlCapture(STORE_NAME, COOKIE_NAME, mockLocalServer);
  assertTrue('exists failed for ' + STORE_NAME, uut.exists());
  mockLocalServer.$verify();
}

/**
 * Tests resource store does not exist.
 */
function testNotExists() {
  var mockLocalServer = new goog.testing.LooseMock(newStubLocalServer());
  mockLocalServer.openStore(STORE_NAME, COOKIE_NAME).$returns(false);
  mockLocalServer.$replay();

  var uut = new goog.gears.UrlCapture(STORE_NAME, COOKIE_NAME, mockLocalServer);
  assertFalse('exists succeeded for ' + STORE_NAME, uut.exists());
  mockLocalServer.$verify();
}

/**
 * Test remove resource store.
 */
function testRemoveStore() {
  var mockLocalServer = new goog.testing.LooseMock(newStubLocalServer());
  mockLocalServer.removeStore(STORE_NAME, COOKIE_NAME);
  mockLocalServer.$replay();

  var uut = new goog.gears.UrlCapture(STORE_NAME, COOKIE_NAME, mockLocalServer);
  uut.removeStore();
  mockLocalServer.$verify();
}

/**
 * Test empty capture request.
 */
function testEmptyCapture() {
  var uut = new goog.gears.UrlCapture(STORE_NAME, COOKIE_NAME,
      newStubLocalServer());
  assertThrows('should fail for null uris', function() { uut.capture(); });
  assertThrows('should fail for 0 uris', function() { uut.capture([]); });
}

/**
 * Test successful capture.
 */
function testCaptureUrlSuccess() {
  // Create stubs and mocks.
  var stubResourceStore = newStubResourceStore();
  var mockLocalServer = newMockLocalServerCreateStore(stubResourceStore);

  // Create url capturer and event listener.
  var uut = new goog.gears.UrlCapture(STORE_NAME, COOKIE_NAME, mockLocalServer);
  var listener = newListener(uut);

  // Start capture.
  var url = '/foo.html';
  var id = uut.capture([url]);
  mockLocalServer.$verify();
  assertEquals('wrong capture ID', 0, id);
  var callback = stubResourceStore.callbacks[id];
  assertEquals('missing callback', 'function', typeof callback);

  // Callback capture success.
  var uncapturedUrl = '/uncaptured.html';
  assertIsCaptured([], [url, uncapturedUrl], uut);
  callback(url, true, id);
  assertIsCaptured([url], [uncapturedUrl], uut);

  // Assert events.
  assertEquals('url error', 0, listener.url_error.length);
  assertEquals('abort', 0, listener.abort.length);
  assertEquals('url success', 1, listener.url_success.length);
  assertEventEquals(goog.gears.UrlCapture.EventType.URL_SUCCESS, id, url, [],
      listener.url_success[0]);
  assertEquals('url complete', 1, listener.complete.length);
  assertEventEquals(goog.gears.UrlCapture.EventType.COMPLETE, id, null, [],
      listener.complete[0]);
}

/**
 * Test unsuccessful capture.
 */
function testCaptureUrlFail() {
  // Create stubs and mocks.
  var stubResourceStore = newStubResourceStore();
  var mockLocalServer = newMockLocalServerCreateStore(stubResourceStore);

  // Create url capturer and event listener.
  var uut = new goog.gears.UrlCapture(STORE_NAME, COOKIE_NAME, mockLocalServer);
  var listener = newListener(uut);

  // Start capture.
  var url = '/foo.html';
  var id = uut.capture([url]);
  mockLocalServer.$verify();
  assertEquals('wrong capture ID', 0, id);
  var callback = stubResourceStore.callbacks[id];
  assertEquals('missing callback', 'function', typeof callback);

  // Callback capture success.
  assertIsCaptured([], [url], uut);
  callback(url, false, id);
  assertIsCaptured([], [url], uut);

  // Assert events.
  assertEquals('url error', 1, listener.url_error.length);
  assertEventEquals(goog.gears.UrlCapture.EventType.URL_ERROR, id, url, [],
      listener.url_error[0]);
  assertEquals('abort', 0, listener.abort.length);
  assertEquals('url success', 0, listener.url_success.length);
  assertEquals('url complete', 1, listener.complete.length);
  assertEventEquals(goog.gears.UrlCapture.EventType.COMPLETE, id, null, [url],
      listener.complete[0]);
}

/**
 * Test abort capture.
 */
function testAbortCapture() {
  // Create stubs and mocks.
  var stubResourceStore = newStubResourceStore();
  var mockLocalServer = newMockLocalServerCreateStore(stubResourceStore);

  // Create url capturer and event listener.
  var uut = new goog.gears.UrlCapture(STORE_NAME, COOKIE_NAME, mockLocalServer);
  var listener = newListener(uut);

  // Throw exception for aborting with no ID.
  assertThrows('should fail for no ID', function() { uut.abort(); });
  assertThrows('should fail for null ID', function() { uut.abort(null); });

  // Start capture.
  var url = '/foo.html';
  var id = uut.capture([url]);
  mockLocalServer.$verify();
  var callback = stubResourceStore.callbacks[id];
  assertEquals('missing callback', 'function', typeof callback);

  // Abort capture.
  assertIsCaptured([], [url], uut);
  uut.abort(id);
  assertEquals('abort', 1, listener.abort.length);
  assertFalse('found aborted callback', !!stubResourceStore.callbacks[id]);
  assertIsCaptured([], [url], uut);

  // Callback after abort captures URL but dispatches no events.
  callback(url, true, id);
  assertIsCaptured([url], [], uut);

  // Assert events.
  assertEquals('url error', 0, listener.url_error.length);
  assertEquals('url success', 0, listener.url_success.length);
  assertEquals('url complete', 0, listener.complete.length);
}

/**
 * Test removing captured URL.
 */
function testRemoveUrl() {
  // Create stubs and mocks.
  var stubResourceStore = newStubResourceStore();
  var mockLocalServer = newMockLocalServerCreateStore(stubResourceStore);

  // Create url capturer.
  var uut = new goog.gears.UrlCapture(STORE_NAME, COOKIE_NAME, mockLocalServer);

  // Throw exception for removing with no URL.
  assertThrows('should fail for no URL', function() { uut.remove(); });
  assertThrows('should fail for null URL', function() { uut.remove(null); });

  // Start capture.
  var url = '/foo.html';
  var id = uut.capture([url]);
  mockLocalServer.$verify();
  assertIsCaptured([], [url], uut);

  // Capture callback success.
  stubResourceStore.callbacks[id](url, true, id);
  assertIsCaptured([url], [], uut);

  // Remove URL.
  uut.remove(url);
  assertIsCaptured([], [url], uut);
}

/**
 * Test renaming captured URL.
 */
function testRenameUrl() {
  // Create stubs and mocks.
  var stubResourceStore = newStubResourceStore();
  var mockLocalServer = newMockLocalServerCreateStore(stubResourceStore);

  // Create url capturer.
  var uut = new goog.gears.UrlCapture(STORE_NAME, COOKIE_NAME, mockLocalServer);

  // Throw exception for removing with no URL.
  assertThrows('should fail for no URL', function() { uut.rename(); });
  assertThrows('should fail for null URL', function() { uut.rename(null); });

  // Start capture.
  var url = '/foo.html';
  var renamedUrl = '/renamed.html';
  var id = uut.capture([url]);
  mockLocalServer.$verify();
  assertIsCaptured([], [url, renamedUrl], uut);

  // Callback capture success.
  stubResourceStore.callbacks[id](url, true, id);
  assertIsCaptured([url], [renamedUrl], uut);

  // Rename URL.
  uut.rename(url, renamedUrl);
  assertIsCaptured([renamedUrl], [url], uut);
}

/**
 * Test copying captured URL.
 */
function testCopyUrl() {
  // Create stubs and mocks.
  var stubResourceStore = newStubResourceStore();
  var mockLocalServer = newMockLocalServerCreateStore(stubResourceStore);

  // Create url capturer.
  var uut = new goog.gears.UrlCapture(STORE_NAME, COOKIE_NAME, mockLocalServer);

  // Throw exception for removing with no URL.
  assertThrows('should fail for no URL', function() { uut.copy(); });
  assertThrows('should fail for null URL', function() { uut.copy(null); });

  // Start capture.
  var url = '/foo.html';
  var copiedUrl = '/renamed.html';
  var id = uut.capture([url]);
  mockLocalServer.$verify();
  assertIsCaptured([], [url, copiedUrl], uut);

  // Callback capture success.
  stubResourceStore.callbacks[id](url, true, id);
  assertIsCaptured([url], [copiedUrl], uut);

  // Copy URL.
  uut.copy(url, copiedUrl);
  assertIsCaptured([url, copiedUrl], [], uut);
}

</script>

</body>
</html>