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