Debian Codegraph @

closure-library/closure/goog/fx/draglistgroup_test.js

raw
1 // Copyright 2011 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.fx.DragListGroupTest');
16 goog.setTestOnly('goog.fx.DragListGroupTest');
17
18 goog.require('goog.array');
19 goog.require('goog.dom');
20 goog.require('goog.dom.TagName');
21 goog.require('goog.dom.classlist');
22 goog.require('goog.events');
23 goog.require('goog.events.BrowserEvent');
24 goog.require('goog.events.BrowserFeature');
25 goog.require('goog.events.Event');
26 goog.require('goog.events.EventType');
27 goog.require('goog.fx.DragEvent');
28 goog.require('goog.fx.DragListDirection');
29 goog.require('goog.fx.DragListGroup');
30 goog.require('goog.fx.Dragger');
31 goog.require('goog.math.Coordinate');
32 goog.require('goog.object');
33 goog.require('goog.testing.events');
34 goog.require('goog.testing.jsunit');
35
36
37 /** @type {goog.fx.DragListGroup} */
38 var dlg;
39
40
41 /** @type {goog.dom.Element} */
42 var list;
43
44
45 /** @type {goog.events.BrowserEvent} */
46 var event;
47
48
49 /**
50 * The number of event listeners registered by the DragListGroup after the
51 * init() call.
52 * @type {number}
53 */
54 var initialListenerCount;
55
56
57 /**
58 * Type of events fired by the DragListGroup.
59 * @type {!Array<string>}
60 */
61 var firedEventTypes;
62
63 function setUp() {
64 var sandbox = goog.dom.getElement('sandbox');
65 list = goog.dom.createDom(goog.dom.TagName.DIV, {'id': 'horiz_div'});
66 list.appendChild(
67 goog.dom.createDom(
68 goog.dom.TagName.DIV, null, goog.dom.createTextNode('1')));
69 list.appendChild(
70 goog.dom.createDom(
71 goog.dom.TagName.DIV, null, goog.dom.createTextNode('2')));
72 list.appendChild(
73 goog.dom.createDom(
74 goog.dom.TagName.DIV, null, goog.dom.createTextNode('3')));
75 sandbox.appendChild(list);
76
77 dlg = new goog.fx.DragListGroup();
78 dlg.setDragItemHoverClass('opacity_40', 'cursor_move');
79 dlg.setDragItemHandleHoverClass('opacity_40', 'cursor_pointer');
80 dlg.setCurrDragItemClass('blue_bg', 'opacity_40');
81 dlg.setDraggerElClass('cursor_move', 'blue_bg');
82 dlg.addDragList(list, goog.fx.DragListDirection.RIGHT);
83 dlg.init();
84
85 initialListenerCount = goog.object.getCount(dlg.eventHandler_.keys_);
86
87 event = new goog.events.BrowserEvent();
88 event.currentTarget =
89 goog.dom.getElementsByTagName(goog.dom.TagName.DIV, list)[0];
90
91 firedEventTypes = [];
92 goog.events.listen(
93 dlg, goog.object.getValues(goog.fx.DragListGroup.EventType),
94 function(e) { firedEventTypes.push(e.type); });
95 }
96
97 function tearDown() {
98 dlg.dispose();
99 goog.dom.removeChildren(goog.dom.getElement('sandbox'));
100 }
101
102
103 /**
104 * Test the initial assumptions.
105 *
106 * Verify that the setter methods work properly, i.e., the CSS classes are
107 * stored in the private arrays after init() but are not added yet to target.
108 * (Since initially, we are not yet hovering over any list, in particular,
109 * over this target.)
110 */
111 function testSettersAfterInit() {
112 assertTrue(
113 goog.array.equals(
114 dlg.dragItemHoverClasses_, ['opacity_40', 'cursor_move']));
115 assertTrue(
116 goog.array.equals(
117 dlg.dragItemHandleHoverClasses_, ['opacity_40', 'cursor_pointer']));
118 assertTrue(
119 goog.array.equals(dlg.currDragItemClasses_, ['blue_bg', 'opacity_40']));
120
121 assertFalse(
122 'Should have no cursor_move class after init',
123 goog.dom.classlist.contains(event.currentTarget, 'cursor_move'));
124 assertFalse(
125 'Should have no cursor_pointer class after init',
126 goog.dom.classlist.contains(event.currentTarget, 'cursor_pointer'));
127 assertFalse(
128 'Should have no opacity_40 class after init',
129 goog.dom.classlist.contains(event.currentTarget, 'opacity_40'));
130 assertFalse(
131 'Should not have blue_bg class after init',
132 goog.dom.classlist.contains(event.currentTarget, 'blue_bg'));
133 }
134
135
136 /**
137 * Test the effect of hovering over a list.
138 *
139 * Check that after the MOUSEOVER browser event these classes are added to
140 * the current target of the event.
141 */
142 function testAddDragItemHoverClasses() {
143 dlg.handleDragItemMouseover_(event);
144
145 assertTrue(
146 'Should have cursor_move class after MOUSEOVER',
147 goog.dom.classlist.contains(event.currentTarget, 'cursor_move'));
148 assertTrue(
149 'Should have opacity_40 class after MOUSEOVER',
150 goog.dom.classlist.contains(event.currentTarget, 'opacity_40'));
151 assertFalse(
152 'Should not have cursor_pointer class after MOUSEOVER',
153 goog.dom.classlist.contains(event.currentTarget, 'cursor_pointer'));
154 assertFalse(
155 'Should not have blue_bg class after MOUSEOVER',
156 goog.dom.classlist.contains(event.currentTarget, 'blue_bg'));
157 }
158
159 function testAddDragItemHandleHoverClasses() {
160 dlg.handleDragItemHandleMouseover_(event);
161
162 assertFalse(
163 'Should not have cursor_move class after MOUSEOVER',
164 goog.dom.classlist.contains(event.currentTarget, 'cursor_move'));
165 assertTrue(
166 'Should have opacity_40 class after MOUSEOVER',
167 goog.dom.classlist.contains(event.currentTarget, 'opacity_40'));
168 assertTrue(
169 'Should have cursor_pointer class after MOUSEOVER',
170 goog.dom.classlist.contains(event.currentTarget, 'cursor_pointer'));
171 assertFalse(
172 'Should not have blue_bg class after MOUSEOVER',
173 goog.dom.classlist.contains(event.currentTarget, 'blue_bg'));
174 }
175
176
177 /**
178 * Test the effect of stopping hovering over a list.
179 *
180 * Check that after the MOUSEOUT browser event all CSS classes are removed
181 * from the target (as we are no longer hovering over the it).
182 */
183 function testRemoveDragItemHoverClasses() {
184 dlg.handleDragItemMouseover_(event);
185 dlg.handleDragItemMouseout_(event);
186
187 assertFalse(
188 'Should have no cursor_move class after MOUSEOUT',
189 goog.dom.classlist.contains(event.currentTarget, 'cursor_move'));
190 assertFalse(
191 'Should have no cursor_pointer class after MOUSEOUT',
192 goog.dom.classlist.contains(event.currentTarget, 'cursor_pointer'));
193 assertFalse(
194 'Should have no opacity_40 class after MOUSEOUT',
195 goog.dom.classlist.contains(event.currentTarget, 'opacity_40'));
196 assertFalse(
197 'Should have no blue_bg class after MOUSEOUT',
198 goog.dom.classlist.contains(event.currentTarget, 'blue_bg'));
199 }
200
201 function testRemoveDragItemHandleHoverClasses() {
202 dlg.handleDragItemHandleMouseover_(event);
203 dlg.handleDragItemHandleMouseout_(event);
204
205 assertFalse(
206 'Should have no cursor_move class after MOUSEOUT',
207 goog.dom.classlist.contains(event.currentTarget, 'cursor_move'));
208 assertFalse(
209 'Should have no cursor_pointer class after MOUSEOUT',
210 goog.dom.classlist.contains(event.currentTarget, 'cursor_pointer'));
211 assertFalse(
212 'Should have no opacity_40 class after MOUSEOUT',
213 goog.dom.classlist.contains(event.currentTarget, 'opacity_40'));
214 assertFalse(
215 'Should have no blue_bg class after MOUSEOUT',
216 goog.dom.classlist.contains(event.currentTarget, 'blue_bg'));
217 }
218
219
220 /**
221 * Test the effect of dragging an item. (DRAGSTART event.)
222 *
223 * Check that after the MOUSEDOWN browser event is handled by the
224 * handlePotentialDragStart_() method the currDragItem has the CSS classes
225 * set by the setter method.
226 */
227 function testAddCurrentDragItemClasses() {
228 var be = new goog.events.BrowserEvent({
229 type: goog.events.EventType.MOUSEDOWN,
230 button: goog.events.BrowserFeature.HAS_W3C_BUTTON ? 0 : 1
231 });
232 event.event_ = be;
233
234 dlg.handlePotentialDragStart_(event);
235
236 assertFalse(
237 'Should have no cursor_move class after MOUSEDOWN',
238 goog.dom.classlist.contains(dlg.currDragItem_, 'cursor_move'));
239 assertFalse(
240 'Should have no cursor_pointer class after MOUSEDOWN',
241 goog.dom.classlist.contains(dlg.currDragItem_, 'cursor_pointer'));
242 assertTrue(
243 'Should have opacity_40 class after MOUSEDOWN',
244 goog.dom.classlist.contains(dlg.currDragItem_, 'opacity_40'));
245 assertTrue(
246 'Should have blue_bg class after MOUSEDOWN',
247 goog.dom.classlist.contains(dlg.currDragItem_, 'blue_bg'));
248 }
249
250
251 /**
252 * Test the effect of dragging an item. (DRAGEND event.)
253 *
254 * Check that after the MOUSEUP browser event handled by the handleDragEnd_()
255 * method the currDragItem has no CSS classes set in the dispatched event.
256 */
257 function testRemoveCurrentDragItemClasses() {
258 var be = new goog.events.BrowserEvent({
259 type: goog.events.EventType.MOUSEDOWN,
260 button: goog.events.BrowserFeature.HAS_W3C_BUTTON ? 0 : 1
261 });
262 event.event_ = be;
263 dlg.handlePotentialDragStart_(event);
264
265 // Need to catch the dispatched event because the temporary fields
266 // including dlg.currentDragItem_ are cleared after the dragging has ended.
267 var currDragItem = goog.dom.createDom(
268 goog.dom.TagName.DIV, ['cursor_move', 'blue_bg'],
269 goog.dom.createTextNode('4'));
270 goog.events.listen(dlg, goog.fx.DragListGroup.EventType.DRAGEND, function(e) {
271 currDragItem = dlg.currDragItem_;
272 });
273
274 var dragger = new goog.fx.Dragger(event.currentTarget);
275 be.type = goog.events.EventType.MOUSEUP;
276 be.clientX = 1;
277 be.clientY = 2;
278 var dragEvent = new goog.fx.DragEvent(
279 goog.fx.Dragger.EventType.END, dragger, be.clientX, be.clientY, be);
280 dlg.handleDragEnd_(dragEvent); // this method dispatches the DRAGEND event
281 dragger.dispose();
282
283 assertFalse(
284 'Should have no cursor_move class after MOUSEUP',
285 goog.dom.classlist.contains(currDragItem, 'cursor_move'));
286 assertFalse(
287 'Should have no cursor_pointer class after MOUSEUP',
288 goog.dom.classlist.contains(currDragItem, 'cursor_pointer'));
289 assertFalse(
290 'Should have no opacity_40 class after MOUSEUP',
291 goog.dom.classlist.contains(currDragItem, 'opacity_40'));
292 assertFalse(
293 'Should have no blue_bg class after MOUSEUP',
294 goog.dom.classlist.contains(currDragItem, 'blue_bg'));
295 }
296
297
298 /**
299 * Asserts that the DragListGroup is in idle state.
300 * @param {!goog.fx.DragListGroup} dlg The DragListGroup to examine.
301 */
302 function assertIdle(dlg) {
303 assertFalse(dlg.isDragging());
304 assertNull('dragger element has been cleaned up', dlg.draggerEl_);
305 assertNull('dragger has been cleaned up', dlg.dragger_);
306 assertEquals(
307 'the additional event listeners have been removed', initialListenerCount,
308 goog.object.getCount(dlg.eventHandler_.keys_));
309 }
310
311 function testFiredEvents() {
312 goog.testing.events.fireClickSequence(list.firstChild);
313 assertArrayEquals(
314 'event types in case of zero distance dragging',
315 [
316 goog.fx.DragListGroup.EventType.DRAGGERCREATED.toString(),
317 goog.fx.DragListGroup.EventType.BEFOREDRAGSTART.toString(),
318 goog.fx.DragListGroup.EventType.DRAGSTART.toString(),
319 goog.fx.DragListGroup.EventType.BEFOREDRAGEND.toString(),
320 goog.fx.DragListGroup.EventType.DRAGGERREMOVED.toString(),
321 goog.fx.DragListGroup.EventType.DRAGEND.toString()
322 ],
323 firedEventTypes);
324 assertIdle(dlg);
325 }
326
327 function testFiredEventsWithHysteresis() {
328 dlg.setHysteresis(2);
329
330 goog.testing.events.fireClickSequence(list.firstChild);
331 assertArrayEquals(
332 'only clone events are fired on click if hysteresis is enabled',
333 [
334 goog.fx.DragListGroup.EventType.DRAGGERCREATED.toString(),
335 goog.fx.DragListGroup.EventType.DRAGGERREMOVED.toString()
336 ],
337 firedEventTypes);
338 firedEventTypes.length = 0;
339 assertIdle(dlg);
340
341 goog.testing.events.fireMouseDownEvent(
342 list.firstChild, null, new goog.math.Coordinate(0, 0));
343 goog.testing.events.fireMouseMoveEvent(
344 list.firstChild, new goog.math.Coordinate(1, 0));
345 assertArrayEquals(
346 'only potential-start event is fired on click if hysteresis is enabled',
347 [goog.fx.DragListGroup.EventType.DRAGGERCREATED.toString()],
348 firedEventTypes);
349 firedEventTypes.length = 0;
350
351 goog.testing.events.fireMouseMoveEvent(
352 list.firstChild, new goog.math.Coordinate(3, 0));
353 assertArrayEquals(
354 'start+move events are fired over hysteresis distance',
355 [
356 goog.fx.DragListGroup.EventType.BEFOREDRAGSTART.toString(),
357 goog.fx.DragListGroup.EventType.DRAGSTART.toString(),
358 goog.fx.DragListGroup.EventType.BEFOREDRAGMOVE.toString(),
359 goog.fx.DragListGroup.EventType.DRAGMOVE.toString()
360 ],
361 firedEventTypes);
362 assertTrue(dlg.isDragging());
363
364 firedEventTypes.length = 0;
365 goog.testing.events.fireMouseUpEvent(
366 list.firstChild, null, new goog.math.Coordinate(3, 0));
367 assertArrayEquals(
368 'end events are fired on mouseup',
369 [
370 goog.fx.DragListGroup.EventType.BEFOREDRAGEND.toString(),
371 goog.fx.DragListGroup.EventType.DRAGGERREMOVED.toString(),
372 goog.fx.DragListGroup.EventType.DRAGEND.toString()
373 ],
374 firedEventTypes);
375 assertIdle(dlg);
376 }
377
378 function testPreventDefaultBeforeDragStart() {
379 goog.events.listen(
380 dlg, goog.fx.DragListGroup.EventType.BEFOREDRAGSTART,
381 goog.events.Event.preventDefault);
382
383 goog.testing.events.fireMouseDownEvent(list.firstChild);
384 assertArrayEquals(
385 'event types if dragging is prevented',
386 [
387 goog.fx.DragListGroup.EventType.DRAGGERCREATED.toString(),
388 goog.fx.DragListGroup.EventType.BEFOREDRAGSTART.toString(),
389 goog.fx.DragListGroup.EventType.DRAGGERREMOVED.toString()
390 ],
391 firedEventTypes);
392 assertIdle(dlg);
393 }
394
395 function testPreventDefaultBeforeDragStartWithHysteresis() {
396 dlg.setHysteresis(5);
397 goog.events.listen(
398 dlg, goog.fx.DragListGroup.EventType.BEFOREDRAGSTART,
399 goog.events.Event.preventDefault);
400
401 goog.testing.events.fireMouseDownEvent(
402 list.firstChild, null, new goog.math.Coordinate(0, 0));
403 goog.testing.events.fireMouseMoveEvent(
404 list.firstChild, new goog.math.Coordinate(10, 0));
405 assertArrayEquals(
406 'event types if dragging is prevented',
407 [
408 goog.fx.DragListGroup.EventType.DRAGGERCREATED.toString(),
409 goog.fx.DragListGroup.EventType.BEFOREDRAGSTART.toString(),
410 goog.fx.DragListGroup.EventType.DRAGGERREMOVED.toString()
411 ],
412 firedEventTypes);
413 assertIdle(dlg);
414 }
415
416 function testRightClick() {
417 goog.testing.events.fireMouseDownEvent(
418 list.firstChild, goog.events.BrowserEvent.MouseButton.RIGHT);
419 goog.testing.events.fireMouseUpEvent(
420 list.firstChild, goog.events.BrowserEvent.MouseButton.RIGHT);
421
422 assertArrayEquals(
423 'only clone events are fired on right click',
424 [
425 goog.fx.DragListGroup.EventType.DRAGGERCREATED.toString(),
426 goog.fx.DragListGroup.EventType.DRAGGERREMOVED.toString()
427 ],
428 firedEventTypes);
429 assertIdle(dlg);
430 }
431
432
433 /**
434 * Tests that a new item can be added to a drag list after the control has
435 * been initialized.
436 */
437 function testAddItemToDragList() {
438 var item = goog.dom.createDom(
439 goog.dom.TagName.DIV, null, goog.dom.createTextNode('newItem'));
440
441 dlg.addItemToDragList(list, item);
442
443 assertEquals(item, list.lastChild);
444 assertEquals(4, goog.dom.getChildren(list).length);
445
446 goog.events.listen(
447 dlg, goog.fx.DragListGroup.EventType.BEFOREDRAGSTART,
448 goog.events.Event.preventDefault);
449
450 goog.testing.events.fireMouseDownEvent(item);
451 assertArrayEquals(
452 'Should fire beforedragstart event when clicked',
453 [
454 goog.fx.DragListGroup.EventType.DRAGGERCREATED.toString(),
455 goog.fx.DragListGroup.EventType.BEFOREDRAGSTART.toString(),
456 goog.fx.DragListGroup.EventType.DRAGGERREMOVED.toString()
457 ],
458 firedEventTypes);
459 }
460
461
462 /**
463 * Tests that a new item added to a drag list after the control has been
464 * initialized is inserted at the correct position.
465 */
466 function testInsertItemInDragList() {
467 var item = goog.dom.createDom(
468 goog.dom.TagName.DIV, null, goog.dom.createTextNode('newItem'));
469
470 dlg.addItemToDragList(list, item, 0);
471
472 assertEquals(item, list.firstChild);
473 assertEquals(4, goog.dom.getChildren(list).length);
474
475 goog.events.listen(
476 dlg, goog.fx.DragListGroup.EventType.BEFOREDRAGSTART,
477 goog.events.Event.preventDefault);
478
479 goog.testing.events.fireMouseDownEvent(item);
480 assertArrayEquals(
481 'Should fire beforedragstart event when clicked',
482 [
483 goog.fx.DragListGroup.EventType.DRAGGERCREATED.toString(),
484 goog.fx.DragListGroup.EventType.BEFOREDRAGSTART.toString(),
485 goog.fx.DragListGroup.EventType.DRAGGERREMOVED.toString()
486 ],
487 firedEventTypes);
488 }