summaryrefslogtreecommitdiffstats
path: root/chrome/test/data/extensions/api_test/history/test.js
blob: a97c11b83e5e4519aa30b41c2398524ad385acad (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// History api test for Chrome.
// browser_tests.exe --gtest_filter=ExtensionApiTest.History

var pass = chrome.test.callbackPass;
var fail = chrome.test.callbackFail;
var assertEq = chrome.test.assertEq;

var GOOGLE_URL = 'http://www.google.com/';
var PICASA_URL = 'http://www.picasa.com/';
var A_RELATIVE_URL =
    'http://www.a.com:1337/files/extensions/api_test/history/a.html';
var B_RELATIVE_URL =
    'http://www.b.com:1337/files/extensions/api_test/history/b.html';

/**
* A helper function to flip the setTimeout arguments and make the code more
* readable.
* @param {number} seconds The number of seconds to wait.
* @param {function} callback Closure.
*/
function waitAFewSeconds(seconds, callback) {
  setTimeout(callback, seconds * 1000);
};

/**
* Object used for listening to the chrome.history.onVisited events.  The global
* object 'itemVisited' stores the last item received.
*/
var itemVisitedCallback = null;
function itemVisitedListener(visited) {
  if (null != itemVisitedCallback) {
    itemVisitedCallback(visited);
  };
};

function removeItemVisitedListener() {
  chrome.experimental.history.onVisited.removeListener(itemVisitedListener);
  itemVisitedCallback = null;
}

function setItemVisitedListener(callback) {
  chrome.experimental.history.onVisited.addListener(itemVisitedListener);
  itemVisitedCallback = callback;
}

function setNextItemVisitedListener(callback) {
  itemVisitedCallback = callback;
}

/**
* An object used for listening to the chrome.history.onVisitRemoved events. The
* global object 'itemRemovedInfo' stores the information from the last callback.
*/
var itemRemovedCallback = null;
function itemRemovedListener(removed) {
  if (null != itemRemovedCallback) {
    itemRemovedCallback(removed);
  };
};

function removeItemRemovedListener() {
  chrome.experimental.history.onVisited.removeListener(itemRemovedListener);
  itemRemovedCallback = null;
}

function setItemRemovedListener(callback) {
  chrome.experimental.history.onVisitRemoved.addListener(itemRemovedListener);
  itemRemovedCallback = callback;
}

function setNextItemRemovedListener(callback) {
  itemRemovedCallback = callback;
}

/**
* An object used for listening to the chrome.history.onVisitRemoved events.  Set
* 'tabCompleteCallback' to a function to add extra processing to the callback.
* The global object 'tabsCompleteData' contains a list of the last known state
* of every tab.
*/
var tabCompleteCallback = null;
var tabsCompleteData = {};
function tabsCompleteListener(tabId, changeInfo) {
  if (changeInfo && changeInfo.status) {
    tabsCompleteData[tabId] = changeInfo.status;
  };
  if (null != tabCompleteCallback) {
    tabCompleteCallback();
  };
};

/**
* Queries the entire history for items, calling the closure with an argument
* specifying the the number of items in the query.
* @param {function(number)} callback The closure.
*/
function countItemsInHistory(callback) {
  var query = {'search': ''};
  chrome.experimental.history.search(query, function(results) {
    callback(results.length);
  });
};

/**
* Populates the history by calling addUrl for each url in the array urls.
* @param {Array.<string>} urls The array of urls to populate the history.
* @param {function} callback Closure.
*/
function populateHistory(urls, callback) {
  urls.forEach(function(url) {
    chrome.experimental.history.addUrl({ 'url': url });
  });
  callback();
};

chrome.test.runTests([
  // All the tests require a blank state to start from.  This test is run
  // first to insure that state can be acheived.
  function clearHistory() {
    chrome.experimental.history.deleteAll(pass(function() {
      countItemsInHistory(pass(function(count) {
        assertEq(0, count);
      }));
    }));
  },

  function basicSearch() {
    // basicSearch callback.
    function basicSearchTestVerification() {
      removeItemVisitedListener();
      var query = { 'search': '' };
      chrome.experimental.history.search(query, function(results) {
        assertEq(1, results.length);
        assertEq(GOOGLE_URL, results[0].url);

        // The test has succeeded.
        chrome.test.succeed();
      });
    };

    // basicSearch entry point.
    chrome.experimental.history.deleteAll(function() {
      setItemVisitedListener(basicSearchTestVerification);
      populateHistory([GOOGLE_URL], function() { });
    });
  },

  function timeScopedSearch() {
    var startDate = {};
    var endDate = {};

    function timeScopedSearchTestVerification() {
      removeItemVisitedListener();

      var query = { 'search': '',
                    'startTime': startDate.getTime(),
                    'endTime': endDate.getTime() };
       chrome.experimental.history.search(query, function(results) {
         assertEq(1, results.length);
         assertEq(GOOGLE_URL, results[0].url);

        // The test has succeeded.
        chrome.test.succeed();
       });
    };

    function onAddedItem() {
      // Set the next test callback.
      setNextItemVisitedListener(timeScopedSearchTestVerification);

      // Chrome has seconds resolution, so we must wait in order to search
      // a range.
      waitAFewSeconds(2, function() {
        endDate = new Date();
        endDate.setTime(endDate.getTime() - 500);
        populateHistory([PICASA_URL], function() { });
      });
    };

    // timeScopedSearch entry point.
    chrome.experimental.history.deleteAll(function() {
      // Set the test callback.
      setItemVisitedListener(onAddedItem);
      // Set the start time a few seconds in the past.
      startDate = new Date();
      startDate.setTime(startDate.getTime() - 1000);
      populateHistory([GOOGLE_URL], function() { });
    });
  },

  // Give time epochs x,y,z and history events A,B which occur in the sequence
  // x A y B z, this test scopes the search to the interval [y,z] to test that
  // [x,y) is excluded.  The previous test scoped to the interval [x,y].
  function timeScopedSearch2() {
    var startDate = {};
    var endDate = {};

    function timeScopedSearch2TestVerification() {
      removeItemVisitedListener();

      endDate = new Date();
      endDate.setTime(endDate.getTime() + 1000);
      var query = { 'search': '',
                    'startTime': startDate.getTime(),
                    'endTime': endDate.getTime() };
       chrome.experimental.history.search(query, function(results) {
         assertEq(1, results.length);
         assertEq(PICASA_URL, results[0].url);

        // The test has succeeded.
        chrome.test.succeed();
       });
    };

    function onAddedItem() {
      // Set the next test callback.
      setNextItemVisitedListener(timeScopedSearch2TestVerification);

      // Chrome has seconds resolution, so we must wait in order to search
      // a range.
      waitAFewSeconds(2, function() {
        startDate = new Date();
        startDate.setTime(startDate.getTime() - 1000);
        populateHistory([PICASA_URL], function() { });
      });
    };

    // timeScopedSearch entry point.
    chrome.experimental.history.deleteAll(function() {
      // Set the test callback.
      setItemVisitedListener(onAddedItem);
      populateHistory([GOOGLE_URL], function() { });
    });
  },

  function lengthScopedSearch() {
    var urls = [GOOGLE_URL, PICASA_URL];
    var urlsAdded = 0;

    function lengthScopedSearchTestVerification() {
      // Ensure all urls have been added.
      urlsAdded += 1;
      if (urlsAdded < urls.length)
        return;

      removeItemVisitedListener();

      var query = { 'search': '', 'maxResults': 1 };
      chrome.experimental.history.search(query, function(results) {
        assertEq(1, results.length);
        assertEq(PICASA_URL, results[0].url);

        // The test has succeeded.
        chrome.test.succeed();
      });
    };

    // lengthScopedSearch entry point.
    chrome.experimental.history.deleteAll(function() {
      setItemVisitedListener(lengthScopedSearchTestVerification);
      populateHistory(urls, function() { });
    });
  },

  function fullTextSearch() {
    chrome.experimental.history.deleteAll(function() {
      // The continuation of the test after the windows have been opened.
      var validateTest = function() {
        // Continue with the test.
        // A title search for www.a.com should find a.
        var query = { 'search': 'www.a.com' };
        chrome.experimental.history.search(query, function(results) {
          assertEq(1, results.length);
          assertEq(A_RELATIVE_URL, results[0].url);

          // Text in the body of b.html.
          query = { 'search': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' };
          chrome.experimental.history.search(query, function(results) {
            assertEq(1, results.length);
            assertEq(B_RELATIVE_URL, results[0].url);

            // The test has succeeded.
            chrome.test.succeed();
          });
        });
      };

      // Setup a callback object for tab events.
      var urls = [A_RELATIVE_URL, B_RELATIVE_URL];
      var tabIds = [];

      function listenerCallback() {
        if (tabIds.length < urls.length) {
          return;
        };

        // Ensure both tabs have completed loading.
        for (var index = 0, id; id = tabIds[index]; index++) {
          if (!tabsCompleteData[id] ||
          tabsCompleteData[id] != 'complete') {
            return;
          };
        };

        // Unhook callbacks.
        tabCompleteCallback = null;
        chrome.tabs.onUpdated.removeListener(tabsCompleteListener);

        // Allow indexing to occur.
        waitAFewSeconds(2, function() {
          validateTest();
        });
      };

      tabCompleteCallback = listenerCallback;
      chrome.tabs.onUpdated.addListener(tabsCompleteListener);

      // Navigate to a few pages.
      urls.forEach(function(url) {
        chrome.tabs.create({ 'url': url }, function(tab) {
          tabIds.push(tab.id);
        });
      });
    });
  },

  function getVisits() {
    // getVisits callback.
    function getVisitsTestVerification() {
      removeItemVisitedListener();

      // Verify that we received the url.
      var query = { 'search': '' };
      chrome.experimental.history.search(query, function(results) {
        assertEq(1, results.length);
        assertEq(GOOGLE_URL, results[0].url);

        var id = results[0].id;
        chrome.experimental.history.getVisits({ 'url': GOOGLE_URL }, function(results) {
          assertEq(1, results.length);
          assertEq(id, results[0].id);

          // The test has succeeded.
          chrome.test.succeed();
        });
      });
    };

    // getVisits entry point.
    chrome.experimental.history.deleteAll(function() {
      setItemVisitedListener(getVisitsTestVerification);
      populateHistory([GOOGLE_URL], function() { });
    });
  },

  function deleteUrl() {
    function deleteUrlTestVerification() {
      removeItemRemovedListener();

      var query = { 'search': '' };
      chrome.experimental.history.search(query, function(results) {
        assertEq(0, results.length);

        // The test has succeeded.
        chrome.test.succeed();
      });
    };

    function onAddedItem() {
      removeItemVisitedListener();

      var query = { 'search': '' };
      chrome.experimental.history.search(query, function(results) {
        assertEq(1, results.length);
        assertEq(GOOGLE_URL, results[0].url);

        chrome.experimental.history.deleteUrl({ 'url': GOOGLE_URL });
      });
    };

    // deleteUrl entry point.
    chrome.experimental.history.deleteAll(function() {
      setItemVisitedListener(onAddedItem);
      setItemRemovedListener(deleteUrlTestVerification);
      populateHistory([GOOGLE_URL], function() { });
    });
  },

  function deleteRange() {
    var urls = [GOOGLE_URL, PICASA_URL];
    var startDate = {};
    var endDate = {};
    var itemsAdded = 0;

    function deleteRangeTestVerification() {
      removeItemRemovedListener();

      var query = { 'search': '' };
      chrome.experimental.history.search(query, function(results) {
        assertEq(1, results.length);
        assertEq(PICASA_URL, results[0].url);

        // The test has succeeded.
        chrome.test.succeed();
      });
    };

    function onAddedItem() {
      itemsAdded += 1;

      if (itemsAdded < urls.length) {
        // Chrome has seconds resolution, so we must wait to search a range.
        waitAFewSeconds(2, function() {
          endDate = new Date();
          endDate.setTime(endDate.getTime() - 1000);
          populateHistory([urls[itemsAdded]], function() { });
        });
        return;
      };

      removeItemVisitedListener();
      chrome.experimental.history.deleteRange({ 'startTime': startDate.getTime(),
                                   'endTime': endDate.getTime() },
                                  function() { });
    };

    // deletRange entry point.
    chrome.experimental.history.deleteAll(function() {
      setItemVisitedListener(onAddedItem);
      setItemRemovedListener(deleteRangeTestVerification);

      startDate = new Date();
      startDate.setTime(startDate.getTime() - 1000);

      populateHistory([urls[itemsAdded]], function() { });
    });
  },

  // Suppose we have time epochs x,y,z and history events A,B which occur in the
  // sequence x A y B z.  The previous deleteRange test deleted the range [x,y],
  // this test deletes the range [y,z].
  function deleteRange2() {
    var urls = [GOOGLE_URL, PICASA_URL];
    var startDate = {};
    var endDate = {};
    var itemsAdded = 0;

    function deleteRange2TestVerification() {
      removeItemRemovedListener();

      var query = { 'search': '' };
      chrome.experimental.history.search(query, function(results) {
        assertEq(1, results.length);
        assertEq(GOOGLE_URL, results[0].url);

        // The test has succeeded.
        chrome.test.succeed();
      });
    };

    function onAddedItem() {
      itemsAdded += 1;

      if (itemsAdded < urls.length) {
        // Chrome has seconds resolution, so we must wait to search a range.
        waitAFewSeconds(2, function() {
          startDate = new Date();
          startDate.setTime(startDate.getTime() - 1000);
          populateHistory([urls[itemsAdded]], function() { });
        });
        return;
      };

      removeItemVisitedListener();

      endDate = new Date();
      endDate.setTime(endDate.getTime() + 1000);
      chrome.experimental.history.deleteRange({ 'startTime': startDate.getTime(),
                                   'endTime': endDate.getTime() },
                                  function() { });
    };

    // deletRange entry point.
    chrome.experimental.history.deleteAll(function() {
      setItemVisitedListener(onAddedItem);
      setItemRemovedListener(deleteRange2TestVerification);
      populateHistory([urls[itemsAdded]], function() { });
    });
  },

  // This test has the following format.  From the main entry point we
  // attach a listener and give that listener a state to callback on.  After
  // receipt of the first message we validate it is as excepted in
  // 'firsPageVisit.'  We update the callback to 'secondPageVisit' and modify
  //the history.
  function listenForAdd() {
    function secondPageVisit(visited) {
      // Update state machine.
      itemVisitedCallback = null;
      chrome.experimental.history.onVisited.removeListener(itemVisitedListener);

      // Validate the state.
      countItemsInHistory(function(count) {
        assertEq(2, count);
        assertEq(PICASA_URL, visited.url);

        // The test has succeeded.
        chrome.test.succeed();
      });
    };

    function firstPageVisit(visited) {
      // Update state machine.
      itemVisitedCallback = secondPageVisit;

      // Validate the state.
      countItemsInHistory(function(count) {
        assertEq(1, count);
        assertEq(GOOGLE_URL, visited.url);

        // Transistion to next state.
        populateHistory([PICASA_URL], function() {
        });
      });
    };

    // Populate the history one item at a time.
    itemVisitedCallback = firstPageVisit;
    chrome.experimental.history.onVisited.addListener(itemVisitedListener);

    chrome.experimental.history.deleteAll(function() {
      populateHistory([GOOGLE_URL], function() {
      });
    });
  },

  function listenForDelete() {
    var urls = [GOOGLE_URL, PICASA_URL];
    var visited_urls = {};

    // Callback for deletions.
    function deleteRangeCallback(removed) {
      // We will no longer need to listen to deletions.
      itemRemovedCallback = null;
      chrome.experimental.history.onVisitRemoved.removeListener(
          itemRemovedListener);

      // The two added urls should have been deleted together.
      assertEq(false, removed.allHistory);
      assertEq(2, removed.urls.length);

      // History should be empty.
      countItemsInHistory(function(count) {
        assertEq(0, count);

        // The test has succeeded.
        chrome.test.succeed();
      });
    };

    // Callback for page visits.
    function historyPopulatedCallback(visited) {
      visited_urls[visited_urls.length] = visited.url;

      if (visited_urls.length < urls.length) {
        return;
      }

      // We will no longer need to listen to visits.
      itemVisitedCallback = null;
      chrome.experimental.history.onVisited.removeListener(itemVisitedListener);
      countItemsInHistory(function(count) {
        assertEq(2, count);

        var startDate = new Date();
        startDate.setDate(startDate.getDate() - 1);
        var endDate = new Date();
        chrome.experimental.history.deleteRange(
            { 'startTime': startDate.getTime(), 'endTime': endDate.getTime() },
            function() { });
      });
    };

    // We must be called back after the initial
    itemVisitedCallback = historyPopulatedCallback;
    chrome.experimental.history.onVisited.addListener(itemVisitedListener);

    chrome.experimental.history.deleteAll(function() {
      // Do not add the listener until after we have reset the state of history.
      itemRemovedCallback = deleteRangeCallback;
      chrome.experimental.history.onVisitRemoved.addListener(
          itemRemovedListener);

      // This call triggers the onVisited event handler.
      populateHistory(urls, function() { });
    });
  },
]);