tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_CF_HTML_clipboard.html (4738B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=572642
      5 -->
      6 <head>
      7  <title>Test for Bug 572642</title>
      8  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      9  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     10  <script src="/tests/SimpleTest/EventUtils.js"></script>
     11 </head>
     12 <body>
     13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=572642">Mozilla Bug 572642</a>
     14 <p id="display"></p>
     15 <div id="content">
     16  <div id="editor1" contenteditable="true"></div>
     17  <iframe id="editor2"></iframe>
     18 </div>
     19 <pre id="test">
     20 <script type="application/javascript">
     21 
     22 /** Test for Bug 572642 */
     23 
     24 function copyCF_HTML(cfhtml, success, failure) {
     25  const Cc = SpecialPowers.Cc;
     26  const Ci = SpecialPowers.Ci;
     27  const CF_HTML = "application/x-moz-nativehtml";
     28 
     29  function getLoadContext() {
     30    return SpecialPowers.wrap(window)
     31                 .docShell
     32                 .QueryInterface(Ci.nsILoadContext);
     33  }
     34 
     35  var cb = SpecialPowers.Services.clipboard;
     36 
     37  var counter = 0;
     38  function copyCF_HTML_worker(successFn, failureFn) {
     39    if (++counter > 50) {
     40      ok(false, "Timed out while polling clipboard for pasted data");
     41      failure();
     42      return;
     43    }
     44 
     45    var flavors = [CF_HTML];
     46    if (!cb.hasDataMatchingFlavors(flavors, cb.kGlobalClipboard)) {
     47      setTimeout(function() { copyCF_HTML_worker(successFn, failureFn); }, 100);
     48      return;
     49    }
     50 
     51    var trans = Cc["@mozilla.org/widget/transferable;1"].
     52                createInstance(Ci.nsITransferable);
     53    trans.init(getLoadContext());
     54    trans.addDataFlavor(CF_HTML);
     55    cb.getData(trans, cb.kGlobalClipboard, SpecialPowers.wrap(window).browsingContext.currentWindowContext);
     56    var data = SpecialPowers.createBlankObject();
     57    try {
     58      trans.getTransferData(CF_HTML, data);
     59      data = SpecialPowers.wrap(data).value.QueryInterface(Ci.nsISupportsCString).data;
     60    } catch (e) {
     61      setTimeout(function() { copyCF_HTML_worker(successFn, failureFn); }, 100);
     62      return;
     63    }
     64    success();
     65  }
     66 
     67  var trans = Cc["@mozilla.org/widget/transferable;1"].
     68              createInstance(Ci.nsITransferable);
     69  trans.init(getLoadContext());
     70  trans.addDataFlavor(CF_HTML);
     71  var data = Cc["@mozilla.org/supports-cstring;1"].
     72             createInstance(Ci.nsISupportsCString);
     73  data.data = cfhtml;
     74  trans.setTransferData(CF_HTML, data);
     75  cb.setData(trans, null, cb.kGlobalClipboard);
     76  copyCF_HTML_worker(success, failure);
     77 }
     78 
     79 function loadCF_HTMLdata(filename) {
     80  var req = new XMLHttpRequest();
     81  req.open("GET", filename, false);
     82  req.overrideMimeType("text/plain; charset=x-user-defined");
     83  req.send(null);
     84  is(req.status, 200, "Could not read the binary file " + filename);
     85  return req.responseText;
     86 }
     87 
     88 var gTests = [
     89  // Copied from Firefox
     90  {fileName: "cfhtml-firefox.txt", expected: "Firefox"},
     91  // Copied from OpenOffice.org
     92  {fileName: "cfhtml-ooo.txt", expected: "hello"},
     93  // Copied from IE
     94  {fileName: "cfhtml-ie.txt", expected: "browser"},
     95  // Copied from Chromium
     96  {fileName: "cfhtml-chromium.txt", expected: "Pacific"},
     97  // CF_HTML with no context specified (StartHTML and EndHTML set to -1)
     98  {fileName: "cfhtml-nocontext.txt", expected: "3.1415926535897932"},
     99 ];
    100 var gTestIndex = 0;
    101 
    102 SimpleTest.waitForExplicitFinish();
    103 SimpleTest.requestFlakyTimeout("It's a legacy test.");
    104 
    105 for (var i = 0; i < gTests.length; ++i) {
    106  gTests[i].data = loadCF_HTMLdata("data/" + gTests[i].fileName);
    107 }
    108 
    109 function runTest() {
    110  var test = gTests[gTestIndex++];
    111 
    112  copyCF_HTML(test.data, function() {
    113    // contenteditable
    114    var contentEditable = document.getElementById("editor1");
    115    contentEditable.innerHTML = "";
    116    contentEditable.focus();
    117    synthesizeKey("v", {accelKey: true});
    118    isnot(contentEditable.textContent.indexOf(test.expected), -1,
    119      "Paste operation for " + test.fileName + " should be successful in contenteditable");
    120 
    121    // designMode
    122    var iframe = document.getElementById("editor2");
    123    iframe.addEventListener("load", function() {
    124      var doc = iframe.contentDocument;
    125      var win = doc.defaultView;
    126      setTimeout(function() {
    127        win.addEventListener("focus", function() {
    128          doc.designMode = "on";
    129          synthesizeKey("v", {accelKey: true}, win);
    130          isnot(doc.body.textContent.indexOf(test.expected), -1,
    131            "Paste operation for " + test.fileName + " should be successful in designMode");
    132 
    133          if (gTestIndex == gTests.length)
    134            SimpleTest.finish();
    135          else
    136            runTest();
    137        }, {once: true});
    138        win.focus();
    139      }, 0);
    140    }, {once: true});
    141    iframe.srcdoc = "foo";
    142  }, SimpleTest.finish);
    143 }
    144 
    145 SimpleTest.waitForFocus(runTest);
    146 </script>
    147 </pre>
    148 </body>
    149 </html>