test_paste_selection.html (4340B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Test for middle-click to paste selection with paste events</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <script src="/tests/SimpleTest/EventUtils.js"></script> 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 8 </head> 9 <body> 10 <p id="display"></p> 11 <input id="copy-area" value="CLIPBOARD"> 12 <input id="paste-selection-area" value="" onpaste="pastedSelection(event)"> 13 <input id="paste-global-area" value="" onpaste="pastedGlobal(event)"> 14 15 <script> 16 17 var supportsSelectionClipboard = SpecialPowers.supportsSelectionClipboard(); 18 19 function checkSelectionClipboardText(count) 20 { 21 if ((!supportsSelectionClipboard || 22 SpecialPowers.getClipboardData("text/plain", SpecialPowers.Ci.nsIClipboard.kSelectionClipboard) == "COPY TEXT") && 23 SpecialPowers.getClipboardData("text/plain", SpecialPowers.Ci.nsIClipboard.kGlobalClipboard) == "CLIPBOARD") { 24 pasteArea(); 25 return; 26 } 27 28 if (count > 10) { 29 ok(false, "could not set clipboards"); 30 pasteArea(); 31 SimpleTest.finish(); 32 return; 33 } 34 35 setTimeout(checkSelectionClipboardText, 5, count + 1); 36 } 37 38 function selectArea() 39 { 40 var copyArea = document.getElementById("copy-area"); 41 copyArea.focus(); 42 copyArea.select(); 43 synthesizeKey("x", { accelKey: true }); 44 45 if (supportsSelectionClipboard) { 46 var clipboardHelper = SpecialPowers.Cc["@mozilla.org/widget/clipboardhelper;1"] 47 .getService(SpecialPowers.Ci.nsIClipboardHelper); 48 clipboardHelper.copyStringToClipboard("COPY TEXT", 49 SpecialPowers.Ci.nsIClipboard.kSelectionClipboard); 50 } 51 52 setTimeout(checkSelectionClipboardText, 50, 0); 53 } 54 55 var selectionPasted = false; 56 var globalPasted = false; 57 58 function pasteArea() 59 { 60 var pasteArea = document.getElementById("paste-selection-area"); 61 var pasteAreaRect = pasteArea.getBoundingClientRect(); 62 var pasteAreaCenterX = pasteAreaRect.left + pasteAreaRect.width/2; 63 var pasteAreaCenterY = pasteAreaRect.top + pasteAreaRect.height/2; 64 var wrappedWindow = SpecialPowers.wrap(window); 65 66 pasteArea.focus(); 67 wrappedWindow.synthesizeMouseEvent("mousedown", pasteAreaCenterX, pasteAreaCenterY, { button: 1 }, { toWindow: true }); 68 wrappedWindow.synthesizeMouseEvent("mouseup", pasteAreaCenterX, pasteAreaCenterY, { button: 1 }, { toWindow: true }); 69 70 var usesMouseButtonPaste = SpecialPowers.getBoolPref("middlemouse.paste"); 71 if (usesMouseButtonPaste) { 72 // The data transfer should contain the selection data when the selection clipboard is supported, 73 // not the global clipboard data. 74 var expectedText = supportsSelectionClipboard ? "COPY TEXT" : "CLIPBOARD"; 75 is(document.getElementById("paste-selection-area").value, expectedText, "In pasteArea(): data pasted properly from selection"); 76 ok(selectionPasted, "selection event fired"); 77 } 78 else { 79 is(pasteArea.value, "", "data not pasted when middle click not supported"); 80 } 81 82 var pasteArea = document.getElementById("paste-global-area"); 83 pasteArea.focus(); 84 synthesizeKey("v", { accelKey: true }); 85 86 ok(globalPasted, "global event fired"); 87 is(document.getElementById("paste-global-area").value, "CLIPBOARD", "data pasted properly from global clipboard"); 88 SimpleTest.finish(); 89 } 90 91 function pastedSelection(event) 92 { 93 ok(SpecialPowers.getBoolPref("middlemouse.paste"), "paste on middle click is valid"); 94 95 // Mac and Windows shouldn't get here as the middle mouse preference is false by default 96 ok(!navigator.platform.includes("Mac") && !navigator.platform.includes("Win"), "middle click enabled on right platforms"); 97 98 var expectedText = supportsSelectionClipboard ? "COPY TEXT" : "CLIPBOARD"; 99 is(event.clipboardData.getData("text/plain"), expectedText, "In pastedSelection(): data pasted properly from selection"); 100 101 selectionPasted = true; 102 } 103 104 function pastedGlobal(event) 105 { 106 // The data transfer should contain the global data. 107 is(event.clipboardData.getData("text/plain"), "CLIPBOARD", "data correct in global clipboard data transfer"); 108 globalPasted = true; 109 } 110 111 SimpleTest.waitForExplicitFinish(); 112 SimpleTest.requestFlakyTimeout("untriaged"); 113 SimpleTest.waitForFocus(function() { 114 SpecialPowers.pushPrefEnv({"set": [["general.autoScroll", false]]}, selectArea); 115 }); 116 </script> 117 118 </pre> 119 </body> 120 </html>