Debian Codegraph @

closure-library/closure/goog/events/pastehandler_test.js

raw
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
15 goog.provide('goog.events.PasteHandlerTest');
16 goog.setTestOnly('goog.events.PasteHandlerTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.events');
20 goog.require('goog.events.BrowserEvent');
21 goog.require('goog.events.EventTarget');
22 goog.require('goog.events.EventType');
23 goog.require('goog.events.KeyCodes');
24 goog.require('goog.events.PasteHandler');
25 goog.require('goog.testing.MockClock');
26 goog.require('goog.testing.jsunit');
27 goog.require('goog.userAgent');
28
29 function setUp() {
30 textarea = new goog.events.EventTarget();
31 textarea.value = '';
32 clock = new goog.testing.MockClock(true);
33 handler = new goog.events.PasteHandler(textarea);
34 pasted = false;
35 goog.events.listen(
36 handler, goog.events.PasteHandler.EventType.PASTE,
37 function() { pasted = true; });
38 }
39
40 function tearDown() {
41 textarea.dispose();
42 handler.dispose();
43 clock.dispose();
44 }
45
46 function newBrowserEvent(type) {
47 if (goog.isString(type)) {
48 return new goog.events.BrowserEvent({type: type});
49 } else {
50 return new goog.events.BrowserEvent(type);
51 }
52 }
53
54 function testDispatchingPasteEventSupportedByAFewBrowsersWork() {
55 if (!goog.events.PasteHandler.SUPPORTS_NATIVE_PASTE_EVENT) {
56 return;
57 }
58 var handlerThatSupportsPasteEvents = new goog.events.PasteHandler(textarea);
59 // user clicks on the textarea and give it focus
60 goog.events.listen(
61 handlerThatSupportsPasteEvents, goog.events.PasteHandler.EventType.PASTE,
62 function() { pasted = true; });
63 textarea.dispatchEvent(newBrowserEvent('paste'));
64 assertTrue(pasted);
65 }
66
67 function testJustTypingDoesntFirePasteEvent() {
68 if (goog.events.PasteHandler.SUPPORTS_NATIVE_PASTE_EVENT) {
69 return;
70 }
71 // user clicks on the textarea and give it focus
72 textarea.dispatchEvent(newBrowserEvent(goog.events.EventType.FOCUS));
73 assertFalse(pasted);
74 // user starts typing
75 textarea.dispatchEvent(newBrowserEvent({
76 type: goog.events.EventType.KEYDOWN,
77 keyCode: goog.events.KeyCodes.A
78 }));
79 textarea.value = 'a';
80 assertFalse(pasted);
81
82 // still typing
83 textarea.dispatchEvent(
84 {type: goog.events.EventType.KEYDOWN, keyCode: goog.events.KeyCodes.B});
85 textarea.value = 'ab';
86 assertFalse(pasted);
87
88 // ends typing
89 textarea.dispatchEvent(
90 {type: goog.events.EventType.KEYDOWN, keyCode: goog.events.KeyCodes.C});
91 textarea.value = 'abc';
92 assertFalse(pasted);
93 }
94
95 function testStartsOnInitialState() {
96 assertTrue(handler.getState() == goog.events.PasteHandler.State.INIT);
97 assertFalse(pasted);
98 }
99
100 function testBlurOnInit() {
101 if (goog.events.PasteHandler.SUPPORTS_NATIVE_PASTE_EVENT) {
102 return;
103 }
104 textarea.dispatchEvent(goog.events.EventType.BLUR);
105 assertTrue(handler.getState() == goog.events.PasteHandler.State.INIT);
106 assertFalse(pasted);
107 }
108
109 function testFocusOnInit() {
110 if (goog.events.PasteHandler.SUPPORTS_NATIVE_PASTE_EVENT) {
111 return;
112 }
113 textarea.dispatchEvent(goog.events.EventType.FOCUS);
114 assertTrue(handler.getState() == goog.events.PasteHandler.State.FOCUSED);
115 assertFalse(pasted);
116 }
117
118 function testInputOnFocus() {
119 if (goog.events.PasteHandler.SUPPORTS_NATIVE_PASTE_EVENT) {
120 return;
121 }
122 // user clicks on the textarea
123 textarea.dispatchEvent(newBrowserEvent(goog.events.EventType.FOCUS));
124 clock.tick(
125 goog.events.PasteHandler.MANDATORY_MS_BETWEEN_INPUT_EVENTS_TIE_BREAKER +
126 1);
127 // and right click -> paste a text!
128 textarea.dispatchEvent(newBrowserEvent('input'));
129 assertTrue(handler.getState() == goog.events.PasteHandler.State.FOCUSED);
130 // make sure we detected it
131 assertTrue(pasted);
132 }
133
134 function testKeyPressOnFocus() {
135 if (goog.events.PasteHandler.SUPPORTS_NATIVE_PASTE_EVENT) {
136 return;
137 }
138 // user clicks on the textarea
139 textarea.dispatchEvent(newBrowserEvent(goog.events.EventType.FOCUS));
140
141 // starts typing something
142 textarea.dispatchEvent(newBrowserEvent({
143 type: goog.events.EventType.KEYDOWN,
144 keyCode: goog.events.KeyCodes.A
145 }));
146 assertTrue(handler.getState() == goog.events.PasteHandler.State.TYPING);
147 assertFalse(pasted);
148
149 // and then presses ctrl+v
150 textarea.dispatchEvent(newBrowserEvent({
151 type: goog.events.EventType.KEYDOWN,
152 keyCode: goog.events.KeyCodes.V,
153 ctrlKey: true
154 }));
155 assertTrue(handler.getState() == goog.events.PasteHandler.State.TYPING);
156
157 // makes sure we detected it
158 assertTrue(pasted);
159 }
160
161 function testMouseOverOnInit() {
162 if (goog.events.PasteHandler.SUPPORTS_NATIVE_PASTE_EVENT) {
163 return;
164 }
165 // user has something on the events
166 textarea.value = 'pasted string';
167 // and right click -> paste it on the textarea, WITHOUT giving focus
168 textarea.dispatchEvent(newBrowserEvent(goog.events.EventType.MOUSEOVER));
169 assertTrue(handler.getState() == goog.events.PasteHandler.State.INIT);
170 // makes sure we detect it
171 assertTrue(pasted);
172
173 pasted = false;
174
175 // user normaly mouseovers the textarea, with no text change
176 textarea.dispatchEvent(goog.events.EventType.MOUSEOVER);
177 assertTrue(handler.getState() == goog.events.PasteHandler.State.INIT);
178 // text area value doesn't change
179 assertFalse(pasted);
180 }
181
182 function testMouseOverAfterTyping() {
183 if (goog.events.PasteHandler.SUPPORTS_NATIVE_PASTE_EVENT) {
184 return;
185 }
186 textarea.dispatchEvent(goog.events.EventType.FOCUS);
187 assertFalse(pasted);
188 textarea.dispatchEvent(
189 {type: goog.events.EventType.KEYDOWN, keyCode: goog.events.KeyCodes.A});
190 assertFalse(pasted);
191 textarea.value = 'a';
192 textarea.dispatchEvent('input');
193 assertFalse(pasted);
194 assertEquals('a', handler.oldValue_);
195 textarea.dispatchEvent(goog.events.EventType.MOUSEOVER);
196 assertFalse(pasted);
197 }
198
199 function testTypingAndThenRightClickPaste() {
200 if (goog.events.PasteHandler.SUPPORTS_NATIVE_PASTE_EVENT) {
201 return;
202 }
203 textarea.dispatchEvent(goog.events.EventType.FOCUS);
204
205 textarea.dispatchEvent(
206 {type: goog.events.EventType.KEYDOWN, keyCode: goog.events.KeyCodes.A});
207 assertFalse(pasted);
208 textarea.value = 'a';
209 clock.tick(
210 goog.events.PasteHandler.MANDATORY_MS_BETWEEN_INPUT_EVENTS_TIE_BREAKER +
211 1);
212 textarea.dispatchEvent('input');
213 assertFalse(pasted);
214
215 assertEquals('a', handler.oldValue_);
216
217 textarea.value = 'ab';
218 clock.tick(
219 goog.events.PasteHandler.MANDATORY_MS_BETWEEN_INPUT_EVENTS_TIE_BREAKER +
220 1);
221 textarea.dispatchEvent(newBrowserEvent('input'));
222 assertTrue(pasted);
223 }
224
225 function testTypingReallyFastDispatchesTwoInputEventsBeforeTheKeyDownEvent() {
226 if (goog.events.PasteHandler.SUPPORTS_NATIVE_PASTE_EVENT) {
227 return;
228 }
229 textarea.dispatchEvent(goog.events.EventType.FOCUS);
230
231 // keydown and input events seems to be fired indepently: even though input
232 // should happen after the key event, it doesn't if the user types fast
233 // enough. FF2 + linux doesn't fire keydown events for every key pressed when
234 // you type fast enough. if one of the keydown events gets swallowed, two
235 // input events are fired consecutively. notice that there is a similar
236 // scenario, that actually does produce a valid paste action.
237 // {@see testRightClickRightClickAlsoDispatchesTwoConsecutiveInputEvents}
238
239 textarea.dispatchEvent(
240 {type: goog.events.EventType.KEYDOWN, keyCode: goog.events.KeyCodes.A});
241 assertFalse(pasted);
242 textarea.value = 'a';
243 clock.tick(
244 goog.events.PasteHandler.MANDATORY_MS_BETWEEN_INPUT_EVENTS_TIE_BREAKER -
245 1);
246 textarea.dispatchEvent('input');
247 assertFalse(pasted);
248
249 // second key down events gets fired on a different order
250 textarea.value = 'ab';
251 clock.tick(
252 goog.events.PasteHandler.MANDATORY_MS_BETWEEN_INPUT_EVENTS_TIE_BREAKER -
253 1);
254 textarea.dispatchEvent('input');
255 assertFalse(pasted);
256 }
257
258 function testRightClickRightClickAlsoDispatchesTwoConsecutiveInputEvents() {
259 if (goog.events.PasteHandler.SUPPORTS_NATIVE_PASTE_EVENT) {
260 return;
261 }
262 textarea.dispatchEvent(goog.events.EventType.FOCUS);
263
264 // there is also another case that two consecutive INPUT events are fired,
265 // but in a valid paste action: if the user edit -> paste -> edit -> paste,
266 // it is a valid paste action.
267
268 textarea.value = 'a';
269 clock.tick(
270 goog.events.PasteHandler.MANDATORY_MS_BETWEEN_INPUT_EVENTS_TIE_BREAKER +
271 1);
272 textarea.dispatchEvent(newBrowserEvent('input'));
273 assertTrue(pasted);
274
275 // second key down events gets fired on a different order
276 textarea.value = 'ab';
277 clock.tick(
278 goog.events.PasteHandler.MANDATORY_MS_BETWEEN_INPUT_EVENTS_TIE_BREAKER +
279 1);
280 textarea.dispatchEvent(newBrowserEvent('input'));
281 assertTrue(pasted);
282 }
283
284 function testMiddleClickWithoutFocusTriggersPasteEvent() {
285 if (goog.events.PasteHandler.SUPPORTS_NATIVE_PASTE_EVENT) {
286 return;
287 }
288 // if the textarea is NOT selected, and then we use the middle button,
289 // FF2+linux pastes what was last highlighted, causing a paste action.
290 textarea.dispatchEvent(goog.events.EventType.FOCUS);
291 textarea.dispatchEvent(newBrowserEvent('input'));
292 assertTrue(pasted);
293 }
294
295
296 function testMacRightClickPasteRequiresCtrlBecauseItHasOneButton() {
297 if (!goog.userAgent.OPERA || !goog.userAgent.MAC) {
298 return;
299 }
300 var handler = new goog.events.PasteHandler(textarea);
301 // user clicks on the textarea and give it focus
302 goog.events.listen(
303 handler, goog.events.PasteHandler.EventType.PASTE,
304 function() { pasted = true; });
305 textarea.dispatchEvent(goog.events.EventType.FOCUS);
306 assertFalse(pasted);
307 textarea.dispatchEvent({type: goog.events.EventType.KEYDOWN, keyCode: 0});
308 assertFalse(pasted);
309 clock.tick(
310 goog.events.PasteHandler.MANDATORY_MS_BETWEEN_INPUT_EVENTS_TIE_BREAKER +
311 1);
312 textarea.dispatchEvent(newBrowserEvent('input'));
313 assertTrue(pasted);
314 }
315
316 function testOperaMacFiresKeyCode17WhenAppleKeyPressedButDoesNotFireKeyDown() {
317 if (!goog.userAgent.OPERA || !goog.userAgent.MAC) {
318 return;
319 }
320 var handler = new goog.events.PasteHandler(textarea);
321 // user clicks on the textarea and give it focus
322 goog.events.listen(
323 handler, goog.events.PasteHandler.EventType.PASTE,
324 function() { pasted = true; });
325 textarea.dispatchEvent(goog.events.EventType.FOCUS);
326 assertFalse(pasted);
327 // apple key is pressed, generating a keydown event
328 textarea.dispatchEvent({type: goog.events.EventType.KEYDOWN, keyCode: 17});
329 assertFalse(pasted);
330 clock.tick(
331 goog.events.PasteHandler.MANDATORY_MS_BETWEEN_INPUT_EVENTS_TIE_BREAKER +
332 1);
333 // and then text is added magically without any extra keydown events.
334 textarea.dispatchEvent(newBrowserEvent('input'));
335 assertTrue(pasted);
336 }
337
338 function testScriptingDoesntTriggerPasteEvents() {
339 var handlerUsedToListenForScriptingChanges =
340 new goog.events.PasteHandler(textarea);
341 pasted = false;
342 // user clicks on the textarea and give it focus
343 goog.events.listen(
344 handlerUsedToListenForScriptingChanges,
345 goog.events.PasteHandler.EventType.PASTE, function() { pasted = true; });
346 goog.dom.getElement('foo').value = 'dear paste handler,';
347 assertFalse(pasted);
348 goog.dom.getElement('foo').value = 'please dont misunderstand script changes';
349 assertFalse(pasted);
350 goog.dom.getElement('foo').value = 'with user generated paste events';
351 assertFalse(pasted);
352 goog.dom.getElement('foo').value = 'thanks!';
353 assertFalse(pasted);
354 }
355
356 function testAfterPaste() {
357 if (!goog.events.PasteHandler.SUPPORTS_NATIVE_PASTE_EVENT) {
358 return;
359 }
360 var handlerThatSupportsPasteEvents = new goog.events.PasteHandler(textarea);
361 pasted = false;
362 goog.events.listen(
363 handlerThatSupportsPasteEvents, goog.events.PasteHandler.EventType.PASTE,
364 function() { pasted = true; });
365 var afterPasteFired = false;
366 goog.events.listen(
367 handlerThatSupportsPasteEvents,
368 goog.events.PasteHandler.EventType.AFTER_PASTE,
369 function() { afterPasteFired = true; });
370
371 // Initial paste event comes before AFTER_PASTE has fired.
372 textarea.dispatchEvent(newBrowserEvent('paste'));
373 assertTrue(pasted);
374 assertFalse(afterPasteFired);
375
376 // Once text is pasted, it takes a bit to detect it, at which point
377 // AFTER_PASTE is fired.
378 clock.tick(goog.events.PasteHandler.PASTE_POLLING_PERIOD_MS_);
379 textarea.value = 'text';
380 clock.tick(goog.events.PasteHandler.PASTE_POLLING_PERIOD_MS_);
381 assertTrue(afterPasteFired);
382 }
383
384
385 function testAfterPasteNotFiredIfDelayTooLong() {
386 if (!goog.events.PasteHandler.SUPPORTS_NATIVE_PASTE_EVENT) {
387 return;
388 }
389 var handlerThatSupportsPasteEvents = new goog.events.PasteHandler(textarea);
390 pasted = false;
391 goog.events.listen(
392 handlerThatSupportsPasteEvents, goog.events.PasteHandler.EventType.PASTE,
393 function() { pasted = true; });
394 var afterPasteFired = false;
395 goog.events.listen(
396 handlerThatSupportsPasteEvents,
397 goog.events.PasteHandler.EventType.AFTER_PASTE,
398 function() { afterPasteFired = true; });
399
400 // Initial paste event comes before AFTER_PASTE has fired.
401 textarea.dispatchEvent(newBrowserEvent('paste'));
402 assertTrue(pasted);
403 assertFalse(afterPasteFired);
404
405 // If the new text doesn't show up in time, we never fire AFTER_PASTE.
406 clock.tick(goog.events.PasteHandler.PASTE_POLLING_TIMEOUT_MS_);
407 textarea.value = 'text';
408 clock.tick(goog.events.PasteHandler.PASTE_POLLING_PERIOD_MS_);
409 assertFalse(afterPasteFired);
410 }