menu_test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2008 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.MenuTest');
  15. goog.setTestOnly('goog.ui.MenuTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.events');
  18. goog.require('goog.math.Coordinate');
  19. goog.require('goog.testing.events');
  20. goog.require('goog.testing.jsunit');
  21. goog.require('goog.ui.Component');
  22. goog.require('goog.ui.Menu');
  23. var menu;
  24. var clonedMenuDom;
  25. function setUp() {
  26. clonedMenuDom = goog.dom.getElement('demoMenu').cloneNode(true);
  27. menu = new goog.ui.Menu();
  28. }
  29. function tearDown() {
  30. menu.dispose();
  31. var element = goog.dom.getElement('demoMenu');
  32. element.parentNode.replaceChild(clonedMenuDom, element);
  33. }
  34. /** @bug 1463524 */
  35. function testMouseupDoesntActivateMenuItemImmediately() {
  36. menu.decorate(goog.dom.getElement('demoMenu'));
  37. var fakeEvent = {clientX: 42, clientY: 42};
  38. var itemElem = goog.dom.getElement('menuItem2');
  39. var coords = new goog.math.Coordinate(42, 42);
  40. var menuItem = menu.getChildAt(1);
  41. var actionDispatched = false;
  42. goog.events.listen(menuItem, goog.ui.Component.EventType.ACTION, function(e) {
  43. actionDispatched = true;
  44. });
  45. menu.setVisible(true, false, fakeEvent);
  46. // Makes the menu item active so it can be selected on mouseup.
  47. menuItem.setActive(true);
  48. goog.testing.events.fireMouseUpEvent(itemElem, undefined, coords);
  49. assertFalse(
  50. 'ACTION should not be dispatched after the initial mouseup',
  51. actionDispatched);
  52. goog.testing.events.fireMouseUpEvent(itemElem, undefined, coords);
  53. assertTrue(
  54. 'ACTION should be dispatched after the second mouseup', actionDispatched);
  55. }
  56. function testHoverBehavior() {
  57. menu.decorate(goog.dom.getElement('demoMenu'));
  58. goog.testing.events.fireMouseOverEvent(
  59. goog.dom.getElement('menuItem2'), document.body);
  60. assertEquals(1, menu.getHighlightedIndex());
  61. menu.exitDocument();
  62. assertEquals(-1, menu.getHighlightedIndex());
  63. }
  64. function testIndirectionDecoration() {
  65. menu.decorate(goog.dom.getElement('complexMenu'));
  66. goog.testing.events.fireMouseOverEvent(
  67. goog.dom.getElement('complexItem3'), document.body);
  68. assertEquals(2, menu.getHighlightedIndex());
  69. menu.exitDocument();
  70. assertEquals(-1, menu.getHighlightedIndex());
  71. }
  72. function testSetHighlightedIndex() {
  73. menu.decorate(goog.dom.getElement('scrollableMenu'));
  74. assertEquals(0, menu.getElement().scrollTop);
  75. // Scroll down
  76. var element = goog.dom.getElement('scrollableMenuItem4');
  77. menu.setHighlightedIndex(3);
  78. assertTrue(element.offsetTop >= menu.getElement().scrollTop);
  79. assertTrue(
  80. element.offsetTop <=
  81. menu.getElement().scrollTop + menu.getElement().offsetHeight);
  82. // Scroll up
  83. element = goog.dom.getElement('scrollableMenuItem3');
  84. menu.setHighlightedIndex(2);
  85. assertTrue(element.offsetTop >= menu.getElement().scrollTop);
  86. assertTrue(
  87. element.offsetTop <=
  88. menu.getElement().scrollTop + menu.getElement().offsetHeight);
  89. menu.exitDocument();
  90. assertEquals(-1, menu.getHighlightedIndex());
  91. }