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: // This test performs checks on the history testing area as outlined michael@0: // https://wiki.mozilla.org/Firefox3.1/PrivateBrowsing/TestPlan#History michael@0: // http://developer.mozilla.org/en/Using_the_Places_history_service michael@0: michael@0: let visitedURIs = [ michael@0: "http://www.test-link.com/", michael@0: "http://www.test-typed.com/", michael@0: "http://www.test-bookmark.com/", michael@0: "http://www.test-redirect-permanent.com/", michael@0: "http://www.test-redirect-temporary.com/", michael@0: "http://www.test-embed.com/", michael@0: "http://www.test-framed.com/", michael@0: "http://www.test-download.com/" michael@0: ].map(NetUtil.newURI.bind(NetUtil)); michael@0: michael@0: add_task(function () { michael@0: let windowsToClose = []; michael@0: let placeItemsCount = 0; michael@0: michael@0: registerCleanupFunction(function() { michael@0: windowsToClose.forEach(function(win) { michael@0: win.close(); michael@0: }); michael@0: }); michael@0: michael@0: yield promiseClearHistory(); michael@0: michael@0: // History database should be empty michael@0: is(PlacesUtils.history.hasHistoryEntries, false, michael@0: "History database should be empty"); michael@0: michael@0: // Ensure we wait for the default bookmarks import. michael@0: let bookmarksDeferred = Promise.defer(); michael@0: waitForCondition(() => { michael@0: placeItemsCount = getPlacesItemsCount(); michael@0: return placeItemsCount > 0 michael@0: }, bookmarksDeferred.resolve, "Should have default bookmarks"); michael@0: yield bookmarksDeferred.promise; michael@0: michael@0: // Create a handful of history items with various visit types michael@0: yield promiseAddVisits([ michael@0: { uri: visitedURIs[0], transition: TRANSITION_LINK }, michael@0: { uri: visitedURIs[1], transition: TRANSITION_TYPED }, michael@0: { uri: visitedURIs[2], transition: TRANSITION_BOOKMARK }, michael@0: { uri: visitedURIs[3], transition: TRANSITION_REDIRECT_PERMANENT }, michael@0: { uri: visitedURIs[4], transition: TRANSITION_REDIRECT_TEMPORARY }, michael@0: { uri: visitedURIs[5], transition: TRANSITION_EMBED }, michael@0: { uri: visitedURIs[6], transition: TRANSITION_FRAMED_LINK }, michael@0: { uri: visitedURIs[7], transition: TRANSITION_DOWNLOAD } michael@0: ]); michael@0: michael@0: // History database should have entries michael@0: is(PlacesUtils.history.hasHistoryEntries, true, michael@0: "History database should have entries"); michael@0: michael@0: placeItemsCount += 7; michael@0: // We added 7 new items to history. michael@0: is(getPlacesItemsCount(), placeItemsCount, michael@0: "Check the total items count"); michael@0: michael@0: function* testOnWindow(aIsPrivate, aCount) { michael@0: let deferred = Promise.defer(); michael@0: whenNewWindowLoaded({ private: aIsPrivate }, deferred.resolve); michael@0: let win = yield deferred.promise; michael@0: windowsToClose.push(win); michael@0: michael@0: // History items should be retrievable by query michael@0: yield checkHistoryItems(); michael@0: michael@0: // Updates the place items count michael@0: let count = getPlacesItemsCount(); michael@0: michael@0: // Create Bookmark michael@0: let bookmarkTitle = "title " + windowsToClose.length; michael@0: let bookmarkKeyword = "keyword " + windowsToClose.length; michael@0: let bookmarkUri = NetUtil.newURI("http://test-a-" + windowsToClose.length + ".com/"); michael@0: michael@0: let id = PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarksMenuFolderId, michael@0: bookmarkUri, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: bookmarkTitle); michael@0: PlacesUtils.bookmarks.setKeywordForBookmark(id, bookmarkKeyword); michael@0: count++; michael@0: michael@0: ok(PlacesUtils.bookmarks.isBookmarked(bookmarkUri), michael@0: "Bookmark should be bookmarked, data should be retrievable"); michael@0: is(bookmarkKeyword, PlacesUtils.bookmarks.getKeywordForURI(bookmarkUri), michael@0: "Check bookmark uri keyword"); michael@0: is(getPlacesItemsCount(), count, michael@0: "Check the new bookmark items count"); michael@0: is(isBookmarkAltered(), false, "Check if bookmark has been visited"); michael@0: } michael@0: michael@0: // Test on windows. michael@0: yield testOnWindow(false); michael@0: yield testOnWindow(true); michael@0: yield testOnWindow(false); michael@0: }); michael@0: michael@0: /** michael@0: * Function performs a really simple query on our places entries, michael@0: * and makes sure that the number of entries equal num_places_entries. michael@0: */ michael@0: function getPlacesItemsCount() { michael@0: // Get bookmarks count michael@0: let options = PlacesUtils.history.getNewQueryOptions(); michael@0: options.includeHidden = true; michael@0: options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS; michael@0: let root = PlacesUtils.history.executeQuery( michael@0: PlacesUtils.history.getNewQuery(), options).root; michael@0: root.containerOpen = true; michael@0: let cc = root.childCount; michael@0: root.containerOpen = false; michael@0: michael@0: // Get history item count michael@0: options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY; michael@0: let root = PlacesUtils.history.executeQuery( michael@0: PlacesUtils.history.getNewQuery(), options).root; michael@0: root.containerOpen = true; michael@0: cc += root.childCount; michael@0: root.containerOpen = false; michael@0: michael@0: return cc; michael@0: } michael@0: michael@0: function* checkHistoryItems() { michael@0: for (let i = 0; i < visitedURIs.length; i++) { michael@0: let visitedUri = visitedURIs[i]; michael@0: ok((yield promiseIsURIVisited(visitedUri)), ""); michael@0: if (/embed/.test(visitedUri.spec)) { michael@0: is(!!pageInDatabase(visitedUri), false, "Check if URI is in database"); michael@0: } else { michael@0: ok(!!pageInDatabase(visitedUri), "Check if URI is in database"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Checks if an address is found in the database. michael@0: * @param aURI michael@0: * nsIURI or address to look for. michael@0: * @return place id of the page or 0 if not found michael@0: */ michael@0: function pageInDatabase(aURI) { michael@0: let url = (aURI instanceof Ci.nsIURI ? aURI.spec : aURI); michael@0: let stmt = DBConn().createStatement( michael@0: "SELECT id FROM moz_places WHERE url = :url" michael@0: ); michael@0: stmt.params.url = url; michael@0: try { michael@0: if (!stmt.executeStep()) michael@0: return 0; michael@0: return stmt.getInt64(0); michael@0: } finally { michael@0: stmt.finalize(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Function attempts to check if Bookmark-A has been visited michael@0: * during private browsing mode, function should return false michael@0: * michael@0: * @returns false if the accessCount has not changed michael@0: * true if the accessCount has changed michael@0: */ michael@0: function isBookmarkAltered(){ michael@0: let options = PlacesUtils.history.getNewQueryOptions(); michael@0: options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS; michael@0: options.maxResults = 1; // should only expect a new bookmark michael@0: michael@0: let query = PlacesUtils.history.getNewQuery(); michael@0: query.setFolders([PlacesUtils.bookmarks.bookmarksMenuFolder], 1); michael@0: michael@0: let root = PlacesUtils.history.executeQuery(query, options).root; michael@0: root.containerOpen = true; michael@0: is(root.childCount, options.maxResults, "Check new bookmarks results"); michael@0: let node = root.getChild(0); michael@0: root.containerOpen = false; michael@0: michael@0: return (node.accessCount != 0); michael@0: }