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: * bh.removeAllPages should expire everything but bookmarked pages and valid michael@0: * annos. michael@0: */ michael@0: michael@0: let hs = PlacesUtils.history; michael@0: let bs = PlacesUtils.bookmarks; michael@0: let as = PlacesUtils.annotations; 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 = ( " + michael@0: "SELECT a.id FROM moz_items_annos a " + michael@0: "JOIN moz_anno_attributes n ON n.id = a.anno_attribute_id " + michael@0: "WHERE a.item_id = :id " + michael@0: "AND n.name = :anno_name " + michael@0: "ORDER BY a.dateAdded DESC LIMIT 1 " + michael@0: ")"; 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 = ( " + michael@0: "SELECT a.id FROM moz_annos a " + michael@0: "JOIN moz_anno_attributes n ON n.id = a.anno_attribute_id " + michael@0: "JOIN moz_places h on h.id = a.place_id " + michael@0: "WHERE h.url = :id " + michael@0: "AND n.name = :anno_name " + michael@0: "ORDER BY a.dateAdded DESC LIMIT 1 " + michael@0: ")"; 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: stmt.params.anno_name = aName; 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_removeAllPages() { 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: // Add some bookmarked page with visit and annotations. michael@0: for (let i = 0; i < 5; i++) { michael@0: let pageURI = uri("http://item_anno." + i + ".mozilla.org/"); michael@0: // This visit will be expired. 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: // Will persist because it's an EXPIRE_NEVER item anno. michael@0: as.setItemAnnotation(id, "persist", "test", 0, as.EXPIRE_NEVER); michael@0: // Will persist because the page is bookmarked. michael@0: as.setPageAnnotation(pageURI, "persist", "test", 0, as.EXPIRE_NEVER); michael@0: // All EXPIRE_SESSION annotations are expected to expire on clear history. michael@0: as.setItemAnnotation(id, "expire_session", "test", 0, as.EXPIRE_SESSION); michael@0: as.setPageAnnotation(pageURI, "expire_session", "test", 0, as.EXPIRE_SESSION); michael@0: // Annotations with timed policy will expire regardless bookmarked status. michael@0: add_old_anno(id, "expire_days", "test", as.EXPIRE_DAYS, 8); michael@0: add_old_anno(id, "expire_weeks", "test", as.EXPIRE_WEEKS, 31); michael@0: add_old_anno(id, "expire_months", "test", as.EXPIRE_MONTHS, 181); michael@0: add_old_anno(pageURI, "expire_days", "test", as.EXPIRE_DAYS, 8); michael@0: add_old_anno(pageURI, "expire_weeks", "test", as.EXPIRE_WEEKS, 31); michael@0: add_old_anno(pageURI, "expire_months", "test", as.EXPIRE_MONTHS, 181); michael@0: } michael@0: michael@0: // Add some visited page and annotations for each. michael@0: let now = Date.now() * 1000; michael@0: for (let i = 0; i < 5; i++) { michael@0: // All page annotations related to these expired pages are expected to michael@0: // expire as well. michael@0: let pageURI = uri("http://page_anno." + i + ".mozilla.org/"); michael@0: yield promiseAddVisits({ uri: pageURI, visitDate: now++ }); michael@0: as.setPageAnnotation(pageURI, "expire", "test", 0, as.EXPIRE_NEVER); michael@0: as.setPageAnnotation(pageURI, "expire_session", "test", 0, as.EXPIRE_SESSION); michael@0: add_old_anno(pageURI, "expire_days", "test", as.EXPIRE_DAYS, 8); michael@0: add_old_anno(pageURI, "expire_weeks", "test", as.EXPIRE_WEEKS, 31); 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. This does the same thing as the michael@0: // promiseClearHistory helper, but it is made explicit here because michael@0: // removeAllPages is the function we are testing. michael@0: let promise = michael@0: promiseTopicObserved(PlacesUtils.TOPIC_EXPIRATION_FINISHED); michael@0: hs.QueryInterface(Ci.nsIBrowserHistory).removeAllPages(); michael@0: yield promise; michael@0: michael@0: ["expire_days", "expire_weeks", "expire_months", "expire_session", michael@0: "expire"].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", "expire_session", michael@0: "expire"].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"].forEach(function(aAnno) { michael@0: let pages = as.getPagesWithAnnotation(aAnno); michael@0: do_check_eq(pages.length, 5); michael@0: }); michael@0: michael@0: ["persist"].forEach(function(aAnno) { michael@0: let items = as.getItemsWithAnnotation(aAnno); michael@0: do_check_eq(items.length, 5); michael@0: items.forEach(function(aItemId) { michael@0: // Check item exists. michael@0: bs.getItemIndex(aItemId); michael@0: }); michael@0: }); michael@0: });