File: draglistgroup_test.js

package info (click to toggle)
aseba-plugin-blockly 20180211%2Bgit-2
  • links: PTS
  • area: non-free
  • in suites: buster
  • size: 64,472 kB
  • sloc: xml: 7,976; python: 2,314; sh: 261; lisp: 24; makefile: 10
file content (488 lines) | stat: -rw-r--r-- 16,721 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// 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);
}