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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /**
michael@0 8 * What this is aimed to test:
michael@0 9 *
michael@0 10 * Annotations can be set with a timed expiration policy.
michael@0 11 * Supported policies are:
michael@0 12 * - EXPIRE_DAYS: annotation would be expired after 7 days
michael@0 13 * - EXPIRE_WEEKS: annotation would be expired after 30 days
michael@0 14 * - EXPIRE_MONTHS: annotation would be expired after 180 days
michael@0 15 */
michael@0 16
michael@0 17 let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
michael@0 18 getService(Ci.nsINavBookmarksService);
michael@0 19 let as = Cc["@mozilla.org/browser/annotation-service;1"].
michael@0 20 getService(Ci.nsIAnnotationService);
michael@0 21
michael@0 22 /**
michael@0 23 * Creates an aged annotation.
michael@0 24 *
michael@0 25 * @param aIdentifier Either a page url or an item id.
michael@0 26 * @param aIdentifier Name of the annotation.
michael@0 27 * @param aValue Value for the annotation.
michael@0 28 * @param aExpirePolicy Expiration policy of the annotation.
michael@0 29 * @param aAgeInDays Age in days of the annotation.
michael@0 30 * @param [optional] aLastModifiedAgeInDays Age in days of the annotation, for lastModified.
michael@0 31 */
michael@0 32 let now = Date.now();
michael@0 33 function add_old_anno(aIdentifier, aName, aValue, aExpirePolicy,
michael@0 34 aAgeInDays, aLastModifiedAgeInDays) {
michael@0 35 let expireDate = (now - (aAgeInDays * 86400 * 1000)) * 1000;
michael@0 36 let lastModifiedDate = 0;
michael@0 37 if (aLastModifiedAgeInDays)
michael@0 38 lastModifiedDate = (now - (aLastModifiedAgeInDays * 86400 * 1000)) * 1000;
michael@0 39
michael@0 40 let sql;
michael@0 41 if (typeof(aIdentifier) == "number") {
michael@0 42 // Item annotation.
michael@0 43 as.setItemAnnotation(aIdentifier, aName, aValue, 0, aExpirePolicy);
michael@0 44 // Update dateAdded for the last added annotation.
michael@0 45 sql = "UPDATE moz_items_annos SET dateAdded = :expire_date, lastModified = :last_modified " +
michael@0 46 "WHERE id = (SELECT id FROM moz_items_annos " +
michael@0 47 "WHERE item_id = :id " +
michael@0 48 "ORDER BY dateAdded DESC LIMIT 1)";
michael@0 49 }
michael@0 50 else if (aIdentifier instanceof Ci.nsIURI){
michael@0 51 // Page annotation.
michael@0 52 as.setPageAnnotation(aIdentifier, aName, aValue, 0, aExpirePolicy);
michael@0 53 // Update dateAdded for the last added annotation.
michael@0 54 sql = "UPDATE moz_annos SET dateAdded = :expire_date, lastModified = :last_modified " +
michael@0 55 "WHERE id = (SELECT a.id FROM moz_annos a " +
michael@0 56 "LEFT JOIN moz_places h on h.id = a.place_id " +
michael@0 57 "WHERE h.url = :id " +
michael@0 58 "ORDER BY a.dateAdded DESC LIMIT 1)";
michael@0 59 }
michael@0 60 else
michael@0 61 do_throw("Wrong identifier type");
michael@0 62
michael@0 63 let stmt = DBConn().createStatement(sql);
michael@0 64 stmt.params.id = (typeof(aIdentifier) == "number") ? aIdentifier
michael@0 65 : aIdentifier.spec;
michael@0 66 stmt.params.expire_date = expireDate;
michael@0 67 stmt.params.last_modified = lastModifiedDate;
michael@0 68 try {
michael@0 69 stmt.executeStep();
michael@0 70 }
michael@0 71 finally {
michael@0 72 stmt.finalize();
michael@0 73 }
michael@0 74 }
michael@0 75
michael@0 76 function run_test() {
michael@0 77 run_next_test();
michael@0 78 }
michael@0 79
michael@0 80 add_task(function test_annos_expire_policy() {
michael@0 81 // Set interval to a large value so we don't expire on it.
michael@0 82 setInterval(3600); // 1h
michael@0 83
michael@0 84 // Expire all expirable pages.
michael@0 85 setMaxPages(0);
michael@0 86
michael@0 87 let now = getExpirablePRTime();
michael@0 88 // Add some bookmarked page and timed annotations for each.
michael@0 89 for (let i = 0; i < 5; i++) {
michael@0 90 let pageURI = uri("http://item_anno." + i + ".mozilla.org/");
michael@0 91 yield promiseAddVisits({ uri: pageURI, visitDate: now++ });
michael@0 92 let id = bs.insertBookmark(bs.unfiledBookmarksFolder, pageURI,
michael@0 93 bs.DEFAULT_INDEX, null);
michael@0 94 // Add a 6 days old anno.
michael@0 95 add_old_anno(id, "persist_days", "test", as.EXPIRE_DAYS, 6);
michael@0 96 // Add a 8 days old anno, modified 5 days ago.
michael@0 97 add_old_anno(id, "persist_lm_days", "test", as.EXPIRE_DAYS, 8, 6);
michael@0 98 // Add a 8 days old anno.
michael@0 99 add_old_anno(id, "expire_days", "test", as.EXPIRE_DAYS, 8);
michael@0 100
michael@0 101 // Add a 29 days old anno.
michael@0 102 add_old_anno(id, "persist_weeks", "test", as.EXPIRE_WEEKS, 29);
michael@0 103 // Add a 31 days old anno, modified 29 days ago.
michael@0 104 add_old_anno(id, "persist_lm_weeks", "test", as.EXPIRE_WEEKS, 31, 29);
michael@0 105 // Add a 31 days old anno.
michael@0 106 add_old_anno(id, "expire_weeks", "test", as.EXPIRE_WEEKS, 31);
michael@0 107
michael@0 108 // Add a 179 days old anno.
michael@0 109 add_old_anno(id, "persist_months", "test", as.EXPIRE_MONTHS, 179);
michael@0 110 // Add a 181 days old anno, modified 179 days ago.
michael@0 111 add_old_anno(id, "persist_lm_months", "test", as.EXPIRE_MONTHS, 181, 179);
michael@0 112 // Add a 181 days old anno.
michael@0 113 add_old_anno(id, "expire_months", "test", as.EXPIRE_MONTHS, 181);
michael@0 114
michael@0 115 // Add a 6 days old anno.
michael@0 116 add_old_anno(pageURI, "persist_days", "test", as.EXPIRE_DAYS, 6);
michael@0 117 // Add a 8 days old anno, modified 5 days ago.
michael@0 118 add_old_anno(pageURI, "persist_lm_days", "test", as.EXPIRE_DAYS, 8, 6);
michael@0 119 // Add a 8 days old anno.
michael@0 120 add_old_anno(pageURI, "expire_days", "test", as.EXPIRE_DAYS, 8);
michael@0 121
michael@0 122 // Add a 29 days old anno.
michael@0 123 add_old_anno(pageURI, "persist_weeks", "test", as.EXPIRE_WEEKS, 29);
michael@0 124 // Add a 31 days old anno, modified 29 days ago.
michael@0 125 add_old_anno(pageURI, "persist_lm_weeks", "test", as.EXPIRE_WEEKS, 31, 29);
michael@0 126 // Add a 31 days old anno.
michael@0 127 add_old_anno(pageURI, "expire_weeks", "test", as.EXPIRE_WEEKS, 31);
michael@0 128
michael@0 129 // Add a 179 days old anno.
michael@0 130 add_old_anno(pageURI, "persist_months", "test", as.EXPIRE_MONTHS, 179);
michael@0 131 // Add a 181 days old anno, modified 179 days ago.
michael@0 132 add_old_anno(pageURI, "persist_lm_months", "test", as.EXPIRE_MONTHS, 181, 179);
michael@0 133 // Add a 181 days old anno.
michael@0 134 add_old_anno(pageURI, "expire_months", "test", as.EXPIRE_MONTHS, 181);
michael@0 135 }
michael@0 136
michael@0 137 // Add some visited page and timed annotations for each.
michael@0 138 for (let i = 0; i < 5; i++) {
michael@0 139 let pageURI = uri("http://page_anno." + i + ".mozilla.org/");
michael@0 140 yield promiseAddVisits({ uri: pageURI, visitDate: now++ });
michael@0 141 // Add a 6 days old anno.
michael@0 142 add_old_anno(pageURI, "persist_days", "test", as.EXPIRE_DAYS, 6);
michael@0 143 // Add a 8 days old anno, modified 5 days ago.
michael@0 144 add_old_anno(pageURI, "persist_lm_days", "test", as.EXPIRE_DAYS, 8, 6);
michael@0 145 // Add a 8 days old anno.
michael@0 146 add_old_anno(pageURI, "expire_days", "test", as.EXPIRE_DAYS, 8);
michael@0 147
michael@0 148 // Add a 29 days old anno.
michael@0 149 add_old_anno(pageURI, "persist_weeks", "test", as.EXPIRE_WEEKS, 29);
michael@0 150 // Add a 31 days old anno, modified 29 days ago.
michael@0 151 add_old_anno(pageURI, "persist_lm_weeks", "test", as.EXPIRE_WEEKS, 31, 29);
michael@0 152 // Add a 31 days old anno.
michael@0 153 add_old_anno(pageURI, "expire_weeks", "test", as.EXPIRE_WEEKS, 31);
michael@0 154
michael@0 155 // Add a 179 days old anno.
michael@0 156 add_old_anno(pageURI, "persist_months", "test", as.EXPIRE_MONTHS, 179);
michael@0 157 // Add a 181 days old anno, modified 179 days ago.
michael@0 158 add_old_anno(pageURI, "persist_lm_months", "test", as.EXPIRE_MONTHS, 181, 179);
michael@0 159 // Add a 181 days old anno.
michael@0 160 add_old_anno(pageURI, "expire_months", "test", as.EXPIRE_MONTHS, 181);
michael@0 161 }
michael@0 162
michael@0 163 // Expire all visits for the bookmarks.
michael@0 164 yield promiseForceExpirationStep(5);
michael@0 165
michael@0 166 ["expire_days", "expire_weeks", "expire_months"].forEach(function(aAnno) {
michael@0 167 let pages = as.getPagesWithAnnotation(aAnno);
michael@0 168 do_check_eq(pages.length, 0);
michael@0 169 });
michael@0 170
michael@0 171 ["expire_days", "expire_weeks", "expire_months"].forEach(function(aAnno) {
michael@0 172 let items = as.getItemsWithAnnotation(aAnno);
michael@0 173 do_check_eq(items.length, 0);
michael@0 174 });
michael@0 175
michael@0 176 ["persist_days", "persist_lm_days", "persist_weeks", "persist_lm_weeks",
michael@0 177 "persist_months", "persist_lm_months"].forEach(function(aAnno) {
michael@0 178 let pages = as.getPagesWithAnnotation(aAnno);
michael@0 179 do_check_eq(pages.length, 10);
michael@0 180 });
michael@0 181
michael@0 182 ["persist_days", "persist_lm_days", "persist_weeks", "persist_lm_weeks",
michael@0 183 "persist_months", "persist_lm_months"].forEach(function(aAnno) {
michael@0 184 let items = as.getItemsWithAnnotation(aAnno);
michael@0 185 do_check_eq(items.length, 5);
michael@0 186 });
michael@0 187 });

mercurial