michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: let mDBConn = DBConn(); michael@0: michael@0: function promiseOnClearHistoryObserved() { michael@0: let deferred = Promise.defer(); michael@0: michael@0: let historyObserver = { michael@0: onBeginUpdateBatch: function() {}, michael@0: onEndUpdateBatch: function() {}, michael@0: onVisit: function() {}, michael@0: onTitleChanged: function() {}, michael@0: onDeleteURI: function(aURI) {}, michael@0: onPageChanged: function() {}, michael@0: onDeleteVisits: function() {}, michael@0: michael@0: onClearHistory: function() { michael@0: PlacesUtils.history.removeObserver(this, false); michael@0: deferred.resolve(); michael@0: }, michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([ michael@0: Ci.nsINavHistoryObserver, michael@0: ]) michael@0: } michael@0: PlacesUtils.history.addObserver(historyObserver, false); michael@0: return deferred.promise; michael@0: } michael@0: michael@0: // This global variable is a promise object, initialized in run_test and waited michael@0: // upon in the first asynchronous test. It is resolved when the michael@0: // "places-init-complete" notification is received. We cannot initialize it in michael@0: // the asynchronous test, because then it's too late to register the observer. michael@0: let promiseInit; michael@0: michael@0: function run_test() { michael@0: // places-init-complete is notified after run_test, and it will michael@0: // run a first frecency fix through async statements. michael@0: // To avoid random failures we have to run after all of this. michael@0: promiseInit = promiseTopicObserved(PlacesUtils.TOPIC_INIT_COMPLETE); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function test_history_removeAllPages() michael@0: { michael@0: yield promiseInit; michael@0: michael@0: yield promiseAddVisits([ michael@0: { uri: uri("http://typed.mozilla.org/"), michael@0: transition: TRANSITION_TYPED }, michael@0: { uri: uri("http://link.mozilla.org/"), michael@0: transition: TRANSITION_LINK }, michael@0: { uri: uri("http://download.mozilla.org/"), michael@0: transition: TRANSITION_DOWNLOAD }, michael@0: { uri: uri("http://redir_temp.mozilla.org/"), michael@0: transition: TRANSITION_REDIRECT_TEMPORARY, michael@0: referrer: "http://link.mozilla.org/"}, michael@0: { uri: uri("http://redir_perm.mozilla.org/"), michael@0: transition: TRANSITION_REDIRECT_PERMANENT, michael@0: referrer: "http://link.mozilla.org/"}, michael@0: ]); michael@0: michael@0: // add a place: bookmark michael@0: PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, michael@0: uri("place:folder=4"), michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: "shortcut"); michael@0: michael@0: // Add an expire never annotation michael@0: // Actually expire never annotations are removed as soon as a page is removed michael@0: // from the database, so this should act as a normal visit. michael@0: PlacesUtils.annotations.setPageAnnotation(uri("http://download.mozilla.org/"), michael@0: "never", "never", 0, michael@0: PlacesUtils.annotations.EXPIRE_NEVER); michael@0: michael@0: // Add a bookmark michael@0: // Bookmarked page should have history cleared and frecency = -old_visit_count michael@0: PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, michael@0: uri("http://typed.mozilla.org/"), michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: "bookmark"); michael@0: michael@0: yield promiseAddVisits([ michael@0: { uri: uri("http://typed.mozilla.org/"), michael@0: transition: TRANSITION_BOOKMARK }, michael@0: { uri: uri("http://frecency.mozilla.org/"), michael@0: transition: TRANSITION_LINK }, michael@0: ]); michael@0: yield promiseAsyncUpdates(); michael@0: michael@0: // Clear history and wait for the onClearHistory notification. michael@0: let promiseWaitClearHistory = promiseOnClearHistoryObserved(); michael@0: PlacesUtils.bhistory.removeAllPages(); michael@0: yield promiseWaitClearHistory; michael@0: michael@0: // check browserHistory returns no entries michael@0: do_check_eq(0, PlacesUtils.history.hasHistoryEntries); michael@0: michael@0: yield promiseTopicObserved(PlacesUtils.TOPIC_EXPIRATION_FINISHED); michael@0: yield promiseAsyncUpdates(); michael@0: michael@0: // Check that frecency for not cleared items (bookmarks) has been converted michael@0: // to -MAX(visit_count, 1), so we will be able to recalculate frecency michael@0: // starting from most frecent bookmarks. michael@0: stmt = mDBConn.createStatement( michael@0: "SELECT h.id FROM moz_places h WHERE h.frecency > 0 "); michael@0: do_check_false(stmt.executeStep()); michael@0: stmt.finalize(); michael@0: michael@0: stmt = mDBConn.createStatement( michael@0: "SELECT h.id FROM moz_places h WHERE h.frecency < 0 " + michael@0: "AND EXISTS (SELECT id FROM moz_bookmarks WHERE fk = h.id) LIMIT 1"); michael@0: do_check_true(stmt.executeStep()); michael@0: stmt.finalize(); michael@0: michael@0: // Check that all visit_counts have been brought to 0 michael@0: stmt = mDBConn.createStatement( michael@0: "SELECT id FROM moz_places WHERE visit_count <> 0 LIMIT 1"); michael@0: do_check_false(stmt.executeStep()); michael@0: stmt.finalize(); michael@0: michael@0: // Check that history tables are empty michael@0: stmt = mDBConn.createStatement( michael@0: "SELECT * FROM (SELECT id FROM moz_historyvisits LIMIT 1)"); michael@0: do_check_false(stmt.executeStep()); michael@0: stmt.finalize(); michael@0: michael@0: // Check that all moz_places entries except bookmarks and place: have been removed michael@0: stmt = mDBConn.createStatement( michael@0: "SELECT h.id FROM moz_places h WHERE SUBSTR(h.url, 1, 6) <> 'place:' "+ michael@0: "AND NOT EXISTS (SELECT id FROM moz_bookmarks WHERE fk = h.id) LIMIT 1"); michael@0: do_check_false(stmt.executeStep()); michael@0: stmt.finalize(); michael@0: michael@0: // Check that we only have favicons for retained places michael@0: stmt = mDBConn.createStatement( michael@0: "SELECT f.id FROM moz_favicons f WHERE NOT EXISTS " + michael@0: "(SELECT id FROM moz_places WHERE favicon_id = f.id) LIMIT 1"); michael@0: do_check_false(stmt.executeStep()); michael@0: stmt.finalize(); michael@0: michael@0: // Check that we only have annotations for retained places michael@0: stmt = mDBConn.createStatement( michael@0: "SELECT a.id FROM moz_annos a WHERE NOT EXISTS " + michael@0: "(SELECT id FROM moz_places WHERE id = a.place_id) LIMIT 1"); michael@0: do_check_false(stmt.executeStep()); michael@0: stmt.finalize(); michael@0: michael@0: // Check that we only have inputhistory for retained places michael@0: stmt = mDBConn.createStatement( michael@0: "SELECT i.place_id FROM moz_inputhistory i WHERE NOT EXISTS " + michael@0: "(SELECT id FROM moz_places WHERE id = i.place_id) LIMIT 1"); michael@0: do_check_false(stmt.executeStep()); michael@0: stmt.finalize(); michael@0: michael@0: // Check that place:uris have frecency 0 michael@0: stmt = mDBConn.createStatement( michael@0: "SELECT h.id FROM moz_places h " + michael@0: "WHERE SUBSTR(h.url, 1, 6) = 'place:' AND h.frecency <> 0 LIMIT 1"); michael@0: do_check_false(stmt.executeStep()); michael@0: stmt.finalize(); michael@0: });