1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/tests/test_bug462106_perwindow.xul Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,136 @@ 1.4 +<?xml version="1.0"?> 1.5 +<?xml-stylesheet type="text/css" href="chrome://global/skin"?> 1.6 +<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?> 1.7 +<!-- 1.8 +https://bugzilla.mozilla.org/show_bug.cgi?id=462106 1.9 +--> 1.10 +<window title="Mozilla Bug 462106" 1.11 + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" 1.12 + onload="initAndRunTests()"> 1.13 + <script type="application/javascript" 1.14 + src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> 1.15 + 1.16 + <!-- test results are displayed in the html:body --> 1.17 + <body xmlns="http://www.w3.org/1999/xhtml"> 1.18 + <p id="display"></p> 1.19 + <div id="content" style="display: none"></div> 1.20 + <pre id="test"></pre> 1.21 + </body> 1.22 + 1.23 + <!-- test code goes here --> 1.24 + <script class="testbody" type="application/javascript"> 1.25 + <![CDATA[ 1.26 + 1.27 + /** Test for Bug 462106 **/ 1.28 + 1.29 +const Cc = Components.classes; 1.30 +const Ci = Components.interfaces; 1.31 +const Cu = Components.utils; 1.32 +Cu.import("resource://gre/modules/Services.jsm"); 1.33 + 1.34 +function copy(str, doc) { 1.35 + if (!doc) { 1.36 + doc = document; 1.37 + } 1.38 + Cc["@mozilla.org/widget/clipboardhelper;1"]. 1.39 + getService(Ci.nsIClipboardHelper). 1.40 + copyString(str, doc); 1.41 +} 1.42 + 1.43 +function getLoadContext() { 1.44 + return window.QueryInterface(Ci.nsIInterfaceRequestor) 1.45 + .getInterface(Ci.nsIWebNavigation) 1.46 + .QueryInterface(Ci.nsILoadContext); 1.47 +} 1.48 + 1.49 +function paste() { 1.50 + let trans = Cc["@mozilla.org/widget/transferable;1"]. 1.51 + createInstance(Ci.nsITransferable); 1.52 + trans.init(getLoadContext()); 1.53 + trans.addDataFlavor("text/unicode"); 1.54 + let clip = Cc["@mozilla.org/widget/clipboard;1"]. 1.55 + getService(Ci.nsIClipboard); 1.56 + clip.getData(trans, Ci.nsIClipboard.kGlobalClipboard); 1.57 + let str = {}, length = {}; 1.58 + try { 1.59 + trans.getTransferData("text/unicode", str, length); 1.60 + } catch (e) { 1.61 + str = null; 1.62 + } 1.63 + if (str) { 1.64 + str = str.value.QueryInterface(Ci.nsISupportsString); 1.65 + if (str) 1.66 + str = str.data.substring(0, length.value / 2); 1.67 + } 1.68 + return str; 1.69 +} 1.70 + 1.71 +function whenDelayedStartupFinished(aWindow, aCallback) { 1.72 + Services.obs.addObserver(function observer(aSubject, aTopic) { 1.73 + if (aWindow == aSubject) { 1.74 + Services.obs.removeObserver(observer, aTopic); 1.75 + setTimeout(aCallback, 0); 1.76 + } 1.77 + }, "browser-delayed-startup-finished", false); 1.78 +} 1.79 + 1.80 +function testOnWindow(options, callback) { 1.81 + var mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor) 1.82 + .getInterface(Ci.nsIWebNavigation) 1.83 + .QueryInterface(Ci.nsIDocShellTreeItem) 1.84 + .rootTreeItem 1.85 + .QueryInterface(Ci.nsIInterfaceRequestor) 1.86 + .getInterface(Ci.nsIDOMWindow); 1.87 + 1.88 + let win = mainWindow.OpenBrowserWindow(options); 1.89 + whenDelayedStartupFinished(win, function() { 1.90 + callback(win); 1.91 + }); 1.92 +}; 1.93 + 1.94 +function initAndRunTests() { 1.95 + SimpleTest.waitForExplicitFinish(); 1.96 + 1.97 + const data = "random number: " + Math.random(); 1.98 + copy(data); 1.99 + is(data, paste(), "Data successfully copied before entering the private browsing mode"); 1.100 + testOnWindow({private: true}, function (aPrivateWindow) { 1.101 + aPrivateWindow.close(); 1.102 + 1.103 + // the data should still be on the clipboard because it was copied before 1.104 + // entering the private browsing mode 1.105 + is(data, paste(), "Copied data persisted after leaving the private browsing mode"); 1.106 + 1.107 + const data2 = "another random number: " + Math.random(); 1.108 + 1.109 + testOnWindow({private: true}, function (aPrivateWindow) { 1.110 + copy(data2, aPrivateWindow.document); // copy the data inside the private browsing mode 1.111 + 1.112 + function insidePBPaste() { 1.113 + if (data2 != paste()) { 1.114 + setTimeout(insidePBPaste, 100); 1.115 + return; 1.116 + } 1.117 + // the data should be on the clipboard inside the private browsing mode 1.118 + is(data2, paste(), "Data successfully copied inside the private browsing mode"); 1.119 + aPrivateWindow.close(); 1.120 + 1.121 + function afterClose() { 1.122 + if (data2 == paste()) { 1.123 + setTimeout(afterClose, 100); 1.124 + return; 1.125 + } 1.126 + // the data should no longer be on the clipboard at this stage 1.127 + isnot(data2, paste(), "Data no longer available after leaving the private browsing mode"); 1.128 + SimpleTest.finish(); 1.129 + } 1.130 + afterClose(); 1.131 + } 1.132 + insidePBPaste(); 1.133 + }); 1.134 + }); 1.135 +} 1.136 + 1.137 + ]]> 1.138 + </script> 1.139 +</window>