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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/unit/test_history_removeAllPages.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,167 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +let mDBConn = DBConn();
    1.11 +
    1.12 +function promiseOnClearHistoryObserved() {
    1.13 +  let deferred = Promise.defer();
    1.14 +
    1.15 +  let historyObserver = {
    1.16 +    onBeginUpdateBatch: function() {},
    1.17 +    onEndUpdateBatch: function() {},
    1.18 +    onVisit: function() {},
    1.19 +    onTitleChanged: function() {},
    1.20 +    onDeleteURI: function(aURI) {},
    1.21 +    onPageChanged: function() {},
    1.22 +    onDeleteVisits: function() {},
    1.23 +
    1.24 +    onClearHistory: function() {
    1.25 +      PlacesUtils.history.removeObserver(this, false);
    1.26 +      deferred.resolve();
    1.27 +    },
    1.28 +
    1.29 +    QueryInterface: XPCOMUtils.generateQI([
    1.30 +      Ci.nsINavHistoryObserver,
    1.31 +    ])
    1.32 +  }
    1.33 +  PlacesUtils.history.addObserver(historyObserver, false);
    1.34 +  return deferred.promise;
    1.35 +}
    1.36 +
    1.37 +// This global variable is a promise object, initialized in run_test and waited
    1.38 +// upon in the first asynchronous test.  It is resolved when the
    1.39 +// "places-init-complete" notification is received. We cannot initialize it in
    1.40 +// the asynchronous test, because then it's too late to register the observer.
    1.41 +let promiseInit;
    1.42 +
    1.43 +function run_test() {
    1.44 +  // places-init-complete is notified after run_test, and it will
    1.45 +  // run a first frecency fix through async statements.
    1.46 +  // To avoid random failures we have to run after all of this.
    1.47 +  promiseInit = promiseTopicObserved(PlacesUtils.TOPIC_INIT_COMPLETE);
    1.48 +
    1.49 +  run_next_test();
    1.50 +}
    1.51 +
    1.52 +add_task(function test_history_removeAllPages()
    1.53 +{
    1.54 +  yield promiseInit;
    1.55 +
    1.56 +  yield promiseAddVisits([
    1.57 +    { uri: uri("http://typed.mozilla.org/"),
    1.58 +      transition: TRANSITION_TYPED },
    1.59 +    { uri: uri("http://link.mozilla.org/"),
    1.60 +      transition: TRANSITION_LINK },
    1.61 +    { uri: uri("http://download.mozilla.org/"),
    1.62 +      transition: TRANSITION_DOWNLOAD },
    1.63 +    { uri: uri("http://redir_temp.mozilla.org/"),
    1.64 +      transition: TRANSITION_REDIRECT_TEMPORARY,
    1.65 +      referrer: "http://link.mozilla.org/"},
    1.66 +    { uri: uri("http://redir_perm.mozilla.org/"),
    1.67 +      transition: TRANSITION_REDIRECT_PERMANENT,
    1.68 +      referrer: "http://link.mozilla.org/"},
    1.69 +  ]);
    1.70 +
    1.71 +  // add a place: bookmark
    1.72 +  PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
    1.73 +                                       uri("place:folder=4"),
    1.74 +                                       PlacesUtils.bookmarks.DEFAULT_INDEX,
    1.75 +                                       "shortcut");
    1.76 +
    1.77 +  // Add an expire never annotation
    1.78 +  // Actually expire never annotations are removed as soon as a page is removed
    1.79 +  // from the database, so this should act as a normal visit.
    1.80 +  PlacesUtils.annotations.setPageAnnotation(uri("http://download.mozilla.org/"),
    1.81 +                                            "never", "never", 0,
    1.82 +                                            PlacesUtils.annotations.EXPIRE_NEVER);
    1.83 +
    1.84 +  // Add a bookmark
    1.85 +  // Bookmarked page should have history cleared and frecency = -old_visit_count
    1.86 +  PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
    1.87 +                                       uri("http://typed.mozilla.org/"),
    1.88 +                                       PlacesUtils.bookmarks.DEFAULT_INDEX,
    1.89 +                                       "bookmark");
    1.90 +
    1.91 +  yield promiseAddVisits([
    1.92 +    { uri: uri("http://typed.mozilla.org/"),
    1.93 +      transition: TRANSITION_BOOKMARK },
    1.94 +    { uri: uri("http://frecency.mozilla.org/"),
    1.95 +      transition: TRANSITION_LINK },
    1.96 +  ]);
    1.97 +  yield promiseAsyncUpdates();
    1.98 +
    1.99 +  // Clear history and wait for the onClearHistory notification.
   1.100 +  let promiseWaitClearHistory = promiseOnClearHistoryObserved();
   1.101 +  PlacesUtils.bhistory.removeAllPages();
   1.102 +  yield promiseWaitClearHistory;
   1.103 +
   1.104 +  // check browserHistory returns no entries
   1.105 +  do_check_eq(0, PlacesUtils.history.hasHistoryEntries);
   1.106 +
   1.107 +  yield promiseTopicObserved(PlacesUtils.TOPIC_EXPIRATION_FINISHED);
   1.108 +  yield promiseAsyncUpdates();
   1.109 +
   1.110 +  // Check that frecency for not cleared items (bookmarks) has been converted
   1.111 +  // to -MAX(visit_count, 1), so we will be able to recalculate frecency
   1.112 +  // starting from most frecent bookmarks.
   1.113 +  stmt = mDBConn.createStatement(
   1.114 +    "SELECT h.id FROM moz_places h WHERE h.frecency > 0 ");
   1.115 +  do_check_false(stmt.executeStep());
   1.116 +  stmt.finalize();
   1.117 +
   1.118 +  stmt = mDBConn.createStatement(
   1.119 +    "SELECT h.id FROM moz_places h WHERE h.frecency < 0 " +
   1.120 +      "AND EXISTS (SELECT id FROM moz_bookmarks WHERE fk = h.id) LIMIT 1");
   1.121 +  do_check_true(stmt.executeStep());
   1.122 +  stmt.finalize();
   1.123 +
   1.124 +  // Check that all visit_counts have been brought to 0
   1.125 +  stmt = mDBConn.createStatement(
   1.126 +    "SELECT id FROM moz_places WHERE visit_count <> 0 LIMIT 1");
   1.127 +  do_check_false(stmt.executeStep());
   1.128 +  stmt.finalize();
   1.129 +
   1.130 +  // Check that history tables are empty
   1.131 +  stmt = mDBConn.createStatement(
   1.132 +    "SELECT * FROM (SELECT id FROM moz_historyvisits LIMIT 1)");
   1.133 +  do_check_false(stmt.executeStep());
   1.134 +  stmt.finalize();
   1.135 +
   1.136 +  // Check that all moz_places entries except bookmarks and place: have been removed
   1.137 +  stmt = mDBConn.createStatement(
   1.138 +    "SELECT h.id FROM moz_places h WHERE SUBSTR(h.url, 1, 6) <> 'place:' "+
   1.139 +      "AND NOT EXISTS (SELECT id FROM moz_bookmarks WHERE fk = h.id) LIMIT 1");
   1.140 +  do_check_false(stmt.executeStep());
   1.141 +  stmt.finalize();
   1.142 +
   1.143 +  // Check that we only have favicons for retained places
   1.144 +  stmt = mDBConn.createStatement(
   1.145 +    "SELECT f.id FROM moz_favicons f WHERE NOT EXISTS " +
   1.146 +      "(SELECT id FROM moz_places WHERE favicon_id = f.id) LIMIT 1");
   1.147 +  do_check_false(stmt.executeStep());
   1.148 +  stmt.finalize();
   1.149 +
   1.150 +  // Check that we only have annotations for retained places
   1.151 +  stmt = mDBConn.createStatement(
   1.152 +    "SELECT a.id FROM moz_annos a WHERE NOT EXISTS " +
   1.153 +      "(SELECT id FROM moz_places WHERE id = a.place_id) LIMIT 1");
   1.154 +  do_check_false(stmt.executeStep());
   1.155 +  stmt.finalize();
   1.156 +
   1.157 +  // Check that we only have inputhistory for retained places
   1.158 +  stmt = mDBConn.createStatement(
   1.159 +    "SELECT i.place_id FROM moz_inputhistory i WHERE NOT EXISTS " +
   1.160 +      "(SELECT id FROM moz_places WHERE id = i.place_id) LIMIT 1");
   1.161 +  do_check_false(stmt.executeStep());
   1.162 +  stmt.finalize();
   1.163 +
   1.164 +  // Check that place:uris have frecency 0
   1.165 +  stmt = mDBConn.createStatement(
   1.166 +    "SELECT h.id FROM moz_places h " +
   1.167 +    "WHERE SUBSTR(h.url, 1, 6) = 'place:' AND h.frecency <> 0 LIMIT 1");
   1.168 +  do_check_false(stmt.executeStep());
   1.169 +  stmt.finalize();
   1.170 +});

mercurial