|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
3 */ |
|
4 |
|
5 let secureURL = "https://example.com/browser/browser/base/content/test/general/browser_star_hsts.sjs"; |
|
6 let unsecureURL = "http://example.com/browser/browser/base/content/test/general/browser_star_hsts.sjs"; |
|
7 |
|
8 add_task(function* test_star_redirect() { |
|
9 registerCleanupFunction(function() { |
|
10 // Ensure to remove example.com from the HSTS list. |
|
11 let sss = Cc["@mozilla.org/ssservice;1"] |
|
12 .getService(Ci.nsISiteSecurityService); |
|
13 sss.removeState(Ci.nsISiteSecurityService.HEADER_HSTS, |
|
14 NetUtil.newURI("http://example.com/"), 0); |
|
15 PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.unfiledBookmarksFolderId); |
|
16 gBrowser.removeCurrentTab(); |
|
17 }); |
|
18 |
|
19 let tab = gBrowser.selectedTab = gBrowser.addTab(); |
|
20 // This will add the page to the HSTS cache. |
|
21 yield promiseTabLoadEvent(tab, secureURL, secureURL); |
|
22 // This should transparently be redirected to the secure page. |
|
23 yield promiseTabLoadEvent(tab, unsecureURL, secureURL); |
|
24 |
|
25 yield promiseStarState(BookmarkingUI.STATUS_UNSTARRED); |
|
26 |
|
27 let promiseBookmark = promiseOnItemAdded(gBrowser.currentURI); |
|
28 BookmarkingUI.star.click(); |
|
29 // This resolves on the next tick, so the star should have already been |
|
30 // updated at that point. |
|
31 yield promiseBookmark; |
|
32 |
|
33 is(BookmarkingUI.status, BookmarkingUI.STATUS_STARRED, "The star is starred"); |
|
34 }); |
|
35 |
|
36 /** |
|
37 * Waits for the star to reflect the expected state. |
|
38 */ |
|
39 function promiseStarState(aValue) { |
|
40 let deferred = Promise.defer(); |
|
41 let expectedStatus = aValue ? BookmarkingUI.STATUS_STARRED |
|
42 : BookmarkingUI.STATUS_UNSTARRED; |
|
43 (function checkState() { |
|
44 if (BookmarkingUI.status == BookmarkingUI.STATUS_UPDATING || |
|
45 BookmarkingUI.status != expectedStatus) { |
|
46 info("Waiting for star button change."); |
|
47 setTimeout(checkState, 1000); |
|
48 } else { |
|
49 deferred.resolve(); |
|
50 } |
|
51 })(); |
|
52 return deferred.promise; |
|
53 } |
|
54 |
|
55 /** |
|
56 * Starts a load in an existing tab and waits for it to finish (via some event). |
|
57 * |
|
58 * @param aTab |
|
59 * The tab to load into. |
|
60 * @param aUrl |
|
61 * The url to load. |
|
62 * @param [optional] aFinalURL |
|
63 * The url to wait for, same as aURL if not defined. |
|
64 * @return {Promise} resolved when the event is handled. |
|
65 */ |
|
66 function promiseTabLoadEvent(aTab, aURL, aFinalURL) |
|
67 { |
|
68 if (!aFinalURL) |
|
69 aFinalURL = aURL; |
|
70 let deferred = Promise.defer(); |
|
71 info("Wait for load tab event"); |
|
72 aTab.linkedBrowser.addEventListener("load", function load(event) { |
|
73 if (event.originalTarget != aTab.linkedBrowser.contentDocument || |
|
74 event.target.location.href == "about:blank" || |
|
75 event.target.location.href != aFinalURL) { |
|
76 info("skipping spurious load event"); |
|
77 return; |
|
78 } |
|
79 aTab.linkedBrowser.removeEventListener("load", load, true); |
|
80 info("Tab load event received"); |
|
81 deferred.resolve(); |
|
82 }, true, true); |
|
83 aTab.linkedBrowser.loadURI(aURL); |
|
84 return deferred.promise; |
|
85 } |
|
86 |
|
87 /** |
|
88 * Waits for a bookmark to be added for the given uri. |
|
89 */ |
|
90 function promiseOnItemAdded(aExpectedURI) { |
|
91 let defer = Promise.defer(); |
|
92 let bookmarksObserver = { |
|
93 onItemAdded: function (aItemId, aFolderId, aIndex, aItemType, aURI) { |
|
94 info("Added a bookmark to " + aURI.spec); |
|
95 PlacesUtils.bookmarks.removeObserver(bookmarksObserver); |
|
96 if (aURI.equals(aExpectedURI)) |
|
97 defer.resolve(); |
|
98 else |
|
99 defer.reject(new Error("Added an unexpected bookmark")); |
|
100 }, |
|
101 onBeginUpdateBatch: function () {}, |
|
102 onEndUpdateBatch: function () {}, |
|
103 onItemRemoved: function () {}, |
|
104 onItemChanged: function () {}, |
|
105 onItemVisited: function () {}, |
|
106 onItemMoved: function () {}, |
|
107 QueryInterface: XPCOMUtils.generateQI([ |
|
108 Ci.nsINavBookmarkObserver, |
|
109 ]) |
|
110 }; |
|
111 info("Waiting for a bookmark to be added"); |
|
112 PlacesUtils.bookmarks.addObserver(bookmarksObserver, false); |
|
113 return defer.promise; |
|
114 } |