1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/sessionstore/test/browser_248970_b_perwindowpb.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,167 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +function test() { 1.9 + /** Test (B) for Bug 248970 **/ 1.10 + waitForExplicitFinish(); 1.11 + 1.12 + let windowsToClose = []; 1.13 + let file = Services.dirsvc.get("TmpD", Ci.nsIFile); 1.14 + let filePath = file.path; 1.15 + let fieldList = { 1.16 + "//input[@name='input']": Date.now().toString(), 1.17 + "//input[@name='spaced 1']": Math.random().toString(), 1.18 + "//input[3]": "three", 1.19 + "//input[@type='checkbox']": true, 1.20 + "//input[@name='uncheck']": false, 1.21 + "//input[@type='radio'][1]": false, 1.22 + "//input[@type='radio'][2]": true, 1.23 + "//input[@type='radio'][3]": false, 1.24 + "//select": 2, 1.25 + "//select[@multiple]": [1, 3], 1.26 + "//textarea[1]": "", 1.27 + "//textarea[2]": "Some text... " + Math.random(), 1.28 + "//textarea[3]": "Some more text\n" + new Date(), 1.29 + "//input[@type='file']": filePath 1.30 + }; 1.31 + 1.32 + registerCleanupFunction(function() { 1.33 + windowsToClose.forEach(function(win) { 1.34 + win.close(); 1.35 + }); 1.36 + }); 1.37 + 1.38 + function test(aLambda) { 1.39 + try { 1.40 + return aLambda() || true; 1.41 + } catch(ex) { } 1.42 + return false; 1.43 + } 1.44 + 1.45 + function getElementByXPath(aTab, aQuery) { 1.46 + let doc = aTab.linkedBrowser.contentDocument; 1.47 + let xptype = Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE; 1.48 + return doc.evaluate(aQuery, doc, null, xptype, null).singleNodeValue; 1.49 + } 1.50 + 1.51 + function setFormValue(aTab, aQuery, aValue) { 1.52 + let node = getElementByXPath(aTab, aQuery); 1.53 + if (typeof aValue == "string") 1.54 + node.value = aValue; 1.55 + else if (typeof aValue == "boolean") 1.56 + node.checked = aValue; 1.57 + else if (typeof aValue == "number") 1.58 + node.selectedIndex = aValue; 1.59 + else 1.60 + Array.forEach(node.options, function(aOpt, aIx) 1.61 + (aOpt.selected = aValue.indexOf(aIx) > -1)); 1.62 + } 1.63 + 1.64 + function compareFormValue(aTab, aQuery, aValue) { 1.65 + let node = getElementByXPath(aTab, aQuery); 1.66 + if (!node) 1.67 + return false; 1.68 + if (node instanceof Ci.nsIDOMHTMLInputElement) 1.69 + return aValue == (node.type == "checkbox" || node.type == "radio" ? 1.70 + node.checked : node.value); 1.71 + if (node instanceof Ci.nsIDOMHTMLTextAreaElement) 1.72 + return aValue == node.value; 1.73 + if (!node.multiple) 1.74 + return aValue == node.selectedIndex; 1.75 + return Array.every(node.options, function(aOpt, aIx) 1.76 + (aValue.indexOf(aIx) > -1) == aOpt.selected); 1.77 + } 1.78 + 1.79 + ////////////////////////////////////////////////////////////////// 1.80 + // Test (B) : Session data restoration between windows // 1.81 + ////////////////////////////////////////////////////////////////// 1.82 + 1.83 + let rootDir = getRootDirectory(gTestPath); 1.84 + const testURL = rootDir + "browser_248970_b_sample.html"; 1.85 + const testURL2 = "http://mochi.test:8888/browser/" + 1.86 + "browser/components/sessionstore/test/browser_248970_b_sample.html"; 1.87 + 1.88 + whenNewWindowLoaded({ private: false }, function(aWin) { 1.89 + windowsToClose.push(aWin); 1.90 + 1.91 + // get closed tab count 1.92 + let count = ss.getClosedTabCount(aWin); 1.93 + let max_tabs_undo = 1.94 + Services.prefs.getIntPref("browser.sessionstore.max_tabs_undo"); 1.95 + ok(0 <= count && count <= max_tabs_undo, 1.96 + "getClosedTabCount should return zero or at most max_tabs_undo"); 1.97 + 1.98 + // setup a state for tab (A) so we can check later that is restored 1.99 + let key = "key"; 1.100 + let value = "Value " + Math.random(); 1.101 + let state = { entries: [{ url: testURL }], extData: { key: value } }; 1.102 + 1.103 + // public session, add new tab: (A) 1.104 + let tab_A = aWin.gBrowser.addTab(testURL); 1.105 + ss.setTabState(tab_A, JSON.stringify(state)); 1.106 + whenBrowserLoaded(tab_A.linkedBrowser, function() { 1.107 + // make sure that the next closed tab will increase getClosedTabCount 1.108 + Services.prefs.setIntPref( 1.109 + "browser.sessionstore.max_tabs_undo", max_tabs_undo + 1) 1.110 + 1.111 + // populate tab_A with form data 1.112 + for (let i in fieldList) 1.113 + setFormValue(tab_A, i, fieldList[i]); 1.114 + 1.115 + // public session, close tab: (A) 1.116 + aWin.gBrowser.removeTab(tab_A); 1.117 + 1.118 + // verify that closedTabCount increased 1.119 + ok(ss.getClosedTabCount(aWin) > count, 1.120 + "getClosedTabCount has increased after closing a tab"); 1.121 + 1.122 + // verify tab: (A), in undo list 1.123 + let tab_A_restored = test(function() ss.undoCloseTab(aWin, 0)); 1.124 + ok(tab_A_restored, "a tab is in undo list"); 1.125 + whenTabRestored(tab_A_restored, function() { 1.126 + is(testURL, tab_A_restored.linkedBrowser.currentURI.spec, 1.127 + "it's the same tab that we expect"); 1.128 + aWin.gBrowser.removeTab(tab_A_restored); 1.129 + 1.130 + whenNewWindowLoaded({ private: true }, function(aWin) { 1.131 + windowsToClose.push(aWin); 1.132 + 1.133 + // setup a state for tab (B) so we can check that its duplicated 1.134 + // properly 1.135 + let key1 = "key1"; 1.136 + let value1 = "Value " + Math.random(); 1.137 + let state1 = { 1.138 + entries: [{ url: testURL2 }], extData: { key1: value1 } 1.139 + }; 1.140 + 1.141 + let tab_B = aWin.gBrowser.addTab(testURL2); 1.142 + ss.setTabState(tab_B, JSON.stringify(state1)); 1.143 + whenTabRestored(tab_B, function() { 1.144 + // populate tab: (B) with different form data 1.145 + for (let item in fieldList) 1.146 + setFormValue(tab_B, item, fieldList[item]); 1.147 + 1.148 + // duplicate tab: (B) 1.149 + let tab_C = aWin.gBrowser.duplicateTab(tab_B); 1.150 + whenTabRestored(tab_C, function() { 1.151 + // verify the correctness of the duplicated tab 1.152 + is(ss.getTabValue(tab_C, key1), value1, 1.153 + "tab successfully duplicated - correct state"); 1.154 + 1.155 + for (let item in fieldList) 1.156 + ok(compareFormValue(tab_C, item, fieldList[item]), 1.157 + "The value for \"" + item + "\" was correctly duplicated"); 1.158 + 1.159 + // private browsing session, close tab: (C) and (B) 1.160 + aWin.gBrowser.removeTab(tab_C); 1.161 + aWin.gBrowser.removeTab(tab_B); 1.162 + 1.163 + finish(); 1.164 + }); 1.165 + }); 1.166 + }); 1.167 + }); 1.168 + }); 1.169 + }); 1.170 +}