4 <script src=
"../../resources/js-test.js"></script>
7 border:
1px solid black;
12 border:
1px solid black;
18 function legacyDragStart(dataTransfer
)
20 dataTransfer
.setData('text', 'sample');
21 dataTransfer
.setData('url', 'http://www.google.com/');
22 dataTransfer
.setData('text/html', '<em>Markup</em>');
23 dataTransfer
.setData('custom-data', 'hello world');
28 function itemListDragStart(dataTransfer
)
30 testDataTransfer
= dataTransfer
;
31 shouldThrow('testDataTransfer.items.add()');
32 shouldThrow('testDataTransfer.items.add("sample")');
33 testItem
= dataTransfer
.items
.add('sample', 'text/plain');
34 shouldBeEqualToString('testItem.kind', 'string');
35 shouldBeEqualToString('testItem.type', 'text/plain');
36 testItem
= dataTransfer
.items
.add('http://www.google.com/', 'text/uri-list');
37 shouldBeEqualToString('testItem.kind', 'string');
38 shouldBeEqualToString('testItem.type', 'text/uri-list');
39 testItem
= dataTransfer
.items
.add('<em>Markup</em>', 'text/html');
40 shouldBeEqualToString('testItem.kind', 'string');
41 shouldBeEqualToString('testItem.type', 'text/html');
42 testItem
= dataTransfer
.items
.add('hello world', 'custom-data');
43 shouldBeEqualToString('testItem.kind', 'string');
44 shouldBeEqualToString('testItem.type', 'custom-data');
47 function dragstart(event
)
49 var dragMethod
= document
.getElementById('dragMethod');
50 if (dragMethod
.selectedIndex
== 0)
51 legacyDragStart(event
.dataTransfer
);
52 else if (dragMethod
.selectedIndex
== 1)
53 itemListDragStart(event
.dataTransfer
);
56 function dragenter(event
)
58 event
.preventDefault();
61 function dragover(event
)
63 event
.preventDefault();
66 function legacyDrop(dataTransfer
)
68 testDataTransfer
= dataTransfer
;
69 shouldBe('testDataTransfer.types.length', '4');
70 shouldBeTrue('testDataTransfer.types.indexOf("text/plain") >= 0');
71 shouldBeTrue('testDataTransfer.types.indexOf("text/uri-list") >= 0');
72 shouldBeTrue('testDataTransfer.types.indexOf("text/html") >= 0');
73 shouldBeTrue('testDataTransfer.types.indexOf("custom-data") >= 0');
74 shouldBeEqualToString('testDataTransfer.getData("text")', 'sample');
75 shouldBeEqualToString('testDataTransfer.getData("url")', 'http://www.google.com/');
76 shouldBeEqualToString('testDataTransfer.getData("text/html")', '<em>Markup</em>');
77 shouldBeEqualToString('testDataTransfer.getData("custom-data")', 'hello world');
78 setTimeout(runNext
, 0);
81 var outstandingRequests
;
82 var types
, expectedTypes
;
83 var testData
, expectedTestData
;
84 function itemListDrop(dataTransfer
)
86 testDataTransfer
= dataTransfer
;
87 outstandingRequests
= 0;
88 shouldBe('testDataTransfer.items.length', '4');
89 shouldBeNull('testDataTransfer.items.add("test", "text/plain")'); // Read-only.
90 shouldBe('testDataTransfer.items.length', '4');
92 for (var i
= 0; i
< dataTransfer
.items
.length
; ++i
) {
93 types
.push({kind
: dataTransfer
.items
[i
].kind
, type
: dataTransfer
.items
[i
].type
});
95 types
.sort(function (a
, b
) { return a
.type
.localeCompare(b
.type
); });
97 { kind
: 'string', type
: 'custom-data'},
98 { kind
: 'string', type
: 'text/html'},
99 { kind
: 'string', type
: 'text/plain'},
100 { kind
: 'string', type
: 'text/uri-list'},
102 shouldBe('JSON.stringify(expectedTypes)', 'JSON.stringify(types)');
103 var expectedResults
= {
104 'custom-data': 'hello world',
105 'text/html': '<em>Markup</em>',
106 'text/plain': 'sample',
107 'text/uri-list': 'http://www.google.com/',
109 function makeClosure(expectedData
)
111 ++outstandingRequests
;
112 return function (data
) {
113 expectedTestData
= expectedData
;
115 shouldBe('testData', 'expectedTestData');
116 if (--outstandingRequests
== 0)
117 setTimeout(runNext
, 0);
120 // We use this funky loop to make sure we always print out results in the same order.
121 for (var i
= 0; i
< types
.length
; ++i
) {
122 for (var j
= 0; j
< dataTransfer
.items
.length
; ++j
) {
123 if (types
[i
].type
== dataTransfer
.items
[j
].type
) {
124 dataTransfer
.items
[j
].getAsString(makeClosure(expectedResults
[types
[i
].type
]));
133 var dropMethod
= document
.getElementById('dropMethod');
134 if (dropMethod
.selectedIndex
== 0)
135 legacyDrop(event
.dataTransfer
);
136 else if (dropMethod
.selectedIndex
== 1)
137 itemListDrop(event
.dataTransfer
);
138 event
.preventDefault();
141 function runTest(dragMethodIndex
, dropMethodIndex
)
143 var dragMethod
= document
.getElementById('dragMethod');
144 var dropMethod
= document
.getElementById('dropMethod');
145 dragMethod
.selectedIndex
= dragMethodIndex
;
146 dropMethod
.selectedIndex
= dropMethodIndex
;
147 debug('Running test with ' + dragMethod
.value
+ ' drag handler and ' + dropMethod
.value
+ ' drop handler');
149 var dragElement
= document
.getElementById('drag');
150 eventSender
.mouseMoveTo(dragElement
.offsetLeft
+ dragElement
.offsetWidth
/ 2,
151 dragElement
.offsetTop
+ dragElement
.offsetHeight
/ 2);
152 eventSender
.mouseDown();
153 eventSender
.leapForward(100);
154 var dropElement
= document
.getElementById('drop');
155 eventSender
.mouseMoveTo(dropElement
.offsetLeft
+ dropElement
.offsetWidth
/ 2,
156 dropElement
.offsetTop
+ dropElement
.offsetHeight
/ 2);
157 eventSender
.mouseUp();
168 if (!window
.testRunner
)
170 var testCase
= testCases
.shift();
172 runTest
.apply(null, testCase
);
179 <p>To manually test, drag 'Drag Me' to 'Drop Here' and drop. Several lines that say 'PASS' should appear below.
180 <div draggable=
"true" id=
"drag" ondragstart=
"dragstart(event)">Drag Me
</div>
181 <div id=
"drop" ondragenter=
"dragenter(event)" ondragover=
"dragover(event)" ondrop=
"drop(event)">Drop Here
</div>
183 <div>Drag handler:
<select id=
"dragMethod"><option>Legacy
</option><option>DataTransferItemList
</option></select></div>
184 <div>Drop handler:
<select id=
"dropMethod"><option>Legacy
</option><option>DataTransferItemList
</option></select></div>
185 <div id=
"console"></div>
187 description("Tests drag'n drop and well as DataTransferItemList");
188 window
.jsTestIsAsync
= true;