widget/tests/test_bug462106_perwindow.xul

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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"/>
    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>
    20   <!-- test code goes here -->
    21   <script class="testbody" type="application/javascript">
    22   <![CDATA[
    24   /** Test for Bug 462106 **/
    26 const Cc = Components.classes;
    27 const Ci = Components.interfaces;
    28 const Cu = Components.utils;
    29 Cu.import("resource://gre/modules/Services.jsm");
    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 }
    40 function getLoadContext() {
    41   return window.QueryInterface(Ci.nsIInterfaceRequestor)
    42                .getInterface(Ci.nsIWebNavigation)
    43                .QueryInterface(Ci.nsILoadContext);
    44 }
    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 }
    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 }
    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);
    85   let win = mainWindow.OpenBrowserWindow(options);
    86   whenDelayedStartupFinished(win, function() {
    87     callback(win);
    88   });
    89 };
    91 function initAndRunTests() {
    92   SimpleTest.waitForExplicitFinish();
    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();
   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");
   104     const data2 = "another random number: " + Math.random();
   106     testOnWindow({private: true}, function (aPrivateWindow) {
   107       copy(data2, aPrivateWindow.document); // copy the data inside the private browsing mode
   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();
   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 }
   134   ]]>
   135   </script>
   136 </window>

mercurial