| 1 |
// Copyright 2013 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.CharPickerTest'); |
| 16 |
goog.setTestOnly('goog.ui.CharPickerTest'); |
| 17 |
|
| 18 |
goog.require('goog.a11y.aria'); |
| 19 |
goog.require('goog.a11y.aria.State'); |
| 20 |
goog.require('goog.dispose'); |
| 21 |
goog.require('goog.dom'); |
| 22 |
goog.require('goog.events.Event'); |
| 23 |
goog.require('goog.events.EventType'); |
| 24 |
goog.require('goog.i18n.CharPickerData'); |
| 25 |
goog.require('goog.i18n.uChar.NameFetcher'); |
| 26 |
goog.require('goog.testing.MockControl'); |
| 27 |
goog.require('goog.testing.events'); |
| 28 |
goog.require('goog.testing.jsunit'); |
| 29 |
goog.require('goog.testing.mockmatchers'); |
| 30 |
goog.require('goog.ui.CharPicker'); |
| 31 |
goog.require('goog.ui.FlatButtonRenderer'); |
| 32 |
|
| 33 |
var charPicker, charPickerData, charPickerElement; |
| 34 |
var mockControl, charNameFetcherMock; |
| 35 |
|
| 36 |
function setUp() { |
| 37 |
mockControl = new goog.testing.MockControl(); |
| 38 |
charNameFetcherMock = mockControl.createLooseMock( |
| 39 |
goog.i18n.uChar.NameFetcher, true /* opt_ignoreUnexpectedCalls */); |
| 40 |
|
| 41 |
charPickerData = new goog.i18n.CharPickerData(); |
| 42 |
charPickerElement = goog.dom.getElement('charpicker'); |
| 43 |
|
| 44 |
charPicker = new goog.ui.CharPicker(charPickerData, charNameFetcherMock); |
| 45 |
} |
| 46 |
|
| 47 |
function tearDown() { |
| 48 |
goog.dispose(charPicker); |
| 49 |
goog.dom.removeChildren(charPickerElement); |
| 50 |
mockControl.$tearDown(); |
| 51 |
} |
| 52 |
|
| 53 |
function testAriaLabelIsUpdatedOnFocus() { |
| 54 |
var character = '←'; |
| 55 |
var characterName = 'right arrow'; |
| 56 |
|
| 57 |
charNameFetcherMock.getName(character, goog.testing.mockmatchers.isFunction) |
| 58 |
.$does(function(c, callback) { callback(characterName); }); |
| 59 |
|
| 60 |
mockControl.$replayAll(); |
| 61 |
|
| 62 |
charPicker.decorate(charPickerElement); |
| 63 |
|
| 64 |
// Get the first button elements within the grid div and override its |
| 65 |
// char attribute so the test doesn't depend on the actual grid content. |
| 66 |
var gridElement = goog.dom.getElementByClass( |
| 67 |
goog.getCssName('goog-char-picker-grid'), charPickerElement); |
| 68 |
var buttonElement = goog.dom.getElementsByClass( |
| 69 |
goog.ui.FlatButtonRenderer.CSS_CLASS, gridElement)[0]; |
| 70 |
buttonElement.setAttribute('char', character); |
| 71 |
|
| 72 |
// Trigger a focus event on the button element. |
| 73 |
goog.testing.events.fireBrowserEvent( |
| 74 |
new goog.events.Event(goog.events.EventType.FOCUS, buttonElement)); |
| 75 |
|
| 76 |
mockControl.$verifyAll(); |
| 77 |
|
| 78 |
var ariaLabel = |
| 79 |
goog.a11y.aria.getState(buttonElement, goog.a11y.aria.State.LABEL); |
| 80 |
assertEquals( |
| 81 |
'The aria label should be updated when the button' + |
| 82 |
'gains focus.', |
| 83 |
characterName, ariaLabel); |
| 84 |
} |