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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 // This test performs checks on the history testing area as outlined
     6 // https://wiki.mozilla.org/Firefox3.1/PrivateBrowsing/TestPlan#History
     7 // http://developer.mozilla.org/en/Using_the_Places_history_service
     9 let visitedURIs = [
    10   "http://www.test-link.com/",
    11   "http://www.test-typed.com/",
    12   "http://www.test-bookmark.com/",
    13   "http://www.test-redirect-permanent.com/",
    14   "http://www.test-redirect-temporary.com/",
    15   "http://www.test-embed.com/",
    16   "http://www.test-framed.com/",
    17   "http://www.test-download.com/"
    18 ].map(NetUtil.newURI.bind(NetUtil));
    20 add_task(function () {
    21   let windowsToClose = [];
    22   let placeItemsCount = 0;
    24   registerCleanupFunction(function() {
    25     windowsToClose.forEach(function(win) {
    26       win.close();
    27     });
    28   });
    30   yield promiseClearHistory();
    32   // History database should be empty
    33   is(PlacesUtils.history.hasHistoryEntries, false,
    34      "History database should be empty");
    36    // Ensure we wait for the default bookmarks import.
    37   let bookmarksDeferred = Promise.defer();
    38   waitForCondition(() => {
    39     placeItemsCount = getPlacesItemsCount();
    40     return placeItemsCount > 0
    41   }, bookmarksDeferred.resolve, "Should have default bookmarks");
    42   yield bookmarksDeferred.promise;
    44   // Create a handful of history items with various visit types
    45   yield promiseAddVisits([
    46     { uri: visitedURIs[0], transition: TRANSITION_LINK },
    47     { uri: visitedURIs[1], transition: TRANSITION_TYPED },
    48     { uri: visitedURIs[2], transition: TRANSITION_BOOKMARK },
    49     { uri: visitedURIs[3], transition: TRANSITION_REDIRECT_PERMANENT },
    50     { uri: visitedURIs[4], transition: TRANSITION_REDIRECT_TEMPORARY },
    51     { uri: visitedURIs[5], transition: TRANSITION_EMBED },
    52     { uri: visitedURIs[6], transition: TRANSITION_FRAMED_LINK },
    53     { uri: visitedURIs[7], transition: TRANSITION_DOWNLOAD }
    54   ]);
    56   // History database should have entries
    57   is(PlacesUtils.history.hasHistoryEntries, true,
    58      "History database should have entries");
    60   placeItemsCount += 7;
    61   // We added 7 new items to history.
    62   is(getPlacesItemsCount(), placeItemsCount,
    63      "Check the total items count");
    65   function* testOnWindow(aIsPrivate, aCount) {
    66     let deferred = Promise.defer();
    67     whenNewWindowLoaded({ private: aIsPrivate }, deferred.resolve);
    68     let win = yield deferred.promise;
    69     windowsToClose.push(win);
    71     // History items should be retrievable by query
    72     yield checkHistoryItems();
    74     // Updates the place items count
    75     let count = getPlacesItemsCount();
    77     // Create Bookmark
    78     let bookmarkTitle = "title " + windowsToClose.length;
    79     let bookmarkKeyword = "keyword " + windowsToClose.length;
    80     let bookmarkUri = NetUtil.newURI("http://test-a-" + windowsToClose.length + ".com/");
    82     let id = PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarksMenuFolderId,
    83                                                   bookmarkUri,
    84                                                   PlacesUtils.bookmarks.DEFAULT_INDEX,
    85                                                   bookmarkTitle);
    86     PlacesUtils.bookmarks.setKeywordForBookmark(id, bookmarkKeyword);
    87     count++;
    89     ok(PlacesUtils.bookmarks.isBookmarked(bookmarkUri),
    90        "Bookmark should be bookmarked, data should be retrievable");
    91     is(bookmarkKeyword, PlacesUtils.bookmarks.getKeywordForURI(bookmarkUri),
    92        "Check bookmark uri keyword");
    93     is(getPlacesItemsCount(), count,
    94        "Check the new bookmark items count");
    95     is(isBookmarkAltered(), false, "Check if bookmark has been visited");
    96   }
    98   // Test on windows.
    99   yield testOnWindow(false);
   100   yield testOnWindow(true);
   101   yield testOnWindow(false);
   102 });
   104 /**
   105  * Function performs a really simple query on our places entries,
   106  * and makes sure that the number of entries equal num_places_entries.
   107  */
   108 function getPlacesItemsCount() {
   109   // Get bookmarks count
   110   let options = PlacesUtils.history.getNewQueryOptions();
   111   options.includeHidden = true;
   112   options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS;
   113   let root = PlacesUtils.history.executeQuery(
   114     PlacesUtils.history.getNewQuery(), options).root;
   115   root.containerOpen = true;
   116   let cc = root.childCount;
   117   root.containerOpen = false;
   119   // Get history item count
   120   options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY;
   121   let root = PlacesUtils.history.executeQuery(
   122     PlacesUtils.history.getNewQuery(), options).root;
   123   root.containerOpen = true;
   124   cc += root.childCount;
   125   root.containerOpen = false;
   127   return cc;
   128 }
   130 function* checkHistoryItems() {
   131   for (let i = 0; i < visitedURIs.length; i++) {
   132     let visitedUri = visitedURIs[i];
   133     ok((yield promiseIsURIVisited(visitedUri)), "");
   134     if (/embed/.test(visitedUri.spec)) {
   135       is(!!pageInDatabase(visitedUri), false, "Check if URI is in database");
   136     } else {
   137       ok(!!pageInDatabase(visitedUri), "Check if URI is in database");
   138     }
   139   }
   140 }
   142 /**
   143  * Checks if an address is found in the database.
   144  * @param aURI
   145  *        nsIURI or address to look for.
   146  * @return place id of the page or 0 if not found
   147  */
   148 function pageInDatabase(aURI) {
   149   let url = (aURI instanceof Ci.nsIURI ? aURI.spec : aURI);
   150   let stmt = DBConn().createStatement(
   151     "SELECT id FROM moz_places WHERE url = :url"
   152   );
   153   stmt.params.url = url;
   154   try {
   155     if (!stmt.executeStep())
   156       return 0;
   157     return stmt.getInt64(0);
   158   } finally {
   159     stmt.finalize();
   160   }
   161 }
   163 /**
   164  * Function attempts to check if Bookmark-A has been visited
   165  * during private browsing mode, function should return false
   166  *
   167  * @returns false if the accessCount has not changed
   168  *          true if the accessCount has changed
   169  */
   170 function isBookmarkAltered(){
   171   let options = PlacesUtils.history.getNewQueryOptions();
   172   options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS;
   173   options.maxResults = 1; // should only expect a new bookmark
   175   let query = PlacesUtils.history.getNewQuery();
   176   query.setFolders([PlacesUtils.bookmarks.bookmarksMenuFolder], 1);
   178   let root = PlacesUtils.history.executeQuery(query, options).root;
   179   root.containerOpen = true;
   180   is(root.childCount, options.maxResults, "Check new bookmarks results");
   181   let node = root.getChild(0);
   182   root.containerOpen = false;
   184   return (node.accessCount != 0);
   185 }

mercurial