dom/tests/mochitest/general/test_paste_selection.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/mochitest/general/test_paste_selection.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,114 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <title>Test for middle-click to paste selection with paste events</title>
     1.8 +  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
     1.9 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.10 +  <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
    1.11 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.12 +</head>
    1.13 +<body>
    1.14 +<p id="display"></p>
    1.15 +<input id="copy-area" value="CLIPBOARD">
    1.16 +<input id="paste-selection-area" value="" onpaste="pastedSelection(event)">
    1.17 +<input id="paste-global-area" value="" onpaste="pastedGlobal(event)">
    1.18 +
    1.19 +<script>
    1.20 +
    1.21 +var supportsSelectionClipboard = SpecialPowers.supportsSelectionClipboard();
    1.22 +
    1.23 +function checkSelectionClipboardText(count)
    1.24 +{
    1.25 +  if ((!supportsSelectionClipboard ||
    1.26 +       SpecialPowers.getClipboardData("text/unicode", SpecialPowers.Ci.nsIClipboard.kSelectionClipboard) == "COPY TEXT") &&
    1.27 +      SpecialPowers.getClipboardData("text/unicode", SpecialPowers.Ci.nsIClipboard.kGlobalClipboard) == "CLIPBOARD") {
    1.28 +    pasteArea();
    1.29 +    return;
    1.30 +  }
    1.31 +
    1.32 +  if (count > 10) {
    1.33 +    ok(false, "could not set clipboards");
    1.34 +    pasteArea();
    1.35 +    SimpleTest.finish();
    1.36 +    return;
    1.37 +  }
    1.38 +
    1.39 +  setTimeout(checkSelectionClipboardText, 5, count + 1);
    1.40 +}
    1.41 +
    1.42 +function selectArea()
    1.43 +{
    1.44 +  var copyArea = document.getElementById("copy-area");
    1.45 +  copyArea.focus();
    1.46 +  copyArea.select();
    1.47 +  synthesizeKey("x", { accelKey: true });
    1.48 +
    1.49 +  if (supportsSelectionClipboard) {
    1.50 +    var clipboardHelper = SpecialPowers.Cc["@mozilla.org/widget/clipboardhelper;1"]
    1.51 +                                       .getService(SpecialPowers.Ci.nsIClipboardHelper);
    1.52 +    clipboardHelper.copyStringToClipboard("COPY TEXT",
    1.53 +                                          SpecialPowers.Ci.nsIClipboard.kSelectionClipboard,
    1.54 +                                          document);
    1.55 +  }
    1.56 +
    1.57 +  setTimeout(checkSelectionClipboardText, 50, 0);
    1.58 +}
    1.59 +
    1.60 +var selectionPasted = false;
    1.61 +var globalPasted = false;
    1.62 +
    1.63 +function pasteArea()
    1.64 +{
    1.65 +  var pasteArea = document.getElementById("paste-selection-area");
    1.66 +  pasteArea.focus();
    1.67 +  synthesizeMouse(pasteArea, 8, 8, { button: 1 });
    1.68 +
    1.69 +  var usesMouseButtonPaste = SpecialPowers.getBoolPref("middlemouse.paste");
    1.70 +  if (usesMouseButtonPaste) {
    1.71 +    // The data transfer should contain the selection data when the selection clipboard is supported,
    1.72 +    // not the global clipboard data.
    1.73 +    var expectedText = supportsSelectionClipboard ? "COPY TEXT" : "CLIPBOARD";
    1.74 +    is(document.getElementById("paste-selection-area").value, expectedText, "data pasted properly from selection");
    1.75 +    ok(selectionPasted, "selection event fired");
    1.76 +  }
    1.77 +  else {
    1.78 +    is(pasteArea.value, "", "data not pasted when middle click not supported");
    1.79 +  }
    1.80 +
    1.81 +  var pasteArea = document.getElementById("paste-global-area");
    1.82 +  pasteArea.focus();
    1.83 +  synthesizeKey("v", { accelKey: true });
    1.84 +
    1.85 +  ok(globalPasted, "global event fired");
    1.86 +  is(document.getElementById("paste-global-area").value, "CLIPBOARD", "data pasted properly from global clipboard");
    1.87 +  SimpleTest.finish();
    1.88 +}
    1.89 +
    1.90 +function pastedSelection(event)
    1.91 +{
    1.92 +  ok(SpecialPowers.getBoolPref("middlemouse.paste"), "paste on middle click is valid");
    1.93 +
    1.94 +  // Mac and Windows shouldn't get here as the middle mouse preference is false by default
    1.95 +  ok(navigator.platform.indexOf("Mac") == -1 && navigator.platform.indexOf("Win") == -1, "middle click enabled on right platforms");
    1.96 +
    1.97 +  var expectedText = supportsSelectionClipboard ? "COPY TEXT" : "CLIPBOARD";
    1.98 +  is(event.clipboardData.getData("text/plain"), expectedText, "data pasted properly from selection");
    1.99 +
   1.100 +  selectionPasted = true;
   1.101 +}
   1.102 +
   1.103 +function pastedGlobal(event)
   1.104 +{
   1.105 +  // The data transfer should contain the global data.
   1.106 +  is(event.clipboardData.getData("text/plain"), "CLIPBOARD", "data correct in global clipboard data transfer");
   1.107 +  globalPasted = true;
   1.108 +}
   1.109 +
   1.110 +SimpleTest.waitForExplicitFinish();
   1.111 +SimpleTest.waitForFocus(selectArea);
   1.112 +</script>
   1.113 +
   1.114 +</pre>
   1.115 +</body>
   1.116 +</html>
   1.117 +

mercurial