UNPKG

sku-pg

Version:

Skulpt is a Javascript implementation of Python 2.x. Python that runs in your browser!

168 lines (137 loc) 4.71 kB
<!DOCTYPE html> <html> <!-- Copyright 2008 The Closure Library Authors. All Rights Reserved. Use of this source code is governed by the Apache License, Version 2.0. See the COPYING file for details. --> <!-- @author nicksantos@google.com (Nick Santos) --> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Closure Unit Tests - goog.ui.Menu</title> <style type='text/css'> .goog-menu { position: absolute; color: #aaa; } .goog-scrollable-menu { max-height:30px; overflow-y:auto; } </style> <script src="../base.js"></script> <script> goog.require('goog.dom'); goog.require('goog.events'); goog.require('goog.events.Event'); goog.require('goog.math'); goog.require('goog.testing.events'); goog.require('goog.testing.jsunit'); goog.require('goog.ui.Component.EventType'); goog.require('goog.ui.Menu'); </script> </head> <body> <script> var menu; var clonedMenuDom; function setUp() { clonedMenuDom = goog.dom.getElement('demoMenu').cloneNode(true); menu = new goog.ui.Menu(); } function tearDown() { menu.dispose(); var element = goog.dom.getElement('demoMenu'); element.parentNode.replaceChild(clonedMenuDom, element); } /** @bug 1463524 */ function testMouseupDoesntActivateMenuItemImmediately() { menu.decorate(goog.dom.getElement('demoMenu')); var fakeEvent = {clientX: 42, clientY: 42}; var itemElem = goog.dom.getElement('menuItem2'); var coords = new goog.math.Coordinate(42, 42); var menuItem = menu.getChildAt(1); var actionDispatched = false; goog.events.listen(menuItem, goog.ui.Component.EventType.ACTION, function(e) { actionDispatched = true; }); menu.setVisible(true, false, fakeEvent); // Makes the menu item active so it can be selected on mouseup. menuItem.setActive(true); goog.testing.events.fireMouseUpEvent(itemElem, undefined, coords); assertFalse('ACTION should not be dispatched after the initial mouseup', actionDispatched); goog.testing.events.fireMouseUpEvent(itemElem, undefined, coords); assertTrue('ACTION should be dispatched after the second mouseup', actionDispatched); } function testHoverBehavior() { menu.decorate(goog.dom.getElement('demoMenu')); goog.testing.events.fireMouseOverEvent(goog.dom.getElement('menuItem2'), document.body); assertEquals(1, menu.getHighlightedIndex()); menu.exitDocument(); assertEquals(-1, menu.getHighlightedIndex()); } function testIndirectionDecoration() { menu.decorate(goog.dom.getElement('complexMenu')); goog.testing.events.fireMouseOverEvent(goog.dom.getElement('complexItem3'), document.body); assertEquals(2, menu.getHighlightedIndex()); menu.exitDocument(); assertEquals(-1, menu.getHighlightedIndex()); } function testSetHighlightedIndex() { menu.decorate(goog.dom.getElement('scrollableMenu')); assertEquals(0, menu.getElement().scrollTop); // Scroll down var element = goog.dom.getElement('scrollableMenuItem4'); menu.setHighlightedIndex(3); assertTrue(element.offsetTop >= menu.getElement().scrollTop); assertTrue(element.offsetTop <= menu.getElement().scrollTop + menu.getElement().offsetHeight); // Scroll up element = goog.dom.getElement('scrollableMenuItem3'); menu.setHighlightedIndex(2); assertTrue(element.offsetTop >= menu.getElement().scrollTop); assertTrue(element.offsetTop <= menu.getElement().scrollTop + menu.getElement().offsetHeight); menu.exitDocument(); assertEquals(-1, menu.getHighlightedIndex()); } </script> <p> Here's a menu defined in markup: </p> <div id="demoMenu" class="goog-menu"> <div id='menuItem1' class="goog-menuitem">Annual Report.pdf</div> <div id='menuItem2' class="goog-menuitem">Quarterly Update.pdf</div> <div id='menuItem3' class="goog-menuitem">Enemies List.txt</div> </div> <p> Here's a menu which has been rendered with an explicit content node: </p> <div id="complexMenu" class="goog-menu"> <div style="border:1px solid black;"> <div class="goog-menu-content"> <div id='complexItem1' class="goog-menuitem">Drizzle</div> <div id='complexItem2' class="goog-menuitem">Rain</div> <div id='complexItem3' class="goog-menuitem">Deluge</div> </div> </div> </div> <p> Here's a scrollable menu: </p> <div id="scrollableMenu" class="goog-menu goog-scrollable-menu"> <div id='scrollableMenuItem1' class="goog-menuitem">Auk</div> <div id='scrollableMenuItem2' class="goog-menuitem">Bird</div> <div id='scrollableMenuItem3' class="goog-menuitem">Crow</div> <div id='scrollableMenuItem4' class="goog-menuitem">Duck</div> <div id='scrollableMenuItem5' class="goog-menuitem">Emu</div> </div> </body> </html>