1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/expiration/test_annos_expire_policy.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,187 @@ 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 + * Annotations can be set with a timed expiration policy. 1.14 + * Supported policies are: 1.15 + * - EXPIRE_DAYS: annotation would be expired after 7 days 1.16 + * - EXPIRE_WEEKS: annotation would be expired after 30 days 1.17 + * - EXPIRE_MONTHS: annotation would be expired after 180 days 1.18 + */ 1.19 + 1.20 +let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]. 1.21 + getService(Ci.nsINavBookmarksService); 1.22 +let as = Cc["@mozilla.org/browser/annotation-service;1"]. 1.23 + getService(Ci.nsIAnnotationService); 1.24 + 1.25 +/** 1.26 + * Creates an aged annotation. 1.27 + * 1.28 + * @param aIdentifier Either a page url or an item id. 1.29 + * @param aIdentifier Name of the annotation. 1.30 + * @param aValue Value for the annotation. 1.31 + * @param aExpirePolicy Expiration policy of the annotation. 1.32 + * @param aAgeInDays Age in days of the annotation. 1.33 + * @param [optional] aLastModifiedAgeInDays Age in days of the annotation, for lastModified. 1.34 + */ 1.35 +let now = Date.now(); 1.36 +function add_old_anno(aIdentifier, aName, aValue, aExpirePolicy, 1.37 + aAgeInDays, aLastModifiedAgeInDays) { 1.38 + let expireDate = (now - (aAgeInDays * 86400 * 1000)) * 1000; 1.39 + let lastModifiedDate = 0; 1.40 + if (aLastModifiedAgeInDays) 1.41 + lastModifiedDate = (now - (aLastModifiedAgeInDays * 86400 * 1000)) * 1000; 1.42 + 1.43 + let sql; 1.44 + if (typeof(aIdentifier) == "number") { 1.45 + // Item annotation. 1.46 + as.setItemAnnotation(aIdentifier, aName, aValue, 0, aExpirePolicy); 1.47 + // Update dateAdded for the last added annotation. 1.48 + sql = "UPDATE moz_items_annos SET dateAdded = :expire_date, lastModified = :last_modified " + 1.49 + "WHERE id = (SELECT id FROM moz_items_annos " + 1.50 + "WHERE item_id = :id " + 1.51 + "ORDER BY dateAdded DESC LIMIT 1)"; 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 = (SELECT a.id FROM moz_annos a " + 1.59 + "LEFT JOIN moz_places h on h.id = a.place_id " + 1.60 + "WHERE h.url = :id " + 1.61 + "ORDER BY a.dateAdded DESC LIMIT 1)"; 1.62 + } 1.63 + else 1.64 + do_throw("Wrong identifier type"); 1.65 + 1.66 + let stmt = DBConn().createStatement(sql); 1.67 + stmt.params.id = (typeof(aIdentifier) == "number") ? aIdentifier 1.68 + : aIdentifier.spec; 1.69 + stmt.params.expire_date = expireDate; 1.70 + stmt.params.last_modified = lastModifiedDate; 1.71 + try { 1.72 + stmt.executeStep(); 1.73 + } 1.74 + finally { 1.75 + stmt.finalize(); 1.76 + } 1.77 +} 1.78 + 1.79 +function run_test() { 1.80 + run_next_test(); 1.81 +} 1.82 + 1.83 +add_task(function test_annos_expire_policy() { 1.84 + // Set interval to a large value so we don't expire on it. 1.85 + setInterval(3600); // 1h 1.86 + 1.87 + // Expire all expirable pages. 1.88 + setMaxPages(0); 1.89 + 1.90 + let now = getExpirablePRTime(); 1.91 + // Add some bookmarked page and timed annotations for each. 1.92 + for (let i = 0; i < 5; i++) { 1.93 + let pageURI = uri("http://item_anno." + i + ".mozilla.org/"); 1.94 + yield promiseAddVisits({ uri: pageURI, visitDate: now++ }); 1.95 + let id = bs.insertBookmark(bs.unfiledBookmarksFolder, pageURI, 1.96 + bs.DEFAULT_INDEX, null); 1.97 + // Add a 6 days old anno. 1.98 + add_old_anno(id, "persist_days", "test", as.EXPIRE_DAYS, 6); 1.99 + // Add a 8 days old anno, modified 5 days ago. 1.100 + add_old_anno(id, "persist_lm_days", "test", as.EXPIRE_DAYS, 8, 6); 1.101 + // Add a 8 days old anno. 1.102 + add_old_anno(id, "expire_days", "test", as.EXPIRE_DAYS, 8); 1.103 + 1.104 + // Add a 29 days old anno. 1.105 + add_old_anno(id, "persist_weeks", "test", as.EXPIRE_WEEKS, 29); 1.106 + // Add a 31 days old anno, modified 29 days ago. 1.107 + add_old_anno(id, "persist_lm_weeks", "test", as.EXPIRE_WEEKS, 31, 29); 1.108 + // Add a 31 days old anno. 1.109 + add_old_anno(id, "expire_weeks", "test", as.EXPIRE_WEEKS, 31); 1.110 + 1.111 + // Add a 179 days old anno. 1.112 + add_old_anno(id, "persist_months", "test", as.EXPIRE_MONTHS, 179); 1.113 + // Add a 181 days old anno, modified 179 days ago. 1.114 + add_old_anno(id, "persist_lm_months", "test", as.EXPIRE_MONTHS, 181, 179); 1.115 + // Add a 181 days old anno. 1.116 + add_old_anno(id, "expire_months", "test", as.EXPIRE_MONTHS, 181); 1.117 + 1.118 + // Add a 6 days old anno. 1.119 + add_old_anno(pageURI, "persist_days", "test", as.EXPIRE_DAYS, 6); 1.120 + // Add a 8 days old anno, modified 5 days ago. 1.121 + add_old_anno(pageURI, "persist_lm_days", "test", as.EXPIRE_DAYS, 8, 6); 1.122 + // Add a 8 days old anno. 1.123 + add_old_anno(pageURI, "expire_days", "test", as.EXPIRE_DAYS, 8); 1.124 + 1.125 + // Add a 29 days old anno. 1.126 + add_old_anno(pageURI, "persist_weeks", "test", as.EXPIRE_WEEKS, 29); 1.127 + // Add a 31 days old anno, modified 29 days ago. 1.128 + add_old_anno(pageURI, "persist_lm_weeks", "test", as.EXPIRE_WEEKS, 31, 29); 1.129 + // Add a 31 days old anno. 1.130 + add_old_anno(pageURI, "expire_weeks", "test", as.EXPIRE_WEEKS, 31); 1.131 + 1.132 + // Add a 179 days old anno. 1.133 + add_old_anno(pageURI, "persist_months", "test", as.EXPIRE_MONTHS, 179); 1.134 + // Add a 181 days old anno, modified 179 days ago. 1.135 + add_old_anno(pageURI, "persist_lm_months", "test", as.EXPIRE_MONTHS, 181, 179); 1.136 + // Add a 181 days old anno. 1.137 + add_old_anno(pageURI, "expire_months", "test", as.EXPIRE_MONTHS, 181); 1.138 + } 1.139 + 1.140 + // Add some visited page and timed annotations for each. 1.141 + for (let i = 0; i < 5; i++) { 1.142 + let pageURI = uri("http://page_anno." + i + ".mozilla.org/"); 1.143 + yield promiseAddVisits({ uri: pageURI, visitDate: now++ }); 1.144 + // Add a 6 days old anno. 1.145 + add_old_anno(pageURI, "persist_days", "test", as.EXPIRE_DAYS, 6); 1.146 + // Add a 8 days old anno, modified 5 days ago. 1.147 + add_old_anno(pageURI, "persist_lm_days", "test", as.EXPIRE_DAYS, 8, 6); 1.148 + // Add a 8 days old anno. 1.149 + add_old_anno(pageURI, "expire_days", "test", as.EXPIRE_DAYS, 8); 1.150 + 1.151 + // Add a 29 days old anno. 1.152 + add_old_anno(pageURI, "persist_weeks", "test", as.EXPIRE_WEEKS, 29); 1.153 + // Add a 31 days old anno, modified 29 days ago. 1.154 + add_old_anno(pageURI, "persist_lm_weeks", "test", as.EXPIRE_WEEKS, 31, 29); 1.155 + // Add a 31 days old anno. 1.156 + add_old_anno(pageURI, "expire_weeks", "test", as.EXPIRE_WEEKS, 31); 1.157 + 1.158 + // Add a 179 days old anno. 1.159 + add_old_anno(pageURI, "persist_months", "test", as.EXPIRE_MONTHS, 179); 1.160 + // Add a 181 days old anno, modified 179 days ago. 1.161 + add_old_anno(pageURI, "persist_lm_months", "test", as.EXPIRE_MONTHS, 181, 179); 1.162 + // Add a 181 days old anno. 1.163 + add_old_anno(pageURI, "expire_months", "test", as.EXPIRE_MONTHS, 181); 1.164 + } 1.165 + 1.166 + // Expire all visits for the bookmarks. 1.167 + yield promiseForceExpirationStep(5); 1.168 + 1.169 + ["expire_days", "expire_weeks", "expire_months"].forEach(function(aAnno) { 1.170 + let pages = as.getPagesWithAnnotation(aAnno); 1.171 + do_check_eq(pages.length, 0); 1.172 + }); 1.173 + 1.174 + ["expire_days", "expire_weeks", "expire_months"].forEach(function(aAnno) { 1.175 + let items = as.getItemsWithAnnotation(aAnno); 1.176 + do_check_eq(items.length, 0); 1.177 + }); 1.178 + 1.179 + ["persist_days", "persist_lm_days", "persist_weeks", "persist_lm_weeks", 1.180 + "persist_months", "persist_lm_months"].forEach(function(aAnno) { 1.181 + let pages = as.getPagesWithAnnotation(aAnno); 1.182 + do_check_eq(pages.length, 10); 1.183 + }); 1.184 + 1.185 + ["persist_days", "persist_lm_days", "persist_weeks", "persist_lm_weeks", 1.186 + "persist_months", "persist_lm_months"].forEach(function(aAnno) { 1.187 + let items = as.getItemsWithAnnotation(aAnno); 1.188 + do_check_eq(items.length, 5); 1.189 + }); 1.190 +});