widget/tests/test_bug466599.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/tests/test_bug466599.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,109 @@
     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="/tests/SimpleTest/test.css"?>
     1.7 +<!--
     1.8 +https://bugzilla.mozilla.org/show_bug.cgi?id=466599
     1.9 +-->
    1.10 +<window title="Mozilla Bug 466599"
    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 466599 **/
    1.28 +
    1.29 +function getLoadContext() {
    1.30 +  const Ci = Components.interfaces;
    1.31 +  return window.QueryInterface(Ci.nsIInterfaceRequestor)
    1.32 +               .getInterface(Ci.nsIWebNavigation)
    1.33 +               .QueryInterface(Ci.nsILoadContext);
    1.34 +}
    1.35 +
    1.36 +function copyToClipboard(txt)
    1.37 +{
    1.38 +  var clipid = Components.interfaces.nsIClipboard;
    1.39 +  var clip =
    1.40 +    Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(clipid);
    1.41 +  if (!clip)
    1.42 +    return false;
    1.43 +  var trans =
    1.44 +    Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
    1.45 +  if (!trans)
    1.46 +    return false;
    1.47 +  trans.init(getLoadContext());
    1.48 +  trans.addDataFlavor('text/html');
    1.49 +  var str =
    1.50 +    Components.classes['@mozilla.org/supports-string;1'].createInstance(Components.interfaces.nsISupportsString);
    1.51 +  var copytext = txt;
    1.52 +  str.data = copytext;
    1.53 +  trans.setTransferData("text/html",str,copytext.length*2);
    1.54 +  if (!clip)
    1.55 +    return false;
    1.56 +  clip.setData(trans,null,clipid.kGlobalClipboard);
    1.57 +  return true;
    1.58 +}
    1.59 +
    1.60 +function readFromClipboard()
    1.61 +{
    1.62 +  var clipid = Components.interfaces.nsIClipboard;
    1.63 +  var clip =
    1.64 +    Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(clipid);
    1.65 +  if (!clip)
    1.66 +    return;
    1.67 +  var trans =
    1.68 +    Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
    1.69 +  if (!trans)
    1.70 +    return;
    1.71 +  trans.init(getLoadContext());
    1.72 +  trans.addDataFlavor('text/html');
    1.73 +  clip.getData(trans,clipid.kGlobalClipboard);
    1.74 +  var str = new Object();
    1.75 +  var strLength = new Object();
    1.76 +  trans.getTransferData("text/html",str,strLength);
    1.77 +  if (str)
    1.78 +    str = str.value.QueryInterface(Components.interfaces.nsISupportsString);
    1.79 +  if (str)
    1.80 +    pastetext = str.data.substring(0,strLength.value / 2);
    1.81 +  return pastetext;
    1.82 +}
    1.83 +
    1.84 +function encodeHtmlEntities(s)
    1.85 +{
    1.86 +  var result = '';
    1.87 +  for (var i = 0; i < s.length; i++) {
    1.88 +    var c = s.charAt(i);
    1.89 +    result += {'<':'&lt;', '>':'&gt;', '&':'&amp;', '"':'&quot;'}[c] || c;
    1.90 +  }
    1.91 +  return result;
    1.92 +}
    1.93 +
    1.94 +function initAndRunTests()
    1.95 +{
    1.96 +  var source = '<p>Lorem ipsum</p>';
    1.97 +  var expect = new RegExp('<html>.*charset=utf-8.*' + source + '.*</html>', 'im');
    1.98 +
    1.99 +  var result = copyToClipboard(source);
   1.100 +  ok(result, "copied HTML data to system pasteboard");
   1.101 +
   1.102 +  result = readFromClipboard();
   1.103 +  ok(expect.test(result), "data on system pasteboard is wrapped with charset metadata");
   1.104 +
   1.105 +  $("display").innerHTML = 
   1.106 +    '<em>source:</em> <pre>' + encodeHtmlEntities(source) + '</pre><br/>' +
   1.107 +    '<em>result:</em> <pre>' + encodeHtmlEntities(result) + '</pre>';
   1.108 +}
   1.109 +
   1.110 +  ]]>
   1.111 +  </script>
   1.112 +</window>

mercurial