michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: let secureURL = "https://example.com/browser/browser/base/content/test/general/browser_star_hsts.sjs"; michael@0: let unsecureURL = "http://example.com/browser/browser/base/content/test/general/browser_star_hsts.sjs"; michael@0: michael@0: add_task(function* test_star_redirect() { michael@0: registerCleanupFunction(function() { michael@0: // Ensure to remove example.com from the HSTS list. michael@0: let sss = Cc["@mozilla.org/ssservice;1"] michael@0: .getService(Ci.nsISiteSecurityService); michael@0: sss.removeState(Ci.nsISiteSecurityService.HEADER_HSTS, michael@0: NetUtil.newURI("http://example.com/"), 0); michael@0: PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.unfiledBookmarksFolderId); michael@0: gBrowser.removeCurrentTab(); michael@0: }); michael@0: michael@0: let tab = gBrowser.selectedTab = gBrowser.addTab(); michael@0: // This will add the page to the HSTS cache. michael@0: yield promiseTabLoadEvent(tab, secureURL, secureURL); michael@0: // This should transparently be redirected to the secure page. michael@0: yield promiseTabLoadEvent(tab, unsecureURL, secureURL); michael@0: michael@0: yield promiseStarState(BookmarkingUI.STATUS_UNSTARRED); michael@0: michael@0: let promiseBookmark = promiseOnItemAdded(gBrowser.currentURI); michael@0: BookmarkingUI.star.click(); michael@0: // This resolves on the next tick, so the star should have already been michael@0: // updated at that point. michael@0: yield promiseBookmark; michael@0: michael@0: is(BookmarkingUI.status, BookmarkingUI.STATUS_STARRED, "The star is starred"); michael@0: }); michael@0: michael@0: /** michael@0: * Waits for the star to reflect the expected state. michael@0: */ michael@0: function promiseStarState(aValue) { michael@0: let deferred = Promise.defer(); michael@0: let expectedStatus = aValue ? BookmarkingUI.STATUS_STARRED michael@0: : BookmarkingUI.STATUS_UNSTARRED; michael@0: (function checkState() { michael@0: if (BookmarkingUI.status == BookmarkingUI.STATUS_UPDATING || michael@0: BookmarkingUI.status != expectedStatus) { michael@0: info("Waiting for star button change."); michael@0: setTimeout(checkState, 1000); michael@0: } else { michael@0: deferred.resolve(); michael@0: } michael@0: })(); michael@0: return deferred.promise; michael@0: } michael@0: michael@0: /** michael@0: * Starts a load in an existing tab and waits for it to finish (via some event). michael@0: * michael@0: * @param aTab michael@0: * The tab to load into. michael@0: * @param aUrl michael@0: * The url to load. michael@0: * @param [optional] aFinalURL michael@0: * The url to wait for, same as aURL if not defined. michael@0: * @return {Promise} resolved when the event is handled. michael@0: */ michael@0: function promiseTabLoadEvent(aTab, aURL, aFinalURL) michael@0: { michael@0: if (!aFinalURL) michael@0: aFinalURL = aURL; michael@0: let deferred = Promise.defer(); michael@0: info("Wait for load tab event"); michael@0: aTab.linkedBrowser.addEventListener("load", function load(event) { michael@0: if (event.originalTarget != aTab.linkedBrowser.contentDocument || michael@0: event.target.location.href == "about:blank" || michael@0: event.target.location.href != aFinalURL) { michael@0: info("skipping spurious load event"); michael@0: return; michael@0: } michael@0: aTab.linkedBrowser.removeEventListener("load", load, true); michael@0: info("Tab load event received"); michael@0: deferred.resolve(); michael@0: }, true, true); michael@0: aTab.linkedBrowser.loadURI(aURL); michael@0: return deferred.promise; michael@0: } michael@0: michael@0: /** michael@0: * Waits for a bookmark to be added for the given uri. michael@0: */ michael@0: function promiseOnItemAdded(aExpectedURI) { michael@0: let defer = Promise.defer(); michael@0: let bookmarksObserver = { michael@0: onItemAdded: function (aItemId, aFolderId, aIndex, aItemType, aURI) { michael@0: info("Added a bookmark to " + aURI.spec); michael@0: PlacesUtils.bookmarks.removeObserver(bookmarksObserver); michael@0: if (aURI.equals(aExpectedURI)) michael@0: defer.resolve(); michael@0: else michael@0: defer.reject(new Error("Added an unexpected bookmark")); michael@0: }, michael@0: onBeginUpdateBatch: function () {}, michael@0: onEndUpdateBatch: function () {}, michael@0: onItemRemoved: function () {}, michael@0: onItemChanged: function () {}, michael@0: onItemVisited: function () {}, michael@0: onItemMoved: function () {}, michael@0: QueryInterface: XPCOMUtils.generateQI([ michael@0: Ci.nsINavBookmarkObserver, michael@0: ]) michael@0: }; michael@0: info("Waiting for a bookmark to be added"); michael@0: PlacesUtils.bookmarks.addObserver(bookmarksObserver, false); michael@0: return defer.promise; michael@0: }