| 1 |
// Copyright 2012 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.PaletteTest'); |
| 16 |
goog.setTestOnly('goog.ui.PaletteTest'); |
| 17 |
|
| 18 |
goog.require('goog.a11y.aria'); |
| 19 |
goog.require('goog.dom'); |
| 20 |
goog.require('goog.events'); |
| 21 |
goog.require('goog.events.EventType'); |
| 22 |
goog.require('goog.events.KeyCodes'); |
| 23 |
goog.require('goog.events.KeyEvent'); |
| 24 |
goog.require('goog.testing.events.Event'); |
| 25 |
goog.require('goog.testing.jsunit'); |
| 26 |
goog.require('goog.testing.recordFunction'); |
| 27 |
goog.require('goog.ui.Component'); |
| 28 |
goog.require('goog.ui.Container'); |
| 29 |
goog.require('goog.ui.Palette'); |
| 30 |
|
| 31 |
var palette; |
| 32 |
var nodes; |
| 33 |
|
| 34 |
function setUp() { |
| 35 |
nodes = []; |
| 36 |
for (var i = 0; i < 23; i++) { |
| 37 |
var node = goog.dom.createTextNode('node[' + i + ']'); |
| 38 |
nodes.push(node); |
| 39 |
} |
| 40 |
palette = new goog.ui.Palette(nodes); |
| 41 |
} |
| 42 |
|
| 43 |
function tearDown() { |
| 44 |
palette.dispose(); |
| 45 |
goog.dom.removeChildren(document.getElementById('sandbox')); |
| 46 |
} |
| 47 |
|
| 48 |
function testAfterHighlightListener() { |
| 49 |
palette.setHighlightedIndex(0); |
| 50 |
var handler = new goog.testing.recordFunction(); |
| 51 |
goog.events.listen( |
| 52 |
palette, goog.ui.Palette.EventType.AFTER_HIGHLIGHT, handler); |
| 53 |
palette.setHighlightedIndex(2); |
| 54 |
assertEquals(1, handler.getCallCount()); |
| 55 |
palette.setHighlightedIndex(-1); |
| 56 |
assertEquals(2, handler.getCallCount()); |
| 57 |
} |
| 58 |
|
| 59 |
function testHighlightItemUpdatesParentA11yActiveDescendant() { |
| 60 |
var container = new goog.ui.Container(); |
| 61 |
container.render(document.getElementById('sandbox')); |
| 62 |
container.addChild(palette, true); |
| 63 |
|
| 64 |
palette.setHighlightedItem(nodes[0]); |
| 65 |
assertEquals( |
| 66 |
'Node 0 cell should be the container\'s active descendant', |
| 67 |
palette.getRenderer().getCellForItem(nodes[0]), |
| 68 |
goog.a11y.aria.getActiveDescendant(container.getElement())); |
| 69 |
|
| 70 |
palette.setHighlightedItem(nodes[1]); |
| 71 |
assertEquals( |
| 72 |
'Node 1 cell should be the container\'s active descendant', |
| 73 |
palette.getRenderer().getCellForItem(nodes[1]), |
| 74 |
goog.a11y.aria.getActiveDescendant(container.getElement())); |
| 75 |
|
| 76 |
palette.setHighlightedItem(); |
| 77 |
assertNull( |
| 78 |
'Container should have no active descendant', |
| 79 |
goog.a11y.aria.getActiveDescendant(container.getElement())); |
| 80 |
|
| 81 |
container.dispose(); |
| 82 |
} |
| 83 |
|
| 84 |
function testHighlightCellEvents() { |
| 85 |
var container = new goog.ui.Container(); |
| 86 |
container.render(document.getElementById('sandbox')); |
| 87 |
container.addChild(palette, true); |
| 88 |
var renderer = palette.getRenderer(); |
| 89 |
|
| 90 |
var events = []; |
| 91 |
var targetElements = []; |
| 92 |
var handleEvent = function(e) { |
| 93 |
events.push(e); |
| 94 |
targetElements.push(e.target.getElement()); |
| 95 |
}; |
| 96 |
palette.getHandler().listen( |
| 97 |
palette, |
| 98 |
[ |
| 99 |
this, goog.ui.Component.EventType.HIGHLIGHT, this, |
| 100 |
goog.ui.Component.EventType.UNHIGHLIGHT |
| 101 |
], |
| 102 |
handleEvent); |
| 103 |
|
| 104 |
// Test highlight events on first selection |
| 105 |
palette.setHighlightedItem(nodes[0]); |
| 106 |
assertEquals('Should have fired 1 event', 1, events.length); |
| 107 |
assertEquals( |
| 108 |
'HIGHLIGHT event should be fired', goog.ui.Component.EventType.HIGHLIGHT, |
| 109 |
events[0].type); |
| 110 |
assertEquals( |
| 111 |
'Event should be fired for node[0] cell', |
| 112 |
renderer.getCellForItem(nodes[0]), targetElements[0]); |
| 113 |
|
| 114 |
events = []; |
| 115 |
targetElements = []; |
| 116 |
|
| 117 |
// Only fire highlight events when there is a selection change |
| 118 |
palette.setHighlightedItem(nodes[0]); |
| 119 |
assertEquals('Should have fired 0 events', 0, events.length); |
| 120 |
|
| 121 |
// Test highlight events on cell change |
| 122 |
palette.setHighlightedItem(nodes[1]); |
| 123 |
assertEquals('Should have fired 2 events', 2, events.length); |
| 124 |
var unhighlightEvent = events.shift(); |
| 125 |
var highlightEvent = events.shift(); |
| 126 |
assertEquals( |
| 127 |
'UNHIGHLIGHT should be fired first', |
| 128 |
goog.ui.Component.EventType.UNHIGHLIGHT, unhighlightEvent.type); |
| 129 |
assertEquals( |
| 130 |
'UNHIGHLIGHT should be fired for node[0] cell', |
| 131 |
renderer.getCellForItem(nodes[0]), targetElements[0]); |
| 132 |
assertEquals( |
| 133 |
'HIGHLIGHT should be fired after UNHIGHLIGHT', |
| 134 |
goog.ui.Component.EventType.HIGHLIGHT, highlightEvent.type); |
| 135 |
assertEquals( |
| 136 |
'HIGHLIGHT should be fired for node[1] cell', |
| 137 |
renderer.getCellForItem(nodes[1]), targetElements[1]); |
| 138 |
|
| 139 |
events = []; |
| 140 |
targetElements = []; |
| 141 |
|
| 142 |
// Test highlight events when a cell is unselected |
| 143 |
palette.setHighlightedItem(); |
| 144 |
|
| 145 |
assertEquals('Should have fired 1 event', 1, events.length); |
| 146 |
assertEquals( |
| 147 |
'UNHIGHLIGHT event should be fired', |
| 148 |
goog.ui.Component.EventType.UNHIGHLIGHT, events[0].type); |
| 149 |
assertEquals( |
| 150 |
'Event should be fired for node[1] cell', |
| 151 |
renderer.getCellForItem(nodes[1]), targetElements[0]); |
| 152 |
} |
| 153 |
|
| 154 |
function testHandleKeyEventLoops() { |
| 155 |
palette.setHighlightedIndex(0); |
| 156 |
var createKeyEvent = function(keyCode) { |
| 157 |
return new goog.events.KeyEvent( |
| 158 |
keyCode, 0 /* charCode */, false /* repeat */, |
| 159 |
new goog.testing.events.Event(goog.events.EventType.KEYDOWN)); |
| 160 |
}; |
| 161 |
palette.handleKeyEvent(createKeyEvent(goog.events.KeyCodes.LEFT)); |
| 162 |
assertEquals(nodes.length - 1, palette.getHighlightedIndex()); |
| 163 |
|
| 164 |
palette.handleKeyEvent(createKeyEvent(goog.events.KeyCodes.RIGHT)); |
| 165 |
assertEquals(0, palette.getHighlightedIndex()); |
| 166 |
} |
| 167 |
|
| 168 |
function testSetHighlight() { |
| 169 |
assertEquals(-1, palette.getHighlightedIndex()); |
| 170 |
palette.setHighlighted(true); |
| 171 |
assertEquals(0, palette.getHighlightedIndex()); |
| 172 |
|
| 173 |
palette.setHighlightedIndex(3); |
| 174 |
palette.setHighlighted(false); |
| 175 |
assertEquals(-1, palette.getHighlightedIndex()); |
| 176 |
palette.setHighlighted(true); |
| 177 |
assertEquals(3, palette.getHighlightedIndex()); |
| 178 |
|
| 179 |
palette.setHighlighted(false); |
| 180 |
palette.setHighlightedIndex(5); |
| 181 |
palette.setHighlighted(true); |
| 182 |
assertEquals(5, palette.getHighlightedIndex()); |
| 183 |
palette.setHighlighted(true); |
| 184 |
assertEquals(5, palette.getHighlightedIndex()); |
| 185 |
} |
| 186 |
|
| 187 |
function testPerformActionInternal() { |
| 188 |
var container = new goog.ui.Container(); |
| 189 |
container.render(document.getElementById('sandbox')); |
| 190 |
container.addChild(palette, true); |
| 191 |
palette.setActive(true); |
| 192 |
palette.setSelectedIndex(1); |
| 193 |
palette.setHighlightedIndex(3); |
| 194 |
palette.setHighlighted(true); |
| 195 |
assertEquals(1, palette.getSelectedIndex()); |
| 196 |
assertEquals(3, palette.getHighlightedIndex()); |
| 197 |
|
| 198 |
// Click somewhere in the palette, but not inside a cell. |
| 199 |
var mouseUp = new goog.events.BrowserEvent( |
| 200 |
{type: 'mouseup', button: 1, target: palette}); |
| 201 |
palette.handleMouseUp(mouseUp); |
| 202 |
|
| 203 |
// Highlight and selection are both unchanged (user did not select anything). |
| 204 |
assertEquals(1, palette.getSelectedIndex()); |
| 205 |
assertEquals(3, palette.getHighlightedIndex()); |
| 206 |
} |
| 207 |
|
| 208 |
function testSetAriaLabel() { |
| 209 |
assertNull( |
| 210 |
'Palette must not have aria label by default', palette.getAriaLabel()); |
| 211 |
palette.setAriaLabel('My Palette'); |
| 212 |
palette.render(); |
| 213 |
var element = palette.getElementStrict(); |
| 214 |
assertNotNull('Element must not be null', element); |
| 215 |
assertEquals( |
| 216 |
'Palette element must have expected aria-label', 'My Palette', |
| 217 |
element.getAttribute('aria-label')); |
| 218 |
assertEquals( |
| 219 |
'Palette element must have expected aria role', 'grid', |
| 220 |
element.getAttribute('role')); |
| 221 |
palette.setAriaLabel('My new Palette'); |
| 222 |
assertEquals( |
| 223 |
'Palette element must have updated aria-label', 'My new Palette', |
| 224 |
element.getAttribute('aria-label')); |
| 225 |
} |