charpicker_test.js 2.9 KB

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