widget/tests/test_bug462106_perwindow.xul

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

mercurial