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 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | function test() { |
michael@0 | 6 | /** Test (B) for Bug 248970 **/ |
michael@0 | 7 | waitForExplicitFinish(); |
michael@0 | 8 | |
michael@0 | 9 | let windowsToClose = []; |
michael@0 | 10 | let file = Services.dirsvc.get("TmpD", Ci.nsIFile); |
michael@0 | 11 | let filePath = file.path; |
michael@0 | 12 | let fieldList = { |
michael@0 | 13 | "//input[@name='input']": Date.now().toString(), |
michael@0 | 14 | "//input[@name='spaced 1']": Math.random().toString(), |
michael@0 | 15 | "//input[3]": "three", |
michael@0 | 16 | "//input[@type='checkbox']": true, |
michael@0 | 17 | "//input[@name='uncheck']": false, |
michael@0 | 18 | "//input[@type='radio'][1]": false, |
michael@0 | 19 | "//input[@type='radio'][2]": true, |
michael@0 | 20 | "//input[@type='radio'][3]": false, |
michael@0 | 21 | "//select": 2, |
michael@0 | 22 | "//select[@multiple]": [1, 3], |
michael@0 | 23 | "//textarea[1]": "", |
michael@0 | 24 | "//textarea[2]": "Some text... " + Math.random(), |
michael@0 | 25 | "//textarea[3]": "Some more text\n" + new Date(), |
michael@0 | 26 | "//input[@type='file']": filePath |
michael@0 | 27 | }; |
michael@0 | 28 | |
michael@0 | 29 | registerCleanupFunction(function() { |
michael@0 | 30 | windowsToClose.forEach(function(win) { |
michael@0 | 31 | win.close(); |
michael@0 | 32 | }); |
michael@0 | 33 | }); |
michael@0 | 34 | |
michael@0 | 35 | function test(aLambda) { |
michael@0 | 36 | try { |
michael@0 | 37 | return aLambda() || true; |
michael@0 | 38 | } catch(ex) { } |
michael@0 | 39 | return false; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | function getElementByXPath(aTab, aQuery) { |
michael@0 | 43 | let doc = aTab.linkedBrowser.contentDocument; |
michael@0 | 44 | let xptype = Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE; |
michael@0 | 45 | return doc.evaluate(aQuery, doc, null, xptype, null).singleNodeValue; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | function setFormValue(aTab, aQuery, aValue) { |
michael@0 | 49 | let node = getElementByXPath(aTab, aQuery); |
michael@0 | 50 | if (typeof aValue == "string") |
michael@0 | 51 | node.value = aValue; |
michael@0 | 52 | else if (typeof aValue == "boolean") |
michael@0 | 53 | node.checked = aValue; |
michael@0 | 54 | else if (typeof aValue == "number") |
michael@0 | 55 | node.selectedIndex = aValue; |
michael@0 | 56 | else |
michael@0 | 57 | Array.forEach(node.options, function(aOpt, aIx) |
michael@0 | 58 | (aOpt.selected = aValue.indexOf(aIx) > -1)); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | function compareFormValue(aTab, aQuery, aValue) { |
michael@0 | 62 | let node = getElementByXPath(aTab, aQuery); |
michael@0 | 63 | if (!node) |
michael@0 | 64 | return false; |
michael@0 | 65 | if (node instanceof Ci.nsIDOMHTMLInputElement) |
michael@0 | 66 | return aValue == (node.type == "checkbox" || node.type == "radio" ? |
michael@0 | 67 | node.checked : node.value); |
michael@0 | 68 | if (node instanceof Ci.nsIDOMHTMLTextAreaElement) |
michael@0 | 69 | return aValue == node.value; |
michael@0 | 70 | if (!node.multiple) |
michael@0 | 71 | return aValue == node.selectedIndex; |
michael@0 | 72 | return Array.every(node.options, function(aOpt, aIx) |
michael@0 | 73 | (aValue.indexOf(aIx) > -1) == aOpt.selected); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | ////////////////////////////////////////////////////////////////// |
michael@0 | 77 | // Test (B) : Session data restoration between windows // |
michael@0 | 78 | ////////////////////////////////////////////////////////////////// |
michael@0 | 79 | |
michael@0 | 80 | let rootDir = getRootDirectory(gTestPath); |
michael@0 | 81 | const testURL = rootDir + "browser_248970_b_sample.html"; |
michael@0 | 82 | const testURL2 = "http://mochi.test:8888/browser/" + |
michael@0 | 83 | "browser/components/sessionstore/test/browser_248970_b_sample.html"; |
michael@0 | 84 | |
michael@0 | 85 | whenNewWindowLoaded({ private: false }, function(aWin) { |
michael@0 | 86 | windowsToClose.push(aWin); |
michael@0 | 87 | |
michael@0 | 88 | // get closed tab count |
michael@0 | 89 | let count = ss.getClosedTabCount(aWin); |
michael@0 | 90 | let max_tabs_undo = |
michael@0 | 91 | Services.prefs.getIntPref("browser.sessionstore.max_tabs_undo"); |
michael@0 | 92 | ok(0 <= count && count <= max_tabs_undo, |
michael@0 | 93 | "getClosedTabCount should return zero or at most max_tabs_undo"); |
michael@0 | 94 | |
michael@0 | 95 | // setup a state for tab (A) so we can check later that is restored |
michael@0 | 96 | let key = "key"; |
michael@0 | 97 | let value = "Value " + Math.random(); |
michael@0 | 98 | let state = { entries: [{ url: testURL }], extData: { key: value } }; |
michael@0 | 99 | |
michael@0 | 100 | // public session, add new tab: (A) |
michael@0 | 101 | let tab_A = aWin.gBrowser.addTab(testURL); |
michael@0 | 102 | ss.setTabState(tab_A, JSON.stringify(state)); |
michael@0 | 103 | whenBrowserLoaded(tab_A.linkedBrowser, function() { |
michael@0 | 104 | // make sure that the next closed tab will increase getClosedTabCount |
michael@0 | 105 | Services.prefs.setIntPref( |
michael@0 | 106 | "browser.sessionstore.max_tabs_undo", max_tabs_undo + 1) |
michael@0 | 107 | |
michael@0 | 108 | // populate tab_A with form data |
michael@0 | 109 | for (let i in fieldList) |
michael@0 | 110 | setFormValue(tab_A, i, fieldList[i]); |
michael@0 | 111 | |
michael@0 | 112 | // public session, close tab: (A) |
michael@0 | 113 | aWin.gBrowser.removeTab(tab_A); |
michael@0 | 114 | |
michael@0 | 115 | // verify that closedTabCount increased |
michael@0 | 116 | ok(ss.getClosedTabCount(aWin) > count, |
michael@0 | 117 | "getClosedTabCount has increased after closing a tab"); |
michael@0 | 118 | |
michael@0 | 119 | // verify tab: (A), in undo list |
michael@0 | 120 | let tab_A_restored = test(function() ss.undoCloseTab(aWin, 0)); |
michael@0 | 121 | ok(tab_A_restored, "a tab is in undo list"); |
michael@0 | 122 | whenTabRestored(tab_A_restored, function() { |
michael@0 | 123 | is(testURL, tab_A_restored.linkedBrowser.currentURI.spec, |
michael@0 | 124 | "it's the same tab that we expect"); |
michael@0 | 125 | aWin.gBrowser.removeTab(tab_A_restored); |
michael@0 | 126 | |
michael@0 | 127 | whenNewWindowLoaded({ private: true }, function(aWin) { |
michael@0 | 128 | windowsToClose.push(aWin); |
michael@0 | 129 | |
michael@0 | 130 | // setup a state for tab (B) so we can check that its duplicated |
michael@0 | 131 | // properly |
michael@0 | 132 | let key1 = "key1"; |
michael@0 | 133 | let value1 = "Value " + Math.random(); |
michael@0 | 134 | let state1 = { |
michael@0 | 135 | entries: [{ url: testURL2 }], extData: { key1: value1 } |
michael@0 | 136 | }; |
michael@0 | 137 | |
michael@0 | 138 | let tab_B = aWin.gBrowser.addTab(testURL2); |
michael@0 | 139 | ss.setTabState(tab_B, JSON.stringify(state1)); |
michael@0 | 140 | whenTabRestored(tab_B, function() { |
michael@0 | 141 | // populate tab: (B) with different form data |
michael@0 | 142 | for (let item in fieldList) |
michael@0 | 143 | setFormValue(tab_B, item, fieldList[item]); |
michael@0 | 144 | |
michael@0 | 145 | // duplicate tab: (B) |
michael@0 | 146 | let tab_C = aWin.gBrowser.duplicateTab(tab_B); |
michael@0 | 147 | whenTabRestored(tab_C, function() { |
michael@0 | 148 | // verify the correctness of the duplicated tab |
michael@0 | 149 | is(ss.getTabValue(tab_C, key1), value1, |
michael@0 | 150 | "tab successfully duplicated - correct state"); |
michael@0 | 151 | |
michael@0 | 152 | for (let item in fieldList) |
michael@0 | 153 | ok(compareFormValue(tab_C, item, fieldList[item]), |
michael@0 | 154 | "The value for \"" + item + "\" was correctly duplicated"); |
michael@0 | 155 | |
michael@0 | 156 | // private browsing session, close tab: (C) and (B) |
michael@0 | 157 | aWin.gBrowser.removeTab(tab_C); |
michael@0 | 158 | aWin.gBrowser.removeTab(tab_B); |
michael@0 | 159 | |
michael@0 | 160 | finish(); |
michael@0 | 161 | }); |
michael@0 | 162 | }); |
michael@0 | 163 | }); |
michael@0 | 164 | }); |
michael@0 | 165 | }); |
michael@0 | 166 | }); |
michael@0 | 167 | } |