/** Test an event that supports shift keys by testing each shift key individually. */ private void testTriggerEventWithShiftKeys( BubbleAssertingEventListener listener, EventCreator creator) { Event.setEventListener(parent, listener); Event.setEventListener(child, listener); listener.setExpectedShiftKeys(true, true, true, true); child.dispatchEvent(creator.createEvent(true, true, true, true)); listener.assertReceived(); listener.setExpectedShiftKeys(true, false, false, false); child.dispatchEvent(creator.createEvent(true, false, false, false)); listener.assertReceived(); listener.setExpectedShiftKeys(false, true, false, false); child.dispatchEvent(creator.createEvent(false, true, false, false)); listener.assertReceived(); listener.setExpectedShiftKeys(false, false, true, false); child.dispatchEvent(creator.createEvent(false, false, true, false)); listener.assertReceived(); listener.setExpectedShiftKeys(false, false, false, true); child.dispatchEvent(creator.createEvent(false, false, false, true)); listener.assertReceived(); }
/** Tests that {@link Event#getCurrentEvent()} returns the right value for synthesized events. */ public void testGetCurrentEvent() { CurrentEventListener listener = new CurrentEventListener(); Event.setEventListener(child, listener); // Test all three major event types. child.dispatchEvent(Document.get().createClickEvent(0, 0, 0, 0, 0, false, false, false, false)); child.dispatchEvent(Document.get().createKeyPressEvent(false, false, false, false, 65)); child.dispatchEvent(Document.get().createFocusEvent()); assertTrue("Expecting click as current event", listener.gotClick); assertTrue("Expecting keypress as current event", listener.gotKeyPress); assertTrue("Expecting focus as current event", listener.gotFocus); }
/** Tests createDblClickEvent(). */ public void testTriggerDblClickEvent() { BubbleAssertingEventListener listener = new BubbleAssertingEventListener("dblclick", true) { @Override public void onBrowserEvent(Event event) { if (cancelled) { return; } if (event.getTypeInt() == Event.ONCLICK) { // Some browsers (IE, I'm looking at you) synthesize an extra click // event when a double-click is triggered. This synthesized click // will *not* have the same properties as the dblclick, so we will // not try to assert them here. return; } super.onBrowserEvent(event); assertMouseCoordinates(event); } }; Event.setEventListener(parent, listener); Event.setEventListener(child, listener); child.dispatchEvent( Document.get() .createDblClickEvent( MOUSE_DETAIL, SCREEN_X, SCREEN_Y, CLIENT_X, CLIENT_Y, true, true, true, true)); listener.cancel(); assertTrue("Expected child to receive event", listener.childReceived); assertTrue("Expected parent to receive event", listener.parentReceived); }
/** Tests createChangeEvent(). */ public void testTriggerChangeEvent() { BubbleAssertingEventListener listener = new BubbleAssertingEventListener("change", false); Event.setEventListener(parent, listener); Event.setEventListener(child, listener); child.dispatchEvent(Document.get().createChangeEvent()); listener.cancel(); assertTrue("Expected child to receive event", listener.childReceived); }
/** Tests createFocusEvent(). */ public void testTriggerFocusEvent() { NonBubbleAssertingEventListener listener = new NonBubbleAssertingEventListener("focus") { @Override public void onBrowserEvent(Event event) { if (cancelled) { return; } super.onBrowserEvent(event); assertEquals("focus", event.getType()); } }; Event.setEventListener(parent, listener); Event.setEventListener(child, listener); child.dispatchEvent(Document.get().createFocusEvent()); listener.cancel(); assertTrue("Expected child to receive event", listener.childReceived); }
/** * Tests createKeyPressEvent(). * * <p>Failed in all modes due to HtmlUnit bug: */ @DoNotRunWith({Platform.HtmlUnitBug}) public void testTriggerScrollEvent() { NonBubbleAssertingEventListener listener = new NonBubbleAssertingEventListener("scroll") { @Override public void onBrowserEvent(Event event) { if (cancelled) { return; } super.onBrowserEvent(event); assertEquals("scroll", event.getType()); } }; Event.setEventListener(parent, listener); Event.setEventListener(child, listener); child.dispatchEvent(Document.get().createScrollEvent()); listener.cancel(); assertTrue("Expected child to receive event", listener.childReceived); }
/** Tests NativeEvent.stopPropagation(). */ public void testStopPropagation() { NonBubbleAssertingEventListener listener = new NonBubbleAssertingEventListener("click") { @Override public void onBrowserEvent(Event event) { super.onBrowserEvent(event); if (event.getCurrentEventTarget().equals(child)) { event.stopPropagation(); } } }; Event.setEventListener(parent, listener); Event.setEventListener(child, listener); child.dispatchEvent( Document.get() .createClickEvent( MOUSE_DETAIL, SCREEN_X, SCREEN_Y, CLIENT_X, CLIENT_Y, true, true, true, true)); listener.cancel(); assertTrue("Expected child to receive event", listener.childReceived); }
/** Tests createClickEvent(). */ public void testTriggerClickEvent() { BubbleAssertingEventListener listener = new BubbleAssertingEventListener("click", true) { @Override public void onBrowserEvent(Event event) { if (cancelled) { return; } super.onBrowserEvent(event); assertMouseCoordinates(event); } }; Event.setEventListener(parent, listener); Event.setEventListener(child, listener); child.dispatchEvent( Document.get() .createClickEvent( MOUSE_DETAIL, SCREEN_X, SCREEN_Y, CLIENT_X, CLIENT_Y, true, true, true, true)); listener.cancel(); assertTrue("Expected child to receive event", listener.childReceived); assertTrue("Expected parent to receive event", listener.parentReceived); }