Debian Codegraph @

packages/widgets/tests/src/menubar.spec.ts

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3 /*-----------------------------------------------------------------------------
4 | Copyright (c) 2014-2017, PhosphorJS Contributors
5 |
6 | Distributed under the terms of the BSD 3-Clause License.
7 |
8 | The full license is in the file LICENSE, distributed with this software.
9 |----------------------------------------------------------------------------*/
10 import { expect } from 'chai';
11
12 import { JSONObject } from '@lumino/coreutils';
13
14 import { CommandRegistry } from '@lumino/commands';
15
16 import { DisposableSet } from '@lumino/disposable';
17
18 import { Message, MessageLoop } from '@lumino/messaging';
19
20 import { VirtualDOM, VirtualElement } from '@lumino/virtualdom';
21
22 import { Menu, MenuBar, Widget } from '@lumino/widgets';
23
24 class LogMenuBar extends MenuBar {
25 events: string[] = [];
26
27 methods: string[] = [];
28
29 handleEvent(event: Event): void {
30 super.handleEvent(event);
31 this.events.push(event.type);
32 }
33
34 protected onBeforeAttach(msg: Message): void {
35 super.onBeforeAttach(msg);
36 this.methods.push('onBeforeAttach');
37 }
38
39 protected onAfterDetach(msg: Message): void {
40 super.onAfterDetach(msg);
41 this.methods.push('onAfterDetach');
42 }
43
44 protected onUpdateRequest(msg: Message): void {
45 super.onUpdateRequest(msg);
46 this.methods.push('onUpdateRequest');
47 }
48 }
49
50 const bubbles = true;
51 const cancelable = true;
52
53 describe('@lumino/widgets', () => {
54 const DEFAULT_CMD = 'menubar.spec.ts:defaultCmd';
55
56 const disposables = new DisposableSet();
57
58 let commands: CommandRegistry;
59
60 function createMenu() {
61 let menu = new Menu({ commands });
62 // Several tests rely on this function returning a non-empty menu
63 menu.addItem({ command: DEFAULT_CMD });
64 return menu;
65 }
66
67 function createMenuBar(options?: MenuBar.IOptions): MenuBar {
68 let bar = new MenuBar(options);
69 for (let i = 0; i < 3; i++) {
70 let menu = createMenu();
71 menu.title.label = `Menu${i}`;
72 menu.title.mnemonic = 4;
73 bar.addMenu(menu);
74 }
75 bar.activeIndex = 0;
76 Widget.attach(bar, document.body);
77 // Force an update.
78 MessageLoop.sendMessage(bar, Widget.Msg.UpdateRequest);
79 return bar;
80 }
81
82 /**
83 * Create a MenuBar that has no active menu item.
84 */
85 function createUnfocusedMenuBar(): MenuBar {
86 const bar = createMenuBar();
87 bar.activeIndex = -1;
88 return bar;
89 }
90
91 before(() => {
92 commands = new CommandRegistry();
93 const iconRenderer = {
94 render: (host: HTMLElement, options?: any) => {
95 const renderNode = document.createElement('div');
96 renderNode.className = 'foo';
97 host.appendChild(renderNode);
98 }
99 };
100 let cmd = commands.addCommand(DEFAULT_CMD, {
101 execute: (args: JSONObject) => {
102 return args;
103 },
104 label: 'LABEL',
105 icon: iconRenderer,
106 className: 'bar',
107 isToggled: (args: JSONObject) => {
108 return true;
109 },
110 mnemonic: 1
111 });
112 let kbd = commands.addKeyBinding({
113 keys: ['A'],
114 selector: '*',
115 command: DEFAULT_CMD
116 });
117 disposables.add(cmd);
118 disposables.add(kbd);
119 });
120
121 after(() => {
122 disposables.dispose();
123 });
124
125 describe('MenuBar', () => {
126 describe('#constructor()', () => {
127 it('should take no arguments', () => {
128 let bar = new MenuBar();
129 expect(bar).to.be.an.instanceof(MenuBar);
130 bar.dispose();
131 });
132
133 it('should take options for initializing the menu bar', () => {
134 let renderer = new MenuBar.Renderer();
135 let bar = new MenuBar({ renderer });
136 expect(bar).to.be.an.instanceof(MenuBar);
137 bar.dispose();
138 });
139
140 it('should accept only isVisible option', () => {
141 const bar = new MenuBar({
142 overflowMenuOptions: { isVisible: false }
143 });
144
145 expect(bar).to.be.an.instanceOf(MenuBar);
146
147 bar.dispose();
148 });
149
150 it('should add the `lm-MenuBar` class', () => {
151 let bar = new MenuBar();
152 expect(bar.hasClass('lm-MenuBar')).to.equal(true);
153 bar.dispose();
154 });
155 });
156
157 describe('#dispose()', () => {
158 it('should dispose of the resources held by the menu bar', () => {
159 let bar = new MenuBar();
160 bar.addMenu(new Menu({ commands }));
161 bar.dispose();
162 expect(bar.isDisposed).to.equal(true);
163 bar.dispose();
164 expect(bar.isDisposed).to.equal(true);
165 });
166 });
167
168 describe('#renderer', () => {
169 it('should get the renderer for the menu bar', () => {
170 let renderer = Object.create(MenuBar.defaultRenderer);
171 let bar = new MenuBar({ renderer });
172 expect(bar.renderer).to.equal(renderer);
173 bar.dispose();
174 });
175 });
176
177 describe('#childMenu', () => {
178 it('should get the child menu of the menu bar', () => {
179 let bar = new MenuBar();
180 let menu = createMenu();
181 bar.addMenu(menu);
182 bar.activeIndex = 0;
183 bar.openActiveMenu();
184 expect(bar.childMenu).to.equal(menu);
185 bar.dispose();
186 });
187
188 it('should be `null` if there is no open menu', () => {
189 let bar = new MenuBar();
190 let menu = new Menu({ commands });
191 bar.addMenu(menu);
192 bar.activeIndex = 0;
193 expect(bar.childMenu).to.equal(null);
194 bar.dispose();
195 });
196 });
197
198 describe('#contentNode', () => {
199 it('should get the menu content node', () => {
200 let bar = new MenuBar();
201 let content = bar.contentNode;
202 expect(content.classList.contains('lm-MenuBar-content')).to.equal(true);
203 bar.dispose();
204 });
205 });
206
207 describe('#activeMenu', () => {
208 it('should get the active menu of the menu bar', () => {
209 let bar = new MenuBar();
210 let menu = createMenu();
211 bar.addMenu(menu);
212 bar.activeIndex = 0;
213 expect(bar.activeMenu).to.equal(menu);
214 bar.dispose();
215 });
216
217 it('should be `null` if there is no active menu', () => {
218 let bar = new MenuBar();
219 let menu = new Menu({ commands });
220 bar.addMenu(menu);
221 expect(bar.activeMenu).to.equal(null);
222 bar.dispose();
223 });
224
225 it('should set the currently active menu', () => {
226 let bar = new MenuBar();
227 let menu = createMenu();
228 bar.addMenu(menu);
229 bar.activeMenu = menu;
230 expect(bar.activeMenu).to.equal(menu);
231 bar.dispose();
232 });
233
234 it('should set to `null` if the menu is not in the menu bar', () => {
235 let bar = new MenuBar();
236 let menu = new Menu({ commands });
237 bar.activeMenu = menu;
238 expect(bar.activeMenu).to.equal(null);
239 bar.dispose();
240 });
241 });
242
243 describe('#activeIndex', () => {
244 it('should get the index of the currently active menu', () => {
245 let bar = new MenuBar();
246 let menu = createMenu();
247 bar.addMenu(menu);
248 bar.activeMenu = menu;
249 expect(bar.activeIndex).to.equal(0);
250 bar.dispose();
251 });
252
253 it('should be `-1` if no menu is active', () => {
254 let bar = new MenuBar();
255 let menu = new Menu({ commands });
256 bar.addMenu(menu);
257 expect(bar.activeIndex).to.equal(-1);
258 bar.dispose();
259 });
260
261 it('should set the index of the currently active menu', () => {
262 let bar = new MenuBar();
263 let menu = createMenu();
264 bar.addMenu(menu);
265 bar.activeIndex = 0;
266 expect(bar.activeIndex).to.equal(0);
267 bar.dispose();
268 });
269
270 it('should set to `-1` if the index is out of range', () => {
271 let bar = new MenuBar();
272 let menu = new Menu({ commands });
273 bar.addMenu(menu);
274 bar.activeIndex = -2;
275 expect(bar.activeIndex).to.equal(-1);
276 bar.activeIndex = 1;
277 expect(bar.activeIndex).to.equal(-1);
278 bar.dispose();
279 });
280
281 it('should add `lm-mod-active` to the active node', () => {
282 let bar = createMenuBar();
283 let node = bar.contentNode.firstChild as HTMLElement;
284 expect(node.classList.contains('lm-mod-active')).to.equal(true);
285 expect(bar.activeIndex).to.equal(0);
286 bar.dispose();
287 });
288
289 it('should set to `-1` if menu at index is empty', () => {
290 let bar = createMenuBar();
291 let emptyMenu = new Menu({ commands });
292 bar.insertMenu(1, emptyMenu);
293 bar.activeIndex = 1;
294 expect(bar.activeIndex).to.equal(-1);
295 bar.dispose();
296 });
297 });
298
299 describe('#menus', () => {
300 it('should get a read-only array of the menus in the menu bar', () => {
301 let bar = new MenuBar();
302 let menu0 = new Menu({ commands });
303 let menu1 = new Menu({ commands });
304 bar.addMenu(menu0);
305 bar.addMenu(menu1);
306 let menus = bar.menus;
307 expect(menus.length).to.equal(2);
308 expect(menus[0]).to.equal(menu0);
309 expect(menus[1]).to.equal(menu1);
310 bar.dispose();
311 });
312 });
313
314 describe('#openActiveMenu()', () => {
315 it('should open the active menu and activate its first menu item', () => {
316 let bar = new MenuBar();
317 let menu = new Menu({ commands });
318 let item = menu.addItem({ command: DEFAULT_CMD });
319 menu.addItem(item);
320 bar.addMenu(menu);
321 bar.activeMenu = menu;
322 bar.openActiveMenu();
323 expect(menu.isAttached).to.equal(true);
324 expect(menu.activeItem!.command).to.equal(item.command);
325 bar.dispose();
326 });
327
328 it('should be a no-op if there is no active menu', () => {
329 let bar = new MenuBar();
330 let menu = new Menu({ commands });
331 let item = menu.addItem({ command: DEFAULT_CMD });
332 menu.addItem(item);
333 bar.addMenu(menu);
334 bar.openActiveMenu();
335 expect(menu.isAttached).to.equal(false);
336 bar.dispose();
337 });
338
339 it('should be a no-op if the active menu is empty', () => {
340 let bar = new MenuBar();
341 let menu = new Menu({ commands });
342 bar.addMenu(menu);
343 bar.activeMenu = menu;
344 bar.openActiveMenu();
345 expect(menu.isAttached).to.equal(false);
346 bar.dispose();
347 });
348 });
349
350 describe('#addMenu()', () => {
351 it('should add a menu to the end of the menu bar', () => {
352 let bar = new MenuBar();
353 let menu = new Menu({ commands });
354 let item = menu.addItem({ command: DEFAULT_CMD });
355 menu.addItem(item);
356 bar.addMenu(new Menu({ commands }));
357 bar.addMenu(menu);
358 expect(bar.menus.length).to.equal(2);
359 expect(bar.menus[1]).to.equal(menu);
360 bar.dispose();
361 });
362
363 it('should move an existing menu to the end', () => {
364 let bar = new MenuBar();
365 let menu = new Menu({ commands });
366 let item = menu.addItem({ command: DEFAULT_CMD });
367 menu.addItem(item);
368 bar.addMenu(menu);
369 bar.addMenu(new Menu({ commands }));
370 bar.addMenu(menu);
371 expect(bar.menus.length).to.equal(2);
372 expect(bar.menus[1]).to.equal(menu);
373 bar.dispose();
374 });
375 });
376
377 describe('#insertMenu()', () => {
378 it('should insert a menu into the menu bar at the specified index', () => {
379 let bar = new MenuBar();
380 let menu = new Menu({ commands });
381 bar.addMenu(new Menu({ commands }));
382 bar.insertMenu(0, menu);
383 expect(bar.menus.length).to.equal(2);
384 expect(bar.menus[0]).to.equal(menu);
385 bar.dispose();
386 });
387
388 it('should clamp the index to the bounds of the menus', () => {
389 let bar = new MenuBar();
390 let menu = new Menu({ commands });
391 bar.addMenu(new Menu({ commands }));
392 bar.insertMenu(-1, menu);
393 expect(bar.menus.length).to.equal(2);
394 expect(bar.menus[0]).to.equal(menu);
395
396 menu = new Menu({ commands });
397 bar.insertMenu(10, menu);
398 expect(bar.menus.length).to.equal(3);
399 expect(bar.menus[2]).to.equal(menu);
400
401 bar.dispose();
402 });
403
404 it('should move an existing menu', () => {
405 let bar = new MenuBar();
406 let menu = new Menu({ commands });
407 bar.addMenu(new Menu({ commands }));
408 bar.insertMenu(0, menu);
409 bar.insertMenu(10, menu);
410 expect(bar.menus.length).to.equal(2);
411 expect(bar.menus[1]).to.equal(menu);
412 bar.dispose();
413 });
414
415 it('should be a no-op if there is no effective move', () => {
416 let bar = new MenuBar();
417 let menu = new Menu({ commands });
418 bar.addMenu(new Menu({ commands }));
419 bar.insertMenu(0, menu);
420 bar.insertMenu(0, menu);
421 expect(bar.menus.length).to.equal(2);
422 expect(bar.menus[0]).to.equal(menu);
423 bar.dispose();
424 });
425 });
426
427 describe('#removeMenu()', () => {
428 it('should remove a menu from the menu bar by value', () => {
429 let bar = new MenuBar();
430 let menu = new Menu({ commands });
431 bar.addMenu(new Menu({ commands }));
432 bar.addMenu(menu);
433 bar.removeMenu(menu);
434 expect(bar.menus.length).to.equal(1);
435 expect(bar.menus[0]).to.not.equal(menu);
436 bar.dispose();
437 });
438
439 it('should return be a no-op if the menu is not in the menu bar', () => {
440 let bar = new MenuBar();
441 let menu = new Menu({ commands });
442 bar.addMenu(new Menu({ commands }));
443 bar.addMenu(menu);
444 bar.removeMenu(menu);
445 bar.removeMenu(menu);
446 expect(bar.menus.length).to.equal(0);
447 bar.dispose();
448 });
449 });
450
451 describe('#removeMenuAt()', () => {
452 it('should remove a menu from the menu bar by index', () => {
453 let bar = new MenuBar();
454 let menu = new Menu({ commands });
455 bar.addMenu(new Menu({ commands }));
456 bar.addMenu(menu);
457 bar.removeMenuAt(1);
458 expect(bar.menus.length).to.equal(1);
459 expect(bar.menus[0]).to.not.equal(menu);
460 bar.dispose();
461 });
462
463 it('should be a no-op if the index is out of range', () => {
464 let bar = new MenuBar();
465 let menu = new Menu({ commands });
466 bar.addMenu(new Menu({ commands }));
467 bar.addMenu(menu);
468 bar.removeMenuAt(1);
469 bar.removeMenuAt(1);
470 expect(bar.menus.length).to.equal(1);
471 bar.dispose();
472 });
473 });
474
475 describe('#clearMenus()', () => {
476 it('should remove all menus from the menu bar', () => {
477 let bar = new MenuBar();
478 bar.addMenu(new Menu({ commands }));
479 bar.addMenu(new Menu({ commands }));
480 bar.clearMenus();
481 expect(bar.menus).to.eql([]);
482 bar.dispose();
483 });
484
485 it('should be a no-op if there are no menus', () => {
486 let bar = new MenuBar();
487 bar.clearMenus();
488 expect(bar.menus).to.eql([]);
489 bar.dispose();
490 });
491 });
492
493 describe('#handleEvent()', () => {
494 let bar: MenuBar;
495
496 beforeEach(() => {
497 bar = createMenuBar();
498 });
499
500 afterEach(() => {
501 bar.dispose();
502 });
503
504 context('keydown', () => {
505 it('should bail on Tab', () => {
506 let event = new KeyboardEvent('keydown', {
507 cancelable: true,
508 keyCode: 9
509 });
510 bar.node.dispatchEvent(event);
511 expect(event.defaultPrevented).to.equal(false);
512 });
513
514 it('should open the active menu on Enter', () => {
515 let menu = bar.activeMenu!;
516 bar.node.dispatchEvent(
517 new KeyboardEvent('keydown', {
518 bubbles,
519 keyCode: 13
520 })
521 );
522 expect(menu.isAttached).to.equal(true);
523 });
524
525 it('should open the active menu on Space', () => {
526 let menu = bar.activeMenu!;
527 bar.node.dispatchEvent(
528 new KeyboardEvent('keydown', {
529 bubbles,
530 keyCode: 32
531 })
532 );
533 expect(menu.isAttached).to.equal(true);
534 });
535
536 it('should open the active menu on Up Arrow', () => {
537 let menu = bar.activeMenu!;
538 bar.node.dispatchEvent(
539 new KeyboardEvent('keydown', {
540 bubbles,
541 keyCode: 38
542 })
543 );
544 expect(menu.isAttached).to.equal(true);
545 });
546
547 it('should open the active menu on Down Arrow', () => {
548 let menu = bar.activeMenu!;
549 bar.node.dispatchEvent(
550 new KeyboardEvent('keydown', {
551 bubbles,
552 keyCode: 40
553 })
554 );
555 expect(menu.isAttached).to.equal(true);
556 });
557
558 it('should close the active menu on Escape', () => {
559 let menu = bar.activeMenu!;
560 bar.openActiveMenu();
561 bar.node.dispatchEvent(
562 new KeyboardEvent('keydown', {
563 bubbles,
564 keyCode: 27
565 })
566 );
567 expect(menu.isAttached).to.equal(false);
568 expect(menu.activeIndex).to.equal(-1);
569 expect(menu.node.contains(document.activeElement)).to.equal(false);
570 });
571
572 it('should activate the previous menu on Left Arrow', () => {
573 bar.node.dispatchEvent(
574 new KeyboardEvent('keydown', {
575 bubbles,
576 keyCode: 37
577 })
578 );
579 expect(bar.activeIndex!).to.equal(2);
580 bar.node.dispatchEvent(
581 new KeyboardEvent('keydown', {
582 bubbles,
583 keyCode: 37
584 })
585 );
586 expect(bar.activeIndex!).to.equal(1);
587 });
588
589 it('should skip past empty menu on Left Arrow', () => {
590 let emptyMenu = new Menu({ commands });
591 bar.insertMenu(0, emptyMenu);
592 // Update DOM so we can focus the item node.
593 MessageLoop.flush();
594 // Call .focus() because keyboard shortcuts work from the item that
595 // has focus which may not match `bar.activeIndex`. Focus the item
596 // just to the right of the item that matches the empty menu.
597 (bar.contentNode.children[1] as HTMLElement).focus();
598 bar.node.dispatchEvent(
599 new KeyboardEvent('keydown', {
600 bubbles,
601 keyCode: 37
602 })
603 );
604 expect(bar.activeIndex).to.equal(bar.menus.length - 1);
605 });
606
607 it('should activate the next menu on Right Arrow', () => {
608 bar.node.dispatchEvent(
609 new KeyboardEvent('keydown', {
610 bubbles,
611 keyCode: 39
612 })
613 );
614 expect(bar.activeIndex!).to.equal(1);
615 bar.node.dispatchEvent(
616 new KeyboardEvent('keydown', {
617 bubbles,
618 keyCode: 39
619 })
620 );
621 expect(bar.activeIndex!).to.equal(2);
622 bar.node.dispatchEvent(
623 new KeyboardEvent('keydown', {
624 bubbles,
625 keyCode: 39
626 })
627 );
628 expect(bar.activeIndex!).to.equal(0);
629 });
630
631 it('should skip past empty menu on Right Arrow', () => {
632 let emptyMenu = new Menu({ commands });
633 bar.addMenu(emptyMenu);
634 // Update DOM so we can focus the item node.
635 MessageLoop.flush();
636 let emptyIndex = bar.menus.indexOf(emptyMenu);
637 // Call .focus() because keyboard shortcuts work from the item that
638 // has focus which may not match `bar.activeIndex`. Focus the item
639 // just to the left of the item that matches the empty menu.
640 (bar.contentNode.children[emptyIndex - 1] as HTMLElement).focus();
641 bar.node.dispatchEvent(
642 new KeyboardEvent('keydown', {
643 bubbles,
644 keyCode: 39
645 })
646 );
647 expect(bar.activeIndex).to.equal(0);
648 });
649
650 it('should open the menu matching a mnemonic', () => {
651 bar.node.dispatchEvent(
652 new KeyboardEvent('keydown', {
653 bubbles,
654 keyCode: 97 // `1` key
655 })
656 );
657 expect(bar.activeIndex!).to.equal(1);
658 let menu = bar.activeMenu!;
659 expect(menu.isAttached).to.equal(true);
660 });
661
662 it('should select the next menu matching by first letter', () => {
663 bar.activeIndex = 1;
664 bar.node.dispatchEvent(
665 new KeyboardEvent('keydown', {
666 bubbles,
667 keyCode: 77 // `M` key
668 })
669 );
670 expect(bar.activeIndex!).to.equal(1);
671 let menu = bar.activeMenu!;
672 expect(menu.isAttached).to.equal(false);
673 });
674
675 it('should select the first menu matching the mnemonic', () => {
676 let menu = new Menu({ commands });
677 menu.title.label = 'Test1';
678 menu.title.mnemonic = 4;
679 bar.addMenu(menu);
680 bar.node.dispatchEvent(
681 new KeyboardEvent('keydown', {
682 bubbles,
683 keyCode: 97 // `1` key
684 })
685 );
686 expect(bar.activeIndex).to.equal(1);
687 menu = bar.activeMenu!;
688 expect(menu.isAttached).to.equal(false);
689 });
690
691 it('should select the only menu matching the first letter', () => {
692 let menu = createMenu();
693 menu.title.label = 'Test1';
694 bar.addMenu(menu);
695 bar.addMenu(new Menu({ commands }));
696 bar.node.dispatchEvent(
697 new KeyboardEvent('keydown', {
698 bubbles,
699 keyCode: 84 // `T` key
700 })
701 );
702 expect(bar.activeIndex).to.equal(3);
703 expect(bar.activeMenu).to.equal(menu);
704 expect(menu.isAttached).to.equal(false);
705 });
706 });
707
708 context('mousedown', () => {
709 it('should bail if the mouse press was not on the menu bar', () => {
710 let event = new MouseEvent('mousedown', {
711 bubbles,
712 cancelable: true,
713 clientX: -10
714 });
715 bar.node.dispatchEvent(event);
716 expect(event.defaultPrevented).to.equal(false);
717 });
718
719 it('should close an open menu if the press was not on an item', () => {
720 bar.openActiveMenu();
721 let menu = bar.activeMenu!;
722 bar.node.dispatchEvent(new MouseEvent('mousedown', { bubbles }));
723 expect(bar.activeIndex).to.equal(-1);
724 expect(menu.isAttached).to.equal(false);
725 });
726
727 it('should close an active menu', () => {
728 bar.openActiveMenu();
729 let menu = bar.activeMenu!;
730 let firstItemNode = bar.node.getElementsByClassName(
731 'lm-MenuBar-item'
732 )[0] as HTMLElement;
733 let rect = firstItemNode.getBoundingClientRect();
734 let mouseEvent = new MouseEvent('mousedown', {
735 bubbles,
736 cancelable: true,
737 clientX: rect.left + 1,
738 clientY: rect.top + 1
739 });
740 expect(bar.activeIndex).to.equal(0);
741 bar.node.dispatchEvent(mouseEvent);
742 expect(bar.activeIndex).to.equal(0);
743 expect(menu.isAttached).to.equal(false);
744 // Ensure that mousedown default is not prevented, to allow browser
745 // focus to go to an item that a user clicks in the menu bar.
746 expect(mouseEvent.defaultPrevented).to.equal(false);
747 });
748
749 it('should open an active menu', () => {
750 let menu = bar.activeMenu!;
751 let node = bar.node.getElementsByClassName(
752 'lm-MenuBar-item'
753 )[0] as HTMLElement;
754 let rect = node.getBoundingClientRect();
755 let mouseEvent = new MouseEvent('mousedown', {
756 bubbles,
757 cancelable: true,
758 clientX: rect.left + 1,
759 clientY: rect.top + 1
760 });
761 bar.node.dispatchEvent(mouseEvent);
762 expect(bar.activeIndex).to.equal(0);
763 expect(menu.isAttached).to.equal(true);
764 // When opening a menu, be sure to prevent default during the
765 // mousedown so that the item being clicked in the menu bar does not
766 // "steal" focus from the menu being opened.
767 mouseEvent.preventDefault();
768 expect(mouseEvent.defaultPrevented).to.equal(true);
769 });
770
771 it('should not close an active menu if not a left mouse press', () => {
772 bar.openActiveMenu();
773 let menu = bar.activeMenu!;
774 let node = bar.node.getElementsByClassName(
775 'lm-MenuBar-item'
776 )[0] as HTMLElement;
777 let rect = node.getBoundingClientRect();
778 bar.node.dispatchEvent(
779 new MouseEvent('mousedown', {
780 bubbles,
781 button: 1,
782 clientX: rect.left + 1,
783 clientY: rect.top + 1
784 })
785 );
786 expect(bar.activeIndex).to.equal(0);
787 expect(menu.isAttached).to.equal(true);
788 });
789
790 it('should not work on a menu bar item whose menu is empty', () => {
791 let emptyMenu = new Menu({ commands });
792 // Add title to empty menu, otherwise it will have zero width in the
793 // menu bar, which makes it impossible to test with mousedown.
794 emptyMenu.title.label = 'Empty Menu';
795 bar.insertMenu(0, emptyMenu);
796 let node = bar.node.getElementsByClassName(
797 'lm-MenuBar-item'
798 )[0] as HTMLElement;
799 let rect = node.getBoundingClientRect();
800 bar.node.dispatchEvent(
801 new MouseEvent('mousedown', {
802 bubbles,
803 button: 0,
804 clientX: rect.left + 1,
805 clientY: rect.top + 1
806 })
807 );
808 expect(bar.activeIndex).to.equal(-1);
809 expect(emptyMenu.isAttached).to.equal(false);
810 });
811 });
812
813 context('mousemove', () => {
814 it('should open a new menu if a menu is already open', () => {
815 bar.openActiveMenu();
816 let menu = bar.activeMenu!;
817 let node = bar.node.getElementsByClassName(
818 'lm-MenuBar-item'
819 )[1] as HTMLElement;
820 let rect = node.getBoundingClientRect();
821 bar.node.dispatchEvent(
822 new MouseEvent('mousemove', {
823 bubbles,
824 clientX: rect.left + 1,
825 clientY: rect.top + 1
826 })
827 );
828 expect(bar.activeIndex).to.equal(1);
829 expect(menu.isAttached).to.equal(false);
830 expect(bar.activeMenu!.isAttached).to.equal(true);
831 });
832
833 it('should be a no-op if the active index will not change', () => {
834 bar.openActiveMenu();
835 let menu = bar.activeMenu!;
836 let node = bar.node.getElementsByClassName(
837 'lm-MenuBar-item'
838 )[0] as HTMLElement;
839 let rect = node.getBoundingClientRect();
840 bar.node.dispatchEvent(
841 new MouseEvent('mousemove', {
842 bubbles,
843 clientX: rect.left,
844 clientY: rect.top + 1
845 })
846 );
847 expect(bar.activeIndex).to.equal(0);
848 expect(menu.isAttached).to.equal(true);
849 });
850
851 it('should be a no-op if the mouse is not over an item and there is a menu open', () => {
852 bar.openActiveMenu();
853 let menu = bar.activeMenu!;
854 bar.node.dispatchEvent(new MouseEvent('mousemove', { bubbles }));
855 expect(bar.activeIndex).to.equal(0);
856 expect(menu.isAttached).to.equal(true);
857 });
858 });
859
860 context('focusout', () => {
861 it('should reset the active index if there is no open menu', () => {
862 bar.node.dispatchEvent(new FocusEvent('focusout', { bubbles }));
863 expect(bar.activeIndex).to.equal(-1);
864 });
865
866 it('should be a no-op if there is an open menu', () => {
867 bar.openActiveMenu();
868 let menu = bar.activeMenu!;
869 bar.node.dispatchEvent(new FocusEvent('focusous', { bubbles }));
870 expect(bar.activeIndex).to.equal(0);
871 expect(menu.isAttached).to.equal(true);
872 });
873 });
874
875 context('contextmenu', () => {
876 it('should prevent default', () => {
877 let event = new MouseEvent('contextmenu', { bubbles, cancelable });
878 let cancelled = !bar.node.dispatchEvent(event);
879 expect(cancelled).to.equal(true);
880 });
881 });
882
883 context('focus', () => {
884 it('should lose focus on tab key', () => {
885 let bar = createUnfocusedMenuBar();
886 bar.activate();
887 MessageLoop.flush();
888 expect(bar.contentNode.contains(document.activeElement)).to.equal(
889 true
890 );
891 let event = new KeyboardEvent('keydown', { keyCode: 9, bubbles });
892 bar.contentNode.dispatchEvent(event);
893 expect(bar.activeIndex).to.equal(-1);
894 bar.dispose();
895 });
896
897 it('should lose focus on shift-tab key', () => {
898 let bar = createUnfocusedMenuBar();
899 bar.activate();
900 MessageLoop.flush();
901 expect(bar.contentNode.contains(document.activeElement)).to.equal(
902 true
903 );
904 let event = new KeyboardEvent('keydown', {
905 keyCode: 9,
906 shiftKey: true,
907 bubbles
908 });
909 bar.contentNode.dispatchEvent(event);
910 expect(bar.activeIndex).to.equal(-1);
911 bar.dispose();
912 });
913 });
914 });
915
916 describe('#onBeforeAttach()', () => {
917 it('should add event listeners', () => {
918 let bar = new LogMenuBar();
919 let node = bar.node;
920 Widget.attach(bar, document.body);
921 expect(bar.methods.indexOf('onBeforeAttach')).to.not.equal(-1);
922 node.dispatchEvent(new KeyboardEvent('keydown', { bubbles }));
923 expect(bar.events.indexOf('keydown')).to.not.equal(-1);
924 node.dispatchEvent(new MouseEvent('mousedown', { bubbles }));
925 expect(bar.events.indexOf('mousedown')).to.not.equal(-1);
926 node.dispatchEvent(new MouseEvent('mousemove', { bubbles }));
927 expect(bar.events.indexOf('mousemove')).to.not.equal(-1);
928 node.dispatchEvent(new FocusEvent('focusout', { bubbles }));
929 expect(bar.events.indexOf('focusout')).to.not.equal(-1);
930 node.dispatchEvent(new MouseEvent('contextmenu', { bubbles }));
931 expect(bar.events.indexOf('contextmenu')).to.not.equal(-1);
932 bar.dispose();
933 });
934 });
935
936 describe('#onAfterDetach()', () => {
937 it('should remove event listeners', () => {
938 let bar = new LogMenuBar();
939 let node = bar.node;
940 Widget.attach(bar, document.body);
941 Widget.detach(bar);
942 expect(bar.methods.indexOf('onBeforeAttach')).to.not.equal(-1);
943 node.dispatchEvent(new KeyboardEvent('keydown', { bubbles }));
944 expect(bar.events.indexOf('keydown')).to.equal(-1);
945 node.dispatchEvent(new MouseEvent('mousedown', { bubbles }));
946 expect(bar.events.indexOf('mousedown')).to.equal(-1);
947 node.dispatchEvent(new MouseEvent('mousemove', { bubbles }));
948 expect(bar.events.indexOf('mousemove')).to.equal(-1);
949 node.dispatchEvent(new FocusEvent('focusout', { bubbles }));
950 expect(bar.events.indexOf('focusout')).to.equal(-1);
951 node.dispatchEvent(new MouseEvent('contextmenu', { bubbles }));
952 expect(bar.events.indexOf('contextmenu')).to.equal(-1);
953 bar.dispose();
954 });
955 });
956
957 describe('#onActivateRequest()', () => {
958 it('should be a no-op if not attached', () => {
959 let bar = createMenuBar();
960 Widget.detach(bar);
961 MessageLoop.sendMessage(bar, Widget.Msg.ActivateRequest);
962 expect(bar.contentNode.contains(document.activeElement)).to.equal(
963 false
964 );
965 bar.dispose();
966 });
967
968 it('should focus the node if attached', () => {
969 let bar = createUnfocusedMenuBar();
970 MessageLoop.sendMessage(bar, Widget.Msg.ActivateRequest);
971 expect(
972 bar.contentNode.contains(document.activeElement) &&
973 bar.contentNode !== document.activeElement
974 ).to.equal(true);
975 bar.dispose();
976 });
977 });
978
979 describe('#onUpdateRequest()', () => {
980 it('should be called when the title of a menu changes', () => {
981 let bar = new LogMenuBar();
982 let menu = new Menu({ commands });
983 bar.addMenu(menu);
984 bar.activeIndex = 0;
985 expect(bar.methods).to.not.include('onUpdateRequest');
986 menu.title.label = 'foo';
987 MessageLoop.flush();
988 expect(bar.methods).to.include('onUpdateRequest');
989 bar.dispose();
990 });
991
992 it('should render the content', () => {
993 let bar = new LogMenuBar();
994 let menu = new Menu({ commands });
995 bar.addMenu(menu);
996 expect(bar.contentNode.children.length).to.equal(0);
997 MessageLoop.sendMessage(bar, Widget.Msg.UpdateRequest);
998 let child = bar.contentNode.firstChild as HTMLElement;
999 expect(child.className).to.contain('lm-MenuBar-item');
1000 bar.dispose();
1001 });
1002
1003 it('should render the overflow menu', () => {
1004 let bar = createMenuBar();
1005 expect(bar.overflowIndex).to.equal(-1);
1006 expect(bar.overflowMenu).to.equal(null);
1007 bar.node.style.maxWidth = '70px';
1008 MessageLoop.sendMessage(bar, Widget.Msg.UpdateRequest);
1009 MessageLoop.flush();
1010 expect(bar.overflowMenu).to.not.equal(null);
1011 expect(bar.overflowIndex).to.not.equal(-1);
1012 bar.dispose();
1013 });
1014
1015 it('should hide the overflow menu', () => {
1016 let bar = createMenuBar();
1017 expect(bar.overflowIndex).to.equal(-1);
1018 expect(bar.overflowMenu).to.equal(null);
1019 bar.node.style.maxWidth = '70px';
1020 MessageLoop.sendMessage(bar, Widget.Msg.UpdateRequest);
1021 bar.node.style.maxWidth = '400px';
1022 MessageLoop.sendMessage(bar, Widget.Msg.UpdateRequest);
1023 MessageLoop.flush();
1024 expect(bar.overflowMenu).to.equal(null);
1025 expect(bar.overflowIndex).to.equal(-1);
1026 bar.dispose();
1027 });
1028
1029 it('should not use the overflow menu', () => {
1030 let bar = createMenuBar({
1031 overflowMenuOptions: { isVisible: false }
1032 });
1033 expect(bar.overflowIndex).to.equal(-1);
1034 expect(bar.overflowMenu).to.equal(null);
1035 bar.node.style.maxWidth = '70px';
1036 MessageLoop.sendMessage(bar, Widget.Msg.UpdateRequest);
1037 MessageLoop.flush();
1038 expect(bar.overflowIndex).to.equal(-1);
1039 expect(bar.overflowMenu).to.equal(null);
1040 bar.dispose();
1041 });
1042 });
1043
1044 context('`menuRequested` signal', () => {
1045 it('should activate the next menu', () => {
1046 let bar = createMenuBar();
1047 bar.openActiveMenu();
1048 (bar.activeMenu!.menuRequested as any).emit('next');
1049 expect(bar.activeIndex).to.equal(1);
1050 bar.dispose();
1051 });
1052
1053 it('should activate the previous menu', () => {
1054 let bar = createMenuBar();
1055 bar.openActiveMenu();
1056 (bar.activeMenu!.menuRequested as any).emit('previous');
1057 expect(bar.activeIndex).to.equal(2);
1058 bar.dispose();
1059 });
1060
1061 it('should be a no-op if the sender is not the open menu', () => {
1062 let bar = createMenuBar();
1063 (bar.activeMenu!.menuRequested as any).emit('next');
1064 expect(bar.activeIndex).to.equal(0);
1065 bar.dispose();
1066 });
1067 });
1068
1069 describe('.Renderer', () => {
1070 const renderer = new MenuBar.Renderer();
1071 let data: MenuBar.IRenderData;
1072
1073 before(() => {
1074 let widget = new Widget();
1075 widget.title.label = 'foo';
1076 widget.title.iconClass = 'bar';
1077 widget.title.className = 'baz';
1078 widget.title.closable = true;
1079 data = {
1080 title: widget.title,
1081 active: true,
1082 tabbable: true,
1083 disabled: false
1084 };
1085 });
1086
1087 describe('#renderItem()', () => {
1088 it('should render the virtual element for a menu bar item', () => {
1089 let node = VirtualDOM.realize(renderer.renderItem(data));
1090 expect(node.classList.contains('lm-MenuBar-item')).to.equal(true);
1091 expect(
1092 node.getElementsByClassName('lm-MenuBar-itemIcon').length
1093 ).to.equal(1);
1094 expect(
1095 node.getElementsByClassName('lm-MenuBar-itemLabel').length
1096 ).to.equal(1);
1097 });
1098 });
1099
1100 describe('#renderIcon()', () => {
1101 it('should render the icon element for a menu bar item', () => {
1102 let node = VirtualDOM.realize(renderer.renderIcon(data));
1103 expect(node.className).to.contain('lm-MenuBar-itemIcon');
1104 expect(node.className).to.contain('bar');
1105 });
1106 });
1107
1108 describe('#renderLabel()', () => {
1109 it('should render the label element for a menu item', () => {
1110 let node = VirtualDOM.realize(renderer.renderLabel(data));
1111 expect(node.className).to.contain('lm-MenuBar-itemLabel');
1112 expect(node.textContent).to.equal('foo');
1113 });
1114 });
1115
1116 describe('#createItemClass()', () => {
1117 it('should create the class name for the menu bar item', () => {
1118 let itemClass = renderer.createItemClass(data);
1119 expect(itemClass).to.contain('baz');
1120 expect(itemClass).to.contain('lm-mod-active');
1121 });
1122 });
1123
1124 describe('#createIconClass()', () => {
1125 it('should create the class name for the menu bar item icon', () => {
1126 let iconClass = renderer.createIconClass(data);
1127 expect(iconClass).to.contain('lm-MenuBar-itemIcon');
1128 expect(iconClass).to.contain('bar');
1129 });
1130 });
1131
1132 describe('#formatLabel()', () => {
1133 it('should format a label into HTML for display', () => {
1134 data.title.mnemonic = 1;
1135 let label = renderer.formatLabel(data);
1136 expect((label as any)[0]).to.equal('f');
1137 let node = VirtualDOM.realize((label as any)[1] as VirtualElement);
1138 expect(node.className).to.contain('lm-MenuBar-itemMnemonic');
1139 expect(node.textContent).to.equal('o');
1140 expect((label as any)[2]).to.equal('o');
1141 });
1142
1143 it('should not add a mnemonic if the index is out of range', () => {
1144 data.title.mnemonic = -1;
1145 let label = renderer.formatLabel(data);
1146 expect(label).to.equal('foo');
1147 });
1148 });
1149 });
1150
1151 describe('.defaultRenderer', () => {
1152 it('should be an instance of `Renderer`', () => {
1153 expect(MenuBar.defaultRenderer).to.be.an.instanceof(MenuBar.Renderer);
1154 });
1155 });
1156 });
1157 });