Debian Codegraph @

closure-library/closure/goog/ui/toolbar_test.js

raw
1 // Copyright 2014 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.ui.ToolbarTest');
16 goog.setTestOnly('goog.ui.ToolbarTest');
17
18 goog.require('goog.a11y.aria');
19 goog.require('goog.dom');
20 goog.require('goog.events.EventType');
21 goog.require('goog.testing.events');
22 goog.require('goog.testing.events.Event');
23 goog.require('goog.testing.jsunit');
24 goog.require('goog.ui.Toolbar');
25 goog.require('goog.ui.ToolbarMenuButton');
26
27 var toolbar;
28 var toolbarWrapper;
29 var buttons;
30
31 function setUp() {
32 toolbar = new goog.ui.Toolbar();
33 toolbarWrapper = goog.dom.getElement('toolbar-wrapper');
34
35 // Render and populate the toolbar.
36 toolbar.render(toolbarWrapper);
37 var toolbarElem = toolbar.getElement();
38 var button1 = new goog.ui.ToolbarMenuButton('button 1');
39 var button2 = new goog.ui.ToolbarMenuButton('button 2');
40 var button3 = new goog.ui.ToolbarMenuButton('button 3');
41 button1.render(toolbarElem);
42 button2.render(toolbarElem);
43 button3.render(toolbarElem);
44 toolbar.addChild(button1);
45 toolbar.addChild(button2);
46 toolbar.addChild(button3);
47 buttons = [button1, button2, button3];
48 }
49
50 function tearDown() {
51 toolbar.dispose();
52 }
53
54 function testHighlightFirstOnFocus() {
55 var firstButton = buttons[0];
56
57 // Verify that focusing the toolbar via the keyboard (i.e. no click event)
58 // highlights the first item and sets it as the active descendant.
59 goog.testing.events.fireFocusEvent(toolbar.getElement());
60 assertEquals(0, toolbar.getHighlightedIndex());
61 assertTrue(firstButton.isHighlighted());
62 assertEquals(
63 firstButton.getElement(),
64 goog.a11y.aria.getActiveDescendant(toolbar.getElement()));
65
66 // Verify that removing focus unhighlights the first item and removes it as
67 // the active descendant.
68 goog.testing.events.fireBlurEvent(toolbar.getElement());
69 assertEquals(-1, toolbar.getHighlightedIndex());
70 assertNull(goog.a11y.aria.getActiveDescendant(toolbar.getElement()));
71 assertFalse(firstButton.isHighlighted());
72 }
73
74 function testHighlightSelectedOnClick() {
75 var firstButton = buttons[0];
76 var secondButton = buttons[1];
77
78 // Verify that mousing over and clicking on a toolbar button selects only the
79 // correct item.
80 var mouseover = new goog.testing.events.Event(
81 goog.events.EventType.MOUSEOVER, secondButton.getElement());
82 goog.testing.events.fireBrowserEvent(mouseover);
83 var mousedown = new goog.testing.events.Event(
84 goog.events.EventType.MOUSEDOWN, toolbar.getElement());
85 goog.testing.events.fireBrowserEvent(mousedown);
86 assertEquals(1, toolbar.getHighlightedIndex());
87 assertTrue(secondButton.isHighlighted());
88 assertFalse(firstButton.isHighlighted());
89 assertEquals(
90 secondButton.getElement(),
91 goog.a11y.aria.getActiveDescendant(toolbar.getElement()));
92 }