| <!DOCTYPE html> |
| <html> |
| <head> |
| <title>event_firing_test</title> |
| <script type="text/javascript" src="test_bootstrap.js"></script> |
| <script type="text/javascript"> |
| goog.require('bot.userAgent'); |
| goog.require('goog.events'); |
| goog.require('goog.testing.jsunit'); |
| goog.require('goog.userAgent.product'); |
| goog.require('core.events'); |
| goog.require('bot.locators'); |
| </script> |
| |
| <script type="text/javascript"> |
| var fired; |
| |
| // These should work, they just dosn't. |
| var CLICKING_WITH_COORDINATES_BROKEN = goog.userAgent.product.SAFARI || |
| goog.userAgent.product.ANDROID || |
| (goog.userAgent.product.OPERA && |
| bot.userAgent.isEngineVersion(12)); |
| |
| function setUp() { |
| fired = false; |
| } |
| |
| function addListener(element, eventName) { |
| goog.events.listenOnce(element, eventName, function() { |
| fired = true; |
| }); |
| } |
| |
| function testShouldFireCustomEvents() { |
| if (bot.userAgent.IE_DOC_PRE9) { |
| // IE before 9 doesn't allow custom event firing |
| return; |
| } |
| |
| var element = bot.locators.findElement({id: 'target'}); |
| addListener(element, 'magic'); |
| |
| core.events.fire('id=target', 'magic'); |
| |
| assertTrue(fired); |
| } |
| |
| function testShouldFireEventsWhenArgumentIsAnElement() { |
| var element = bot.locators.findElement({id: 'target'}); |
| addListener(element, 'focus'); |
| |
| core.events.fire(element, 'focus'); |
| |
| assertTrue(fired); |
| } |
| |
| function testCanFireAnEventAtASpecificLocationRelativeToAnElement() { |
| if (CLICKING_WITH_COORDINATES_BROKEN) { |
| return; |
| } |
| var mouseTarget = bot.locators.findElement({id: 'location'}); |
| var clickLocation = goog.style.getBounds(mouseTarget); |
| |
| var offsetX = 5; |
| var offsetY = 7; |
| |
| var fired = false; |
| goog.events.listenOnce(mouseTarget, goog.events.EventType.MOUSEUP, function(e) { |
| fired = true; |
| assertEquals(clickLocation.left + offsetX, e.clientX); |
| assertEquals(clickLocation.top + offsetY, e.clientY); |
| }); |
| |
| core.events.fireAt( |
| 'id=location', 'mouseup', offsetX + ',' + offsetY); |
| |
| assertTrue(fired); |
| } |
| |
| function testFiringEventsAtAParticularLocationWillDefaultToTopLeft() { |
| if (CLICKING_WITH_COORDINATES_BROKEN) { |
| return; |
| } |
| var mouseTarget = bot.locators.findElement({id: 'location'}); |
| var clickLocation = goog.style.getBounds(mouseTarget); |
| |
| var fired = false; |
| goog.events.listenOnce(mouseTarget, goog.events.EventType.MOUSEUP, function(e) { |
| fired = true; |
| assertEquals(clickLocation.left, e.clientX); |
| assertEquals(clickLocation.top, e.clientY); |
| }); |
| |
| core.events.fireAt('id=location', goog.events.EventType.MOUSEUP); |
| |
| assertTrue(fired); |
| } |
| |
| function testCanOverrideTheValueOfAnInputElement() { |
| var expected = "i like cheese"; |
| core.events.setValue("id=location", expected); |
| |
| var element = bot.locators.findElement({id: 'location'}); |
| assertEquals(expected, element.value); |
| } |
| |
| function testSettingValueFiresChangeEvent() { |
| var element = bot.locators.findElement({id: 'location'}); |
| var expected = "something different"; |
| |
| var fired = false; |
| goog.events.listenOnce(element, goog.events.EventType.CHANGE, function(e) { |
| fired = true; |
| }); |
| |
| core.events.setValue("id=location", expected); |
| |
| assertTrue(fired); |
| } |
| </script> |
| </head> |
| <body> |
| <p id="target">This is the target</p> |
| |
| <form action="#" action="get"> |
| <label for="location">Click me:</label><input id="location" /><br/> |
| </form> |
| </body> |
| </html> |