toolkit/components/places/tests/unit/test_history_removeAllPages.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial