123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- // Copyright 2011 The Closure Library Authors. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS-IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- goog.provide('goog.fx.DragListGroupTest');
- goog.setTestOnly('goog.fx.DragListGroupTest');
- goog.require('goog.array');
- goog.require('goog.dom');
- goog.require('goog.dom.TagName');
- goog.require('goog.dom.classlist');
- goog.require('goog.events');
- goog.require('goog.events.BrowserEvent');
- goog.require('goog.events.BrowserFeature');
- goog.require('goog.events.Event');
- goog.require('goog.events.EventType');
- goog.require('goog.fx.DragEvent');
- goog.require('goog.fx.DragListDirection');
- goog.require('goog.fx.DragListGroup');
- goog.require('goog.fx.Dragger');
- goog.require('goog.math.Coordinate');
- goog.require('goog.object');
- goog.require('goog.testing.events');
- goog.require('goog.testing.jsunit');
- /** @type {goog.fx.DragListGroup} */
- var dlg;
- /** @type {goog.dom.Element} */
- var list;
- /** @type {goog.events.BrowserEvent} */
- var event;
- /**
- * The number of event listeners registered by the DragListGroup after the
- * init() call.
- * @type {number}
- */
- var initialListenerCount;
- /**
- * Type of events fired by the DragListGroup.
- * @type {!Array<string>}
- */
- var firedEventTypes;
- function setUp() {
- var sandbox = goog.dom.getElement('sandbox');
- list = goog.dom.createDom(goog.dom.TagName.DIV, {'id': 'horiz_div'});
- list.appendChild(
- goog.dom.createDom(
- goog.dom.TagName.DIV, null, goog.dom.createTextNode('1')));
- list.appendChild(
- goog.dom.createDom(
- goog.dom.TagName.DIV, null, goog.dom.createTextNode('2')));
- list.appendChild(
- goog.dom.createDom(
- goog.dom.TagName.DIV, null, goog.dom.createTextNode('3')));
- sandbox.appendChild(list);
- dlg = new goog.fx.DragListGroup();
- dlg.setDragItemHoverClass('opacity_40', 'cursor_move');
- dlg.setDragItemHandleHoverClass('opacity_40', 'cursor_pointer');
- dlg.setCurrDragItemClass('blue_bg', 'opacity_40');
- dlg.setDraggerElClass('cursor_move', 'blue_bg');
- dlg.addDragList(list, goog.fx.DragListDirection.RIGHT);
- dlg.init();
- initialListenerCount = goog.object.getCount(dlg.eventHandler_.keys_);
- event = new goog.events.BrowserEvent();
- event.currentTarget =
- goog.dom.getElementsByTagName(goog.dom.TagName.DIV, list)[0];
- firedEventTypes = [];
- goog.events.listen(
- dlg, goog.object.getValues(goog.fx.DragListGroup.EventType),
- function(e) { firedEventTypes.push(e.type); });
- }
- function tearDown() {
- dlg.dispose();
- goog.dom.removeChildren(goog.dom.getElement('sandbox'));
- }
- /**
- * Test the initial assumptions.
- *
- * Verify that the setter methods work properly, i.e., the CSS classes are
- * stored in the private arrays after init() but are not added yet to target.
- * (Since initially, we are not yet hovering over any list, in particular,
- * over this target.)
- */
- function testSettersAfterInit() {
- assertTrue(
- goog.array.equals(
- dlg.dragItemHoverClasses_, ['opacity_40', 'cursor_move']));
- assertTrue(
- goog.array.equals(
- dlg.dragItemHandleHoverClasses_, ['opacity_40', 'cursor_pointer']));
- assertTrue(
- goog.array.equals(dlg.currDragItemClasses_, ['blue_bg', 'opacity_40']));
- assertFalse(
- 'Should have no cursor_move class after init',
- goog.dom.classlist.contains(event.currentTarget, 'cursor_move'));
- assertFalse(
- 'Should have no cursor_pointer class after init',
- goog.dom.classlist.contains(event.currentTarget, 'cursor_pointer'));
- assertFalse(
- 'Should have no opacity_40 class after init',
- goog.dom.classlist.contains(event.currentTarget, 'opacity_40'));
- assertFalse(
- 'Should not have blue_bg class after init',
- goog.dom.classlist.contains(event.currentTarget, 'blue_bg'));
- }
- /**
- * Test the effect of hovering over a list.
- *
- * Check that after the MOUSEOVER browser event these classes are added to
- * the current target of the event.
- */
- function testAddDragItemHoverClasses() {
- dlg.handleDragItemMouseover_(event);
- assertTrue(
- 'Should have cursor_move class after MOUSEOVER',
- goog.dom.classlist.contains(event.currentTarget, 'cursor_move'));
- assertTrue(
- 'Should have opacity_40 class after MOUSEOVER',
- goog.dom.classlist.contains(event.currentTarget, 'opacity_40'));
- assertFalse(
- 'Should not have cursor_pointer class after MOUSEOVER',
- goog.dom.classlist.contains(event.currentTarget, 'cursor_pointer'));
- assertFalse(
- 'Should not have blue_bg class after MOUSEOVER',
- goog.dom.classlist.contains(event.currentTarget, 'blue_bg'));
- }
- function testAddDragItemHandleHoverClasses() {
- dlg.handleDragItemHandleMouseover_(event);
- assertFalse(
- 'Should not have cursor_move class after MOUSEOVER',
- goog.dom.classlist.contains(event.currentTarget, 'cursor_move'));
- assertTrue(
- 'Should have opacity_40 class after MOUSEOVER',
- goog.dom.classlist.contains(event.currentTarget, 'opacity_40'));
- assertTrue(
- 'Should have cursor_pointer class after MOUSEOVER',
- goog.dom.classlist.contains(event.currentTarget, 'cursor_pointer'));
- assertFalse(
- 'Should not have blue_bg class after MOUSEOVER',
- goog.dom.classlist.contains(event.currentTarget, 'blue_bg'));
- }
- /**
- * Test the effect of stopping hovering over a list.
- *
- * Check that after the MOUSEOUT browser event all CSS classes are removed
- * from the target (as we are no longer hovering over the it).
- */
- function testRemoveDragItemHoverClasses() {
- dlg.handleDragItemMouseover_(event);
- dlg.handleDragItemMouseout_(event);
- assertFalse(
- 'Should have no cursor_move class after MOUSEOUT',
- goog.dom.classlist.contains(event.currentTarget, 'cursor_move'));
- assertFalse(
- 'Should have no cursor_pointer class after MOUSEOUT',
- goog.dom.classlist.contains(event.currentTarget, 'cursor_pointer'));
- assertFalse(
- 'Should have no opacity_40 class after MOUSEOUT',
- goog.dom.classlist.contains(event.currentTarget, 'opacity_40'));
- assertFalse(
- 'Should have no blue_bg class after MOUSEOUT',
- goog.dom.classlist.contains(event.currentTarget, 'blue_bg'));
- }
- function testRemoveDragItemHandleHoverClasses() {
- dlg.handleDragItemHandleMouseover_(event);
- dlg.handleDragItemHandleMouseout_(event);
- assertFalse(
- 'Should have no cursor_move class after MOUSEOUT',
- goog.dom.classlist.contains(event.currentTarget, 'cursor_move'));
- assertFalse(
- 'Should have no cursor_pointer class after MOUSEOUT',
- goog.dom.classlist.contains(event.currentTarget, 'cursor_pointer'));
- assertFalse(
- 'Should have no opacity_40 class after MOUSEOUT',
- goog.dom.classlist.contains(event.currentTarget, 'opacity_40'));
- assertFalse(
- 'Should have no blue_bg class after MOUSEOUT',
- goog.dom.classlist.contains(event.currentTarget, 'blue_bg'));
- }
- /**
- * Test the effect of dragging an item. (DRAGSTART event.)
- *
- * Check that after the MOUSEDOWN browser event is handled by the
- * handlePotentialDragStart_() method the currDragItem has the CSS classes
- * set by the setter method.
- */
- function testAddCurrentDragItemClasses() {
- var be = new goog.events.BrowserEvent({
- type: goog.events.EventType.MOUSEDOWN,
- button: goog.events.BrowserFeature.HAS_W3C_BUTTON ? 0 : 1
- });
- event.event_ = be;
- dlg.handlePotentialDragStart_(event);
- assertFalse(
- 'Should have no cursor_move class after MOUSEDOWN',
- goog.dom.classlist.contains(dlg.currDragItem_, 'cursor_move'));
- assertFalse(
- 'Should have no cursor_pointer class after MOUSEDOWN',
- goog.dom.classlist.contains(dlg.currDragItem_, 'cursor_pointer'));
- assertTrue(
- 'Should have opacity_40 class after MOUSEDOWN',
- goog.dom.classlist.contains(dlg.currDragItem_, 'opacity_40'));
- assertTrue(
- 'Should have blue_bg class after MOUSEDOWN',
- goog.dom.classlist.contains(dlg.currDragItem_, 'blue_bg'));
- }
- /**
- * Test the effect of dragging an item. (DRAGEND event.)
- *
- * Check that after the MOUSEUP browser event handled by the handleDragEnd_()
- * method the currDragItem has no CSS classes set in the dispatched event.
- */
- function testRemoveCurrentDragItemClasses() {
- var be = new goog.events.BrowserEvent({
- type: goog.events.EventType.MOUSEDOWN,
- button: goog.events.BrowserFeature.HAS_W3C_BUTTON ? 0 : 1
- });
- event.event_ = be;
- dlg.handlePotentialDragStart_(event);
- // Need to catch the dispatched event because the temporary fields
- // including dlg.currentDragItem_ are cleared after the dragging has ended.
- var currDragItem = goog.dom.createDom(
- goog.dom.TagName.DIV, ['cursor_move', 'blue_bg'],
- goog.dom.createTextNode('4'));
- goog.events.listen(dlg, goog.fx.DragListGroup.EventType.DRAGEND, function(e) {
- currDragItem = dlg.currDragItem_;
- });
- var dragger = new goog.fx.Dragger(event.currentTarget);
- be.type = goog.events.EventType.MOUSEUP;
- be.clientX = 1;
- be.clientY = 2;
- var dragEvent = new goog.fx.DragEvent(
- goog.fx.Dragger.EventType.END, dragger, be.clientX, be.clientY, be);
- dlg.handleDragEnd_(dragEvent); // this method dispatches the DRAGEND event
- dragger.dispose();
- assertFalse(
- 'Should have no cursor_move class after MOUSEUP',
- goog.dom.classlist.contains(currDragItem, 'cursor_move'));
- assertFalse(
- 'Should have no cursor_pointer class after MOUSEUP',
- goog.dom.classlist.contains(currDragItem, 'cursor_pointer'));
- assertFalse(
- 'Should have no opacity_40 class after MOUSEUP',
- goog.dom.classlist.contains(currDragItem, 'opacity_40'));
- assertFalse(
- 'Should have no blue_bg class after MOUSEUP',
- goog.dom.classlist.contains(currDragItem, 'blue_bg'));
- }
- /**
- * Asserts that the DragListGroup is in idle state.
- * @param {!goog.fx.DragListGroup} dlg The DragListGroup to examine.
- */
- function assertIdle(dlg) {
- assertFalse(dlg.isDragging());
- assertNull('dragger element has been cleaned up', dlg.draggerEl_);
- assertNull('dragger has been cleaned up', dlg.dragger_);
- assertEquals(
- 'the additional event listeners have been removed', initialListenerCount,
- goog.object.getCount(dlg.eventHandler_.keys_));
- }
- function testFiredEvents() {
- goog.testing.events.fireClickSequence(list.firstChild);
- assertArrayEquals(
- 'event types in case of zero distance dragging',
- [
- goog.fx.DragListGroup.EventType.DRAGGERCREATED.toString(),
- goog.fx.DragListGroup.EventType.BEFOREDRAGSTART.toString(),
- goog.fx.DragListGroup.EventType.DRAGSTART.toString(),
- goog.fx.DragListGroup.EventType.BEFOREDRAGEND.toString(),
- goog.fx.DragListGroup.EventType.DRAGGERREMOVED.toString(),
- goog.fx.DragListGroup.EventType.DRAGEND.toString()
- ],
- firedEventTypes);
- assertIdle(dlg);
- }
- function testFiredEventsWithHysteresis() {
- dlg.setHysteresis(2);
- goog.testing.events.fireClickSequence(list.firstChild);
- assertArrayEquals(
- 'only clone events are fired on click if hysteresis is enabled',
- [
- goog.fx.DragListGroup.EventType.DRAGGERCREATED.toString(),
- goog.fx.DragListGroup.EventType.DRAGGERREMOVED.toString()
- ],
- firedEventTypes);
- firedEventTypes.length = 0;
- assertIdle(dlg);
- goog.testing.events.fireMouseDownEvent(
- list.firstChild, null, new goog.math.Coordinate(0, 0));
- goog.testing.events.fireMouseMoveEvent(
- list.firstChild, new goog.math.Coordinate(1, 0));
- assertArrayEquals(
- 'only potential-start event is fired on click if hysteresis is enabled',
- [goog.fx.DragListGroup.EventType.DRAGGERCREATED.toString()],
- firedEventTypes);
- firedEventTypes.length = 0;
- goog.testing.events.fireMouseMoveEvent(
- list.firstChild, new goog.math.Coordinate(3, 0));
- assertArrayEquals(
- 'start+move events are fired over hysteresis distance',
- [
- goog.fx.DragListGroup.EventType.BEFOREDRAGSTART.toString(),
- goog.fx.DragListGroup.EventType.DRAGSTART.toString(),
- goog.fx.DragListGroup.EventType.BEFOREDRAGMOVE.toString(),
- goog.fx.DragListGroup.EventType.DRAGMOVE.toString()
- ],
- firedEventTypes);
- assertTrue(dlg.isDragging());
- firedEventTypes.length = 0;
- goog.testing.events.fireMouseUpEvent(
- list.firstChild, null, new goog.math.Coordinate(3, 0));
- assertArrayEquals(
- 'end events are fired on mouseup',
- [
- goog.fx.DragListGroup.EventType.BEFOREDRAGEND.toString(),
- goog.fx.DragListGroup.EventType.DRAGGERREMOVED.toString(),
- goog.fx.DragListGroup.EventType.DRAGEND.toString()
- ],
- firedEventTypes);
- assertIdle(dlg);
- }
- function testPreventDefaultBeforeDragStart() {
- goog.events.listen(
- dlg, goog.fx.DragListGroup.EventType.BEFOREDRAGSTART,
- goog.events.Event.preventDefault);
- goog.testing.events.fireMouseDownEvent(list.firstChild);
- assertArrayEquals(
- 'event types if dragging is prevented',
- [
- goog.fx.DragListGroup.EventType.DRAGGERCREATED.toString(),
- goog.fx.DragListGroup.EventType.BEFOREDRAGSTART.toString(),
- goog.fx.DragListGroup.EventType.DRAGGERREMOVED.toString()
- ],
- firedEventTypes);
- assertIdle(dlg);
- }
- function testPreventDefaultBeforeDragStartWithHysteresis() {
- dlg.setHysteresis(5);
- goog.events.listen(
- dlg, goog.fx.DragListGroup.EventType.BEFOREDRAGSTART,
- goog.events.Event.preventDefault);
- goog.testing.events.fireMouseDownEvent(
- list.firstChild, null, new goog.math.Coordinate(0, 0));
- goog.testing.events.fireMouseMoveEvent(
- list.firstChild, new goog.math.Coordinate(10, 0));
- assertArrayEquals(
- 'event types if dragging is prevented',
- [
- goog.fx.DragListGroup.EventType.DRAGGERCREATED.toString(),
- goog.fx.DragListGroup.EventType.BEFOREDRAGSTART.toString(),
- goog.fx.DragListGroup.EventType.DRAGGERREMOVED.toString()
- ],
- firedEventTypes);
- assertIdle(dlg);
- }
- function testRightClick() {
- goog.testing.events.fireMouseDownEvent(
- list.firstChild, goog.events.BrowserEvent.MouseButton.RIGHT);
- goog.testing.events.fireMouseUpEvent(
- list.firstChild, goog.events.BrowserEvent.MouseButton.RIGHT);
- assertArrayEquals(
- 'only clone events are fired on right click',
- [
- goog.fx.DragListGroup.EventType.DRAGGERCREATED.toString(),
- goog.fx.DragListGroup.EventType.DRAGGERREMOVED.toString()
- ],
- firedEventTypes);
- assertIdle(dlg);
- }
- /**
- * Tests that a new item can be added to a drag list after the control has
- * been initialized.
- */
- function testAddItemToDragList() {
- var item = goog.dom.createDom(
- goog.dom.TagName.DIV, null, goog.dom.createTextNode('newItem'));
- dlg.addItemToDragList(list, item);
- assertEquals(item, list.lastChild);
- assertEquals(4, goog.dom.getChildren(list).length);
- goog.events.listen(
- dlg, goog.fx.DragListGroup.EventType.BEFOREDRAGSTART,
- goog.events.Event.preventDefault);
- goog.testing.events.fireMouseDownEvent(item);
- assertArrayEquals(
- 'Should fire beforedragstart event when clicked',
- [
- goog.fx.DragListGroup.EventType.DRAGGERCREATED.toString(),
- goog.fx.DragListGroup.EventType.BEFOREDRAGSTART.toString(),
- goog.fx.DragListGroup.EventType.DRAGGERREMOVED.toString()
- ],
- firedEventTypes);
- }
- /**
- * Tests that a new item added to a drag list after the control has been
- * initialized is inserted at the correct position.
- */
- function testInsertItemInDragList() {
- var item = goog.dom.createDom(
- goog.dom.TagName.DIV, null, goog.dom.createTextNode('newItem'));
- dlg.addItemToDragList(list, item, 0);
- assertEquals(item, list.firstChild);
- assertEquals(4, goog.dom.getChildren(list).length);
- goog.events.listen(
- dlg, goog.fx.DragListGroup.EventType.BEFOREDRAGSTART,
- goog.events.Event.preventDefault);
- goog.testing.events.fireMouseDownEvent(item);
- assertArrayEquals(
- 'Should fire beforedragstart event when clicked',
- [
- goog.fx.DragListGroup.EventType.DRAGGERCREATED.toString(),
- goog.fx.DragListGroup.EventType.BEFOREDRAGSTART.toString(),
- goog.fx.DragListGroup.EventType.DRAGGERREMOVED.toString()
- ],
- firedEventTypes);
- }
|