1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/general/test_clipboard_events.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,682 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<head> 1.7 + <title>Test for Clipboard 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 +<div id="content" style="border: 3px solid black; padding: 3em;">CONTENT TEXT<input id="content-input" value="INPUT TEXT"></div> 1.16 +<button id="button">Button</button> 1.17 + 1.18 +<div id="syntheticSpot" oncut="compareSynthetic(event, 'cut')" 1.19 + oncopy="compareSynthetic(event, 'copy')" 1.20 + onpaste="compareSynthetic(event, 'paste')">Spot</div> 1.21 + 1.22 +<pre id="test"> 1.23 +<script class="testbody" type="text/javascript;version=1.7"> 1.24 + 1.25 +var content = document.getElementById("content"); 1.26 +var contentInput = document.getElementById("content-input"); 1.27 +var clipboardInitialValue = "empty"; 1.28 + 1.29 +// Test that clearing and reading the clipboard works. A random number 1.30 +// is used to make sure that leftover clipboard values from a previous 1.31 +// test run don't cause a false-positive test. 1.32 +var cb_text = "empty_" + Math.random(); 1.33 +setClipboardText(cb_text); 1.34 + 1.35 +is(getClipboardText(), cb_text, "set/get clipboard text failed"); 1.36 + 1.37 +// Some test functions need to be run with delays. 1.38 +var delayedTests = []; 1.39 + 1.40 +var cachedCutData, cachedCopyData, cachedPasteData; 1.41 + 1.42 +// Ensure window focus before running tests, otherwise key events can 1.43 +// misfire. We set the onfocus event handler here to actually begin 1.44 +// running tests, and call window.focus() afterwards. 1.45 +window.onfocus = function() 1.46 +{ 1.47 + window.onfocus = null; 1.48 + 1.49 + // A list of test functions to run. Before each test function is run, the 1.50 + // clipboard is initialized to clipboardInitialValue, and the contents of 1.51 + // div#content are set as the window's selection. 1.52 + var testFunctions = [ 1.53 + test_dom_oncopy, 1.54 + test_dom_oncut, 1.55 + test_dom_onpaste, 1.56 + test_dom_oncopy_abort, 1.57 + test_input_oncopy, 1.58 + test_input_oncut, 1.59 + test_input_onpaste, 1.60 + test_input_oncopy_abort, 1.61 + test_input_oncut_abort, 1.62 + test_input_onpaste_abort, 1.63 + test_input_cut_dataTransfer, 1.64 + test_input_cut_abort_dataTransfer, 1.65 + test_input_copy_dataTransfer, 1.66 + test_input_paste_dataTransfer, 1.67 + test_input_paste_abort_dataTransfer, 1.68 + test_input_copypaste_dataTransfer_multiple, 1.69 + test_input_copy_button_dataTransfer, 1.70 + test_eventspref_disabled 1.71 + ]; 1.72 + 1.73 + // Run the main tests. This will also populate the delayedTests array 1.74 + for (let i = 0; i < testFunctions.length; i++) { 1.75 + // Init clipboard 1.76 + setClipboardText(clipboardInitialValue); 1.77 + 1.78 + // Reset value of contentInput. 1.79 + contentInput.value = "INPUT TEXT"; 1.80 + 1.81 + var func = testFunctions[i]; 1.82 + func(); 1.83 + } 1.84 + 1.85 + // Check if the cached clipboard data can be accessed or modified 1.86 + // and whether it modifies the real clipboard 1.87 + checkCachedDataTransfer(cachedCutData, "cut"); 1.88 + checkCachedDataTransfer(cachedCopyData, "copy"); 1.89 + checkCachedDataTransfer(cachedPasteData, "paste"); 1.90 + 1.91 + checkSyntheticEvents(); 1.92 + 1.93 + SimpleTest.finish(); 1.94 +} 1.95 + 1.96 +// Calling .focus begins the test run. 1.97 +SimpleTest.waitForExplicitFinish(); 1.98 +window.focus(); 1.99 + 1.100 +function getLoadContext() { 1.101 + return SpecialPowers.wrap(window).QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor) 1.102 + .getInterface(SpecialPowers.Ci.nsIWebNavigation) 1.103 + .QueryInterface(SpecialPowers.Ci.nsILoadContext); 1.104 +} 1.105 + 1.106 +function getClipboardText() { 1.107 + return SpecialPowers.getClipboardData("text/unicode"); 1.108 +} 1.109 + 1.110 + 1.111 +function setClipboardText(text) { 1.112 + var helper = SpecialPowers.Cc["@mozilla.org/widget/clipboardhelper;1"] 1.113 + .getService(SpecialPowers.Ci.nsIClipboardHelper); 1.114 + helper.copyString(text); 1.115 +} 1.116 + 1.117 +function selectContentDiv() { 1.118 + // Set selection 1.119 + var selection = window.getSelection(); 1.120 + selection.removeAllRanges(); 1.121 + selection.selectAllChildren(content); 1.122 +} 1.123 + 1.124 +function selectContentInput() { 1.125 + contentInput.select(); 1.126 + contentInput.focus(); 1.127 +} 1.128 + 1.129 +function test_dom_oncopy() { 1.130 + // Setup an oncopy event handler, fire copy. Ensure that the event 1.131 + // handler was called, and the clipboard contents have set to CONTENT TEXT. 1.132 + // Test firing oncopy event on ctrl-c: 1.133 + selectContentDiv(); 1.134 + 1.135 + var oncopy_fired = false; 1.136 + content.oncopy = function() { oncopy_fired = true; }; 1.137 + try { 1.138 + synthesizeKey("c", {accelKey: 1}); 1.139 + ok(oncopy_fired, "copy event firing on DOM element"); 1.140 + is(getClipboardText(), "CONTENT TEXT", 1.141 + "copy on DOM element set clipboard correctly"); 1.142 + } finally { 1.143 + content.oncopy = null; 1.144 + } 1.145 +} 1.146 + 1.147 +function test_dom_oncut() { 1.148 + // Setup an oncut event handler, fire cut. Ensure that the event handler 1.149 + // was called. The <div> doesn't handle a cut, so ensure that the 1.150 + // clipboard text is clipboardInitialValue, NOT "CONTENT TEXT". 1.151 + selectContentDiv(); 1.152 + var oncut_fired = false; 1.153 + content.oncut = function() { oncut_fired = true; }; 1.154 + try { 1.155 + synthesizeKey("x", {accelKey: 1}); 1.156 + ok(!oncut_fired, "cut event firing on DOM element") 1.157 + is(getClipboardText(), clipboardInitialValue, 1.158 + "cut on DOM element did not modify clipboard"); 1.159 + } finally { 1.160 + content.oncut = null; 1.161 + } 1.162 +} 1.163 + 1.164 + 1.165 +function test_dom_onpaste() { 1.166 + // Setup an onpaste event handler, fire paste. Ensure that the event 1.167 + // handler was called. 1.168 + selectContentDiv(); 1.169 + var onpaste_fired = false; 1.170 + content.onpaste = function() { onpaste_fired = true; }; 1.171 + try { 1.172 + synthesizeKey("v", {accelKey: 1}); 1.173 + ok(!onpaste_fired, "paste event firing on DOM element"); 1.174 + } finally { 1.175 + content.onpaste = null; 1.176 + } 1.177 +} 1.178 + 1.179 + 1.180 +function test_dom_oncopy_abort() { 1.181 + // Setup an oncopy event handler that aborts the copy, and fire the copy 1.182 + // event. Ensure that the event handler was fired, and the clipboard 1.183 + // contents have not been modified. 1.184 + selectContentDiv(); 1.185 + var oncopy_fired = false; 1.186 + content.oncopy = function() { oncopy_fired = true; return false; }; 1.187 + try { 1.188 + synthesizeKey("c", {accelKey: 1}); 1.189 + ok(oncopy_fired, "copy event (to-be-cancelled) firing on DOM element"); 1.190 + is(getClipboardText(), clipboardInitialValue, 1.191 + "aborted copy on DOM element did not modify clipboard"); 1.192 + } finally { 1.193 + content.oncopy = null; 1.194 + } 1.195 +} 1.196 + 1.197 + 1.198 +function test_input_oncopy() { 1.199 + // Setup an oncopy event handler, fire copy. Ensure that the event 1.200 + // handler was called, and the clipboard contents have been set to 'PUT TE', 1.201 + // which is the part that is selected below. 1.202 + selectContentInput(); 1.203 + contentInput.focus(); 1.204 + contentInput.setSelectionRange(2, 8); 1.205 + 1.206 + var oncopy_fired = false; 1.207 + contentInput.oncopy = function() { oncopy_fired = true; }; 1.208 + try { 1.209 + synthesizeKey("c", {accelKey: 1}); 1.210 + ok(oncopy_fired, "copy event firing on plaintext editor"); 1.211 + is(getClipboardText(), "PUT TE", 1.212 + "copy on plaintext editor set clipboard correctly"); 1.213 + } finally { 1.214 + contentInput.oncopy = null; 1.215 + } 1.216 +} 1.217 + 1.218 + 1.219 +function test_input_oncut() { 1.220 + // Setup an oncut event handler, and fire cut. Ensure that the event 1.221 + // handler was fired, the clipboard contains the INPUT TEXT, and 1.222 + // that the input itself is empty. 1.223 + selectContentInput(); 1.224 + var oncut_fired = false; 1.225 + contentInput.oncut = function() { oncut_fired = true; }; 1.226 + try { 1.227 + synthesizeKey("x", {accelKey: 1}); 1.228 + ok(oncut_fired, "cut event firing on plaintext editor"); 1.229 + is(getClipboardText(), "INPUT TEXT", 1.230 + "cut on plaintext editor set clipboard correctly"); 1.231 + is(contentInput.value, "", 1.232 + "cut on plaintext editor emptied editor"); 1.233 + } finally { 1.234 + contentInput.oncut = null; 1.235 + } 1.236 +} 1.237 + 1.238 + 1.239 +function test_input_onpaste() { 1.240 + // Setup an onpaste event handler, and fire paste. Ensure that the event 1.241 + // handler was fired, the clipboard contents didn't change, and that the 1.242 + // input value did change (ie. paste succeeded). 1.243 + selectContentInput(); 1.244 + var onpaste_fired = false; 1.245 + contentInput.onpaste = function() { onpaste_fired = true; }; 1.246 + try { 1.247 + synthesizeKey("v", {accelKey: 1}); 1.248 + ok(onpaste_fired, "paste event firing on plaintext editor"); 1.249 + is(getClipboardText(), clipboardInitialValue, 1.250 + "paste on plaintext editor did not modify clipboard contents"); 1.251 + is(contentInput.value, clipboardInitialValue, 1.252 + "paste on plaintext editor did modify editor value"); 1.253 + } finally { 1.254 + contentInput.onpaste = null; 1.255 + } 1.256 +} 1.257 + 1.258 + 1.259 +function test_input_oncopy_abort() { 1.260 + // Setup an oncopy event handler, fire copy. Ensure that the event 1.261 + // handler was called, and that the clipboard value did NOT change. 1.262 + selectContentInput(); 1.263 + var oncopy_fired = false; 1.264 + contentInput.oncopy = function() { oncopy_fired = true; return false; }; 1.265 + try { 1.266 + synthesizeKey("c", {accelKey: 1}); 1.267 + ok(oncopy_fired, "copy event (to-be-cancelled) firing on plaintext editor"); 1.268 + is(getClipboardText(), clipboardInitialValue, 1.269 + "aborted copy on plaintext editor did not modify clipboard"); 1.270 + } finally { 1.271 + contentInput.oncopy = null; 1.272 + } 1.273 +} 1.274 + 1.275 + 1.276 +function test_input_oncut_abort() { 1.277 + // Setup an oncut event handler, and fire cut. Ensure that the event 1.278 + // handler was fired, the clipboard contains the INPUT TEXT, and 1.279 + // that the input itself is empty. 1.280 + selectContentInput(); 1.281 + var oncut_fired = false; 1.282 + contentInput.oncut = function() { oncut_fired = true; return false; }; 1.283 + try { 1.284 + synthesizeKey("x", {accelKey: 1}); 1.285 + ok(oncut_fired, "cut event (to-be-cancelled) firing on plaintext editor"); 1.286 + is(getClipboardText(), clipboardInitialValue, 1.287 + "aborted cut on plaintext editor did not modify clipboard."); 1.288 + is(contentInput.value, "INPUT TEXT", 1.289 + "aborted cut on plaintext editor did not modify editor contents"); 1.290 + } finally { 1.291 + contentInput.oncut = null; 1.292 + } 1.293 +} 1.294 + 1.295 + 1.296 +function test_input_onpaste_abort() { 1.297 + // Setup an onpaste event handler, and fire paste. Ensure that the event 1.298 + // handler was fired, the clipboard contents didn't change, and that the 1.299 + // input value did change (ie. paste succeeded). 1.300 + selectContentInput(); 1.301 + var onpaste_fired = false; 1.302 + contentInput.onpaste = function() { onpaste_fired = true; return false; }; 1.303 + try { 1.304 + synthesizeKey("v", {accelKey: 1}); 1.305 + ok(onpaste_fired, 1.306 + "paste event (to-be-cancelled) firing on plaintext editor"); 1.307 + is(getClipboardText(), clipboardInitialValue, 1.308 + "aborted paste on plaintext editor did not modify clipboard"); 1.309 + is(contentInput.value, "INPUT TEXT", 1.310 + "aborted paste on plaintext editor did not modify modified editor value"); 1.311 + } finally { 1.312 + contentInput.onpaste = null; 1.313 + } 1.314 +} 1.315 + 1.316 + 1.317 +function test_input_cut_dataTransfer() { 1.318 + // Cut using event.dataTransfer. The event is not cancelled so the default 1.319 + // cut should occur 1.320 + selectContentInput(); 1.321 + contentInput.oncut = function(event) { 1.322 + ok(event instanceof ClipboardEvent, "cut event is a ClipboardEvent"); 1.323 + ok(event.clipboardData instanceof DataTransfer, "cut event dataTransfer is a DataTransfer"); 1.324 + is(event.target, contentInput, "cut event target"); 1.325 + is(event.clipboardData.mozItemCount, 0, "cut event mozItemCount"); 1.326 + is(event.clipboardData.getData("text/plain"), "", "cut event getData"); 1.327 + event.clipboardData.setData("text/plain", "This is some dataTransfer text"); 1.328 + cachedCutData = event.clipboardData; 1.329 + }; 1.330 + try { 1.331 + synthesizeKey("x", {accelKey: 1}); 1.332 + is(getClipboardText(), "INPUT TEXT", 1.333 + "cut using dataTransfer on plaintext editor set clipboard correctly"); 1.334 + is(contentInput.value, "", 1.335 + "cut using dataTransfer on plaintext editor cleared input"); 1.336 + } finally { 1.337 + contentInput.oncut = null; 1.338 + } 1.339 +} 1.340 + 1.341 + 1.342 +function test_input_cut_abort_dataTransfer() { 1.343 + // Cut using event.dataTransfer but cancel the event. The data should be 1.344 + // put on the clipboard but since we don't modify the input value, the input 1.345 + // should have the same value. 1.346 + selectContentInput(); 1.347 + contentInput.oncut = function(event) { 1.348 + event.clipboardData.setData("text/plain", "Cut dataTransfer text"); 1.349 + return false; 1.350 + }; 1.351 + try { 1.352 + synthesizeKey("x", {accelKey: 1}); 1.353 + is(getClipboardText(), "Cut dataTransfer text", 1.354 + "aborted cut using dataTransfer on plaintext editor set clipboard correctly"); 1.355 + is(contentInput.value, "INPUT TEXT", 1.356 + "aborted cut using dataTransfer on plaintext editor did not modify input"); 1.357 + } finally { 1.358 + contentInput.oncut = null; 1.359 + } 1.360 +} 1.361 + 1.362 + 1.363 +function test_input_copy_dataTransfer() { 1.364 + // Copy using event.dataTransfer 1.365 + selectContentInput(); 1.366 + contentInput.oncopy = function(event) { 1.367 + ok(event instanceof ClipboardEvent, "copy event is a ClipboardEvent"); 1.368 + ok(event.clipboardData instanceof DataTransfer, "copy event dataTransfer is a DataTransfer"); 1.369 + is(event.target, contentInput, "copy event target"); 1.370 + is(event.clipboardData.mozItemCount, 0, "copy event mozItemCount"); 1.371 + is(event.clipboardData.getData("text/plain"), "", "copy event getData"); 1.372 + event.clipboardData.setData("text/plain", "Copied dataTransfer text"); 1.373 + cachedCopyData = event.clipboardData; 1.374 + }; 1.375 + try { 1.376 + synthesizeKey("c", {accelKey: 1}); 1.377 + is(getClipboardText(), "INPUT TEXT", 1.378 + "copy using dataTransfer on plaintext editor set clipboard correctly"); 1.379 + is(contentInput.value, "INPUT TEXT", 1.380 + "copy using dataTransfer on plaintext editor did not modify input"); 1.381 + } finally { 1.382 + contentInput.oncopy = null; 1.383 + } 1.384 +} 1.385 + 1.386 + 1.387 +function test_input_copy_abort_dataTransfer() { 1.388 + // Copy using event.dataTransfer but cancel the event. 1.389 + selectContentInput(); 1.390 + contentInput.oncopy = function(event) { 1.391 + event.clipboardData.setData("text/plain", "Copy dataTransfer text"); 1.392 + return false; 1.393 + }; 1.394 + try { 1.395 + synthesizeKey("x", {accelKey: 1}); 1.396 + is(getClipboardText(), "Copy dataTransfer text", 1.397 + "aborted copy using dataTransfer on plaintext editor set clipboard correctly"); 1.398 + is(contentInput.value, "INPUT TEXT", 1.399 + "aborted copy using dataTransfer on plaintext editor did not modify input"); 1.400 + } finally { 1.401 + contentInput.oncopy = null; 1.402 + } 1.403 +} 1.404 + 1.405 + 1.406 +function test_input_paste_dataTransfer() { 1.407 + // Paste using event.dataTransfer 1.408 + selectContentInput(); 1.409 + contentInput.onpaste = function(event) { 1.410 + ok(event instanceof ClipboardEvent, "paste event is an ClipboardEvent"); 1.411 + ok(event.clipboardData instanceof DataTransfer, "paste event dataTransfer is a DataTransfer"); 1.412 + is(event.target, contentInput, "paste event target"); 1.413 + is(event.clipboardData.mozItemCount, 1, "paste event mozItemCount"); 1.414 + is(event.clipboardData.getData("text/plain"), clipboardInitialValue, "paste event getData"); 1.415 + cachedPasteData = event.clipboardData; 1.416 + }; 1.417 + try { 1.418 + synthesizeKey("v", {accelKey: 1}); 1.419 + is(getClipboardText(), clipboardInitialValue, 1.420 + "paste using dataTransfer on plaintext editor did not modify clipboard contents"); 1.421 + is(contentInput.value, clipboardInitialValue, 1.422 + "paste using dataTransfer on plaintext editor modified input"); 1.423 + } finally { 1.424 + contentInput.onpaste = null; 1.425 + } 1.426 +} 1.427 + 1.428 + 1.429 +function test_input_paste_abort_dataTransfer() { 1.430 + // Paste using event.dataTransfer but cancel the event 1.431 + selectContentInput(); 1.432 + contentInput.onpaste = function(event) { 1.433 + is(event.clipboardData.getData("text/plain"), clipboardInitialValue, "get data on aborted paste"); 1.434 + contentInput.value = "Alternate Paste"; 1.435 + return false; 1.436 + }; 1.437 + try { 1.438 + synthesizeKey("v", {accelKey: 1}); 1.439 + is(getClipboardText(), clipboardInitialValue, 1.440 + "aborted paste using dataTransfer on plaintext editor did not modify clipboard contents"); 1.441 + is(contentInput.value, "Alternate Paste", 1.442 + "aborted paste using dataTransfer on plaintext editor modified input"); 1.443 + } finally { 1.444 + contentInput.onpaste = null; 1.445 + } 1.446 +} 1.447 + 1.448 +function test_input_copypaste_dataTransfer_multiple() { 1.449 + // Cut several types of data and paste it again 1.450 + contentInput.value = "This is a line of text"; 1.451 + contentInput.oncopy = function(event) { 1.452 + var cd = event.clipboardData; 1.453 + cd.setData("text/plain", "would be a phrase"); 1.454 + 1.455 + var exh = false; 1.456 + try { cd.mozSetDataAt("text/plain", "Text", 1); } catch (ex) { exh = true; } 1.457 + ok(exh, "exception occured mozSetDataAt 1"); 1.458 + exh = false; 1.459 + try { cd.mozTypesAt(1); } catch (ex) { exh = true; } 1.460 + ok(exh, "exception occured mozTypesAt 1"); 1.461 + exh = false; 1.462 + try { cd.mozGetDataAt("text/plain", 1); } catch (ex) { exh = true; } 1.463 + ok(exh, "exception occured mozGetDataAt 1"); 1.464 + exh = false; 1.465 + try { cd.mozClearDataAt("text/plain", 1); } catch (ex) { exh = true; } 1.466 + ok(exh, "exception occured mozClearDataAt 1"); 1.467 + 1.468 + cd.setData("text/x-moz-url", "http://www.mozilla.org"); 1.469 + cd.mozSetDataAt("text/x-custom", "Custom Text", 0); 1.470 + is(cd.mozItemCount, 1, "mozItemCount after set multiple types"); 1.471 + return false; 1.472 + }; 1.473 + 1.474 + try { 1.475 + selectContentInput(); 1.476 + synthesizeKey("c", {accelKey: 1}); 1.477 + } 1.478 + finally { 1.479 + contentInput.oncopy = null; 1.480 + } 1.481 + 1.482 + is(getClipboardText(), "would be a phrase", "copy multiple types text"); 1.483 + 1.484 + contentInput.setSelectionRange(5, 14); 1.485 + 1.486 + contentInput.onpaste = function(event) { 1.487 + var cd = event.clipboardData; 1.488 + is(cd.mozItemCount, 1, "paste after copy multiple types mozItemCount"); 1.489 + is(cd.getData("text/plain"), "would be a phrase", "paste text/plain multiple types"); 1.490 + 1.491 + // Firefox for Android's clipboard code doesn't handle x-moz-url. Therefore 1.492 + // disabling the following test. Enable this once bug #840101 is fixed. 1.493 + if (navigator.appVersion.indexOf("Android") == -1) { 1.494 + is(cd.getData("text/x-moz-url"), "http://www.mozilla.org", "paste text/x-moz-url multiple types"); 1.495 + } 1.496 + // this is empty because only the built-in types are supported at the moment 1.497 + is(cd.getData("text/x-custom"), "", "paste text/custom multiple types"); 1.498 + 1.499 + exh = false; 1.500 + try { cd.setData("text/plain", "Text on Paste"); } catch (ex) { exh = true; } 1.501 + ok(exh, "exception occured setData on paste"); 1.502 + 1.503 + is(cd.getData("text/plain"), "would be a phrase", "text/plain data unchanged"); 1.504 + }; 1.505 + try { 1.506 + synthesizeKey("v", {accelKey: 1}); 1.507 + is(contentInput.value, "This would be a phrase of text", 1.508 + "default paste after copy multiple types"); 1.509 + } finally { 1.510 + contentInput.onpaste = null; 1.511 + } 1.512 +} 1.513 + 1.514 +function test_input_copy_button_dataTransfer() { 1.515 + // Copy using event.dataTransfer when a button is focused. 1.516 + var button = document.getElementById("button"); 1.517 + button.focus(); 1.518 + button.oncopy = function(event) { 1.519 + ok(false, "should not be firing copy event on button"); 1.520 + return false; 1.521 + }; 1.522 + try { 1.523 + // copy should not occur here because buttons don't have any controller 1.524 + // for the copy command 1.525 + synthesizeKey("c", {accelKey: 1}); 1.526 + is(getClipboardText(), clipboardInitialValue, 1.527 + "copy using dataTransfer on plaintext editor set clipboard correctly for button"); 1.528 + 1.529 + selectContentDiv(); 1.530 + synthesizeKey("c", {accelKey: 1}); 1.531 + is(getClipboardText(), "CONTENT TEXT", 1.532 + "copy using dataTransfer with selection on plaintext editor set clipboard correctly for button"); 1.533 + 1.534 + } finally { 1.535 + document.documentElement.oncopy = null; 1.536 + } 1.537 +} 1.538 + 1.539 +function test_eventspref_disabled() { 1.540 + // Disable clipboard events 1.541 + SpecialPowers.setBoolPref("dom.event.clipboardevents.enabled", false); 1.542 + 1.543 + var event_fired = false; 1.544 + contentInput.oncut = function() { event_fired = true; }; 1.545 + contentInput.oncopy = function() { event_fired = true; }; 1.546 + contentInput.onpaste = function() { event_fired = true; }; 1.547 + try { 1.548 + selectContentInput(); 1.549 + contentInput.setSelectionRange(1, 4); 1.550 + synthesizeKey("x", {accelKey: 1}); 1.551 + is(contentInput.value, "IT TEXT", "cut changed text when preference is disabled"); 1.552 + is(getClipboardText(), "NPU", "cut changed clipboard when preference is disabled"); 1.553 + ok(!event_fired, "cut event did not fire when preference is disabled") 1.554 + 1.555 + event_fired = false; 1.556 + contentInput.setSelectionRange(3, 6); 1.557 + synthesizeKey("c", {accelKey: 1}); 1.558 + is(getClipboardText(), "TEX", "copy changed clipboard when preference is disabled"); 1.559 + ok(!event_fired, "copy event did not fire when preference is disabled") 1.560 + 1.561 + event_fired = false; 1.562 + contentInput.setSelectionRange(0, 2); 1.563 + synthesizeKey("v", {accelKey: 1}); 1.564 + is(contentInput.value, "TEX TEXT", "paste changed text when preference is disabled"); 1.565 + ok(!event_fired, "paste event did not fire when preference is disabled") 1.566 + } finally { 1.567 + contentInput.oncut = null; 1.568 + contentInput.oncopy = null; 1.569 + contentInput.onpaste = null; 1.570 + } 1.571 + 1.572 + SpecialPowers.clearUserPref("dom.event.clipboardevents.enabled"); 1.573 +} 1.574 + 1.575 +let expectedData = []; 1.576 + 1.577 +// Check to make that synthetic events do not change the clipboard 1.578 +function checkSyntheticEvents() 1.579 +{ 1.580 + let syntheticSpot = document.getElementById("syntheticSpot"); 1.581 + setClipboardText(clipboardInitialValue); 1.582 + 1.583 + // No dataType specified 1.584 + let event = new ClipboardEvent("cut", { data: "something" }); 1.585 + expectedData = { type: "cut", data: null } 1.586 + compareSynthetic(event, "before"); 1.587 + syntheticSpot.dispatchEvent(event); 1.588 + ok(expectedData.eventFired, "cut event fired"); 1.589 + compareSynthetic(event, "after"); 1.590 + 1.591 + event = new ClipboardEvent("cut", { dataType: "text/plain", data: "something" }); 1.592 + expectedData = { type: "cut", dataType: "text/plain", data: "something" } 1.593 + compareSynthetic(event, "before"); 1.594 + syntheticSpot.dispatchEvent(event); 1.595 + ok(expectedData.eventFired, "cut event fired"); 1.596 + compareSynthetic(event, "after"); 1.597 + 1.598 + event = new ClipboardEvent("copy", { dataType: "text/plain", data: "something" }); 1.599 + expectedData = { type: "copy", dataType: "text/plain", data: "something" } 1.600 + compareSynthetic(event, "before"); 1.601 + syntheticSpot.dispatchEvent(event); 1.602 + ok(expectedData.eventFired, "copy event fired"); 1.603 + compareSynthetic(event, "after"); 1.604 + 1.605 + event = new ClipboardEvent("copy", { dataType: "text/plain" }); 1.606 + expectedData = { type: "copy", dataType: "text/plain", data: "" } 1.607 + compareSynthetic(event, "before"); 1.608 + syntheticSpot.dispatchEvent(event); 1.609 + ok(expectedData.eventFired, "copy event fired"); 1.610 + compareSynthetic(event, "after"); 1.611 + 1.612 + event = new ClipboardEvent("paste", { dataType: "text/plain", data: "something" }); 1.613 + expectedData = { type: "paste", dataType: "text/plain", data: "something" } 1.614 + compareSynthetic(event, "before"); 1.615 + syntheticSpot.dispatchEvent(event); 1.616 + ok(expectedData.eventFired, "paste event fired"); 1.617 + compareSynthetic(event, "after"); 1.618 + 1.619 + event = new ClipboardEvent("paste", { dataType: "application/unknown", data: "unknown" }); 1.620 + expectedData = { type: "paste", dataType: "application/unknown", data: "unknown" } 1.621 + compareSynthetic(event, "before"); 1.622 + syntheticSpot.dispatchEvent(event); 1.623 + ok(expectedData.eventFired, "paste event fired"); 1.624 + compareSynthetic(event, "after"); 1.625 +} 1.626 + 1.627 +function compareSynthetic(event, eventtype) 1.628 +{ 1.629 + let step = (eventtype == "cut" || eventtype == "copy" || eventtype == "paste") ? "during" : eventtype; 1.630 + if (step == "during") { 1.631 + is(eventtype, expectedData.type, "synthetic " + eventtype + " event fired"); 1.632 + } 1.633 + 1.634 + ok(event.clipboardData instanceof DataTransfer, "clipboardData is assigned"); 1.635 + 1.636 + is(event.type, expectedData.type, "synthetic " + eventtype + " event type"); 1.637 + if (expectedData.data === null) { 1.638 + is(event.clipboardData.mozItemCount, 0, "synthetic " + eventtype + " empty data"); 1.639 + } 1.640 + else { 1.641 + is(event.clipboardData.mozItemCount, 1, "synthetic " + eventtype + " item count"); 1.642 + is(event.clipboardData.types.length, 1, "synthetic " + eventtype + " types length"); 1.643 + is(event.clipboardData.getData(expectedData.dataType), expectedData.data, 1.644 + "synthetic " + eventtype + " data"); 1.645 + } 1.646 + 1.647 + is(getClipboardText(), "empty", "event does not change the clipboard " + step + " dispatch"); 1.648 + 1.649 + if (step == "during") { 1.650 + expectedData.eventFired = true; 1.651 + } 1.652 +} 1.653 + 1.654 +function checkCachedDataTransfer(cd, eventtype) 1.655 +{ 1.656 + var testprefix = "cached " + eventtype + " dataTransfer"; 1.657 + 1.658 + setClipboardText("Some Clipboard Text"); 1.659 + 1.660 + var oldtext = cd.getData("text/plain"); 1.661 + ok(oldtext != "Some Clipboard Text", "clipboard get using " + testprefix); 1.662 + 1.663 + var exh = false; 1.664 + try { cd.mozSetDataAt("text/plain", "Test Cache Data", 0); } catch (ex) { exh = true; } 1.665 + ok(eventtype == "paste" ? exh : !exh, "exception occured setting " + testprefix); 1.666 + 1.667 + var newtext = (eventtype == "paste") ? cd.getData("text/plain") : 1.668 + cd.mozGetDataAt("text/plain", 0); 1.669 + is(newtext, (eventtype == "paste") ? "" : "Test Cache Data", 1.670 + " clipboardData not changed using " + testprefix); 1.671 + 1.672 + is(getClipboardText(), "Some Clipboard Text", "clipboard not changed using " + testprefix); 1.673 + 1.674 + var exh = false; 1.675 + try { cd.mozClearDataAt("text/plain", 0); } catch (ex) { exh = true; } 1.676 + ok(eventtype == "paste" ? exh : !exh, "exception occured clearing " + testprefix); 1.677 + 1.678 + is(getClipboardText(), "Some Clipboard Text", "clipboard not changed using " + testprefix); 1.679 +} 1.680 + 1.681 +</script> 1.682 +</pre> 1.683 +</body> 1.684 +</html> 1.685 +