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
|
// Copyright 2012 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.ui.PaletteTest');
goog.setTestOnly('goog.ui.PaletteTest');
goog.require('goog.a11y.aria');
goog.require('goog.dom');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.events.KeyCodes');
goog.require('goog.events.KeyEvent');
goog.require('goog.testing.events.Event');
goog.require('goog.testing.jsunit');
goog.require('goog.testing.recordFunction');
goog.require('goog.ui.Component');
goog.require('goog.ui.Container');
goog.require('goog.ui.Palette');
var palette;
var nodes;
function setUp() {
nodes = [];
for (var i = 0; i < 23; i++) {
var node = goog.dom.createTextNode('node[' + i + ']');
nodes.push(node);
}
palette = new goog.ui.Palette(nodes);
}
function tearDown() {
palette.dispose();
goog.dom.removeChildren(document.getElementById('sandbox'));
}
function testAfterHighlightListener() {
palette.setHighlightedIndex(0);
var handler = new goog.testing.recordFunction();
goog.events.listen(
palette, goog.ui.Palette.EventType.AFTER_HIGHLIGHT, handler);
palette.setHighlightedIndex(2);
assertEquals(1, handler.getCallCount());
palette.setHighlightedIndex(-1);
assertEquals(2, handler.getCallCount());
}
function testHighlightItemUpdatesParentA11yActiveDescendant() {
var container = new goog.ui.Container();
container.render(document.getElementById('sandbox'));
container.addChild(palette, true);
palette.setHighlightedItem(nodes[0]);
assertEquals(
'Node 0 cell should be the container\'s active descendant',
palette.getRenderer().getCellForItem(nodes[0]),
goog.a11y.aria.getActiveDescendant(container.getElement()));
palette.setHighlightedItem(nodes[1]);
assertEquals(
'Node 1 cell should be the container\'s active descendant',
palette.getRenderer().getCellForItem(nodes[1]),
goog.a11y.aria.getActiveDescendant(container.getElement()));
palette.setHighlightedItem();
assertNull(
'Container should have no active descendant',
goog.a11y.aria.getActiveDescendant(container.getElement()));
container.dispose();
}
function testHighlightCellEvents() {
var container = new goog.ui.Container();
container.render(document.getElementById('sandbox'));
container.addChild(palette, true);
var renderer = palette.getRenderer();
var events = [];
var targetElements = [];
var handleEvent = function(e) {
events.push(e);
targetElements.push(e.target.getElement());
};
palette.getHandler().listen(
palette,
[
this, goog.ui.Component.EventType.HIGHLIGHT, this,
goog.ui.Component.EventType.UNHIGHLIGHT
],
handleEvent);
// Test highlight events on first selection
palette.setHighlightedItem(nodes[0]);
assertEquals('Should have fired 1 event', 1, events.length);
assertEquals(
'HIGHLIGHT event should be fired', goog.ui.Component.EventType.HIGHLIGHT,
events[0].type);
assertEquals(
'Event should be fired for node[0] cell',
renderer.getCellForItem(nodes[0]), targetElements[0]);
events = [];
targetElements = [];
// Only fire highlight events when there is a selection change
palette.setHighlightedItem(nodes[0]);
assertEquals('Should have fired 0 events', 0, events.length);
// Test highlight events on cell change
palette.setHighlightedItem(nodes[1]);
assertEquals('Should have fired 2 events', 2, events.length);
var unhighlightEvent = events.shift();
var highlightEvent = events.shift();
assertEquals(
'UNHIGHLIGHT should be fired first',
goog.ui.Component.EventType.UNHIGHLIGHT, unhighlightEvent.type);
assertEquals(
'UNHIGHLIGHT should be fired for node[0] cell',
renderer.getCellForItem(nodes[0]), targetElements[0]);
assertEquals(
'HIGHLIGHT should be fired after UNHIGHLIGHT',
goog.ui.Component.EventType.HIGHLIGHT, highlightEvent.type);
assertEquals(
'HIGHLIGHT should be fired for node[1] cell',
renderer.getCellForItem(nodes[1]), targetElements[1]);
events = [];
targetElements = [];
// Test highlight events when a cell is unselected
palette.setHighlightedItem();
assertEquals('Should have fired 1 event', 1, events.length);
assertEquals(
'UNHIGHLIGHT event should be fired',
goog.ui.Component.EventType.UNHIGHLIGHT, events[0].type);
assertEquals(
'Event should be fired for node[1] cell',
renderer.getCellForItem(nodes[1]), targetElements[0]);
}
function testHandleKeyEventLoops() {
palette.setHighlightedIndex(0);
var createKeyEvent = function(keyCode) {
return new goog.events.KeyEvent(
keyCode, 0 /* charCode */, false /* repeat */,
new goog.testing.events.Event(goog.events.EventType.KEYDOWN));
};
palette.handleKeyEvent(createKeyEvent(goog.events.KeyCodes.LEFT));
assertEquals(nodes.length - 1, palette.getHighlightedIndex());
palette.handleKeyEvent(createKeyEvent(goog.events.KeyCodes.RIGHT));
assertEquals(0, palette.getHighlightedIndex());
}
function testSetHighlight() {
assertEquals(-1, palette.getHighlightedIndex());
palette.setHighlighted(true);
assertEquals(0, palette.getHighlightedIndex());
palette.setHighlightedIndex(3);
palette.setHighlighted(false);
assertEquals(-1, palette.getHighlightedIndex());
palette.setHighlighted(true);
assertEquals(3, palette.getHighlightedIndex());
palette.setHighlighted(false);
palette.setHighlightedIndex(5);
palette.setHighlighted(true);
assertEquals(5, palette.getHighlightedIndex());
palette.setHighlighted(true);
assertEquals(5, palette.getHighlightedIndex());
}
function testPerformActionInternal() {
var container = new goog.ui.Container();
container.render(document.getElementById('sandbox'));
container.addChild(palette, true);
palette.setActive(true);
palette.setSelectedIndex(1);
palette.setHighlightedIndex(3);
palette.setHighlighted(true);
assertEquals(1, palette.getSelectedIndex());
assertEquals(3, palette.getHighlightedIndex());
// Click somewhere in the palette, but not inside a cell.
var mouseUp = new goog.events.BrowserEvent(
{type: 'mouseup', button: 1, target: palette});
palette.handleMouseUp(mouseUp);
// Highlight and selection are both unchanged (user did not select anything).
assertEquals(1, palette.getSelectedIndex());
assertEquals(3, palette.getHighlightedIndex());
}
function testSetAriaLabel() {
assertNull(
'Palette must not have aria label by default', palette.getAriaLabel());
palette.setAriaLabel('My Palette');
palette.render();
var element = palette.getElementStrict();
assertNotNull('Element must not be null', element);
assertEquals(
'Palette element must have expected aria-label', 'My Palette',
element.getAttribute('aria-label'));
assertEquals(
'Palette element must have expected aria role', 'grid',
element.getAttribute('role'));
palette.setAriaLabel('My new Palette');
assertEquals(
'Palette element must have updated aria-label', 'My new Palette',
element.getAttribute('aria-label'));
}
|