|
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:set ts=2 sw=2 sts=2 et: */ |
|
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 let mDBConn = DBConn(); |
|
8 |
|
9 function promiseOnClearHistoryObserved() { |
|
10 let deferred = Promise.defer(); |
|
11 |
|
12 let historyObserver = { |
|
13 onBeginUpdateBatch: function() {}, |
|
14 onEndUpdateBatch: function() {}, |
|
15 onVisit: function() {}, |
|
16 onTitleChanged: function() {}, |
|
17 onDeleteURI: function(aURI) {}, |
|
18 onPageChanged: function() {}, |
|
19 onDeleteVisits: function() {}, |
|
20 |
|
21 onClearHistory: function() { |
|
22 PlacesUtils.history.removeObserver(this, false); |
|
23 deferred.resolve(); |
|
24 }, |
|
25 |
|
26 QueryInterface: XPCOMUtils.generateQI([ |
|
27 Ci.nsINavHistoryObserver, |
|
28 ]) |
|
29 } |
|
30 PlacesUtils.history.addObserver(historyObserver, false); |
|
31 return deferred.promise; |
|
32 } |
|
33 |
|
34 // This global variable is a promise object, initialized in run_test and waited |
|
35 // upon in the first asynchronous test. It is resolved when the |
|
36 // "places-init-complete" notification is received. We cannot initialize it in |
|
37 // the asynchronous test, because then it's too late to register the observer. |
|
38 let promiseInit; |
|
39 |
|
40 function run_test() { |
|
41 // places-init-complete is notified after run_test, and it will |
|
42 // run a first frecency fix through async statements. |
|
43 // To avoid random failures we have to run after all of this. |
|
44 promiseInit = promiseTopicObserved(PlacesUtils.TOPIC_INIT_COMPLETE); |
|
45 |
|
46 run_next_test(); |
|
47 } |
|
48 |
|
49 add_task(function test_history_removeAllPages() |
|
50 { |
|
51 yield promiseInit; |
|
52 |
|
53 yield promiseAddVisits([ |
|
54 { uri: uri("http://typed.mozilla.org/"), |
|
55 transition: TRANSITION_TYPED }, |
|
56 { uri: uri("http://link.mozilla.org/"), |
|
57 transition: TRANSITION_LINK }, |
|
58 { uri: uri("http://download.mozilla.org/"), |
|
59 transition: TRANSITION_DOWNLOAD }, |
|
60 { uri: uri("http://redir_temp.mozilla.org/"), |
|
61 transition: TRANSITION_REDIRECT_TEMPORARY, |
|
62 referrer: "http://link.mozilla.org/"}, |
|
63 { uri: uri("http://redir_perm.mozilla.org/"), |
|
64 transition: TRANSITION_REDIRECT_PERMANENT, |
|
65 referrer: "http://link.mozilla.org/"}, |
|
66 ]); |
|
67 |
|
68 // add a place: bookmark |
|
69 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, |
|
70 uri("place:folder=4"), |
|
71 PlacesUtils.bookmarks.DEFAULT_INDEX, |
|
72 "shortcut"); |
|
73 |
|
74 // Add an expire never annotation |
|
75 // Actually expire never annotations are removed as soon as a page is removed |
|
76 // from the database, so this should act as a normal visit. |
|
77 PlacesUtils.annotations.setPageAnnotation(uri("http://download.mozilla.org/"), |
|
78 "never", "never", 0, |
|
79 PlacesUtils.annotations.EXPIRE_NEVER); |
|
80 |
|
81 // Add a bookmark |
|
82 // Bookmarked page should have history cleared and frecency = -old_visit_count |
|
83 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, |
|
84 uri("http://typed.mozilla.org/"), |
|
85 PlacesUtils.bookmarks.DEFAULT_INDEX, |
|
86 "bookmark"); |
|
87 |
|
88 yield promiseAddVisits([ |
|
89 { uri: uri("http://typed.mozilla.org/"), |
|
90 transition: TRANSITION_BOOKMARK }, |
|
91 { uri: uri("http://frecency.mozilla.org/"), |
|
92 transition: TRANSITION_LINK }, |
|
93 ]); |
|
94 yield promiseAsyncUpdates(); |
|
95 |
|
96 // Clear history and wait for the onClearHistory notification. |
|
97 let promiseWaitClearHistory = promiseOnClearHistoryObserved(); |
|
98 PlacesUtils.bhistory.removeAllPages(); |
|
99 yield promiseWaitClearHistory; |
|
100 |
|
101 // check browserHistory returns no entries |
|
102 do_check_eq(0, PlacesUtils.history.hasHistoryEntries); |
|
103 |
|
104 yield promiseTopicObserved(PlacesUtils.TOPIC_EXPIRATION_FINISHED); |
|
105 yield promiseAsyncUpdates(); |
|
106 |
|
107 // Check that frecency for not cleared items (bookmarks) has been converted |
|
108 // to -MAX(visit_count, 1), so we will be able to recalculate frecency |
|
109 // starting from most frecent bookmarks. |
|
110 stmt = mDBConn.createStatement( |
|
111 "SELECT h.id FROM moz_places h WHERE h.frecency > 0 "); |
|
112 do_check_false(stmt.executeStep()); |
|
113 stmt.finalize(); |
|
114 |
|
115 stmt = mDBConn.createStatement( |
|
116 "SELECT h.id FROM moz_places h WHERE h.frecency < 0 " + |
|
117 "AND EXISTS (SELECT id FROM moz_bookmarks WHERE fk = h.id) LIMIT 1"); |
|
118 do_check_true(stmt.executeStep()); |
|
119 stmt.finalize(); |
|
120 |
|
121 // Check that all visit_counts have been brought to 0 |
|
122 stmt = mDBConn.createStatement( |
|
123 "SELECT id FROM moz_places WHERE visit_count <> 0 LIMIT 1"); |
|
124 do_check_false(stmt.executeStep()); |
|
125 stmt.finalize(); |
|
126 |
|
127 // Check that history tables are empty |
|
128 stmt = mDBConn.createStatement( |
|
129 "SELECT * FROM (SELECT id FROM moz_historyvisits LIMIT 1)"); |
|
130 do_check_false(stmt.executeStep()); |
|
131 stmt.finalize(); |
|
132 |
|
133 // Check that all moz_places entries except bookmarks and place: have been removed |
|
134 stmt = mDBConn.createStatement( |
|
135 "SELECT h.id FROM moz_places h WHERE SUBSTR(h.url, 1, 6) <> 'place:' "+ |
|
136 "AND NOT EXISTS (SELECT id FROM moz_bookmarks WHERE fk = h.id) LIMIT 1"); |
|
137 do_check_false(stmt.executeStep()); |
|
138 stmt.finalize(); |
|
139 |
|
140 // Check that we only have favicons for retained places |
|
141 stmt = mDBConn.createStatement( |
|
142 "SELECT f.id FROM moz_favicons f WHERE NOT EXISTS " + |
|
143 "(SELECT id FROM moz_places WHERE favicon_id = f.id) LIMIT 1"); |
|
144 do_check_false(stmt.executeStep()); |
|
145 stmt.finalize(); |
|
146 |
|
147 // Check that we only have annotations for retained places |
|
148 stmt = mDBConn.createStatement( |
|
149 "SELECT a.id FROM moz_annos a WHERE NOT EXISTS " + |
|
150 "(SELECT id FROM moz_places WHERE id = a.place_id) LIMIT 1"); |
|
151 do_check_false(stmt.executeStep()); |
|
152 stmt.finalize(); |
|
153 |
|
154 // Check that we only have inputhistory for retained places |
|
155 stmt = mDBConn.createStatement( |
|
156 "SELECT i.place_id FROM moz_inputhistory i WHERE NOT EXISTS " + |
|
157 "(SELECT id FROM moz_places WHERE id = i.place_id) LIMIT 1"); |
|
158 do_check_false(stmt.executeStep()); |
|
159 stmt.finalize(); |
|
160 |
|
161 // Check that place:uris have frecency 0 |
|
162 stmt = mDBConn.createStatement( |
|
163 "SELECT h.id FROM moz_places h " + |
|
164 "WHERE SUBSTR(h.url, 1, 6) = 'place:' AND h.frecency <> 0 LIMIT 1"); |
|
165 do_check_false(stmt.executeStep()); |
|
166 stmt.finalize(); |
|
167 }); |