toolbar_test.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2014 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.ToolbarTest');
  15. goog.setTestOnly('goog.ui.ToolbarTest');
  16. goog.require('goog.a11y.aria');
  17. goog.require('goog.dom');
  18. goog.require('goog.events.EventType');
  19. goog.require('goog.testing.events');
  20. goog.require('goog.testing.events.Event');
  21. goog.require('goog.testing.jsunit');
  22. goog.require('goog.ui.Toolbar');
  23. goog.require('goog.ui.ToolbarMenuButton');
  24. var toolbar;
  25. var toolbarWrapper;
  26. var buttons;
  27. function setUp() {
  28. toolbar = new goog.ui.Toolbar();
  29. toolbarWrapper = goog.dom.getElement('toolbar-wrapper');
  30. // Render and populate the toolbar.
  31. toolbar.render(toolbarWrapper);
  32. var toolbarElem = toolbar.getElement();
  33. var button1 = new goog.ui.ToolbarMenuButton('button 1');
  34. var button2 = new goog.ui.ToolbarMenuButton('button 2');
  35. var button3 = new goog.ui.ToolbarMenuButton('button 3');
  36. button1.render(toolbarElem);
  37. button2.render(toolbarElem);
  38. button3.render(toolbarElem);
  39. toolbar.addChild(button1);
  40. toolbar.addChild(button2);
  41. toolbar.addChild(button3);
  42. buttons = [button1, button2, button3];
  43. }
  44. function tearDown() {
  45. toolbar.dispose();
  46. }
  47. function testHighlightFirstOnFocus() {
  48. var firstButton = buttons[0];
  49. // Verify that focusing the toolbar via the keyboard (i.e. no click event)
  50. // highlights the first item and sets it as the active descendant.
  51. goog.testing.events.fireFocusEvent(toolbar.getElement());
  52. assertEquals(0, toolbar.getHighlightedIndex());
  53. assertTrue(firstButton.isHighlighted());
  54. assertEquals(
  55. firstButton.getElement(),
  56. goog.a11y.aria.getActiveDescendant(toolbar.getElement()));
  57. // Verify that removing focus unhighlights the first item and removes it as
  58. // the active descendant.
  59. goog.testing.events.fireBlurEvent(toolbar.getElement());
  60. assertEquals(-1, toolbar.getHighlightedIndex());
  61. assertNull(goog.a11y.aria.getActiveDescendant(toolbar.getElement()));
  62. assertFalse(firstButton.isHighlighted());
  63. }
  64. function testHighlightSelectedOnClick() {
  65. var firstButton = buttons[0];
  66. var secondButton = buttons[1];
  67. // Verify that mousing over and clicking on a toolbar button selects only the
  68. // correct item.
  69. var mouseover = new goog.testing.events.Event(
  70. goog.events.EventType.MOUSEOVER, secondButton.getElement());
  71. goog.testing.events.fireBrowserEvent(mouseover);
  72. var mousedown = new goog.testing.events.Event(
  73. goog.events.EventType.MOUSEDOWN, toolbar.getElement());
  74. goog.testing.events.fireBrowserEvent(mousedown);
  75. assertEquals(1, toolbar.getHighlightedIndex());
  76. assertTrue(secondButton.isHighlighted());
  77. assertFalse(firstButton.isHighlighted());
  78. assertEquals(
  79. secondButton.getElement(),
  80. goog.a11y.aria.getActiveDescendant(toolbar.getElement()));
  81. }