|
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 * bh.removeAllPages should expire everything but bookmarked pages and valid |
|
11 * annos. |
|
12 */ |
|
13 |
|
14 let hs = PlacesUtils.history; |
|
15 let bs = PlacesUtils.bookmarks; |
|
16 let as = PlacesUtils.annotations; |
|
17 |
|
18 /** |
|
19 * Creates an aged annotation. |
|
20 * |
|
21 * @param aIdentifier Either a page url or an item id. |
|
22 * @param aIdentifier Name of the annotation. |
|
23 * @param aValue Value for the annotation. |
|
24 * @param aExpirePolicy Expiration policy of the annotation. |
|
25 * @param aAgeInDays Age in days of the annotation. |
|
26 * @param [optional] aLastModifiedAgeInDays Age in days of the annotation, for lastModified. |
|
27 */ |
|
28 let now = Date.now(); |
|
29 function add_old_anno(aIdentifier, aName, aValue, aExpirePolicy, |
|
30 aAgeInDays, aLastModifiedAgeInDays) { |
|
31 let expireDate = (now - (aAgeInDays * 86400 * 1000)) * 1000; |
|
32 let lastModifiedDate = 0; |
|
33 if (aLastModifiedAgeInDays) |
|
34 lastModifiedDate = (now - (aLastModifiedAgeInDays * 86400 * 1000)) * 1000; |
|
35 |
|
36 let sql; |
|
37 if (typeof(aIdentifier) == "number") { |
|
38 // Item annotation. |
|
39 as.setItemAnnotation(aIdentifier, aName, aValue, 0, aExpirePolicy); |
|
40 // Update dateAdded for the last added annotation. |
|
41 sql = "UPDATE moz_items_annos SET dateAdded = :expire_date, lastModified = :last_modified " + |
|
42 "WHERE id = ( " + |
|
43 "SELECT a.id FROM moz_items_annos a " + |
|
44 "JOIN moz_anno_attributes n ON n.id = a.anno_attribute_id " + |
|
45 "WHERE a.item_id = :id " + |
|
46 "AND n.name = :anno_name " + |
|
47 "ORDER BY a.dateAdded DESC LIMIT 1 " + |
|
48 ")"; |
|
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 = ( " + |
|
56 "SELECT a.id FROM moz_annos a " + |
|
57 "JOIN moz_anno_attributes n ON n.id = a.anno_attribute_id " + |
|
58 "JOIN moz_places h on h.id = a.place_id " + |
|
59 "WHERE h.url = :id " + |
|
60 "AND n.name = :anno_name " + |
|
61 "ORDER BY a.dateAdded DESC LIMIT 1 " + |
|
62 ")"; |
|
63 } |
|
64 else |
|
65 do_throw("Wrong identifier type"); |
|
66 |
|
67 let stmt = DBConn().createStatement(sql); |
|
68 stmt.params.id = (typeof(aIdentifier) == "number") ? aIdentifier |
|
69 : aIdentifier.spec; |
|
70 stmt.params.expire_date = expireDate; |
|
71 stmt.params.last_modified = lastModifiedDate; |
|
72 stmt.params.anno_name = aName; |
|
73 try { |
|
74 stmt.executeStep(); |
|
75 } |
|
76 finally { |
|
77 stmt.finalize(); |
|
78 } |
|
79 } |
|
80 |
|
81 function run_test() { |
|
82 run_next_test(); |
|
83 } |
|
84 |
|
85 add_task(function test_removeAllPages() { |
|
86 // Set interval to a large value so we don't expire on it. |
|
87 setInterval(3600); // 1h |
|
88 |
|
89 // Expire all expirable pages. |
|
90 setMaxPages(0); |
|
91 |
|
92 // Add some bookmarked page with visit and annotations. |
|
93 for (let i = 0; i < 5; i++) { |
|
94 let pageURI = uri("http://item_anno." + i + ".mozilla.org/"); |
|
95 // This visit will be expired. |
|
96 yield promiseAddVisits({ uri: pageURI, visitDate: now++ }); |
|
97 let id = bs.insertBookmark(bs.unfiledBookmarksFolder, pageURI, |
|
98 bs.DEFAULT_INDEX, null); |
|
99 // Will persist because it's an EXPIRE_NEVER item anno. |
|
100 as.setItemAnnotation(id, "persist", "test", 0, as.EXPIRE_NEVER); |
|
101 // Will persist because the page is bookmarked. |
|
102 as.setPageAnnotation(pageURI, "persist", "test", 0, as.EXPIRE_NEVER); |
|
103 // All EXPIRE_SESSION annotations are expected to expire on clear history. |
|
104 as.setItemAnnotation(id, "expire_session", "test", 0, as.EXPIRE_SESSION); |
|
105 as.setPageAnnotation(pageURI, "expire_session", "test", 0, as.EXPIRE_SESSION); |
|
106 // Annotations with timed policy will expire regardless bookmarked status. |
|
107 add_old_anno(id, "expire_days", "test", as.EXPIRE_DAYS, 8); |
|
108 add_old_anno(id, "expire_weeks", "test", as.EXPIRE_WEEKS, 31); |
|
109 add_old_anno(id, "expire_months", "test", as.EXPIRE_MONTHS, 181); |
|
110 add_old_anno(pageURI, "expire_days", "test", as.EXPIRE_DAYS, 8); |
|
111 add_old_anno(pageURI, "expire_weeks", "test", as.EXPIRE_WEEKS, 31); |
|
112 add_old_anno(pageURI, "expire_months", "test", as.EXPIRE_MONTHS, 181); |
|
113 } |
|
114 |
|
115 // Add some visited page and annotations for each. |
|
116 let now = Date.now() * 1000; |
|
117 for (let i = 0; i < 5; i++) { |
|
118 // All page annotations related to these expired pages are expected to |
|
119 // expire as well. |
|
120 let pageURI = uri("http://page_anno." + i + ".mozilla.org/"); |
|
121 yield promiseAddVisits({ uri: pageURI, visitDate: now++ }); |
|
122 as.setPageAnnotation(pageURI, "expire", "test", 0, as.EXPIRE_NEVER); |
|
123 as.setPageAnnotation(pageURI, "expire_session", "test", 0, as.EXPIRE_SESSION); |
|
124 add_old_anno(pageURI, "expire_days", "test", as.EXPIRE_DAYS, 8); |
|
125 add_old_anno(pageURI, "expire_weeks", "test", as.EXPIRE_WEEKS, 31); |
|
126 add_old_anno(pageURI, "expire_months", "test", as.EXPIRE_MONTHS, 181); |
|
127 } |
|
128 |
|
129 // Expire all visits for the bookmarks. This does the same thing as the |
|
130 // promiseClearHistory helper, but it is made explicit here because |
|
131 // removeAllPages is the function we are testing. |
|
132 let promise = |
|
133 promiseTopicObserved(PlacesUtils.TOPIC_EXPIRATION_FINISHED); |
|
134 hs.QueryInterface(Ci.nsIBrowserHistory).removeAllPages(); |
|
135 yield promise; |
|
136 |
|
137 ["expire_days", "expire_weeks", "expire_months", "expire_session", |
|
138 "expire"].forEach(function(aAnno) { |
|
139 let pages = as.getPagesWithAnnotation(aAnno); |
|
140 do_check_eq(pages.length, 0); |
|
141 }); |
|
142 |
|
143 ["expire_days", "expire_weeks", "expire_months", "expire_session", |
|
144 "expire"].forEach(function(aAnno) { |
|
145 let items = as.getItemsWithAnnotation(aAnno); |
|
146 do_check_eq(items.length, 0); |
|
147 }); |
|
148 |
|
149 ["persist"].forEach(function(aAnno) { |
|
150 let pages = as.getPagesWithAnnotation(aAnno); |
|
151 do_check_eq(pages.length, 5); |
|
152 }); |
|
153 |
|
154 ["persist"].forEach(function(aAnno) { |
|
155 let items = as.getItemsWithAnnotation(aAnno); |
|
156 do_check_eq(items.length, 5); |
|
157 items.forEach(function(aItemId) { |
|
158 // Check item exists. |
|
159 bs.getItemIndex(aItemId); |
|
160 }); |
|
161 }); |
|
162 }); |