Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / editing / pasteboard / data-transfer-items.html
blob5538c8855015aa322a6705c0dfffb1fde56b4bb1
1 <!DOCTYPE html>
2 <html>
3 <body oncopy="copy(event)" onpaste="paste(event)">
4 <div>This file tests the basic functionality and properties of DataTransferItems. This test requires DRT.</div>
5 <div id="console"></div>
7 <script src="../editing.js"></script>
8 <script>
9 var undefined;
10 var pendingCallbacks = 0;
11 var savedDataTransferItems = null;
12 var savedDataTransferItem = null;
14 function log(text)
16 var console = document.getElementById('console');
17 console.appendChild(document.createTextNode(text));
18 console.appendChild(document.createElement('br'));
21 function removeFontName(text)
23 if (!text)
24 return text;
25 return text.replace(/font-family: [^;]+;/g, '');
28 function handleEvent(data)
30 log(data);
31 if (--pendingCallbacks == 0) {
32 window.testRunner.notifyDone();
36 function copy(ev)
38 var items = event.clipboardData.items;
39 log('Populating DataTransferItems...');
40 items.add('Hello World!', 'text/plain');
41 items.add('<b>Hello World!', 'text/html');
43 // Check that an exception is properly raised when attempting to add a duplicate string type.
44 try {
45 items.add('Moo', 'text/plain');
46 } catch (e) {
47 log('Caught exception "' + e + '" as expected.');
50 // Check that the container didn't change.
51 log('Verifying contents of DataTransferItems...');
52 log('items.length: ' + items.length);
53 log('items[0].kind: ' + items[0].kind);
54 log('items[0].type: ' + items[0].type);
55 log('items[1].kind: ' + items[1].kind);
56 log('items[1].type: ' + items[1].type);
57 items[0].getAsString(function (data) { handleEvent('copy: items[0] value: ' + data); });
58 pendingCallbacks++;
59 items[1].getAsString(function (data) { handleEvent('copy: items[1] value: ' + data); });
60 pendingCallbacks++;
62 log('Checking if items past the end of the collection can be indexed:');
63 log('items[2] is undefined: ' + typeof items[2]);
66 function paste(ev)
68 var items = event.clipboardData.items;
70 // Cache references to make sure they aren't accessible outside the event handler.
71 savedDataTransferItems = items;
72 savedDataTransferItem = items[0];
74 var originalLength = items.length;
75 log('Checking that a read-only DataTransferItems cannot be mutated...');
76 // Should be immutable.
77 items.add('Hello World!', 'text/plain');
78 log('items.length: ' + items.length);
79 log('items[0].kind: ' + items[0].kind);
80 log('items[0].type: ' + items[0].type);
81 items[0].getAsString(function (data) { handleEvent('paste: items[0] value: ' + removeFontName(data)); });
82 pendingCallbacks++;
83 log('items[1].kind: ' + items[1].kind);
84 log('items[1].type: ' + items[1].type);
85 items[1].getAsString(function (data) { handleEvent('paste: items[1] value: ' + removeFontName(data)); });
86 pendingCallbacks++;
87 log('items[2] is undefined: ' + typeof items[2]);
90 function runTest() {
91 if (!window.testRunner)
92 return;
93 testRunner.waitUntilDone();
94 testRunner.dumpAsText();
96 eventSender.mouseMoveTo(0, 0);
97 for (var i = 0; i < 3; i++) {
98 eventSender.mouseDown();
99 eventSender.mouseUp();
101 copyCommand();
102 pasteCommand();
104 log('Testing if DataTransferItems can be accessed outside an event handler...');
105 if (savedDataTransferItems.length != 0) {
106 log('DataTransferItems.length non-zero outside event handler!');
108 savedDataTransferItems.add('Security?', 'text/foo');
109 if (savedDataTransferItems.length != 0) {
110 log('DataTransferItems mutated outside event handler!');
112 if (typeof savedDataTransferItems[0] == 'undefined') {
113 log('DataTransferItem accessed outside event handler!');
115 if (typeof savedDataTransferItem == 'undefined') {
116 log('DataTransferItem accessed outside event handler!');
120 runTest();
122 </script>
123 </body>
124 </html>