toolkit/components/places/tests/expiration/test_removeAllPages.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/expiration/test_removeAllPages.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,162 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
     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 +/**
    1.11 + * What this is aimed to test:
    1.12 + *
    1.13 + * bh.removeAllPages should expire everything but bookmarked pages and valid
    1.14 + * annos.
    1.15 + */
    1.16 +
    1.17 +let hs = PlacesUtils.history;
    1.18 +let bs = PlacesUtils.bookmarks;
    1.19 +let as = PlacesUtils.annotations;
    1.20 +
    1.21 +/**
    1.22 + * Creates an aged annotation.
    1.23 + *
    1.24 + * @param aIdentifier Either a page url or an item id.
    1.25 + * @param aIdentifier Name of the annotation.
    1.26 + * @param aValue Value for the annotation.
    1.27 + * @param aExpirePolicy Expiration policy of the annotation.
    1.28 + * @param aAgeInDays Age in days of the annotation.
    1.29 + * @param [optional] aLastModifiedAgeInDays Age in days of the annotation, for lastModified.
    1.30 + */
    1.31 +let now = Date.now();
    1.32 +function add_old_anno(aIdentifier, aName, aValue, aExpirePolicy,
    1.33 +                      aAgeInDays, aLastModifiedAgeInDays) {
    1.34 +  let expireDate = (now - (aAgeInDays * 86400 * 1000)) * 1000;
    1.35 +  let lastModifiedDate = 0;
    1.36 +  if (aLastModifiedAgeInDays)
    1.37 +    lastModifiedDate = (now - (aLastModifiedAgeInDays * 86400 * 1000)) * 1000;
    1.38 +
    1.39 +  let sql;
    1.40 +  if (typeof(aIdentifier) == "number") {
    1.41 +    // Item annotation.
    1.42 +    as.setItemAnnotation(aIdentifier, aName, aValue, 0, aExpirePolicy);
    1.43 +    // Update dateAdded for the last added annotation.
    1.44 +    sql = "UPDATE moz_items_annos SET dateAdded = :expire_date, lastModified = :last_modified " +
    1.45 +          "WHERE id = ( " +
    1.46 +            "SELECT a.id FROM moz_items_annos a " +
    1.47 +            "JOIN moz_anno_attributes n ON n.id = a.anno_attribute_id " +
    1.48 +            "WHERE a.item_id = :id " +
    1.49 +              "AND n.name = :anno_name " +
    1.50 +            "ORDER BY a.dateAdded DESC LIMIT 1 " +
    1.51 +          ")";
    1.52 +  }
    1.53 +  else if (aIdentifier instanceof Ci.nsIURI){
    1.54 +    // Page annotation.
    1.55 +    as.setPageAnnotation(aIdentifier, aName, aValue, 0, aExpirePolicy);
    1.56 +    // Update dateAdded for the last added annotation.
    1.57 +    sql = "UPDATE moz_annos SET dateAdded = :expire_date, lastModified = :last_modified " +
    1.58 +          "WHERE id = ( " +
    1.59 +            "SELECT a.id FROM moz_annos a " +
    1.60 +            "JOIN moz_anno_attributes n ON n.id = a.anno_attribute_id " +
    1.61 +            "JOIN moz_places h on h.id = a.place_id " +
    1.62 +            "WHERE h.url = :id " +
    1.63 +            "AND n.name = :anno_name " +
    1.64 +            "ORDER BY a.dateAdded DESC LIMIT 1 " +
    1.65 +          ")";
    1.66 +  }
    1.67 +  else
    1.68 +    do_throw("Wrong identifier type");
    1.69 +
    1.70 +  let stmt = DBConn().createStatement(sql);
    1.71 +  stmt.params.id = (typeof(aIdentifier) == "number") ? aIdentifier
    1.72 +                                                     : aIdentifier.spec;
    1.73 +  stmt.params.expire_date = expireDate;
    1.74 +  stmt.params.last_modified = lastModifiedDate;
    1.75 +  stmt.params.anno_name = aName;
    1.76 +  try {
    1.77 +    stmt.executeStep();
    1.78 +  }
    1.79 +  finally {
    1.80 +    stmt.finalize();
    1.81 +  }
    1.82 +}
    1.83 +
    1.84 +function run_test() {
    1.85 +  run_next_test();
    1.86 +}
    1.87 +
    1.88 +add_task(function test_removeAllPages() {
    1.89 +  // Set interval to a large value so we don't expire on it.
    1.90 +  setInterval(3600); // 1h
    1.91 +
    1.92 +  // Expire all expirable pages.
    1.93 +  setMaxPages(0);
    1.94 +
    1.95 +  // Add some bookmarked page with visit and annotations.
    1.96 +  for (let i = 0; i < 5; i++) {
    1.97 +    let pageURI = uri("http://item_anno." + i + ".mozilla.org/");
    1.98 +    // This visit will be expired.
    1.99 +    yield promiseAddVisits({ uri: pageURI, visitDate: now++ });
   1.100 +    let id = bs.insertBookmark(bs.unfiledBookmarksFolder, pageURI,
   1.101 +                               bs.DEFAULT_INDEX, null);
   1.102 +    // Will persist because it's an EXPIRE_NEVER item anno.
   1.103 +    as.setItemAnnotation(id, "persist", "test", 0, as.EXPIRE_NEVER);
   1.104 +    // Will persist because the page is bookmarked.
   1.105 +    as.setPageAnnotation(pageURI, "persist", "test", 0, as.EXPIRE_NEVER);
   1.106 +    // All EXPIRE_SESSION annotations are expected to expire on clear history.
   1.107 +    as.setItemAnnotation(id, "expire_session", "test", 0, as.EXPIRE_SESSION);
   1.108 +    as.setPageAnnotation(pageURI, "expire_session", "test", 0, as.EXPIRE_SESSION);
   1.109 +    // Annotations with timed policy will expire regardless bookmarked status.
   1.110 +    add_old_anno(id, "expire_days", "test", as.EXPIRE_DAYS, 8);
   1.111 +    add_old_anno(id, "expire_weeks", "test", as.EXPIRE_WEEKS, 31);
   1.112 +    add_old_anno(id, "expire_months", "test", as.EXPIRE_MONTHS, 181);
   1.113 +    add_old_anno(pageURI, "expire_days", "test", as.EXPIRE_DAYS, 8);
   1.114 +    add_old_anno(pageURI, "expire_weeks", "test", as.EXPIRE_WEEKS, 31);
   1.115 +    add_old_anno(pageURI, "expire_months", "test", as.EXPIRE_MONTHS, 181);
   1.116 +  }
   1.117 +
   1.118 +  // Add some visited page and annotations for each.
   1.119 +  let now = Date.now() * 1000;
   1.120 +  for (let i = 0; i < 5; i++) {
   1.121 +    // All page annotations related to these expired pages are expected to
   1.122 +    // expire as well.
   1.123 +    let pageURI = uri("http://page_anno." + i + ".mozilla.org/");
   1.124 +    yield promiseAddVisits({ uri: pageURI, visitDate: now++ });
   1.125 +    as.setPageAnnotation(pageURI, "expire", "test", 0, as.EXPIRE_NEVER);
   1.126 +    as.setPageAnnotation(pageURI, "expire_session", "test", 0, as.EXPIRE_SESSION);
   1.127 +    add_old_anno(pageURI, "expire_days", "test", as.EXPIRE_DAYS, 8);
   1.128 +    add_old_anno(pageURI, "expire_weeks", "test", as.EXPIRE_WEEKS, 31);
   1.129 +    add_old_anno(pageURI, "expire_months", "test", as.EXPIRE_MONTHS, 181);
   1.130 +  }
   1.131 +
   1.132 +  // Expire all visits for the bookmarks.  This does the same thing as the
   1.133 +  // promiseClearHistory helper, but it is made explicit here because
   1.134 +  // removeAllPages is the function we are testing.
   1.135 +  let promise =
   1.136 +      promiseTopicObserved(PlacesUtils.TOPIC_EXPIRATION_FINISHED);
   1.137 +  hs.QueryInterface(Ci.nsIBrowserHistory).removeAllPages();
   1.138 +  yield promise;
   1.139 +
   1.140 +  ["expire_days", "expire_weeks", "expire_months", "expire_session",
   1.141 +   "expire"].forEach(function(aAnno) {
   1.142 +    let pages = as.getPagesWithAnnotation(aAnno);
   1.143 +    do_check_eq(pages.length, 0);
   1.144 +  });
   1.145 +
   1.146 +  ["expire_days", "expire_weeks", "expire_months", "expire_session",
   1.147 +   "expire"].forEach(function(aAnno) {
   1.148 +    let items = as.getItemsWithAnnotation(aAnno);
   1.149 +    do_check_eq(items.length, 0);
   1.150 +  });
   1.151 +
   1.152 +  ["persist"].forEach(function(aAnno) {
   1.153 +    let pages = as.getPagesWithAnnotation(aAnno);
   1.154 +    do_check_eq(pages.length, 5);
   1.155 +  });
   1.156 +
   1.157 +  ["persist"].forEach(function(aAnno) {
   1.158 +    let items = as.getItemsWithAnnotation(aAnno);
   1.159 +    do_check_eq(items.length, 5);
   1.160 +    items.forEach(function(aItemId) {
   1.161 +      // Check item exists.
   1.162 +      bs.getItemIndex(aItemId);
   1.163 +    });
   1.164 +  });
   1.165 +});

mercurial