filedrophandler_test.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // Copyright 2009 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.events.FileDropHandlerTest');
  15. goog.setTestOnly('goog.events.FileDropHandlerTest');
  16. goog.require('goog.events');
  17. goog.require('goog.events.BrowserEvent');
  18. goog.require('goog.events.EventTarget');
  19. goog.require('goog.events.EventType');
  20. goog.require('goog.events.FileDropHandler');
  21. goog.require('goog.testing.jsunit');
  22. goog.require('goog.userAgent');
  23. var textarea;
  24. var doc;
  25. var handler;
  26. var dnd;
  27. var files;
  28. function setUp() {
  29. textarea = new goog.events.EventTarget();
  30. doc = new goog.events.EventTarget();
  31. textarea.ownerDocument = doc;
  32. handler = new goog.events.FileDropHandler(textarea);
  33. dnd = false;
  34. files = null;
  35. goog.events.listen(
  36. handler, goog.events.FileDropHandler.EventType.DROP, function(e) {
  37. dnd = true;
  38. files = e.getBrowserEvent().dataTransfer.files;
  39. });
  40. }
  41. function tearDown() {
  42. textarea.dispose();
  43. doc.dispose();
  44. handler.dispose();
  45. }
  46. function testOneFile() {
  47. var preventDefault = false;
  48. var expectedfiles = [{fileName: 'file1.jpg'}];
  49. var dt = {types: ['Files'], files: expectedfiles};
  50. // Assert that default actions are prevented on dragenter.
  51. textarea.dispatchEvent(new goog.events.BrowserEvent({
  52. preventDefault: function() { preventDefault = true; },
  53. type: goog.events.EventType.DRAGENTER,
  54. dataTransfer: dt
  55. }));
  56. assertTrue(preventDefault);
  57. preventDefault = false;
  58. // Assert that default actions are prevented on dragover.
  59. textarea.dispatchEvent(new goog.events.BrowserEvent({
  60. preventDefault: function() { preventDefault = true; },
  61. type: goog.events.EventType.DRAGOVER,
  62. dataTransfer: dt
  63. }));
  64. assertTrue(preventDefault);
  65. preventDefault = false;
  66. // Assert that the drop effect is set to 'copy'.
  67. assertEquals('copy', dt.dropEffect);
  68. // Assert that default actions are prevented on drop.
  69. textarea.dispatchEvent(new goog.events.BrowserEvent({
  70. preventDefault: function() { preventDefault = true; },
  71. type: goog.events.EventType.DROP,
  72. dataTransfer: dt
  73. }));
  74. assertTrue(preventDefault);
  75. // Assert that DROP has been fired.
  76. assertTrue(dnd);
  77. assertEquals(1, files.length);
  78. assertEquals(expectedfiles[0].fileName, files[0].fileName);
  79. }
  80. function testMultipleFiles() {
  81. var preventDefault = false;
  82. var expectedfiles = [{fileName: 'file1.jpg'}, {fileName: 'file2.jpg'}];
  83. var dt = {types: ['Files', 'text'], files: expectedfiles};
  84. // Assert that default actions are prevented on dragenter.
  85. textarea.dispatchEvent(new goog.events.BrowserEvent({
  86. preventDefault: function() { preventDefault = true; },
  87. type: goog.events.EventType.DRAGENTER,
  88. dataTransfer: dt
  89. }));
  90. assertTrue(preventDefault);
  91. preventDefault = false;
  92. // Assert that default actions are prevented on dragover.
  93. textarea.dispatchEvent(new goog.events.BrowserEvent({
  94. preventDefault: function() { preventDefault = true; },
  95. type: goog.events.EventType.DRAGOVER,
  96. dataTransfer: dt
  97. }));
  98. assertTrue(preventDefault);
  99. preventDefault = false;
  100. // Assert that the drop effect is set to 'copy'.
  101. assertEquals('copy', dt.dropEffect);
  102. // Assert that default actions are prevented on drop.
  103. textarea.dispatchEvent(new goog.events.BrowserEvent({
  104. preventDefault: function() { preventDefault = true; },
  105. type: goog.events.EventType.DROP,
  106. dataTransfer: dt
  107. }));
  108. assertTrue(preventDefault);
  109. // Assert that DROP has been fired.
  110. assertTrue(dnd);
  111. assertEquals(2, files.length);
  112. assertEquals(expectedfiles[0].fileName, files[0].fileName);
  113. assertEquals(expectedfiles[1].fileName, files[1].fileName);
  114. }
  115. function testNoFiles() {
  116. var preventDefault = false;
  117. var dt = {types: ['text']};
  118. // Assert that default actions are not prevented on dragenter.
  119. textarea.dispatchEvent(new goog.events.BrowserEvent({
  120. preventDefault: function() { preventDefault = true; },
  121. type: goog.events.EventType.DRAGENTER,
  122. dataTransfer: dt
  123. }));
  124. assertFalse(preventDefault);
  125. preventDefault = false;
  126. // Assert that default actions are not prevented on dragover.
  127. textarea.dispatchEvent(new goog.events.BrowserEvent({
  128. preventDefault: function() { preventDefault = true; },
  129. type: goog.events.EventType.DRAGOVER,
  130. dataTransfer: dt
  131. }));
  132. assertFalse(preventDefault);
  133. preventDefault = false;
  134. // Assert that default actions are not prevented on drop.
  135. textarea.dispatchEvent(new goog.events.BrowserEvent({
  136. preventDefault: function() { preventDefault = true; },
  137. type: goog.events.EventType.DROP,
  138. dataTransfer: dt
  139. }));
  140. assertFalse(preventDefault);
  141. // Assert that DROP has not been fired.
  142. assertFalse(dnd);
  143. assertNull(files);
  144. }
  145. function testDragEnter() {
  146. var preventDefault = false;
  147. // Assert that default actions are prevented on dragenter.
  148. // In Chrome the dragenter event has an empty file list and the types is
  149. // set to 'Files'.
  150. textarea.dispatchEvent(new goog.events.BrowserEvent({
  151. preventDefault: function() { preventDefault = true; },
  152. type: goog.events.EventType.DRAGENTER,
  153. dataTransfer: {types: ['Files'], files: []}
  154. }));
  155. assertTrue(preventDefault);
  156. preventDefault = false;
  157. // Assert that default actions are prevented on dragenter.
  158. // In Safari 4 the dragenter event has an empty file list and the types is
  159. // set to 'public.file-url'.
  160. textarea.dispatchEvent(new goog.events.BrowserEvent({
  161. preventDefault: function() { preventDefault = true; },
  162. type: goog.events.EventType.DRAGENTER,
  163. dataTransfer: {types: ['public.file-url'], files: []}
  164. }));
  165. assertTrue(preventDefault);
  166. preventDefault = false;
  167. // Assert that default actions are not prevented on dragenter
  168. // when the drag contains no files.
  169. textarea.dispatchEvent(new goog.events.BrowserEvent({
  170. preventDefault: function() { preventDefault = true; },
  171. type: goog.events.EventType.DRAGENTER,
  172. dataTransfer: {types: ['text'], files: []}
  173. }));
  174. assertFalse(preventDefault);
  175. }
  176. function testPreventDropOutside() {
  177. var preventDefault = false;
  178. var dt = {types: ['Files'], files: [{fileName: 'file1.jpg'}]};
  179. // Assert that default actions are not prevented on dragenter on the
  180. // document outside the text area.
  181. doc.dispatchEvent(new goog.events.BrowserEvent({
  182. preventDefault: function() { preventDefault = true; },
  183. type: goog.events.EventType.DRAGENTER,
  184. dataTransfer: dt
  185. }));
  186. assertFalse(preventDefault);
  187. preventDefault = false;
  188. // Assert that default actions are not prevented on dragover on the
  189. // document outside the text area.
  190. doc.dispatchEvent(new goog.events.BrowserEvent({
  191. preventDefault: function() { preventDefault = true; },
  192. type: goog.events.EventType.DRAGOVER,
  193. dataTransfer: dt
  194. }));
  195. assertFalse(preventDefault);
  196. preventDefault = false;
  197. handler.dispose();
  198. // Create a new FileDropHandler that prevents drops outside the text area.
  199. handler = new goog.events.FileDropHandler(textarea, true);
  200. // Assert that default actions are now prevented on dragenter on the
  201. // document outside the text area.
  202. doc.dispatchEvent(new goog.events.BrowserEvent({
  203. preventDefault: function() { preventDefault = true; },
  204. type: goog.events.EventType.DRAGENTER,
  205. dataTransfer: dt
  206. }));
  207. assertTrue(preventDefault);
  208. preventDefault = false;
  209. // Assert that default actions are now prevented on dragover on the
  210. // document outside the text area.
  211. doc.dispatchEvent(new goog.events.BrowserEvent({
  212. preventDefault: function() { preventDefault = true; },
  213. type: goog.events.EventType.DRAGOVER,
  214. dataTransfer: dt
  215. }));
  216. assertTrue(preventDefault);
  217. preventDefault = false;
  218. // Assert also that the drop effect is set to 'none'.
  219. assertEquals('none', dt.dropEffect);
  220. }
  221. function testEffectAllowedExceptionIsCaught() {
  222. // This bug was only affecting IE10+.
  223. if (!goog.userAgent.IE || !goog.userAgent.isVersionOrHigher(10)) {
  224. return;
  225. }
  226. var preventDefault = false;
  227. var expectedfiles = [{fileName: 'file1.jpg'}];
  228. var dt = {types: ['Files'], files: expectedfiles};
  229. // We construct a mock DataTransfer object that define a setter will throw
  230. // SCRIPT65535 when attempt to set property effectAllowed to simulate IE Bug
  231. // #811625. See more: https://github.com/google/closure-library/issues/485.
  232. Object.defineProperty(
  233. dt, 'effectAllowed',
  234. {set: function(v) { throw new Error('SCRIPT65535'); }});
  235. // Assert that default actions are prevented on dragenter.
  236. textarea.dispatchEvent(new goog.events.BrowserEvent({
  237. preventDefault: function() { preventDefault = true; },
  238. type: goog.events.EventType.DRAGENTER,
  239. dataTransfer: dt
  240. }));
  241. assertTrue(preventDefault);
  242. preventDefault = false;
  243. // Assert that default actions are prevented on dragover.
  244. textarea.dispatchEvent(new goog.events.BrowserEvent({
  245. preventDefault: function() { preventDefault = true; },
  246. type: goog.events.EventType.DRAGOVER,
  247. dataTransfer: dt
  248. }));
  249. assertTrue(preventDefault);
  250. }