4 <script src=
"../../resources/js-test.js"></script>
7 var nonConvertibleToString
= {toString: function() { throw "Exception in toString()"; }};
8 function legacyCopyStart(dataTransfer
)
10 testDataTransfer
= dataTransfer
;
11 dataTransfer
.setData('text', 'sample');
12 dataTransfer
.setData('url', 'http://www.google.com/');
13 dataTransfer
.setData('text/html', '<em>Markup</em>');
14 dataTransfer
.setData('custom-data', 'hello world');
15 shouldBeEqualToString('testDataTransfer.getData("custom-data")', 'hello world');
16 shouldThrow('testDataTransfer.clearData(nonConvertibleToString)', '"Exception in toString()"');
17 shouldBeEqualToString('testDataTransfer.getData("custom-data")', 'hello world');
20 function itemListCopyStart(dataTransfer
)
22 testDataTransfer
= dataTransfer
;
23 shouldBeUndefined('testDataTransfer.items.item');
24 dataTransfer
.items
.add('sample', 'text/plain');
25 dataTransfer
.items
.add('http://www.google.com/', 'text/uri-list');
26 dataTransfer
.items
.add('<em>Markup</em>', 'text/html');
27 dataTransfer
.items
.add('hello world', 'custom-data');
32 event
.preventDefault();
33 var copyMethod
= document
.getElementById('copyMethod');
34 if (copyMethod
.selectedIndex
== 0)
35 legacyCopyStart(event
.clipboardData
);
36 else if (copyMethod
.selectedIndex
== 1)
37 itemListCopyStart(event
.clipboardData
);
40 function legacyPaste(dataTransfer
)
42 testDataTransfer
= dataTransfer
;
43 shouldBe('testDataTransfer.types.length', '4');
44 shouldBeTrue('testDataTransfer.types.indexOf("text/plain") >= 0');
45 shouldBeTrue('testDataTransfer.types.indexOf("text/uri-list") >= 0');
46 shouldBeTrue('testDataTransfer.types.indexOf("text/html") >= 0');
47 shouldBeTrue('testDataTransfer.types.indexOf("custom-data") >= 0');
48 shouldBeEqualToString('testDataTransfer.getData("text")', 'sample');
49 shouldBeEqualToString('testDataTransfer.getData("url")', 'http://www.google.com/');
50 shouldBeEqualToString('testDataTransfer.getData("text/html")', '<em>Markup</em>');
51 shouldBeEqualToString('testDataTransfer.getData("custom-data")', 'hello world');
52 setTimeout(runNext
, 0);
55 var testData
, expectedTestData
;
56 var types
, expectedTypes
;
57 var outstandingRequests
;
58 function itemListPaste(dataTransfer
)
60 testDataTransfer
= dataTransfer
;
61 outstandingRequests
= 0;
62 shouldBe('testDataTransfer.items.length', '4');
64 for (var i
= 0; i
< dataTransfer
.items
.length
; ++i
) {
65 types
.push({kind
: dataTransfer
.items
[i
].kind
, type
: dataTransfer
.items
[i
].type
});
67 types
.sort(function (a
, b
) { return a
.type
.localeCompare(b
.type
); });
69 { kind
: 'string', type
: 'custom-data'},
70 { kind
: 'string', type
: 'text/html'},
71 { kind
: 'string', type
: 'text/plain'},
72 { kind
: 'string', type
: 'text/uri-list'},
74 shouldBe('JSON.stringify(expectedTypes)', 'JSON.stringify(types)');
75 var expectedResults
= {
76 'custom-data': 'hello world',
77 'text/html': '<em>Markup</em>',
78 'text/plain': 'sample',
79 'text/uri-list': 'http://www.google.com/',
81 function makeClosure(expectedData
)
83 ++outstandingRequests
;
84 return function (data
) {
85 expectedTestData
= expectedData
;
87 shouldBe('testData', 'expectedTestData');
88 if (--outstandingRequests
== 0)
89 setTimeout(runNext
, 0);
92 // We use this funky loop to make sure we always print out results in the same order.
93 for (var i
= 0; i
< types
.length
; ++i
) {
94 for (var j
= 0; j
< dataTransfer
.items
.length
; ++j
) {
95 if (types
[i
].type
== dataTransfer
.items
[j
].type
) {
96 dataTransfer
.items
[j
].getAsString(makeClosure(expectedResults
[types
[i
].type
]));
103 function paste(event
)
105 var pasteMethod
= document
.getElementById('pasteMethod');
106 if (pasteMethod
.selectedIndex
== 0)
107 legacyPaste(event
.clipboardData
);
108 else if (pasteMethod
.selectedIndex
== 1)
109 itemListPaste(event
.clipboardData
);
112 function runTest(copyMethodIndex
, pasteMethodIndex
)
114 var copyMethod
= document
.getElementById('copyMethod');
115 var pasteMethod
= document
.getElementById('pasteMethod');
116 copyMethod
.selectedIndex
= copyMethodIndex
;
117 pasteMethod
.selectedIndex
= pasteMethodIndex
;
118 debug('Running test with ' + copyMethod
.value
+ ' copy handler and ' + pasteMethod
.value
+ ' paste handler');
120 document
.execCommand('copy');
121 document
.execCommand('paste');
132 if (!window
.testRunner
)
134 var testCase
= testCases
.shift();
136 runTest
.apply(null, testCase
);
143 <body oncopy=
"copy(event)" onpaste=
"paste(event)">
144 <p>To manually test, press your browser shortcut for copy and then for paste. Several lines that say 'PASS' should appear below.
145 <div>Copy handler:
<select id=
"copyMethod"><option>Legacy
</option><option>DataTransferItemList
</option></select></div>
146 <div>Paste handler:
<select id=
"pasteMethod"><option>Legacy
</option><option>DataTransferItemList
</option></select></div>
147 <div id=
"console"></div>
149 description("Tests copy / paste and DataTransferItemList");
151 window
.jsTestIsAsync
= true;