dom/tests/mochitest/general/test_paste_selection.html

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

mercurial