selectionmenubutton_test.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Copyright 2009 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.SelectionMenuButtonTest');
  15. goog.setTestOnly('goog.ui.SelectionMenuButtonTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.events');
  18. goog.require('goog.testing.events');
  19. goog.require('goog.testing.jsunit');
  20. goog.require('goog.ui.Component');
  21. goog.require('goog.ui.SelectionMenuButton');
  22. var selectionMenuButton;
  23. var clonedSelectionMenuButtonDom;
  24. function setUp() {
  25. clonedSelectionMenuButtonDom =
  26. goog.dom.getElement('demoSelectionMenuButton').cloneNode(true);
  27. selectionMenuButton = new goog.ui.SelectionMenuButton();
  28. }
  29. function tearDown() {
  30. selectionMenuButton.dispose();
  31. var element = goog.dom.getElement('demoSelectionMenuButton');
  32. element.parentNode.replaceChild(clonedSelectionMenuButtonDom, element);
  33. }
  34. /**
  35. * Open the menu and click on the menu item inside.
  36. */
  37. function testBasicButtonBehavior() {
  38. var node = goog.dom.getElement('demoSelectionMenuButton');
  39. selectionMenuButton.decorate(node);
  40. goog.testing.events.fireClickSequence(node);
  41. assertTrue('Menu must open after click', selectionMenuButton.isOpen());
  42. var menuItemClicked = 0;
  43. var lastMenuItemClicked = null;
  44. goog.events.listen(
  45. selectionMenuButton.getMenu(), goog.ui.Component.EventType.ACTION,
  46. function(e) {
  47. menuItemClicked++;
  48. lastMenuItemClicked = e.target;
  49. });
  50. var menuItem2 = goog.dom.getElement('menuItem2');
  51. goog.testing.events.fireClickSequence(menuItem2);
  52. assertFalse(
  53. 'Menu must close on clicking when open', selectionMenuButton.isOpen());
  54. assertEquals('Number of menu items clicked should be 1', 1, menuItemClicked);
  55. assertEquals(
  56. 'menuItem2 should be the last menuitem clicked', menuItem2,
  57. lastMenuItemClicked.getElement());
  58. }
  59. /**
  60. * Tests that the checkbox fires the same events as the first 2 items.
  61. */
  62. function testCheckboxFireEvents() {
  63. var node = goog.dom.getElement('demoSelectionMenuButton');
  64. selectionMenuButton.decorate(node);
  65. var menuItemClicked = 0;
  66. var lastMenuItemClicked = null;
  67. goog.events.listen(
  68. selectionMenuButton.getMenu(), goog.ui.Component.EventType.ACTION,
  69. function(e) {
  70. menuItemClicked++;
  71. lastMenuItemClicked = e.target;
  72. });
  73. var checkbox = goog.dom.getElement('demoCheckbox');
  74. assertFalse('Checkbox must be unchecked (i.e. unselected)', checkbox.checked);
  75. checkbox.checked = true;
  76. goog.testing.events.fireClickSequence(checkbox);
  77. assertFalse(
  78. 'Menu must be closed when clicking checkbox',
  79. selectionMenuButton.isOpen());
  80. assertEquals('Number of menu items clicked should be 1', 1, menuItemClicked);
  81. assertEquals(
  82. 'menuItem1 should be the last menuitem clicked',
  83. goog.dom.getElement('menuItem1'), lastMenuItemClicked.getElement());
  84. checkbox.checked = false;
  85. goog.testing.events.fireClickSequence(checkbox);
  86. assertFalse(
  87. 'Menu must be closed when clicking checkbox',
  88. selectionMenuButton.isOpen());
  89. assertEquals('Number of menu items clicked should be 2', 2, menuItemClicked);
  90. assertEquals(
  91. 'menuItem2 should be the last menuitem clicked',
  92. goog.dom.getElement('menuItem2'), lastMenuItemClicked.getElement());
  93. }
  94. /**
  95. * Tests that the checkbox state gets updated when the first 2 events fire
  96. */
  97. function testCheckboxReceiveEvents() {
  98. var node = goog.dom.getElement('demoSelectionMenuButton');
  99. selectionMenuButton.decorate(node);
  100. var checkbox = goog.dom.getElement('demoCheckbox');
  101. assertFalse('Checkbox must be unchecked (i.e. unselected)', checkbox.checked);
  102. goog.testing.events.fireClickSequence(goog.dom.getElement('menuItem1'));
  103. assertTrue('Checkbox must be checked (i.e. selected)', checkbox.checked);
  104. goog.testing.events.fireClickSequence(goog.dom.getElement('menuItem2'));
  105. assertFalse('Checkbox must be unchecked (i.e. unselected)', checkbox.checked);
  106. }
  107. /**
  108. * Tests that set/getSelectionState correctly changes the state
  109. */
  110. function testSelectionState() {
  111. var node = goog.dom.getElement('demoSelectionMenuButton');
  112. selectionMenuButton.decorate(node);
  113. var checkbox = goog.dom.getElement('demoCheckbox');
  114. assertFalse('Checkbox must be unchecked (i.e. unselected)', checkbox.checked);
  115. selectionMenuButton.setSelectionState(
  116. goog.ui.SelectionMenuButton.SelectionState.ALL);
  117. assertTrue('Checkbox should be checked when selecting all', checkbox.checked);
  118. assertEquals(
  119. 'selectionState should be ALL', selectionMenuButton.getSelectionState(),
  120. goog.ui.SelectionMenuButton.SelectionState.ALL);
  121. selectionMenuButton.setSelectionState(
  122. goog.ui.SelectionMenuButton.SelectionState.NONE);
  123. assertFalse(
  124. 'Checkbox should be checked when selecting all', checkbox.checked);
  125. assertEquals(
  126. 'selectionState should be NONE', selectionMenuButton.getSelectionState(),
  127. goog.ui.SelectionMenuButton.SelectionState.NONE);
  128. selectionMenuButton.setSelectionState(
  129. goog.ui.SelectionMenuButton.SelectionState.SOME);
  130. assertTrue('Checkbox should be checked when selecting all', checkbox.checked);
  131. assertEquals(
  132. 'selectionState should be SOME', selectionMenuButton.getSelectionState(),
  133. goog.ui.SelectionMenuButton.SelectionState.SOME);
  134. }
  135. /**
  136. * Tests that the checkbox gets disabled when the button is disabled
  137. */
  138. function testCheckboxDisabled() {
  139. var node = goog.dom.getElement('demoSelectionMenuButton');
  140. selectionMenuButton.decorate(node);
  141. var checkbox = goog.dom.getElement('demoCheckbox');
  142. assertFalse('Checkbox must be enabled', checkbox.disabled);
  143. selectionMenuButton.setEnabled(false);
  144. assertTrue('Checkbox must be disabled', checkbox.disabled);
  145. selectionMenuButton.setEnabled(true);
  146. assertFalse('Checkbox must be enabled', checkbox.disabled);
  147. }
  148. /**
  149. * Tests that clicking the checkbox does not open the menu
  150. */
  151. function testCheckboxClickMenuClosed() {
  152. var node = goog.dom.getElement('demoSelectionMenuButton');
  153. selectionMenuButton.decorate(node);
  154. var checkbox = goog.dom.getElement('demoCheckbox');
  155. goog.testing.events.fireMouseDownEvent(checkbox);
  156. assertFalse(
  157. 'Menu must be closed when mousedown checkbox',
  158. selectionMenuButton.isOpen());
  159. goog.testing.events.fireMouseUpEvent(checkbox);
  160. assertFalse(
  161. 'Menu must remain closed when mouseup checkbox',
  162. selectionMenuButton.isOpen());
  163. selectionMenuButton.setOpen(true);
  164. goog.testing.events.fireClickSequence(checkbox);
  165. assertFalse(
  166. 'Menu must close when clickin checkbox', selectionMenuButton.isOpen());
  167. }