aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/submenu_test.html
blob: 71e5bb413720735d33e213020c4d171f0dfb7091 (plain)
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--

  @author nicksantos@google.com (Nick Santos)
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.ui.SubMenu</title>
<style type='text/css'>
.goog-menu {
  position: absolute;
  color: #000;
  border: 1px solid #B5B6B5;
  background-color: #F3F3F7;
  cursor: default;
  font: normal small arial, helvetica, sans-serif;
  margin: 0;
  padding: 0;
  outline: none;
}

.goog-menuitem {
  padding: 2px 1.5em 2px 5px;
  margin: 0;
  list-style: none;
}

.goog-menuitem-rtl {
  padding: 2px 5px 2px 1.5em;
}

.goog-submenu-arrow {
  text-align: right;
  position: absolute;
  right: 0;
  left: auto;
}

.goog-menuitem-rtl .goog-submenu-arrow {
  text-align: left;
  position: absolute;
  left: 0;
  right: auto;
}

.goog-menuitem-disabled .goog-submenu-arrow {
  display: none;
}
</style>
<script src="../base.js"></script>
<script>
  goog.require('goog.dom');
  goog.require('goog.dom.classes');
  goog.require('goog.events');
  goog.require('goog.positioning');
  goog.require('goog.positioning.Overflow');
  goog.require('goog.style');
  goog.require('goog.testing.MockClock');
  goog.require('goog.testing.events');
  goog.require('goog.testing.jsunit');
  goog.require('goog.ui.Component.EventType');
  goog.require('goog.ui.Menu');
  goog.require('goog.ui.MenuItem');
  goog.require('goog.ui.Popup');
  goog.require('goog.ui.SubMenu');
  goog.require('goog.ui.SubMenuRenderer');


</script>
</head>
<body>
<script>

var menu;
var clonedMenuDom;

var mockClock;

// mock out goog.positioning.positionAtCoordinate so that
// the menu always fits. (we don't care about testing the
// dynamic menu positioning if the menu doesn't fit in the window.)
var oldPositionFn = goog.positioning.positionAtCoordinate;
goog.positioning.positionAtCoordinate = function(absolutePos, movableElement,
                                                 movableElementCorner,
                                                 opt_margin, opt_overflow) {
  return oldPositionFn.call(null, absolutePos, movableElement,
      movableElementCorner, opt_margin, goog.positioning.Overflow.IGNORE);
};

function setUp() {
  clonedMenuDom = goog.dom.getElement('demoMenu').cloneNode(true);

  menu = new goog.ui.Menu();
}

function tearDown() {
  document.body.style.direction = 'ltr';
  menu.dispose();

  var element = goog.dom.getElement('demoMenu');
  element.parentNode.replaceChild(clonedMenuDom, element);

  goog.dom.getElement('sandbox').innerHTML = '';

  if (mockClock) {
    mockClock.uninstall();
    mockClock = null;
  }
}

function assertKeyHandlingIsCorrect(keyToOpenSubMenu, keyToCloseSubMenu) {
  menu.setFocusable(true);
  menu.decorate(goog.dom.getElement('demoMenu'));

  var KeyCodes = goog.events.KeyCodes;
  var plainItem = menu.getChildAt(0);
  plainItem.setMnemonic(KeyCodes.F);

  var subMenuItem1 = menu.getChildAt(1);
  subMenuItem1.setMnemonic(KeyCodes.S);
  var subMenuItem1Menu = subMenuItem1.getMenu();
  menu.setHighlighted(plainItem);

  var fireKeySequence = goog.testing.events.fireKeySequence;

  assertTrue(
      'Expected OpenSubMenu key to not be handled',
      fireKeySequence(plainItem.getElement(), keyToOpenSubMenu));
  assertFalse(subMenuItem1Menu.isVisible());

  assertFalse(
      'Expected F key to be handled',
      fireKeySequence(plainItem.getElement(), KeyCodes.F));

  assertFalse(
      'Expected DOWN key to be handled',
      fireKeySequence(plainItem.getElement(), KeyCodes.DOWN));
  assertEquals(subMenuItem1, menu.getChildAt(1));

  assertFalse(
      'Expected OpenSubMenu key to be handled',
      fireKeySequence(subMenuItem1.getElement(), keyToOpenSubMenu));
  assertTrue(subMenuItem1Menu.isVisible());

  assertFalse(
      'Expected CloseSubMenu key to be handled',
      fireKeySequence(subMenuItem1.getElement(), keyToCloseSubMenu));
  assertFalse(subMenuItem1Menu.isVisible());

  assertFalse(
      'Expected UP key to be handled',
      fireKeySequence(subMenuItem1.getElement(), KeyCodes.UP));

  assertFalse(
      'Expected S key to be handled',
      fireKeySequence(plainItem.getElement(), KeyCodes.S));
  assertTrue(subMenuItem1Menu.isVisible());
}

function testKeyHandling_ltr() {
  assertKeyHandlingIsCorrect(goog.events.KeyCodes.RIGHT,
      goog.events.KeyCodes.LEFT);
}

function testKeyHandling_rtl() {
  document.body.style.direction = 'rtl';
  assertKeyHandlingIsCorrect(goog.events.KeyCodes.LEFT,
      goog.events.KeyCodes.RIGHT);
}

function testNormalLtrSubMenu() {
  menu.decorate(goog.dom.getElement('demoMenu'));
  var subMenu = menu.getChildAt(1);
  assertArrowDirection(subMenu, false);
  assertRenderDirection(subMenu, false);
  assertArrowPosition(subMenu, false);
}

function testNormalRtlSubMenu() {
  document.body.style.direction = 'rtl';
  menu.decorate(goog.dom.getElement('demoMenu'));
  var subMenu = menu.getChildAt(1);
  assertArrowDirection(subMenu, true);
  assertRenderDirection(subMenu, true);
  assertArrowPosition(subMenu, true);
}

function testLtrSubMenuAlignedToStart() {
  menu.decorate(goog.dom.getElement('demoMenu'));
  var subMenu = menu.getChildAt(1);
  subMenu.setAlignToEnd(false);
  assertArrowDirection(subMenu, true);
  assertRenderDirection(subMenu, true);
  assertArrowPosition(subMenu, false);
}

function testNullContentElement() {
  var subMenu = new goog.ui.SubMenu();
  subMenu.setContent('demo');
}

function testRtlSubMenuAlignedToStart() {
  document.body.style.direction = 'rtl';
  menu.decorate(goog.dom.getElement('demoMenu'));
  var subMenu = menu.getChildAt(1);
  subMenu.setAlignToEnd(false);
  assertArrowDirection(subMenu, false);
  assertRenderDirection(subMenu, false);
  assertArrowPosition(subMenu, true);
}

function testSetContentKeepsArrow_ltr() {
  document.body.style.direction = 'ltr';
  menu.decorate(goog.dom.getElement('demoMenu'));
  var subMenu = menu.getChildAt(1);
  subMenu.setAlignToEnd(false);
  subMenu.setContent('test');
  assertArrowDirection(subMenu, true);
  assertRenderDirection(subMenu, true);
  assertArrowPosition(subMenu, false);
}

function testSetContentKeepsArrow_rtl() {
  document.body.style.direction = 'rtl';
  menu.decorate(goog.dom.getElement('demoMenu'));
  var subMenu = menu.getChildAt(1);
  subMenu.setAlignToEnd(false);
  subMenu.setContent('test');
  assertArrowDirection(subMenu, false);
  assertRenderDirection(subMenu, false);
  assertArrowPosition(subMenu, true);
}

function testExitDocument() {
  menu.decorate(goog.dom.getElement('demoMenu'));
  var subMenu = menu.getChildAt(1);
  var innerMenu = subMenu.getMenu();

  assertTrue('Top-level menu was not in document', menu.isInDocument());
  assertTrue('Submenu was not in document', subMenu.isInDocument());
  assertTrue('Inner menu was not in document', innerMenu.isInDocument());

  menu.exitDocument();

  assertFalse('Top-level menu was in document', menu.isInDocument());
  assertFalse('Submenu was in document', subMenu.isInDocument());
  assertFalse('Inner menu was in document', innerMenu.isInDocument());
}

function testDisposal() {
  menu.decorate(goog.dom.getElement('demoMenu'));
  var subMenu = menu.getChildAt(1);
  var innerMenu = subMenu.getMenu();
  menu.dispose();

  assert('Top-level menu was not disposed', menu.getDisposed());
  assert('Submenu was not disposed', subMenu.getDisposed());
  assert('Inner menu was not disposed', innerMenu.getDisposed());
}

function testShowAndDismissSubMenu() {
  var openEventDispatched = false;
  var closeEventDispatched = false;

  function handleEvent(e) {
    switch (e.type) {
      case goog.ui.Component.EventType.OPEN:
        openEventDispatched = true;
        break;
      case goog.ui.Component.EventType.CLOSE:
        closeEventDispatched = true;
        break;
      default:
        fail('Invalid event type: ' + e.type);
    }
  }

  menu.decorate(goog.dom.getElement('demoMenu'));
  var subMenu = menu.getChildAt(1);
  subMenu.setHighlighted(true);

  goog.events.listen(subMenu, [
    goog.ui.Component.EventType.OPEN,
    goog.ui.Component.EventType.CLOSE
  ], handleEvent);

  assertFalse('Submenu must not have "-open" CSS class',
      goog.dom.classes.has(subMenu.getElement(), 'goog-submenu-open'));
  assertFalse('Popup menu must not be visible',
      subMenu.getMenu().isVisible());
  assertFalse('No OPEN event must have been dispatched', openEventDispatched);
  assertFalse('No CLOSE event must have been dispatched', closeEventDispatched);

  subMenu.showSubMenu();
  assertTrue('Submenu must have "-open" CSS class',
      goog.dom.classes.has(subMenu.getElement(), 'goog-submenu-open'));
  assertTrue('Popup menu must be visible',
      subMenu.getMenu().isVisible());
  assertTrue('OPEN event must have been dispatched', openEventDispatched);
  assertFalse('No CLOSE event must have been dispatched', closeEventDispatched);

  subMenu.dismissSubMenu();
  assertFalse('Submenu must not have "-open" CSS class',
      goog.dom.classes.has(subMenu.getElement(), 'goog-submenu-open'));
  assertFalse('Popup menu must not be visible',
      subMenu.getMenu().isVisible());
  assertTrue('CLOSE event must have been dispatched', closeEventDispatched);

  goog.events.unlisten(subMenu, [
    goog.ui.Component.EventType.OPEN,
    goog.ui.Component.EventType.CLOSE
  ], handleEvent);
}

function testLazyInstantiateSubMenu() {
  menu.decorate(goog.dom.getElement('demoMenu'));
  var subMenu = menu.getChildAt(1);
  subMenu.setHighlighted(true);

  var lazyMenu;

  var key = goog.events.listen(subMenu, goog.ui.Component.EventType.OPEN,
      function(e) {
        lazyMenu = new goog.ui.Menu();
        lazyMenu.addItem(new goog.ui.MenuItem('foo'));
        lazyMenu.addItem(new goog.ui.MenuItem('bar'));
        subMenu.setMenu(lazyMenu, /* opt_internal */ false);
      });

  subMenu.showSubMenu();
  assertNotNull('Popup menu must have been created', lazyMenu);
  assertEquals('Popup menu must be a child of the submenu', subMenu,
      lazyMenu.getParent());
  assertTrue('Popup menu must have been rendered', lazyMenu.isInDocument());
  assertTrue('Popup menu must be visible', lazyMenu.isVisible());

  menu.dispose();
  assertTrue('Submenu must have been disposed of', subMenu.isDisposed());
  assertFalse('Popup menu must not have been disposed of',
      lazyMenu.isDisposed());

  lazyMenu.dispose();

  goog.events.unlistenByKey(key);
}

function testReusableMenu() {
  var subMenuOne = new goog.ui.SubMenu('SubMenu One');
  var subMenuTwo = new goog.ui.SubMenu('SubMenu Two');
  menu.addItem(subMenuOne);
  menu.addItem(subMenuTwo);
  menu.render(goog.dom.getElement('sandbox'));

  // It is possible for the same popup menu to be shared between different
  // submenus.
  var sharedMenu = new goog.ui.Menu();
  sharedMenu.addItem(new goog.ui.MenuItem('Hello'));
  sharedMenu.addItem(new goog.ui.MenuItem('World'));

  assertNull('Shared menu must not have a parent', sharedMenu.getParent());

  subMenuOne.setMenu(sharedMenu);
  assertEquals('SubMenuOne must point to the shared menu', sharedMenu,
      subMenuOne.getMenu());
  assertEquals('SubMenuOne must be the shared menu\'s parent', subMenuOne,
      sharedMenu.getParent());

  subMenuTwo.setMenu(sharedMenu);
  assertEquals('SubMenuTwo must point to the shared menu', sharedMenu,
      subMenuTwo.getMenu());
  assertEquals('SubMenuTwo must be the shared menu\'s parent', subMenuTwo,
      sharedMenu.getParent());
  assertEquals('SubMenuOne must still point to the shared menu', sharedMenu,
      subMenuOne.getMenu());

  menu.setHighlighted(subMenuOne);
  subMenuOne.showSubMenu();
  assertEquals('SubMenuOne must point to the shared menu', sharedMenu,
      subMenuOne.getMenu());
  assertEquals('SubMenuOne must be the shared menu\'s parent', subMenuOne,
      sharedMenu.getParent());
  assertEquals('SubMenuTwo must still point to the shared menu', sharedMenu,
      subMenuTwo.getMenu());
  assertTrue('Shared menu must be visible', sharedMenu.isVisible());

  menu.setHighlighted(subMenuTwo);
  subMenuTwo.showSubMenu();
  assertEquals('SubMenuTwo must point to the shared menu', sharedMenu,
      subMenuTwo.getMenu());
  assertEquals('SubMenuTwo must be the shared menu\'s parent', subMenuTwo,
      sharedMenu.getParent());
  assertEquals('SubMenuOne must still point to the shared menu', sharedMenu,
      subMenuOne.getMenu());
  assertTrue('Shared menu must be visible', sharedMenu.isVisible());
}

/**
 * If you remove a submenu in the interval between when a mouseover event
 * is fired on it, and showSubMenu() is called, showSubMenu causes a null
 * value to be dereferenced. This test validates that the fix for this works.
 * (See bug 1823144).
 */
function testDeleteItemDuringSubmenuDisplayInterval() {
  mockClock = new goog.testing.MockClock(true);

  var submenu = new goog.ui.SubMenu('submenu');
  submenu.addItem(new goog.ui.MenuItem("submenu item 1"));
  menu.addItem(submenu);

  // Trigger mouseover, and remove item before showSubMenu can be called.
  var e = new goog.events.Event();
  submenu.handleMouseOver(e);
  menu.removeItem(submenu);
  mockClock.tick(goog.ui.SubMenu.MENU_DELAY_MS);
  // (No JS error should occur.)
}

function testShowSubMenuAfterRemoval() {
  var submenu = new goog.ui.SubMenu('submenu');
  menu.addItem(submenu);
  menu.removeItem(submenu);
  submenu.showSubMenu();
  // (No JS error should occur.)
}

function testSubmenuSelectable() {
  var submenu = new goog.ui.SubMenu('submenu');
  submenu.addItem(new goog.ui.MenuItem('submenu item 1'));
  menu.addItem(submenu);
  submenu.setSelectable(true);

  var numClicks = 0;
  var menuClickedFn = function(e) {
    numClicks++;
  }

  goog.events.listen(submenu, goog.ui.Component.EventType.ACTION,
      menuClickedFn);
  submenu.performActionInternal(null);
  submenu.performActionInternal(null);

  assertEquals('The submenu should have fired an event', 2, numClicks);

  submenu.setSelectable(false);
  submenu.performActionInternal(null);

  assertEquals('The submenu should not have fired any further events', 2,
      numClicks);
}

/**
 * Asserts that this sub menu renders in the right direction relative to
 * the parent menu.
 * @param {goog.ui.SubMenu} subMenu The sub menu.
 * @param {boolean} left True for left-pointing, false for right-pointing.
 */
function assertRenderDirection(subMenu, left) {
  subMenu.getParent().setHighlighted(subMenu);
  subMenu.showSubMenu();
  var menuItemPosition = goog.style.getPageOffset(subMenu.getElement());
  var menuPosition = goog.style.getPageOffset(subMenu.getMenu().getElement());
  assert(Math.abs(menuItemPosition.y - menuPosition.y) < 5);
  assertEquals(
      "Menu at: " + menuPosition.x +
      ", submenu item at: " + menuItemPosition.x,
      left, menuPosition.x < menuItemPosition.x);
}

/**
 * Asserts that this sub menu has a properly-oriented arrow.
 * @param {goog.ui.SubMenu} subMenu The sub menu.
 * @param {boolean} left True for left-pointing, false for right-pointing.
 */
function assertArrowDirection(subMenu, left) {
  assertEquals(
      left ? goog.ui.SubMenuRenderer.LEFT_ARROW_ :
      goog.ui.SubMenuRenderer.RIGHT_ARROW_,
      getArrowElement(subMenu).innerHTML);
}

/**
 * Asserts that the arrow position is correct.
 * @param {goog.ui.SubMenu} subMenu The sub menu.
 * @param {boolean} leftAlign True for left-aligned, false for right-aligned.
 */
function assertArrowPosition(subMenu, left) {
  var arrow = getArrowElement(subMenu);
  var expectedLeft =
      left ? 0 : arrow.offsetParent.offsetWidth - arrow.offsetWidth;
  var actualLeft = arrow.offsetLeft;
  assertTrue('Expected left offset: ' + expectedLeft + '\n' +
             'Actual left offset: ' + actualLeft + '\n',
             Math.abs(expectedLeft - actualLeft) < 5);
}

/**
 * Gets the arrow element of a sub menu.
 * @param {goog.ui.SubMenu} subMenu The sub menu.
 * @return {Element} The arrow.
 */
function getArrowElement(subMenu) {
  return subMenu.getContentElement().lastChild;
}

</script>

<p>
  Here's a menu (with submenus) defined in markup:
</p>
<div id="demoMenu" class="goog-menu">
  <div class="goog-menuitem">Open...</div>
  <div class="goog-submenu">Open Recent
    <div class="goog-menu">
      <div class="goog-menuitem">Annual Report.pdf</div>
      <div class="goog-menuitem">Quarterly Update.pdf</div>
      <div class="goog-menuitem">Enemies List.txt</div>
      <div class="goog-submenu">More
        <div class="goog-menu">
          <div class="goog-menuitem">Foo.txt</div>
          <div class="goog-menuitem">Bar.txt</div>
        </div>
      </div>
    </div>
  </div>
</div>
<div id="sandbox"></div>

</body>
</html>