michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * What this is aimed to test: michael@0: * michael@0: * Annotations can be set with a timed expiration policy. michael@0: * Supported policies are: michael@0: * - EXPIRE_DAYS: annotation would be expired after 7 days michael@0: * - EXPIRE_WEEKS: annotation would be expired after 30 days michael@0: * - EXPIRE_MONTHS: annotation would be expired after 180 days michael@0: */ michael@0: michael@0: let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]. michael@0: getService(Ci.nsINavBookmarksService); michael@0: let as = Cc["@mozilla.org/browser/annotation-service;1"]. michael@0: getService(Ci.nsIAnnotationService); michael@0: michael@0: /** michael@0: * Creates an aged annotation. michael@0: * michael@0: * @param aIdentifier Either a page url or an item id. michael@0: * @param aIdentifier Name of the annotation. michael@0: * @param aValue Value for the annotation. michael@0: * @param aExpirePolicy Expiration policy of the annotation. michael@0: * @param aAgeInDays Age in days of the annotation. michael@0: * @param [optional] aLastModifiedAgeInDays Age in days of the annotation, for lastModified. michael@0: */ michael@0: let now = Date.now(); michael@0: function add_old_anno(aIdentifier, aName, aValue, aExpirePolicy, michael@0: aAgeInDays, aLastModifiedAgeInDays) { michael@0: let expireDate = (now - (aAgeInDays * 86400 * 1000)) * 1000; michael@0: let lastModifiedDate = 0; michael@0: if (aLastModifiedAgeInDays) michael@0: lastModifiedDate = (now - (aLastModifiedAgeInDays * 86400 * 1000)) * 1000; michael@0: michael@0: let sql; michael@0: if (typeof(aIdentifier) == "number") { michael@0: // Item annotation. michael@0: as.setItemAnnotation(aIdentifier, aName, aValue, 0, aExpirePolicy); michael@0: // Update dateAdded for the last added annotation. michael@0: sql = "UPDATE moz_items_annos SET dateAdded = :expire_date, lastModified = :last_modified " + michael@0: "WHERE id = (SELECT id FROM moz_items_annos " + michael@0: "WHERE item_id = :id " + michael@0: "ORDER BY dateAdded DESC LIMIT 1)"; michael@0: } michael@0: else if (aIdentifier instanceof Ci.nsIURI){ michael@0: // Page annotation. michael@0: as.setPageAnnotation(aIdentifier, aName, aValue, 0, aExpirePolicy); michael@0: // Update dateAdded for the last added annotation. michael@0: sql = "UPDATE moz_annos SET dateAdded = :expire_date, lastModified = :last_modified " + michael@0: "WHERE id = (SELECT a.id FROM moz_annos a " + michael@0: "LEFT JOIN moz_places h on h.id = a.place_id " + michael@0: "WHERE h.url = :id " + michael@0: "ORDER BY a.dateAdded DESC LIMIT 1)"; michael@0: } michael@0: else michael@0: do_throw("Wrong identifier type"); michael@0: michael@0: let stmt = DBConn().createStatement(sql); michael@0: stmt.params.id = (typeof(aIdentifier) == "number") ? aIdentifier michael@0: : aIdentifier.spec; michael@0: stmt.params.expire_date = expireDate; michael@0: stmt.params.last_modified = lastModifiedDate; michael@0: try { michael@0: stmt.executeStep(); michael@0: } michael@0: finally { michael@0: stmt.finalize(); michael@0: } michael@0: } michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function test_annos_expire_policy() { michael@0: // Set interval to a large value so we don't expire on it. michael@0: setInterval(3600); // 1h michael@0: michael@0: // Expire all expirable pages. michael@0: setMaxPages(0); michael@0: michael@0: let now = getExpirablePRTime(); michael@0: // Add some bookmarked page and timed annotations for each. michael@0: for (let i = 0; i < 5; i++) { michael@0: let pageURI = uri("http://item_anno." + i + ".mozilla.org/"); michael@0: yield promiseAddVisits({ uri: pageURI, visitDate: now++ }); michael@0: let id = bs.insertBookmark(bs.unfiledBookmarksFolder, pageURI, michael@0: bs.DEFAULT_INDEX, null); michael@0: // Add a 6 days old anno. michael@0: add_old_anno(id, "persist_days", "test", as.EXPIRE_DAYS, 6); michael@0: // Add a 8 days old anno, modified 5 days ago. michael@0: add_old_anno(id, "persist_lm_days", "test", as.EXPIRE_DAYS, 8, 6); michael@0: // Add a 8 days old anno. michael@0: add_old_anno(id, "expire_days", "test", as.EXPIRE_DAYS, 8); michael@0: michael@0: // Add a 29 days old anno. michael@0: add_old_anno(id, "persist_weeks", "test", as.EXPIRE_WEEKS, 29); michael@0: // Add a 31 days old anno, modified 29 days ago. michael@0: add_old_anno(id, "persist_lm_weeks", "test", as.EXPIRE_WEEKS, 31, 29); michael@0: // Add a 31 days old anno. michael@0: add_old_anno(id, "expire_weeks", "test", as.EXPIRE_WEEKS, 31); michael@0: michael@0: // Add a 179 days old anno. michael@0: add_old_anno(id, "persist_months", "test", as.EXPIRE_MONTHS, 179); michael@0: // Add a 181 days old anno, modified 179 days ago. michael@0: add_old_anno(id, "persist_lm_months", "test", as.EXPIRE_MONTHS, 181, 179); michael@0: // Add a 181 days old anno. michael@0: add_old_anno(id, "expire_months", "test", as.EXPIRE_MONTHS, 181); michael@0: michael@0: // Add a 6 days old anno. michael@0: add_old_anno(pageURI, "persist_days", "test", as.EXPIRE_DAYS, 6); michael@0: // Add a 8 days old anno, modified 5 days ago. michael@0: add_old_anno(pageURI, "persist_lm_days", "test", as.EXPIRE_DAYS, 8, 6); michael@0: // Add a 8 days old anno. michael@0: add_old_anno(pageURI, "expire_days", "test", as.EXPIRE_DAYS, 8); michael@0: michael@0: // Add a 29 days old anno. michael@0: add_old_anno(pageURI, "persist_weeks", "test", as.EXPIRE_WEEKS, 29); michael@0: // Add a 31 days old anno, modified 29 days ago. michael@0: add_old_anno(pageURI, "persist_lm_weeks", "test", as.EXPIRE_WEEKS, 31, 29); michael@0: // Add a 31 days old anno. michael@0: add_old_anno(pageURI, "expire_weeks", "test", as.EXPIRE_WEEKS, 31); michael@0: michael@0: // Add a 179 days old anno. michael@0: add_old_anno(pageURI, "persist_months", "test", as.EXPIRE_MONTHS, 179); michael@0: // Add a 181 days old anno, modified 179 days ago. michael@0: add_old_anno(pageURI, "persist_lm_months", "test", as.EXPIRE_MONTHS, 181, 179); michael@0: // Add a 181 days old anno. michael@0: add_old_anno(pageURI, "expire_months", "test", as.EXPIRE_MONTHS, 181); michael@0: } michael@0: michael@0: // Add some visited page and timed annotations for each. michael@0: for (let i = 0; i < 5; i++) { michael@0: let pageURI = uri("http://page_anno." + i + ".mozilla.org/"); michael@0: yield promiseAddVisits({ uri: pageURI, visitDate: now++ }); michael@0: // Add a 6 days old anno. michael@0: add_old_anno(pageURI, "persist_days", "test", as.EXPIRE_DAYS, 6); michael@0: // Add a 8 days old anno, modified 5 days ago. michael@0: add_old_anno(pageURI, "persist_lm_days", "test", as.EXPIRE_DAYS, 8, 6); michael@0: // Add a 8 days old anno. michael@0: add_old_anno(pageURI, "expire_days", "test", as.EXPIRE_DAYS, 8); michael@0: michael@0: // Add a 29 days old anno. michael@0: add_old_anno(pageURI, "persist_weeks", "test", as.EXPIRE_WEEKS, 29); michael@0: // Add a 31 days old anno, modified 29 days ago. michael@0: add_old_anno(pageURI, "persist_lm_weeks", "test", as.EXPIRE_WEEKS, 31, 29); michael@0: // Add a 31 days old anno. michael@0: add_old_anno(pageURI, "expire_weeks", "test", as.EXPIRE_WEEKS, 31); michael@0: michael@0: // Add a 179 days old anno. michael@0: add_old_anno(pageURI, "persist_months", "test", as.EXPIRE_MONTHS, 179); michael@0: // Add a 181 days old anno, modified 179 days ago. michael@0: add_old_anno(pageURI, "persist_lm_months", "test", as.EXPIRE_MONTHS, 181, 179); michael@0: // Add a 181 days old anno. michael@0: add_old_anno(pageURI, "expire_months", "test", as.EXPIRE_MONTHS, 181); michael@0: } michael@0: michael@0: // Expire all visits for the bookmarks. michael@0: yield promiseForceExpirationStep(5); michael@0: michael@0: ["expire_days", "expire_weeks", "expire_months"].forEach(function(aAnno) { michael@0: let pages = as.getPagesWithAnnotation(aAnno); michael@0: do_check_eq(pages.length, 0); michael@0: }); michael@0: michael@0: ["expire_days", "expire_weeks", "expire_months"].forEach(function(aAnno) { michael@0: let items = as.getItemsWithAnnotation(aAnno); michael@0: do_check_eq(items.length, 0); michael@0: }); michael@0: michael@0: ["persist_days", "persist_lm_days", "persist_weeks", "persist_lm_weeks", michael@0: "persist_months", "persist_lm_months"].forEach(function(aAnno) { michael@0: let pages = as.getPagesWithAnnotation(aAnno); michael@0: do_check_eq(pages.length, 10); michael@0: }); michael@0: michael@0: ["persist_days", "persist_lm_days", "persist_weeks", "persist_lm_weeks", michael@0: "persist_months", "persist_lm_months"].forEach(function(aAnno) { michael@0: let items = as.getItemsWithAnnotation(aAnno); michael@0: do_check_eq(items.length, 5); michael@0: }); michael@0: });