widget/tests/test_bug462106_perwindow.xul

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 <?xml version="1.0"?>
michael@0 2 <?xml-stylesheet type="text/css" href="chrome://global/skin"?>
michael@0 3 <?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
michael@0 4 <!--
michael@0 5 https://bugzilla.mozilla.org/show_bug.cgi?id=462106
michael@0 6 -->
michael@0 7 <window title="Mozilla Bug 462106"
michael@0 8 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
michael@0 9 onload="initAndRunTests()">
michael@0 10 <script type="application/javascript"
michael@0 11 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
michael@0 12
michael@0 13 <!-- test results are displayed in the html:body -->
michael@0 14 <body xmlns="http://www.w3.org/1999/xhtml">
michael@0 15 <p id="display"></p>
michael@0 16 <div id="content" style="display: none"></div>
michael@0 17 <pre id="test"></pre>
michael@0 18 </body>
michael@0 19
michael@0 20 <!-- test code goes here -->
michael@0 21 <script class="testbody" type="application/javascript">
michael@0 22 <![CDATA[
michael@0 23
michael@0 24 /** Test for Bug 462106 **/
michael@0 25
michael@0 26 const Cc = Components.classes;
michael@0 27 const Ci = Components.interfaces;
michael@0 28 const Cu = Components.utils;
michael@0 29 Cu.import("resource://gre/modules/Services.jsm");
michael@0 30
michael@0 31 function copy(str, doc) {
michael@0 32 if (!doc) {
michael@0 33 doc = document;
michael@0 34 }
michael@0 35 Cc["@mozilla.org/widget/clipboardhelper;1"].
michael@0 36 getService(Ci.nsIClipboardHelper).
michael@0 37 copyString(str, doc);
michael@0 38 }
michael@0 39
michael@0 40 function getLoadContext() {
michael@0 41 return window.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 42 .getInterface(Ci.nsIWebNavigation)
michael@0 43 .QueryInterface(Ci.nsILoadContext);
michael@0 44 }
michael@0 45
michael@0 46 function paste() {
michael@0 47 let trans = Cc["@mozilla.org/widget/transferable;1"].
michael@0 48 createInstance(Ci.nsITransferable);
michael@0 49 trans.init(getLoadContext());
michael@0 50 trans.addDataFlavor("text/unicode");
michael@0 51 let clip = Cc["@mozilla.org/widget/clipboard;1"].
michael@0 52 getService(Ci.nsIClipboard);
michael@0 53 clip.getData(trans, Ci.nsIClipboard.kGlobalClipboard);
michael@0 54 let str = {}, length = {};
michael@0 55 try {
michael@0 56 trans.getTransferData("text/unicode", str, length);
michael@0 57 } catch (e) {
michael@0 58 str = null;
michael@0 59 }
michael@0 60 if (str) {
michael@0 61 str = str.value.QueryInterface(Ci.nsISupportsString);
michael@0 62 if (str)
michael@0 63 str = str.data.substring(0, length.value / 2);
michael@0 64 }
michael@0 65 return str;
michael@0 66 }
michael@0 67
michael@0 68 function whenDelayedStartupFinished(aWindow, aCallback) {
michael@0 69 Services.obs.addObserver(function observer(aSubject, aTopic) {
michael@0 70 if (aWindow == aSubject) {
michael@0 71 Services.obs.removeObserver(observer, aTopic);
michael@0 72 setTimeout(aCallback, 0);
michael@0 73 }
michael@0 74 }, "browser-delayed-startup-finished", false);
michael@0 75 }
michael@0 76
michael@0 77 function testOnWindow(options, callback) {
michael@0 78 var mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 79 .getInterface(Ci.nsIWebNavigation)
michael@0 80 .QueryInterface(Ci.nsIDocShellTreeItem)
michael@0 81 .rootTreeItem
michael@0 82 .QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 83 .getInterface(Ci.nsIDOMWindow);
michael@0 84
michael@0 85 let win = mainWindow.OpenBrowserWindow(options);
michael@0 86 whenDelayedStartupFinished(win, function() {
michael@0 87 callback(win);
michael@0 88 });
michael@0 89 };
michael@0 90
michael@0 91 function initAndRunTests() {
michael@0 92 SimpleTest.waitForExplicitFinish();
michael@0 93
michael@0 94 const data = "random number: " + Math.random();
michael@0 95 copy(data);
michael@0 96 is(data, paste(), "Data successfully copied before entering the private browsing mode");
michael@0 97 testOnWindow({private: true}, function (aPrivateWindow) {
michael@0 98 aPrivateWindow.close();
michael@0 99
michael@0 100 // the data should still be on the clipboard because it was copied before
michael@0 101 // entering the private browsing mode
michael@0 102 is(data, paste(), "Copied data persisted after leaving the private browsing mode");
michael@0 103
michael@0 104 const data2 = "another random number: " + Math.random();
michael@0 105
michael@0 106 testOnWindow({private: true}, function (aPrivateWindow) {
michael@0 107 copy(data2, aPrivateWindow.document); // copy the data inside the private browsing mode
michael@0 108
michael@0 109 function insidePBPaste() {
michael@0 110 if (data2 != paste()) {
michael@0 111 setTimeout(insidePBPaste, 100);
michael@0 112 return;
michael@0 113 }
michael@0 114 // the data should be on the clipboard inside the private browsing mode
michael@0 115 is(data2, paste(), "Data successfully copied inside the private browsing mode");
michael@0 116 aPrivateWindow.close();
michael@0 117
michael@0 118 function afterClose() {
michael@0 119 if (data2 == paste()) {
michael@0 120 setTimeout(afterClose, 100);
michael@0 121 return;
michael@0 122 }
michael@0 123 // the data should no longer be on the clipboard at this stage
michael@0 124 isnot(data2, paste(), "Data no longer available after leaving the private browsing mode");
michael@0 125 SimpleTest.finish();
michael@0 126 }
michael@0 127 afterClose();
michael@0 128 }
michael@0 129 insidePBPaste();
michael@0 130 });
michael@0 131 });
michael@0 132 }
michael@0 133
michael@0 134 ]]>
michael@0 135 </script>
michael@0 136 </window>

mercurial