Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 5 | |
michael@0 | 6 | function SHistoryListener() { |
michael@0 | 7 | } |
michael@0 | 8 | |
michael@0 | 9 | SHistoryListener.prototype = { |
michael@0 | 10 | retval: true, |
michael@0 | 11 | last: "initial", |
michael@0 | 12 | |
michael@0 | 13 | OnHistoryNewEntry: function (aNewURI) { |
michael@0 | 14 | this.last = "newentry"; |
michael@0 | 15 | }, |
michael@0 | 16 | |
michael@0 | 17 | OnHistoryGoBack: function (aBackURI) { |
michael@0 | 18 | this.last = "goback"; |
michael@0 | 19 | return this.retval; |
michael@0 | 20 | }, |
michael@0 | 21 | |
michael@0 | 22 | OnHistoryGoForward: function (aForwardURI) { |
michael@0 | 23 | this.last = "goforward"; |
michael@0 | 24 | return this.retval; |
michael@0 | 25 | }, |
michael@0 | 26 | |
michael@0 | 27 | OnHistoryGotoIndex: function (aIndex, aGotoURI) { |
michael@0 | 28 | this.last = "gotoindex"; |
michael@0 | 29 | return this.retval; |
michael@0 | 30 | }, |
michael@0 | 31 | |
michael@0 | 32 | OnHistoryPurge: function (aNumEntries) { |
michael@0 | 33 | this.last = "purge"; |
michael@0 | 34 | return this.retval; |
michael@0 | 35 | }, |
michael@0 | 36 | |
michael@0 | 37 | OnHistoryReload: function (aReloadURI, aReloadFlags) { |
michael@0 | 38 | this.last = "reload"; |
michael@0 | 39 | return this.retval; |
michael@0 | 40 | }, |
michael@0 | 41 | |
michael@0 | 42 | OnHistoryReplaceEntry: function (aIndex) {}, |
michael@0 | 43 | |
michael@0 | 44 | QueryInterface: XPCOMUtils.generateQI([Ci.nsISHistoryListener, |
michael@0 | 45 | Ci.nsISupportsWeakReference]) |
michael@0 | 46 | }; |
michael@0 | 47 | |
michael@0 | 48 | let gFirstListener = new SHistoryListener(); |
michael@0 | 49 | let gSecondListener = new SHistoryListener(); |
michael@0 | 50 | |
michael@0 | 51 | function test() { |
michael@0 | 52 | TestRunner.run(); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | function runTests() { |
michael@0 | 56 | yield setup(); |
michael@0 | 57 | let browser = gBrowser.selectedBrowser; |
michael@0 | 58 | checkListeners("initial", "listeners initialized"); |
michael@0 | 59 | |
michael@0 | 60 | // Check if all history listeners are always notified. |
michael@0 | 61 | info("# part 1"); |
michael@0 | 62 | browser.loadURI("http://www.example.com/"); |
michael@0 | 63 | yield whenPageShown(browser); |
michael@0 | 64 | checkListeners("newentry", "shistory has a new entry"); |
michael@0 | 65 | ok(browser.canGoBack, "we can go back"); |
michael@0 | 66 | |
michael@0 | 67 | browser.goBack(); |
michael@0 | 68 | yield whenPageShown(browser); |
michael@0 | 69 | checkListeners("goback", "back to the first shentry"); |
michael@0 | 70 | ok(browser.canGoForward, "we can go forward"); |
michael@0 | 71 | |
michael@0 | 72 | browser.goForward(); |
michael@0 | 73 | yield whenPageShown(browser); |
michael@0 | 74 | checkListeners("goforward", "forward to the second shentry"); |
michael@0 | 75 | |
michael@0 | 76 | browser.reload(); |
michael@0 | 77 | yield whenPageShown(browser); |
michael@0 | 78 | checkListeners("reload", "current shentry reloaded"); |
michael@0 | 79 | |
michael@0 | 80 | browser.gotoIndex(0); |
michael@0 | 81 | yield whenPageShown(browser); |
michael@0 | 82 | checkListeners("gotoindex", "back to the first index"); |
michael@0 | 83 | |
michael@0 | 84 | // Check nsISHistoryInternal.notifyOnHistoryReload |
michael@0 | 85 | info("# part 2"); |
michael@0 | 86 | ok(notifyReload(), "reloading has not been canceled"); |
michael@0 | 87 | checkListeners("reload", "saw the reload notification"); |
michael@0 | 88 | |
michael@0 | 89 | // Let the first listener cancel the reload action. |
michael@0 | 90 | info("# part 3"); |
michael@0 | 91 | resetListeners(); |
michael@0 | 92 | gFirstListener.retval = false; |
michael@0 | 93 | ok(!notifyReload(), "reloading has been canceled"); |
michael@0 | 94 | checkListeners("reload", "saw the reload notification"); |
michael@0 | 95 | |
michael@0 | 96 | // Let both listeners cancel the reload action. |
michael@0 | 97 | info("# part 4"); |
michael@0 | 98 | resetListeners(); |
michael@0 | 99 | gSecondListener.retval = false; |
michael@0 | 100 | ok(!notifyReload(), "reloading has been canceled"); |
michael@0 | 101 | checkListeners("reload", "saw the reload notification"); |
michael@0 | 102 | |
michael@0 | 103 | // Let the second listener cancel the reload action. |
michael@0 | 104 | info("# part 5"); |
michael@0 | 105 | resetListeners(); |
michael@0 | 106 | gFirstListener.retval = true; |
michael@0 | 107 | ok(!notifyReload(), "reloading has been canceled"); |
michael@0 | 108 | checkListeners("reload", "saw the reload notification"); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | function checkListeners(aLast, aMessage) { |
michael@0 | 112 | is(gFirstListener.last, aLast, aMessage); |
michael@0 | 113 | is(gSecondListener.last, aLast, aMessage); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | function resetListeners() { |
michael@0 | 117 | gFirstListener.last = gSecondListener.last = "initial"; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | function notifyReload() { |
michael@0 | 121 | let browser = gBrowser.selectedBrowser; |
michael@0 | 122 | let shistory = browser.docShell.sessionHistory; |
michael@0 | 123 | shistory.QueryInterface(Ci.nsISHistoryInternal); |
michael@0 | 124 | return shistory.notifyOnHistoryReload(browser.currentURI, 0); |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | function setup(aCallback) { |
michael@0 | 128 | let tab = gBrowser.selectedTab = gBrowser.addTab("about:mozilla"); |
michael@0 | 129 | let browser = tab.linkedBrowser; |
michael@0 | 130 | registerCleanupFunction(function () gBrowser.removeTab(tab)); |
michael@0 | 131 | |
michael@0 | 132 | whenPageShown(browser, function () { |
michael@0 | 133 | gFirstListener = new SHistoryListener(); |
michael@0 | 134 | gSecondListener = new SHistoryListener(); |
michael@0 | 135 | |
michael@0 | 136 | let shistory = browser.docShell.sessionHistory; |
michael@0 | 137 | shistory.addSHistoryListener(gFirstListener); |
michael@0 | 138 | shistory.addSHistoryListener(gSecondListener); |
michael@0 | 139 | |
michael@0 | 140 | registerCleanupFunction(function () { |
michael@0 | 141 | shistory.removeSHistoryListener(gFirstListener); |
michael@0 | 142 | shistory.removeSHistoryListener(gSecondListener); |
michael@0 | 143 | }); |
michael@0 | 144 | |
michael@0 | 145 | (aCallback || TestRunner.next)(); |
michael@0 | 146 | }); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | function whenPageShown(aBrowser, aCallback) { |
michael@0 | 150 | aBrowser.addEventListener("pageshow", function onLoad() { |
michael@0 | 151 | aBrowser.removeEventListener("pageshow", onLoad, true); |
michael@0 | 152 | executeSoon(aCallback || TestRunner.next); |
michael@0 | 153 | }, true); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | let TestRunner = { |
michael@0 | 157 | run: function () { |
michael@0 | 158 | waitForExplicitFinish(); |
michael@0 | 159 | this._iter = runTests(); |
michael@0 | 160 | this.next(); |
michael@0 | 161 | }, |
michael@0 | 162 | |
michael@0 | 163 | next: function () { |
michael@0 | 164 | try { |
michael@0 | 165 | TestRunner._iter.next(); |
michael@0 | 166 | } catch (e if e instanceof StopIteration) { |
michael@0 | 167 | TestRunner.finish(); |
michael@0 | 168 | } |
michael@0 | 169 | }, |
michael@0 | 170 | |
michael@0 | 171 | finish: function () { |
michael@0 | 172 | finish(); |
michael@0 | 173 | } |
michael@0 | 174 | }; |