paletterenderer_test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. goog.provide('goog.ui.PaletteRendererTest');
  15. goog.setTestOnly('goog.ui.PaletteRendererTest');
  16. goog.require('goog.a11y.aria');
  17. goog.require('goog.a11y.aria.Role');
  18. goog.require('goog.a11y.aria.State');
  19. goog.require('goog.array');
  20. goog.require('goog.dom');
  21. goog.require('goog.dom.TagName');
  22. goog.require('goog.html.testing');
  23. goog.require('goog.testing.jsunit');
  24. goog.require('goog.ui.Palette');
  25. goog.require('goog.ui.PaletteRenderer');
  26. var sandbox;
  27. var items = [
  28. '<div aria-label="label-0"></div>', '<div title="title-1"></div>',
  29. '<div aria-label="label-2" title="title-2"></div>',
  30. '<div><span title="child-title-3"></span></div>'
  31. ];
  32. var itemEls;
  33. var renderer;
  34. var palette;
  35. function setUp() {
  36. sandbox = goog.dom.getElement('sandbox');
  37. itemEls = goog.array.map(items, function(item, index, a) {
  38. return goog.dom.safeHtmlToNode(goog.html.testing.newSafeHtmlForTest(item));
  39. });
  40. renderer = new goog.ui.PaletteRenderer();
  41. palette = new goog.ui.Palette(itemEls, renderer);
  42. palette.setSize(4, 1);
  43. }
  44. function tearDown() {
  45. palette.dispose();
  46. }
  47. function testGridA11yRoles() {
  48. var grid = renderer.createDom(palette);
  49. assertEquals(goog.a11y.aria.Role.GRID, goog.a11y.aria.getRole(grid));
  50. var table = goog.dom.getElementsByTagName(goog.dom.TagName.TABLE, grid)[0];
  51. var row = goog.dom.getElementsByTagName(goog.dom.TagName.TR, table)[0];
  52. assertEquals(goog.a11y.aria.Role.ROW, goog.a11y.aria.getRole(row));
  53. var cell = goog.dom.getElementsByTagName(goog.dom.TagName.TD, row)[0];
  54. assertEquals(goog.a11y.aria.Role.GRIDCELL, goog.a11y.aria.getRole(cell));
  55. }
  56. function testCellA11yLabels() {
  57. var grid = renderer.createDom(palette);
  58. var cells = goog.dom.getElementsByTagName(goog.dom.TagName.TD, grid);
  59. assertEquals(
  60. 'An aria-label is used as a label', 'label-0',
  61. goog.a11y.aria.getLabel(cells[0]));
  62. assertEquals(
  63. 'A title is used as a label', 'title-1',
  64. goog.a11y.aria.getLabel(cells[1]));
  65. assertEquals(
  66. 'An aria-label takes precedence over a title', 'label-2',
  67. goog.a11y.aria.getLabel(cells[2]));
  68. assertEquals(
  69. 'Children are traversed to find labels', 'child-title-3',
  70. goog.a11y.aria.getLabel(cells[3]));
  71. }
  72. function testA11yActiveDescendant() {
  73. palette.render();
  74. var cells = goog.dom.getElementsByTagName(
  75. goog.dom.TagName.TD, palette.getElementStrict());
  76. renderer.highlightCell(palette, cells[1].firstChild, true);
  77. assertEquals(
  78. cells[1].id,
  79. goog.a11y.aria.getState(
  80. palette.getElementStrict(), goog.a11y.aria.State.ACTIVEDESCENDANT));
  81. renderer.highlightCell(palette, cells[0].firstChild, false);
  82. assertEquals(
  83. cells[1].id,
  84. goog.a11y.aria.getState(
  85. palette.getElementStrict(), goog.a11y.aria.State.ACTIVEDESCENDANT));
  86. renderer.highlightCell(palette, cells[1].firstChild, false);
  87. assertNotEquals(
  88. cells[1].id,
  89. goog.a11y.aria.getState(
  90. palette.getElementStrict(), goog.a11y.aria.State.ACTIVEDESCENDANT));
  91. }