toolkit/components/places/tests/unit/test_browserhistory.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/unit/test_browserhistory.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,125 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +const TEST_URI = NetUtil.newURI("http://mozilla.com/");
    1.11 +const TEST_SUBDOMAIN_URI = NetUtil.newURI("http://foobar.mozilla.com/");
    1.12 +
    1.13 +add_test(function test_addPage()
    1.14 +{
    1.15 +  promiseAddVisits(TEST_URI).then(function () {
    1.16 +    do_check_eq(1, PlacesUtils.history.hasHistoryEntries);
    1.17 +    run_next_test();
    1.18 +  });
    1.19 +});
    1.20 +
    1.21 +add_test(function test_removePage()
    1.22 +{
    1.23 +  PlacesUtils.bhistory.removePage(TEST_URI);
    1.24 +  do_check_eq(0, PlacesUtils.history.hasHistoryEntries);
    1.25 +  run_next_test();
    1.26 +});
    1.27 +
    1.28 +add_test(function test_removePages()
    1.29 +{
    1.30 +  let pages = [];
    1.31 +  for (let i = 0; i < 8; i++) {
    1.32 +    pages.push(NetUtil.newURI(TEST_URI.spec + i));
    1.33 +  }
    1.34 +
    1.35 +  promiseAddVisits(pages.map(function (uri) ({ uri: uri }))).then(function () {
    1.36 +    // Bookmarked item should not be removed from moz_places.
    1.37 +    const ANNO_INDEX = 1;
    1.38 +    const ANNO_NAME = "testAnno";
    1.39 +    const ANNO_VALUE = "foo";
    1.40 +    const BOOKMARK_INDEX = 2;
    1.41 +    PlacesUtils.annotations.setPageAnnotation(pages[ANNO_INDEX],
    1.42 +                                              ANNO_NAME, ANNO_VALUE, 0,
    1.43 +                                              Ci.nsIAnnotationService.EXPIRE_NEVER);
    1.44 +    PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
    1.45 +                                         pages[BOOKMARK_INDEX],
    1.46 +                                         PlacesUtils.bookmarks.DEFAULT_INDEX,
    1.47 +                                         "test bookmark");
    1.48 +    PlacesUtils.annotations.setPageAnnotation(pages[BOOKMARK_INDEX],
    1.49 +                                              ANNO_NAME, ANNO_VALUE, 0,
    1.50 +                                              Ci.nsIAnnotationService.EXPIRE_NEVER);
    1.51 +  
    1.52 +    PlacesUtils.bhistory.removePages(pages, pages.length);
    1.53 +    do_check_eq(0, PlacesUtils.history.hasHistoryEntries);
    1.54 +  
    1.55 +    // Check that the bookmark and its annotation still exist.
    1.56 +    do_check_true(PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId, 0) > 0);
    1.57 +    do_check_eq(PlacesUtils.annotations.getPageAnnotation(pages[BOOKMARK_INDEX], ANNO_NAME),
    1.58 +                ANNO_VALUE);
    1.59 +  
    1.60 +    // Check the annotation on the non-bookmarked page does not exist anymore.
    1.61 +    try {
    1.62 +      PlacesUtils.annotations.getPageAnnotation(pages[ANNO_INDEX], ANNO_NAME);
    1.63 +      do_throw("did not expire expire_never anno on a not bookmarked item");
    1.64 +    } catch(ex) {}
    1.65 +  
    1.66 +    // Cleanup.
    1.67 +    PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.unfiledBookmarksFolderId);
    1.68 +    promiseClearHistory().then(run_next_test);
    1.69 +  });
    1.70 +});
    1.71 +
    1.72 +add_test(function test_removePagesByTimeframe()
    1.73 +{
    1.74 +  let visits = [];
    1.75 +  let startDate = Date.now() * 1000;
    1.76 +  for (let i = 0; i < 10; i++) {
    1.77 +    visits.push({
    1.78 +      uri: NetUtil.newURI(TEST_URI.spec + i),
    1.79 +      visitDate: startDate + i
    1.80 +    });
    1.81 +  }
    1.82 +
    1.83 +  promiseAddVisits(visits).then(function () {
    1.84 +    // Delete all pages except the first and the last.
    1.85 +    PlacesUtils.bhistory.removePagesByTimeframe(startDate + 1, startDate + 8);
    1.86 +  
    1.87 +    // Check that we have removed the correct pages.
    1.88 +    for (let i = 0; i < 10; i++) {
    1.89 +      do_check_eq(page_in_database(NetUtil.newURI(TEST_URI.spec + i)) == 0,
    1.90 +                  i > 0 && i < 9);
    1.91 +    }
    1.92 +  
    1.93 +    // Clear remaining items and check that all pages have been removed.
    1.94 +    PlacesUtils.bhistory.removePagesByTimeframe(startDate, startDate + 9);
    1.95 +    do_check_eq(0, PlacesUtils.history.hasHistoryEntries);
    1.96 +    run_next_test();
    1.97 +  });
    1.98 +});
    1.99 +
   1.100 +add_test(function test_removePagesFromHost()
   1.101 +{
   1.102 +  promiseAddVisits(TEST_URI).then(function () {
   1.103 +    PlacesUtils.bhistory.removePagesFromHost("mozilla.com", true);
   1.104 +    do_check_eq(0, PlacesUtils.history.hasHistoryEntries);
   1.105 +    run_next_test();
   1.106 +  });
   1.107 +});
   1.108 +
   1.109 +add_test(function test_removePagesFromHost_keepSubdomains()
   1.110 +{
   1.111 +  promiseAddVisits([{ uri: TEST_URI }, { uri: TEST_SUBDOMAIN_URI }]).then(function () {
   1.112 +    PlacesUtils.bhistory.removePagesFromHost("mozilla.com", false);
   1.113 +    do_check_eq(1, PlacesUtils.history.hasHistoryEntries);
   1.114 +    run_next_test();
   1.115 +  });
   1.116 +});
   1.117 +
   1.118 +add_test(function test_removeAllPages()
   1.119 +{
   1.120 +  PlacesUtils.bhistory.removeAllPages();
   1.121 +  do_check_eq(0, PlacesUtils.history.hasHistoryEntries);
   1.122 +  run_next_test();
   1.123 +});
   1.124 +
   1.125 +function run_test()
   1.126 +{
   1.127 +  run_next_test();
   1.128 +}

mercurial