widget/tests/test_clipboard.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/tests/test_clipboard.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,80 @@
     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="chrome://mochikit/content/tests/SimpleTest/test.css"?>
     1.7 +<!--
     1.8 +https://bugzilla.mozilla.org/show_bug.cgi?id=948065
     1.9 +-->
    1.10 +<window title="Mozilla Bug 948065"
    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 948065 **/
    1.28 +
    1.29 +  const Cc = Components.classes;
    1.30 +  const Ci = Components.interfaces;
    1.31 +
    1.32 +  const kIsMac = navigator.platform.indexOf("Mac") == 0;
    1.33 +
    1.34 +  function getLoadContext() {
    1.35 +    return window.QueryInterface(Ci.nsIInterfaceRequestor)
    1.36 +                 .getInterface(Ci.nsIWebNavigation)
    1.37 +                 .QueryInterface(Ci.nsILoadContext);
    1.38 +  }
    1.39 +
    1.40 +  // Get clipboard data to paste.
    1.41 +  function paste(clipboard) {
    1.42 +    let trans = Cc['@mozilla.org/widget/transferable;1']
    1.43 +                .createInstance(Ci.nsITransferable);
    1.44 +    trans.init(getLoadContext());
    1.45 +    trans.addDataFlavor("text/unicode");
    1.46 +    clipboard.getData(trans, Ci.nsIClipboard.kGlobalClipboard);
    1.47 +    let str = {};
    1.48 +    let length = {};
    1.49 +    try {
    1.50 +      trans.getTransferData('text/unicode', str, length);
    1.51 +    } catch (e) {
    1.52 +      str = '';
    1.53 +    }
    1.54 +    if (str) {
    1.55 +      str = str.value.QueryInterface(Ci.nsISupportsString);
    1.56 +      if (str) {
    1.57 +        str = str.data.substring(0, length.value / 2);
    1.58 +      }
    1.59 +    }
    1.60 +    return str;
    1.61 +  }
    1.62 +
    1.63 +  function initAndRunTests() {
    1.64 +    let clipboard = Cc['@mozilla.org/widget/clipboard;1']
    1.65 +                    .getService(Ci.nsIClipboard);
    1.66 +
    1.67 +    // Test copy.
    1.68 +    const data = "random number: " + Math.random();
    1.69 +    let helper = Cc['@mozilla.org/widget/clipboardhelper;1']
    1.70 +                 .getService(Ci.nsIClipboardHelper);
    1.71 +    helper.copyString(data, document);
    1.72 +    is(paste(clipboard), data, 'Data was successfully copied.');
    1.73 +
    1.74 +    // Test emptyClipboard, disabled for OSX because bug 666254
    1.75 +    if (!kIsMac) {
    1.76 +      clipboard.emptyClipboard(Ci.nsIClipboard.kGlobalClipboard);
    1.77 +      is(paste(clipboard), '', 'Data was successfully cleared.');
    1.78 +    }
    1.79 +  }
    1.80 +
    1.81 +  ]]>
    1.82 +  </script>
    1.83 +</window>

mercurial