| 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 |
|
| 15 |
goog.provide('goog.ui.SubMenuTest'); |
| 16 |
goog.setTestOnly('goog.ui.SubMenuTest'); |
| 17 |
|
| 18 |
goog.require('goog.a11y.aria'); |
| 19 |
goog.require('goog.a11y.aria.State'); |
| 20 |
goog.require('goog.dom'); |
| 21 |
goog.require('goog.dom.classlist'); |
| 22 |
goog.require('goog.events'); |
| 23 |
goog.require('goog.events.Event'); |
| 24 |
goog.require('goog.events.KeyCodes'); |
| 25 |
goog.require('goog.events.KeyHandler'); |
| 26 |
goog.require('goog.functions'); |
| 27 |
goog.require('goog.positioning'); |
| 28 |
goog.require('goog.positioning.Overflow'); |
| 29 |
goog.require('goog.style'); |
| 30 |
goog.require('goog.testing.MockClock'); |
| 31 |
goog.require('goog.testing.events'); |
| 32 |
goog.require('goog.testing.jsunit'); |
| 33 |
goog.require('goog.ui.Component'); |
| 34 |
goog.require('goog.ui.Menu'); |
| 35 |
goog.require('goog.ui.MenuItem'); |
| 36 |
goog.require('goog.ui.SubMenu'); |
| 37 |
goog.require('goog.ui.SubMenuRenderer'); |
| 38 |
|
| 39 |
var menu; |
| 40 |
var clonedMenuDom; |
| 41 |
|
| 42 |
var mockClock; |
| 43 |
|
| 44 |
// mock out goog.positioning.positionAtCoordinate so that |
| 45 |
// the menu always fits. (we don't care about testing the |
| 46 |
// dynamic menu positioning if the menu doesn't fit in the window.) |
| 47 |
var oldPositionFn = goog.positioning.positionAtCoordinate; |
| 48 |
goog.positioning.positionAtCoordinate = function( |
| 49 |
absolutePos, movableElement, movableElementCorner, opt_margin, |
| 50 |
opt_overflow) { |
| 51 |
return oldPositionFn.call( |
| 52 |
null, absolutePos, movableElement, movableElementCorner, opt_margin, |
| 53 |
goog.positioning.Overflow.IGNORE); |
| 54 |
}; |
| 55 |
|
| 56 |
function setUp() { |
| 57 |
clonedMenuDom = goog.dom.getElement('demoMenu').cloneNode(true); |
| 58 |
|
| 59 |
menu = new goog.ui.Menu(); |
| 60 |
} |
| 61 |
|
| 62 |
function tearDown() { |
| 63 |
document.body.style.direction = 'ltr'; |
| 64 |
menu.dispose(); |
| 65 |
|
| 66 |
var element = goog.dom.getElement('demoMenu'); |
| 67 |
element.parentNode.replaceChild(clonedMenuDom, element); |
| 68 |
|
| 69 |
goog.dom.removeChildren(goog.dom.getElement('sandbox')); |
| 70 |
|
| 71 |
if (mockClock) { |
| 72 |
mockClock.uninstall(); |
| 73 |
mockClock = null; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
function assertKeyHandlingIsCorrect(keyToOpenSubMenu, keyToCloseSubMenu) { |
| 78 |
menu.setFocusable(true); |
| 79 |
menu.decorate(goog.dom.getElement('demoMenu')); |
| 80 |
|
| 81 |
var KeyCodes = goog.events.KeyCodes; |
| 82 |
var plainItem = menu.getChildAt(0); |
| 83 |
plainItem.setMnemonic(KeyCodes.F); |
| 84 |
|
| 85 |
var subMenuItem1 = menu.getChildAt(1); |
| 86 |
subMenuItem1.setMnemonic(KeyCodes.S); |
| 87 |
var subMenuItem1Menu = subMenuItem1.getMenu(); |
| 88 |
menu.setHighlighted(plainItem); |
| 89 |
|
| 90 |
var fireKeySequence = goog.testing.events.fireKeySequence; |
| 91 |
|
| 92 |
assertTrue( |
| 93 |
'Expected OpenSubMenu key to not be handled', |
| 94 |
fireKeySequence(plainItem.getElement(), keyToOpenSubMenu)); |
| 95 |
assertFalse(subMenuItem1Menu.isVisible()); |
| 96 |
|
| 97 |
assertFalse( |
| 98 |
'Expected F key to be handled', |
| 99 |
fireKeySequence(plainItem.getElement(), KeyCodes.F)); |
| 100 |
|
| 101 |
assertFalse( |
| 102 |
'Expected DOWN key to be handled', |
| 103 |
fireKeySequence(plainItem.getElement(), KeyCodes.DOWN)); |
| 104 |
assertEquals(subMenuItem1, menu.getChildAt(1)); |
| 105 |
|
| 106 |
assertFalse( |
| 107 |
'Expected OpenSubMenu key to be handled', |
| 108 |
fireKeySequence(subMenuItem1.getElement(), keyToOpenSubMenu)); |
| 109 |
assertTrue(subMenuItem1Menu.isVisible()); |
| 110 |
|
| 111 |
assertFalse( |
| 112 |
'Expected CloseSubMenu key to be handled', |
| 113 |
fireKeySequence(subMenuItem1.getElement(), keyToCloseSubMenu)); |
| 114 |
assertFalse(subMenuItem1Menu.isVisible()); |
| 115 |
|
| 116 |
assertFalse( |
| 117 |
'Expected UP key to be handled', |
| 118 |
fireKeySequence(subMenuItem1.getElement(), KeyCodes.UP)); |
| 119 |
|
| 120 |
assertFalse( |
| 121 |
'Expected S key to be handled', |
| 122 |
fireKeySequence(plainItem.getElement(), KeyCodes.S)); |
| 123 |
assertTrue(subMenuItem1Menu.isVisible()); |
| 124 |
} |
| 125 |
|
| 126 |
function testKeyHandling_ltr() { |
| 127 |
assertKeyHandlingIsCorrect( |
| 128 |
goog.events.KeyCodes.RIGHT, goog.events.KeyCodes.LEFT); |
| 129 |
} |
| 130 |
|
| 131 |
function testKeyHandling_rtl() { |
| 132 |
document.body.style.direction = 'rtl'; |
| 133 |
assertKeyHandlingIsCorrect( |
| 134 |
goog.events.KeyCodes.LEFT, goog.events.KeyCodes.RIGHT); |
| 135 |
} |
| 136 |
|
| 137 |
function testNormalLtrSubMenu() { |
| 138 |
menu.decorate(goog.dom.getElement('demoMenu')); |
| 139 |
var subMenu = menu.getChildAt(1); |
| 140 |
assertArrowDirection(subMenu, false); |
| 141 |
assertRenderDirection(subMenu, false); |
| 142 |
assertArrowPosition(subMenu, false); |
| 143 |
} |
| 144 |
|
| 145 |
function testNormalRtlSubMenu() { |
| 146 |
document.body.style.direction = 'rtl'; |
| 147 |
menu.decorate(goog.dom.getElement('demoMenu')); |
| 148 |
var subMenu = menu.getChildAt(1); |
| 149 |
assertArrowDirection(subMenu, true); |
| 150 |
assertRenderDirection(subMenu, true); |
| 151 |
assertArrowPosition(subMenu, true); |
| 152 |
} |
| 153 |
|
| 154 |
function testLtrSubMenuAlignedToStart() { |
| 155 |
menu.decorate(goog.dom.getElement('demoMenu')); |
| 156 |
var subMenu = menu.getChildAt(1); |
| 157 |
subMenu.setAlignToEnd(false); |
| 158 |
assertArrowDirection(subMenu, true); |
| 159 |
assertRenderDirection(subMenu, true); |
| 160 |
assertArrowPosition(subMenu, false); |
| 161 |
} |
| 162 |
|
| 163 |
function testNullContentElement() { |
| 164 |
var subMenu = new goog.ui.SubMenu(); |
| 165 |
subMenu.setContent('demo'); |
| 166 |
} |
| 167 |
|
| 168 |
function testRtlSubMenuAlignedToStart() { |
| 169 |
document.body.style.direction = 'rtl'; |
| 170 |
menu.decorate(goog.dom.getElement('demoMenu')); |
| 171 |
var subMenu = menu.getChildAt(1); |
| 172 |
subMenu.setAlignToEnd(false); |
| 173 |
assertArrowDirection(subMenu, false); |
| 174 |
assertRenderDirection(subMenu, false); |
| 175 |
assertArrowPosition(subMenu, true); |
| 176 |
} |
| 177 |
|
| 178 |
function testSetContentKeepsArrow_ltr() { |
| 179 |
document.body.style.direction = 'ltr'; |
| 180 |
menu.decorate(goog.dom.getElement('demoMenu')); |
| 181 |
var subMenu = menu.getChildAt(1); |
| 182 |
subMenu.setAlignToEnd(false); |
| 183 |
subMenu.setContent('test'); |
| 184 |
assertArrowDirection(subMenu, true); |
| 185 |
assertRenderDirection(subMenu, true); |
| 186 |
assertArrowPosition(subMenu, false); |
| 187 |
} |
| 188 |
|
| 189 |
function testSetContentKeepsArrow_rtl() { |
| 190 |
document.body.style.direction = 'rtl'; |
| 191 |
menu.decorate(goog.dom.getElement('demoMenu')); |
| 192 |
var subMenu = menu.getChildAt(1); |
| 193 |
subMenu.setAlignToEnd(false); |
| 194 |
subMenu.setContent('test'); |
| 195 |
assertArrowDirection(subMenu, false); |
| 196 |
assertRenderDirection(subMenu, false); |
| 197 |
assertArrowPosition(subMenu, true); |
| 198 |
} |
| 199 |
|
| 200 |
function testExitDocument() { |
| 201 |
menu.decorate(goog.dom.getElement('demoMenu')); |
| 202 |
var subMenu = menu.getChildAt(1); |
| 203 |
var innerMenu = subMenu.getMenu(); |
| 204 |
|
| 205 |
assertTrue('Top-level menu was not in document', menu.isInDocument()); |
| 206 |
assertTrue('Submenu was not in document', subMenu.isInDocument()); |
| 207 |
assertTrue('Inner menu was not in document', innerMenu.isInDocument()); |
| 208 |
|
| 209 |
menu.exitDocument(); |
| 210 |
|
| 211 |
assertFalse('Top-level menu was in document', menu.isInDocument()); |
| 212 |
assertFalse('Submenu was in document', subMenu.isInDocument()); |
| 213 |
assertFalse('Inner menu was in document', innerMenu.isInDocument()); |
| 214 |
} |
| 215 |
|
| 216 |
function testDisposal() { |
| 217 |
menu.decorate(goog.dom.getElement('demoMenu')); |
| 218 |
var subMenu = menu.getChildAt(1); |
| 219 |
var innerMenu = subMenu.getMenu(); |
| 220 |
menu.dispose(); |
| 221 |
|
| 222 |
assert('Top-level menu was not disposed', menu.getDisposed()); |
| 223 |
assert('Submenu was not disposed', subMenu.getDisposed()); |
| 224 |
assert('Inner menu was not disposed', innerMenu.getDisposed()); |
| 225 |
} |
| 226 |
|
| 227 |
function testShowAndDismissSubMenu() { |
| 228 |
var openEventDispatched = false; |
| 229 |
var closeEventDispatched = false; |
| 230 |
|
| 231 |
function handleEvent(e) { |
| 232 |
switch (e.type) { |
| 233 |
case goog.ui.Component.EventType.OPEN: |
| 234 |
openEventDispatched = true; |
| 235 |
break; |
| 236 |
case goog.ui.Component.EventType.CLOSE: |
| 237 |
closeEventDispatched = true; |
| 238 |
break; |
| 239 |
default: |
| 240 |
fail('Invalid event type: ' + e.type); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
menu.decorate(goog.dom.getElement('demoMenu')); |
| 245 |
var subMenu = menu.getChildAt(1); |
| 246 |
subMenu.setHighlighted(true); |
| 247 |
|
| 248 |
goog.events.listen( |
| 249 |
subMenu, |
| 250 |
[goog.ui.Component.EventType.OPEN, goog.ui.Component.EventType.CLOSE], |
| 251 |
handleEvent); |
| 252 |
|
| 253 |
assertFalse( |
| 254 |
'Submenu must not have "-open" CSS class', |
| 255 |
goog.dom.classlist.contains(subMenu.getElement(), 'goog-submenu-open')); |
| 256 |
assertFalse('Popup menu must not be visible', subMenu.getMenu().isVisible()); |
| 257 |
assertFalse('No OPEN event must have been dispatched', openEventDispatched); |
| 258 |
assertFalse('No CLOSE event must have been dispatched', closeEventDispatched); |
| 259 |
|
| 260 |
subMenu.showSubMenu(); |
| 261 |
assertTrue( |
| 262 |
'Submenu must have "-open" CSS class', |
| 263 |
goog.dom.classlist.contains(subMenu.getElement(), 'goog-submenu-open')); |
| 264 |
assertTrue('Popup menu must be visible', subMenu.getMenu().isVisible()); |
| 265 |
assertTrue('OPEN event must have been dispatched', openEventDispatched); |
| 266 |
assertFalse('No CLOSE event must have been dispatched', closeEventDispatched); |
| 267 |
|
| 268 |
subMenu.dismissSubMenu(); |
| 269 |
assertFalse( |
| 270 |
'Submenu must not have "-open" CSS class', |
| 271 |
goog.dom.classlist.contains(subMenu.getElement(), 'goog-submenu-open')); |
| 272 |
assertFalse('Popup menu must not be visible', subMenu.getMenu().isVisible()); |
| 273 |
assertTrue('CLOSE event must have been dispatched', closeEventDispatched); |
| 274 |
|
| 275 |
goog.events.unlisten( |
| 276 |
subMenu, |
| 277 |
[goog.ui.Component.EventType.OPEN, goog.ui.Component.EventType.CLOSE], |
| 278 |
handleEvent); |
| 279 |
} |
| 280 |
|
| 281 |
function testDismissWhenSubMenuNotVisible() { |
| 282 |
var openEventDispatched = false; |
| 283 |
var closeEventDispatched = false; |
| 284 |
|
| 285 |
function handleEvent(e) { |
| 286 |
switch (e.type) { |
| 287 |
case goog.ui.Component.EventType.OPEN: |
| 288 |
openEventDispatched = true; |
| 289 |
break; |
| 290 |
case goog.ui.Component.EventType.CLOSE: |
| 291 |
closeEventDispatched = true; |
| 292 |
break; |
| 293 |
default: |
| 294 |
fail('Invalid event type: ' + e.type); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
menu.decorate(goog.dom.getElement('demoMenu')); |
| 299 |
var subMenu = menu.getChildAt(1); |
| 300 |
subMenu.setHighlighted(true); |
| 301 |
|
| 302 |
goog.events.listen( |
| 303 |
subMenu, |
| 304 |
[goog.ui.Component.EventType.OPEN, goog.ui.Component.EventType.CLOSE], |
| 305 |
handleEvent); |
| 306 |
|
| 307 |
assertFalse( |
| 308 |
'Submenu must not have "-open" CSS class', |
| 309 |
goog.dom.classlist.contains(subMenu.getElement(), 'goog-submenu-open')); |
| 310 |
assertFalse('Popup menu must not be visible', subMenu.getMenu().isVisible()); |
| 311 |
assertFalse('No OPEN event must have been dispatched', openEventDispatched); |
| 312 |
assertFalse('No CLOSE event must have been dispatched', closeEventDispatched); |
| 313 |
|
| 314 |
subMenu.showSubMenu(); |
| 315 |
subMenu.getMenu().setVisible(false); |
| 316 |
|
| 317 |
subMenu.dismissSubMenu(); |
| 318 |
assertFalse( |
| 319 |
'Submenu must not have "-open" CSS class', |
| 320 |
goog.dom.classlist.contains(subMenu.getElement(), 'goog-submenu-open')); |
| 321 |
assertFalse(subMenu.menuIsVisible_); |
| 322 |
assertFalse('Popup menu must not be visible', subMenu.getMenu().isVisible()); |
| 323 |
assertTrue('CLOSE event must have been dispatched', closeEventDispatched); |
| 324 |
|
| 325 |
goog.events.unlisten( |
| 326 |
subMenu, |
| 327 |
[goog.ui.Component.EventType.OPEN, goog.ui.Component.EventType.CLOSE], |
| 328 |
handleEvent); |
| 329 |
} |
| 330 |
|
| 331 |
function testCloseSubMenuBehavior() { |
| 332 |
menu.decorate(goog.dom.getElement('demoMenu')); |
| 333 |
var subMenu = menu.getChildAt(1); |
| 334 |
subMenu.getElement().id = 'subMenu'; |
| 335 |
|
| 336 |
var innerMenu = subMenu.getMenu(); |
| 337 |
innerMenu.getChildAt(0).getElement().id = 'child1'; |
| 338 |
|
| 339 |
subMenu.setHighlighted(true); |
| 340 |
subMenu.showSubMenu(); |
| 341 |
|
| 342 |
function MyFakeEvent(keyCode, opt_eventType) { |
| 343 |
this.type = opt_eventType || goog.events.KeyHandler.EventType.KEY; |
| 344 |
this.keyCode = keyCode; |
| 345 |
this.propagationStopped = false; |
| 346 |
this.preventDefault = goog.nullFunction; |
| 347 |
this.stopPropagation = function() { this.propagationStopped = true; }; |
| 348 |
} |
| 349 |
|
| 350 |
// Focus on the first item in the submenu and verify the activedescendant is |
| 351 |
// set correctly. |
| 352 |
subMenu.handleKeyEvent(new MyFakeEvent(goog.events.KeyCodes.DOWN)); |
| 353 |
assertEquals( |
| 354 |
'First item in submenu must be the aria-activedescendant', 'child1', |
| 355 |
goog.a11y.aria.getState( |
| 356 |
menu.getElement(), goog.a11y.aria.State.ACTIVEDESCENDANT)); |
| 357 |
|
| 358 |
// Dismiss the submenu and verify the activedescendant is updated correctly. |
| 359 |
subMenu.handleKeyEvent(new MyFakeEvent(goog.events.KeyCodes.LEFT)); |
| 360 |
assertEquals( |
| 361 |
'Submenu must be the aria-activedescendant', 'subMenu', |
| 362 |
goog.a11y.aria.getState( |
| 363 |
menu.getElement(), goog.a11y.aria.State.ACTIVEDESCENDANT)); |
| 364 |
} |
| 365 |
|
| 366 |
function testLazyInstantiateSubMenu() { |
| 367 |
menu.decorate(goog.dom.getElement('demoMenu')); |
| 368 |
var subMenu = menu.getChildAt(1); |
| 369 |
subMenu.setHighlighted(true); |
| 370 |
|
| 371 |
var lazyMenu; |
| 372 |
|
| 373 |
var key = goog.events.listen( |
| 374 |
subMenu, goog.ui.Component.EventType.OPEN, function(e) { |
| 375 |
lazyMenu = new goog.ui.Menu(); |
| 376 |
lazyMenu.addItem(new goog.ui.MenuItem('foo')); |
| 377 |
lazyMenu.addItem(new goog.ui.MenuItem('bar')); |
| 378 |
subMenu.setMenu(lazyMenu, /* opt_internal */ false); |
| 379 |
}); |
| 380 |
|
| 381 |
subMenu.showSubMenu(); |
| 382 |
assertNotNull('Popup menu must have been created', lazyMenu); |
| 383 |
assertEquals( |
| 384 |
'Popup menu must be a child of the submenu', subMenu, |
| 385 |
lazyMenu.getParent()); |
| 386 |
assertTrue('Popup menu must have been rendered', lazyMenu.isInDocument()); |
| 387 |
assertTrue('Popup menu must be visible', lazyMenu.isVisible()); |
| 388 |
|
| 389 |
menu.dispose(); |
| 390 |
assertTrue('Submenu must have been disposed of', subMenu.isDisposed()); |
| 391 |
assertFalse( |
| 392 |
'Popup menu must not have been disposed of', lazyMenu.isDisposed()); |
| 393 |
|
| 394 |
lazyMenu.dispose(); |
| 395 |
|
| 396 |
goog.events.unlistenByKey(key); |
| 397 |
} |
| 398 |
|
| 399 |
function testReusableMenu() { |
| 400 |
var subMenuOne = new goog.ui.SubMenu('SubMenu One'); |
| 401 |
var subMenuTwo = new goog.ui.SubMenu('SubMenu Two'); |
| 402 |
menu.addItem(subMenuOne); |
| 403 |
menu.addItem(subMenuTwo); |
| 404 |
menu.render(goog.dom.getElement('sandbox')); |
| 405 |
|
| 406 |
// It is possible for the same popup menu to be shared between different |
| 407 |
// submenus. |
| 408 |
var sharedMenu = new goog.ui.Menu(); |
| 409 |
sharedMenu.addItem(new goog.ui.MenuItem('Hello')); |
| 410 |
sharedMenu.addItem(new goog.ui.MenuItem('World')); |
| 411 |
|
| 412 |
assertNull('Shared menu must not have a parent', sharedMenu.getParent()); |
| 413 |
|
| 414 |
subMenuOne.setMenu(sharedMenu); |
| 415 |
assertEquals( |
| 416 |
'SubMenuOne must point to the shared menu', sharedMenu, |
| 417 |
subMenuOne.getMenu()); |
| 418 |
assertEquals( |
| 419 |
'SubMenuOne must be the shared menu\'s parent', subMenuOne, |
| 420 |
sharedMenu.getParent()); |
| 421 |
|
| 422 |
subMenuTwo.setMenu(sharedMenu); |
| 423 |
assertEquals( |
| 424 |
'SubMenuTwo must point to the shared menu', sharedMenu, |
| 425 |
subMenuTwo.getMenu()); |
| 426 |
assertEquals( |
| 427 |
'SubMenuTwo must be the shared menu\'s parent', subMenuTwo, |
| 428 |
sharedMenu.getParent()); |
| 429 |
assertEquals( |
| 430 |
'SubMenuOne must still point to the shared menu', sharedMenu, |
| 431 |
subMenuOne.getMenu()); |
| 432 |
|
| 433 |
menu.setHighlighted(subMenuOne); |
| 434 |
subMenuOne.showSubMenu(); |
| 435 |
assertEquals( |
| 436 |
'SubMenuOne must point to the shared menu', sharedMenu, |
| 437 |
subMenuOne.getMenu()); |
| 438 |
assertEquals( |
| 439 |
'SubMenuOne must be the shared menu\'s parent', subMenuOne, |
| 440 |
sharedMenu.getParent()); |
| 441 |
assertEquals( |
| 442 |
'SubMenuTwo must still point to the shared menu', sharedMenu, |
| 443 |
subMenuTwo.getMenu()); |
| 444 |
assertTrue('Shared menu must be visible', sharedMenu.isVisible()); |
| 445 |
|
| 446 |
menu.setHighlighted(subMenuTwo); |
| 447 |
subMenuTwo.showSubMenu(); |
| 448 |
assertEquals( |
| 449 |
'SubMenuTwo must point to the shared menu', sharedMenu, |
| 450 |
subMenuTwo.getMenu()); |
| 451 |
assertEquals( |
| 452 |
'SubMenuTwo must be the shared menu\'s parent', subMenuTwo, |
| 453 |
sharedMenu.getParent()); |
| 454 |
assertEquals( |
| 455 |
'SubMenuOne must still point to the shared menu', sharedMenu, |
| 456 |
subMenuOne.getMenu()); |
| 457 |
assertTrue('Shared menu must be visible', sharedMenu.isVisible()); |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
/** |
| 462 |
* If you remove a submenu in the interval between when a mouseover event |
| 463 |
* is fired on it, and showSubMenu() is called, showSubMenu causes a null |
| 464 |
* value to be dereferenced. This test validates that the fix for this works. |
| 465 |
* (See bug 1823144). |
| 466 |
*/ |
| 467 |
function testDeleteItemDuringSubmenuDisplayInterval() { |
| 468 |
mockClock = new goog.testing.MockClock(true); |
| 469 |
|
| 470 |
var submenu = new goog.ui.SubMenu('submenu'); |
| 471 |
submenu.addItem(new goog.ui.MenuItem('submenu item 1')); |
| 472 |
menu.addItem(submenu); |
| 473 |
|
| 474 |
// Trigger mouseover, and remove item before showSubMenu can be called. |
| 475 |
var e = new goog.events.Event(); |
| 476 |
submenu.handleMouseOver(e); |
| 477 |
menu.removeItem(submenu); |
| 478 |
mockClock.tick(goog.ui.SubMenu.MENU_DELAY_MS); |
| 479 |
// (No JS error should occur.) |
| 480 |
} |
| 481 |
|
| 482 |
function testShowSubMenuAfterRemoval() { |
| 483 |
var submenu = new goog.ui.SubMenu('submenu'); |
| 484 |
menu.addItem(submenu); |
| 485 |
menu.removeItem(submenu); |
| 486 |
submenu.showSubMenu(); |
| 487 |
// (No JS error should occur.) |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
/** |
| 492 |
* Tests that if a sub menu is selectable, then it can handle actions. |
| 493 |
*/ |
| 494 |
function testSubmenuSelectable() { |
| 495 |
var submenu = new goog.ui.SubMenu('submenu'); |
| 496 |
submenu.addItem(new goog.ui.MenuItem('submenu item 1')); |
| 497 |
menu.addItem(submenu); |
| 498 |
submenu.setSelectable(true); |
| 499 |
|
| 500 |
var numClicks = 0; |
| 501 |
var menuClickedFn = function(e) { numClicks++; }; |
| 502 |
|
| 503 |
goog.events.listen( |
| 504 |
submenu, goog.ui.Component.EventType.ACTION, menuClickedFn); |
| 505 |
submenu.performActionInternal(null); |
| 506 |
submenu.performActionInternal(null); |
| 507 |
|
| 508 |
assertEquals('The submenu should have fired an event', 2, numClicks); |
| 509 |
|
| 510 |
submenu.setSelectable(false); |
| 511 |
submenu.performActionInternal(null); |
| 512 |
|
| 513 |
assertEquals( |
| 514 |
'The submenu should not have fired any further events', 2, numClicks); |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
/** |
| 519 |
* Tests that if a sub menu is checkable, then it can handle actions. |
| 520 |
*/ |
| 521 |
function testSubmenuCheckable() { |
| 522 |
var submenu = new goog.ui.SubMenu('submenu'); |
| 523 |
submenu.addItem(new goog.ui.MenuItem('submenu item 1')); |
| 524 |
menu.addItem(submenu); |
| 525 |
submenu.setCheckable(true); |
| 526 |
|
| 527 |
var numClicks = 0; |
| 528 |
var menuClickedFn = function(e) { numClicks++; }; |
| 529 |
|
| 530 |
goog.events.listen( |
| 531 |
submenu, goog.ui.Component.EventType.ACTION, menuClickedFn); |
| 532 |
submenu.performActionInternal(null); |
| 533 |
submenu.performActionInternal(null); |
| 534 |
|
| 535 |
assertEquals('The submenu should have fired an event', 2, numClicks); |
| 536 |
|
| 537 |
submenu.setCheckable(false); |
| 538 |
submenu.performActionInternal(null); |
| 539 |
|
| 540 |
assertEquals( |
| 541 |
'The submenu should not have fired any further events', 2, numClicks); |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
/** |
| 546 |
* Tests that entering a child menu cancels the dismiss timer for the submenu. |
| 547 |
*/ |
| 548 |
function testEnteringChildCancelsDismiss() { |
| 549 |
var submenu = new goog.ui.SubMenu('submenu'); |
| 550 |
submenu.isInDocument = goog.functions.TRUE; |
| 551 |
submenu.addItem(new goog.ui.MenuItem('submenu item 1')); |
| 552 |
menu.addItem(submenu); |
| 553 |
|
| 554 |
mockClock = new goog.testing.MockClock(true); |
| 555 |
submenu.getMenu().setVisible(true); |
| 556 |
|
| 557 |
// This starts the dismiss timer. |
| 558 |
submenu.setHighlighted(false); |
| 559 |
|
| 560 |
// This should cancel the dismiss timer. |
| 561 |
submenu.getMenu().dispatchEvent(goog.ui.Component.EventType.ENTER); |
| 562 |
|
| 563 |
// Tick the length of the dismiss timer. |
| 564 |
mockClock.tick(goog.ui.SubMenu.MENU_DELAY_MS); |
| 565 |
|
| 566 |
// Check that the menu is now highlighted and still visible. |
| 567 |
assertTrue(submenu.getMenu().isVisible()); |
| 568 |
assertTrue(submenu.isHighlighted()); |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
/** |
| 573 |
* Asserts that this sub menu renders in the right direction relative to |
| 574 |
* the parent menu. |
| 575 |
* @param {goog.ui.SubMenu} subMenu The sub menu. |
| 576 |
* @param {boolean} left True for left-pointing, false for right-pointing. |
| 577 |
*/ |
| 578 |
function assertRenderDirection(subMenu, left) { |
| 579 |
subMenu.getParent().setHighlighted(subMenu); |
| 580 |
subMenu.showSubMenu(); |
| 581 |
var menuItemPosition = goog.style.getPageOffset(subMenu.getElement()); |
| 582 |
var menuPosition = goog.style.getPageOffset(subMenu.getMenu().getElement()); |
| 583 |
assert(Math.abs(menuItemPosition.y - menuPosition.y) < 5); |
| 584 |
assertEquals( |
| 585 |
'Menu at: ' + menuPosition.x + ', submenu item at: ' + menuItemPosition.x, |
| 586 |
left, menuPosition.x < menuItemPosition.x); |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
/** |
| 591 |
* Asserts that this sub menu has a properly-oriented arrow. |
| 592 |
* @param {goog.ui.SubMenu} subMenu The sub menu. |
| 593 |
* @param {boolean} left True for left-pointing, false for right-pointing. |
| 594 |
*/ |
| 595 |
function assertArrowDirection(subMenu, left) { |
| 596 |
assertEquals( |
| 597 |
left ? goog.ui.SubMenuRenderer.LEFT_ARROW_ : |
| 598 |
goog.ui.SubMenuRenderer.RIGHT_ARROW_, |
| 599 |
getArrowElement(subMenu).innerHTML); |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
/** |
| 604 |
* Asserts that the arrow position is correct. |
| 605 |
* @param {goog.ui.SubMenu} subMenu The sub menu. |
| 606 |
* @param {boolean} leftAlign True for left-aligned, false for right-aligned. |
| 607 |
*/ |
| 608 |
function assertArrowPosition(subMenu, left) { |
| 609 |
var arrow = getArrowElement(subMenu); |
| 610 |
var expectedLeft = |
| 611 |
left ? 0 : arrow.offsetParent.offsetWidth - arrow.offsetWidth; |
| 612 |
var actualLeft = arrow.offsetLeft; |
| 613 |
assertTrue( |
| 614 |
'Expected left offset: ' + expectedLeft + '\n' + |
| 615 |
'Actual left offset: ' + actualLeft + '\n', |
| 616 |
Math.abs(expectedLeft - actualLeft) < 5); |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
/** |
| 621 |
* Gets the arrow element of a sub menu. |
| 622 |
* @param {goog.ui.SubMenu} subMenu The sub menu. |
| 623 |
* @return {Element} The arrow. |
| 624 |
*/ |
| 625 |
function getArrowElement(subMenu) { |
| 626 |
return subMenu.getContentElement().lastChild; |
| 627 |
} |