toolkit/components/places/tests/browser/browser_bug248970.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/browser/browser_bug248970.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,185 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +// This test performs checks on the history testing area as outlined
     1.9 +// https://wiki.mozilla.org/Firefox3.1/PrivateBrowsing/TestPlan#History
    1.10 +// http://developer.mozilla.org/en/Using_the_Places_history_service
    1.11 +
    1.12 +let visitedURIs = [
    1.13 +  "http://www.test-link.com/",
    1.14 +  "http://www.test-typed.com/",
    1.15 +  "http://www.test-bookmark.com/",
    1.16 +  "http://www.test-redirect-permanent.com/",
    1.17 +  "http://www.test-redirect-temporary.com/",
    1.18 +  "http://www.test-embed.com/",
    1.19 +  "http://www.test-framed.com/",
    1.20 +  "http://www.test-download.com/"
    1.21 +].map(NetUtil.newURI.bind(NetUtil));
    1.22 +
    1.23 +add_task(function () {
    1.24 +  let windowsToClose = [];
    1.25 +  let placeItemsCount = 0;
    1.26 +
    1.27 +  registerCleanupFunction(function() {
    1.28 +    windowsToClose.forEach(function(win) {
    1.29 +      win.close();
    1.30 +    });
    1.31 +  });
    1.32 +
    1.33 +  yield promiseClearHistory();
    1.34 +
    1.35 +  // History database should be empty
    1.36 +  is(PlacesUtils.history.hasHistoryEntries, false,
    1.37 +     "History database should be empty");
    1.38 +
    1.39 +   // Ensure we wait for the default bookmarks import.
    1.40 +  let bookmarksDeferred = Promise.defer();
    1.41 +  waitForCondition(() => {
    1.42 +    placeItemsCount = getPlacesItemsCount();
    1.43 +    return placeItemsCount > 0
    1.44 +  }, bookmarksDeferred.resolve, "Should have default bookmarks");
    1.45 +  yield bookmarksDeferred.promise;
    1.46 +
    1.47 +  // Create a handful of history items with various visit types
    1.48 +  yield promiseAddVisits([
    1.49 +    { uri: visitedURIs[0], transition: TRANSITION_LINK },
    1.50 +    { uri: visitedURIs[1], transition: TRANSITION_TYPED },
    1.51 +    { uri: visitedURIs[2], transition: TRANSITION_BOOKMARK },
    1.52 +    { uri: visitedURIs[3], transition: TRANSITION_REDIRECT_PERMANENT },
    1.53 +    { uri: visitedURIs[4], transition: TRANSITION_REDIRECT_TEMPORARY },
    1.54 +    { uri: visitedURIs[5], transition: TRANSITION_EMBED },
    1.55 +    { uri: visitedURIs[6], transition: TRANSITION_FRAMED_LINK },
    1.56 +    { uri: visitedURIs[7], transition: TRANSITION_DOWNLOAD }
    1.57 +  ]);
    1.58 +
    1.59 +  // History database should have entries
    1.60 +  is(PlacesUtils.history.hasHistoryEntries, true,
    1.61 +     "History database should have entries");
    1.62 +
    1.63 +  placeItemsCount += 7;
    1.64 +  // We added 7 new items to history.
    1.65 +  is(getPlacesItemsCount(), placeItemsCount,
    1.66 +     "Check the total items count");
    1.67 +
    1.68 +  function* testOnWindow(aIsPrivate, aCount) {
    1.69 +    let deferred = Promise.defer();
    1.70 +    whenNewWindowLoaded({ private: aIsPrivate }, deferred.resolve);
    1.71 +    let win = yield deferred.promise;
    1.72 +    windowsToClose.push(win);
    1.73 +
    1.74 +    // History items should be retrievable by query
    1.75 +    yield checkHistoryItems();
    1.76 +
    1.77 +    // Updates the place items count
    1.78 +    let count = getPlacesItemsCount();
    1.79 +
    1.80 +    // Create Bookmark
    1.81 +    let bookmarkTitle = "title " + windowsToClose.length;
    1.82 +    let bookmarkKeyword = "keyword " + windowsToClose.length;
    1.83 +    let bookmarkUri = NetUtil.newURI("http://test-a-" + windowsToClose.length + ".com/");
    1.84 +
    1.85 +    let id = PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarksMenuFolderId,
    1.86 +                                                  bookmarkUri,
    1.87 +                                                  PlacesUtils.bookmarks.DEFAULT_INDEX,
    1.88 +                                                  bookmarkTitle);
    1.89 +    PlacesUtils.bookmarks.setKeywordForBookmark(id, bookmarkKeyword);
    1.90 +    count++;
    1.91 +
    1.92 +    ok(PlacesUtils.bookmarks.isBookmarked(bookmarkUri),
    1.93 +       "Bookmark should be bookmarked, data should be retrievable");
    1.94 +    is(bookmarkKeyword, PlacesUtils.bookmarks.getKeywordForURI(bookmarkUri),
    1.95 +       "Check bookmark uri keyword");
    1.96 +    is(getPlacesItemsCount(), count,
    1.97 +       "Check the new bookmark items count");
    1.98 +    is(isBookmarkAltered(), false, "Check if bookmark has been visited");
    1.99 +  }
   1.100 +
   1.101 +  // Test on windows.
   1.102 +  yield testOnWindow(false);
   1.103 +  yield testOnWindow(true);
   1.104 +  yield testOnWindow(false);
   1.105 +});
   1.106 +
   1.107 +/**
   1.108 + * Function performs a really simple query on our places entries,
   1.109 + * and makes sure that the number of entries equal num_places_entries.
   1.110 + */
   1.111 +function getPlacesItemsCount() {
   1.112 +  // Get bookmarks count
   1.113 +  let options = PlacesUtils.history.getNewQueryOptions();
   1.114 +  options.includeHidden = true;
   1.115 +  options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS;
   1.116 +  let root = PlacesUtils.history.executeQuery(
   1.117 +    PlacesUtils.history.getNewQuery(), options).root;
   1.118 +  root.containerOpen = true;
   1.119 +  let cc = root.childCount;
   1.120 +  root.containerOpen = false;
   1.121 +
   1.122 +  // Get history item count
   1.123 +  options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY;
   1.124 +  let root = PlacesUtils.history.executeQuery(
   1.125 +    PlacesUtils.history.getNewQuery(), options).root;
   1.126 +  root.containerOpen = true;
   1.127 +  cc += root.childCount;
   1.128 +  root.containerOpen = false;
   1.129 +
   1.130 +  return cc;
   1.131 +}
   1.132 +
   1.133 +function* checkHistoryItems() {
   1.134 +  for (let i = 0; i < visitedURIs.length; i++) {
   1.135 +    let visitedUri = visitedURIs[i];
   1.136 +    ok((yield promiseIsURIVisited(visitedUri)), "");
   1.137 +    if (/embed/.test(visitedUri.spec)) {
   1.138 +      is(!!pageInDatabase(visitedUri), false, "Check if URI is in database");
   1.139 +    } else {
   1.140 +      ok(!!pageInDatabase(visitedUri), "Check if URI is in database");
   1.141 +    }
   1.142 +  }
   1.143 +}
   1.144 +
   1.145 +/**
   1.146 + * Checks if an address is found in the database.
   1.147 + * @param aURI
   1.148 + *        nsIURI or address to look for.
   1.149 + * @return place id of the page or 0 if not found
   1.150 + */
   1.151 +function pageInDatabase(aURI) {
   1.152 +  let url = (aURI instanceof Ci.nsIURI ? aURI.spec : aURI);
   1.153 +  let stmt = DBConn().createStatement(
   1.154 +    "SELECT id FROM moz_places WHERE url = :url"
   1.155 +  );
   1.156 +  stmt.params.url = url;
   1.157 +  try {
   1.158 +    if (!stmt.executeStep())
   1.159 +      return 0;
   1.160 +    return stmt.getInt64(0);
   1.161 +  } finally {
   1.162 +    stmt.finalize();
   1.163 +  }
   1.164 +}
   1.165 +
   1.166 +/**
   1.167 + * Function attempts to check if Bookmark-A has been visited
   1.168 + * during private browsing mode, function should return false
   1.169 + *
   1.170 + * @returns false if the accessCount has not changed
   1.171 + *          true if the accessCount has changed
   1.172 + */
   1.173 +function isBookmarkAltered(){
   1.174 +  let options = PlacesUtils.history.getNewQueryOptions();
   1.175 +  options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS;
   1.176 +  options.maxResults = 1; // should only expect a new bookmark
   1.177 +
   1.178 +  let query = PlacesUtils.history.getNewQuery();
   1.179 +  query.setFolders([PlacesUtils.bookmarks.bookmarksMenuFolder], 1);
   1.180 +
   1.181 +  let root = PlacesUtils.history.executeQuery(query, options).root;
   1.182 +  root.containerOpen = true;
   1.183 +  is(root.childCount, options.maxResults, "Check new bookmarks results");
   1.184 +  let node = root.getChild(0);
   1.185 +  root.containerOpen = false;
   1.186 +
   1.187 +  return (node.accessCount != 0);
   1.188 +}

mercurial