From: Manolo Carrasco Date: Wed, 5 May 2010 08:48:34 +0000 (+0000) Subject: Fixed event handler with IE and Chrome. Re-factor of Events. Increased coverage. X-Git-Tag: release-1.3.2~753 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=90d91d466c8c15767835733b4de5bd020e06c6ad;p=gwtquery.git Fixed event handler with IE and Chrome. Re-factor of Events. Increased coverage. --- diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Events.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Events.java index 8c6d1364..30120d6a 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Events.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Events.java @@ -16,6 +16,7 @@ package com.google.gwt.query.client; import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.NativeEvent; import com.google.gwt.dom.client.NodeList; import com.google.gwt.user.client.Event; @@ -47,38 +48,81 @@ public class Events extends GQuery { } /** - * Binds a handler to a particular Event for each matched element. - * - * The event handler is passed as a Function that you can use to prevent - * default behaviour. To stop both default action and event bubbling, the + * Binds a set of handlers to a particular Event for each matched element. + * + * The event handlers are passed as Functions that you can use to prevent + * default behavior. To stop both default action and event bubbling, the * function event handler has to return false. - * + * * You can pass an additional Object data to your Function as the second * parameter + * */ - public GQuery bind(int eventbits, final Object data, final Function f) { + public GQuery bind(int eventbits, Object data, Function...funcs) { for (Element e : elements()) { - EventsListener.getInstance(e).bind(eventbits, data, f); + EventsListener.getInstance(e).bind(eventbits, data, funcs); } return this; } - + /** - * Fires an event on each matched element. - * - * Example: fire(Event.ONCLICK) + * Execute all handlers and behaviors attached to the matched elements for the given event types. + * + * Different event types can be passed joining these using the or bit wise operator. + * + * For keyboard events you can pass a second parameter which represents + * the key-code of the pushed key. + * + * Example: fire(Event.ONCLICK | Event.ONFOCUS) + * Example: fire(Event.ONKEYDOWN. 'a'); */ - public GQuery fire(int eventbits, int... keys) { - for (Element e : elements()) { - EventsListener.getInstance(e).fire(eventbits, keys); - } + public GQuery trigger(int eventbits, int... keys) { + if ((eventbits | Event.ONBLUR) == Event.ONBLUR) + dispatchEvent(document.createBlurEvent()); + if ((eventbits | Event.ONCHANGE) == Event.ONCHANGE) + dispatchEvent(document.createChangeEvent()); + if ((eventbits | Event.ONCLICK) == Event.ONCLICK) + dispatchEvent(document.createClickEvent(0, 0, 0, 0, 0, false, false, false, false)); + if ((eventbits | Event.ONDBLCLICK) == Event.ONDBLCLICK) + dispatchEvent(document.createDblClickEvent(0, 0, 0, 0, 0, false, false, false, false)); + if ((eventbits | Event.ONFOCUS) == Event.ONFOCUS) + dispatchEvent(document.createFocusEvent()); + if ((eventbits | Event.ONKEYDOWN) == Event.ONKEYDOWN) + dispatchEvent(document.createKeyDownEvent(false, false, false, false, keys[0], 0)); + if ((eventbits | Event.ONKEYPRESS) == Event.ONKEYPRESS) + dispatchEvent(document.createKeyPressEvent(false, false, false, false, keys[0], 0)); + if ((eventbits | Event.ONKEYUP) == Event.ONKEYUP) + dispatchEvent(document.createKeyUpEvent(false, false, false, false, keys[0], 0)); + if ((eventbits | Event.ONLOSECAPTURE) == Event.ONLOSECAPTURE) + triggerHtmlEvent("losecapture"); + if ((eventbits | Event.ONMOUSEDOWN) == Event.ONMOUSEDOWN) + dispatchEvent(document.createMouseDownEvent(0, 0, 0, 0, 0, false, false, false, false, NativeEvent.BUTTON_LEFT)); + if ((eventbits | Event.ONMOUSEMOVE) == Event.ONMOUSEMOVE) + dispatchEvent(document.createMouseMoveEvent(0, 0, 0, 0, 0, false, false, false, false, NativeEvent.BUTTON_LEFT)); + if ((eventbits | Event.ONMOUSEOUT) == Event.ONMOUSEOUT) + dispatchEvent(document.createMouseOutEvent(0, 0, 0, 0, 0, false, false, false, false, NativeEvent.BUTTON_LEFT, null)); + if ((eventbits | Event.ONMOUSEOVER) == Event.ONMOUSEOVER) + dispatchEvent(document.createMouseOverEvent(0, 0, 0, 0, 0, false, false, false, false, NativeEvent.BUTTON_LEFT, null)); + if ((eventbits | Event.ONMOUSEUP) == Event.ONMOUSEUP) + dispatchEvent(document.createMouseUpEvent(0, 0, 0, 0, 0, false, false, false, false, NativeEvent.BUTTON_LEFT)); + if ((eventbits | Event.ONSCROLL) == Event.ONSCROLL) + dispatchEvent(document.createScrollEvent()); + if ((eventbits | Event.ONERROR) == Event.ONERROR) + dispatchEvent(document.createErrorEvent()); + if ((eventbits | Event.ONMOUSEWHEEL) == Event.ONMOUSEWHEEL) + dispatchEvent(document.createMouseEvent("mousewheel", true, true, 0, 0, 0, 0, 0, false, false, false, false, NativeEvent.BUTTON_LEFT, null)); + return this; + } + + protected GQuery triggerHtmlEvent(String htmlEvent) { + dispatchEvent(document.createHtmlEvent(htmlEvent, false, false)); return this; } /** * Removes all handlers, that matches the events bits passed, from each * element. - * + * * Example: unbind(Event.ONCLICK | Event.ONMOUSEOVER) */ public GQuery unbind(int eventbits) { @@ -87,101 +131,10 @@ public class Events extends GQuery { } return this; } -} - -/** - * Just a class with static methods for firing element events on demand. - */ -class FireEvents { - - public static void fire(Element element, int eventbits, int... keys) { - Event event = null; - - String type = getEventTypeStr(eventbits); - - if ((eventbits & Event.MOUSEEVENTS) != 0 - || (eventbits | Event.ONCLICK) == Event.ONCLICK) { - event = createMouseEventImpl(type); - } else if ((eventbits & Event.KEYEVENTS) != 0) { - event = createKeyEventImpl(type, keys[0]); - } else if ((eventbits & Event.FOCUSEVENTS) != 0) { - event = createHtmlEventImpl(type); - } - - dispatchEvent(element, event); - } - - private static native Event createHtmlEventImpl(String type) /*-{ - var event = $doc.createEvent('HTMLEvents'); - event.initEvent( type, true, true); - return event; - }-*/; - - private static native Event createKeyEventImpl(String type, int keycode) /*-{ - var event; - if( $wnd.KeyEvent ) { - event = $doc.createEvent('KeyEvents'); - event.initKeyEvent( type, true, true, $wnd, false, false, false, false, keycode, 0 ); - } else { - event = $doc.createEvent('UIEvents'); - event.initUIEvent( type, true, true, $wnd, 1 ); - event.keyCode = keycode; - } - return event; - }-*/; - - private static native Event createMouseEventImpl(String type) /*-{ - var event = $doc.createEvent('MouseEvents'); - event.initEvent(type, true, true); - return event; - }-*/; - - private static native void dispatchEvent(Element elem, Event event) /*-{ - elem.dispatchEvent(event); - if (event.type == 'focus' && elem.focus) - elem.focus(); - else if (event.type == 'blur' && elem.focus) - elem.blur(); - }-*/; - - private static String getEventTypeStr(int type) { - switch (type) { - case Event.ONBLUR: - return "blur"; - case Event.ONCHANGE: - return "change"; - case Event.ONCLICK: - return "click"; - case Event.ONDBLCLICK: - return "dblclick"; - case Event.ONFOCUS: - return "focus"; - case Event.ONKEYDOWN: - return "keydown"; - case Event.ONKEYPRESS: - return "keypress"; - case Event.ONKEYUP: - return "keyup"; - case Event.ONLOSECAPTURE: - return "losecapture"; - case Event.ONMOUSEDOWN: - return "mousedown"; - case Event.ONMOUSEMOVE: - return "mousemove"; - case Event.ONMOUSEOUT: - return "mouseout"; - case Event.ONMOUSEOVER: - return "mouseover"; - case Event.ONMOUSEUP: - return "mouseup"; - case Event.ONSCROLL: - return "scroll"; - case Event.ONERROR: - return "error"; - case Event.ONMOUSEWHEEL: - return "mousewheel"; - default: - return ""; + + private void dispatchEvent(NativeEvent evt) { + for (Element e : elements()) { + e.dispatchEvent(evt); } } -} \ No newline at end of file +} diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/EventsListener.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/EventsListener.java index 5f40530b..aa27e0b1 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/EventsListener.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/EventsListener.java @@ -21,14 +21,14 @@ import com.google.gwt.user.client.Event; import com.google.gwt.user.client.EventListener; /** - * This class implements an event queue instance for one element. This queue + * This class implements an event queue instance for one Element. The queue * instance is configured as the default event listener in GWT. - * - * The reference to this queue is stored as a uniq variable in the element's + * + * The reference to this queue is stored as a unique variable in the element's * DOM - * - * The class takes care of calling the appropiate functions for each browser - * event and also calls sinkEvents methods. + * + * The class takes care of calling the appropriate functions for each browser + * event and it also calls sinkEvents method. */ class EventsListener implements EventListener { @@ -84,8 +84,8 @@ class EventsListener implements EventListener { elem.__gqueryevent = gqevent; }-*/; - private JsObjectArray elementEvents - = JsObjectArray.createArray().cast(); + private JsObjectArray elementEvents = JsObjectArray + .createArray().cast(); private Element element; @@ -100,25 +100,22 @@ class EventsListener implements EventListener { if (function == null) { unbind(eventbits); } else { - DOM.sinkEvents((com.google.gwt.user.client.Element) element, - eventbits | DOM - .getEventsSunk((com.google.gwt.user.client.Element) element)); + DOM.sinkEvents((com.google.gwt.user.client.Element) element, eventbits + | DOM.getEventsSunk((com.google.gwt.user.client.Element) element)); if ((eventbits | Event.FOCUSEVENTS) == Event.FOCUSEVENTS) { setFocusable(element); } - elementEvents.add( - new EventsListener.BindFunction(eventbits, function, data, times)); + elementEvents.add(new EventsListener.BindFunction(eventbits, function, + data, times)); } } - public void bind(int eventbits, final Object data, final Function function) { - bind(eventbits, data, function, -1); - } - - public void fire(int eventbits, int... keys) { - FireEvents.fire(element, eventbits, keys); + public void bind(int eventbits, final Object data, Function...funcs) { + for (Function function: funcs) { + bind(eventbits, data, function, -1); + } } public void onBrowserEvent(Event event) { @@ -127,9 +124,11 @@ class EventsListener implements EventListener { EventsListener.BindFunction listener = elementEvents.get(i); if (listener.hasEventType(etype)) { if (!listener.fire(event)) { - event.cancelBubble(true); + System.out.println("Return: " + false); event.stopPropagation(); event.preventDefault(); + } else { + System.out.println("Return: " + true); } } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java index efe340a5..36318ad8 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java @@ -27,7 +27,6 @@ import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.IFrameElement; import com.google.gwt.dom.client.InputElement; -import com.google.gwt.dom.client.NativeEvent; import com.google.gwt.dom.client.Node; import com.google.gwt.dom.client.NodeList; import com.google.gwt.dom.client.OptionElement; @@ -298,7 +297,7 @@ public class GQuery implements Lazy { * Wrap a GQuery around an event's target element. */ public static GQuery $(Event event) { - return $(event.getCurrentTarget()); + return $((Element)event.getCurrentEventTarget().cast()); } /** @@ -638,46 +637,45 @@ public class GQuery implements Lazy { } /** - * Binds a handler to a particular Event (like Event.ONCLICK) for each matched - * element. - * - * The event handler is passed as a Function that you can use to prevent - * default behaviour. To stop both default action and event bubbling, the + * Binds a set of handlers to a particular Event for each matched element. + * + * The event handlers are passed as Functions that you can use to prevent + * default behavior. To stop both default action and event bubbling, the * function event handler has to return false. - * + * * You can pass an additional Object data to your Function as the second * parameter + * */ - public GQuery bind(int eventbits, final Object data, final Function f) { - return as(Events).bind(eventbits, data, f); - } - - /** - * Bind a function to the blur event of each matched element. - */ - public GQuery blur(Function f) { - return bind(Event.ONBLUR, null, f); + public GQuery bind(int eventbits, final Object data, final Function...funcs) { + return as(Events).bind(eventbits, data, funcs); } /** - * Trigger a blur event. + * Bind Handlers or fire Events for each matched element. */ - public GQuery blur() { - return trigger(document.createBlurEvent(), null); + private GQuery bindOrFire(int eventbits, final Object data, final Function...funcs) { + if (funcs.length == 0) { + return trigger(eventbits); + } else { + return bind(eventbits, data, funcs); + } } /** - * Bind a function to the change event of each matched element. + * Bind a set of functions to the blur event of each matched element. + * Or trigger the event if no functions are provided. */ - public GQuery change(Function f) { - return bind(Event.ONCHANGE, null, f); + public GQuery blur(Function...f) { + return bindOrFire(Event.ONBLUR, null, f); } /** - * Trigger a change event. + * Bind a set of functions to the change event of each matched element. + * Or trigger the event if no functions are provided. */ - public GQuery change() { - return trigger(document.createChangeEvent(), null); + public GQuery change(Function...f) { + return bindOrFire(Event.ONCHANGE, null, f); } /** @@ -703,22 +701,13 @@ public class GQuery implements Lazy { } /** - * Trigger a click event. - */ - public GQuery click() { - return trigger( - document.createClickEvent(0, 0, 0, 0, 0, false, false, false, - false), null); - } - - /** - * Triggers the click event of each matched element. Causes all of the - * functions that have been bound to that click event to be executed. + * Bind a set of functions to the click event of each matched element. + * Or trigger the event if no functions are provided. */ - public GQuery click(final Function f) { - return bind(Event.ONCLICK, null, f); + public GQuery click(Function...f) { + return bindOrFire(Event.ONCLICK, null, f); } - + /** * Clone matched DOM Elements and select the clones. This is useful for moving * copies of the elements to another location in the DOM. @@ -858,21 +847,13 @@ public class GQuery implements Lazy { } return this; } - - /** - * Trigger a double click event. - */ - public GQuery dblclick() { - return trigger( - document.createDblClickEvent(0, 0, 0, 0, 0, false, false, false, - false), null); - } - + /** - * Bind a function to the dblclick event of each matched element. + * Bind a set of functions to the dblclick event of each matched element. + * Or trigger the event if no functions are provided. */ - public GQuery dblclick(Function f) { - return bind(Event.ONDBLCLICK, null, f); + public GQuery dblclick(Function...f) { + return bindOrFire(Event.ONDBLCLICK, null, f); } /** @@ -946,19 +927,13 @@ public class GQuery implements Lazy { } /** - * Trigger an error event. + * Bind a set of functions to the error event of each matched element. + * Or trigger the event if no functions are provided. */ - public GQuery error() { - return trigger(document.createErrorEvent(), null); - } - - /** - * Bind a function to the error event of each matched element. - */ - public GQuery error(Function f) { - return bind(Event.ONERROR, null, f); - } - + public GQuery error(Function...f) { + return bindOrFire(Event.ONERROR, null, f); + } + /** * Fade in all matched elements by adjusting their opacity. */ @@ -1048,20 +1023,13 @@ public class GQuery implements Lazy { } /** - * Trigger a focus event. + * Bind a set of functions to the focus event of each matched element. + * Or trigger the event if no functions are provided. */ - public GQuery focus() { - return trigger(document.createFocusEvent(), null); - } - - /** - * Bind a function to the focus event of each matched element. - */ - - public GQuery focus(Function f) { - return bind(Event.ONFOCUS, null, f); - } - + public GQuery focus(Function...f) { + return bindOrFire(Event.ONFOCUS, null, f); + } + /** * Return all elements matched in the GQuery as a NodeList. @see #elements() * for a method which returns them as an immutable Java array. @@ -1264,54 +1232,51 @@ public class GQuery implements Lazy { public boolean is(String... filters) { return filter(filters).size() > 0; } - + /** - * Trigger a keydown event. + * Bind a set of functions to the keydown event of each matched element. + * Or trigger the event if no functions are provided. */ - public GQuery keydown() { - return trigger( - document.createKeyDownEvent(false, false, false, false, 0, 0), - null); - } + public GQuery keydown(Function...f) { + return bindOrFire(Event.ONKEYDOWN, null, f); + } /** - * Bind a function to the keydown event of each matched element. + * Trigger a keydown event passing the key pushed */ - public GQuery keydown(Function f) { - return bind(Event.ONKEYDOWN, null, f); + public GQuery keydown(int key) { + return trigger(Event.ONKEYDOWN, key); } /** - * Trigger a keypress event. + * Bind a set of functions to the keypress event of each matched element. + * Or trigger the event if no functions are provided. */ - public GQuery keypress() { - return trigger( - document.createKeyPressEvent(false, false, false, false, 0, 0), - null); - } + public GQuery keypress(Function...f) { + return bindOrFire(Event.ONKEYPRESS, null, f); + } /** - * Bind a function to the keypress event of each matched element. + * Trigger a keypress event passing the key pushed */ - public GQuery keypressed(Function f) { - return bind(Event.ONKEYPRESS, null, f); + public GQuery keypress(int key) { + return trigger(Event.ONKEYPRESS, key); } /** - * Trigger a keyup event. + * Bind a set of functions to the keyup event of each matched element. + * Or trigger the event if no functions are provided. */ - public GQuery keyup() { - return trigger( - document.createKeyUpEvent(false, false, false, false, 0, 0), - null); - } + public GQuery keyup(Function...f) { + return bindOrFire(Event.ONKEYUP, null, f); + } /** - * Bind a function to the keyup event of each matched element. + * Trigger a keyup event passing the key pushed */ - public GQuery keyup(Function f) { - return bind(Event.ONKEYUP, null, f); - } + public GQuery keyup(int key) { + return trigger(Event.ONKEYUP, key); + } /** * Returns the number of elements currently matched. The size function will @@ -1336,41 +1301,46 @@ public class GQuery implements Lazy { public GQuery lt(int pos) { return $(slice(0, pos)); } - + /** - * Bind a function to the mousedown event of each matched element. + * Bind a set of functions to the mousedown event of each matched element. + * Or trigger the event if no functions are provided. */ - public GQuery mousedown(Function f) { - return bind(Event.ONMOUSEDOWN, null, f); - } - + public GQuery mousedown(Function...f) { + return bindOrFire(Event.ONMOUSEDOWN, null, f); + } + /** - * Bind a function to the mousemove event of each matched element. + * Bind a set of functions to the mousemove event of each matched element. + * Or trigger the event if no functions are provided. */ - public GQuery mousemove(Function f) { - return bind(Event.ONMOUSEMOVE, null, f); - } + public GQuery mousemove(Function...f) { + return bindOrFire(Event.ONMOUSEMOVE, null, f); + } /** - * Bind a function to the mouseout event of each matched element. + * Bind a set of functions to the mouseout event of each matched element. + * Or trigger the event if no functions are provided. */ - public GQuery mouseout(Function f) { - return bind(Event.ONMOUSEOUT, null, f); - } + public GQuery mouseout(Function...f) { + return bindOrFire(Event.ONMOUSEOUT, null, f); + } /** - * Bind a function to the mouseover event of each matched element. + * Bind a set of functions to the mouseover event of each matched element. + * Or trigger the event if no functions are provided. */ - public GQuery mouseover(Function f) { - return bind(Event.ONMOUSEOVER, null, f); - } - + public GQuery mouseover(Function...f) { + return bindOrFire(Event.ONMOUSEOVER, null, f); + } + /** - * Bind a function to the mouseup event of each matched element. + * Bind a set of functions to the mouseup event of each matched element. + * Or trigger the event if no functions are provided. */ - public GQuery mouseup(Function f) { - return bind(Event.ONMOUSEUP, null, f); - } + public GQuery mouseup(Function...f) { + return bindOrFire(Event.ONMOUSEUP, null, f); + } /** * Get a set of elements containing the unique next siblings of each of the @@ -1816,13 +1786,15 @@ public class GQuery implements Lazy { return replaceWith($(elem)); } + /** - * Bind a function to the scroll event of each matched element. + * Bind a set of functions to the scroll event of each matched element. + * Or trigger the event if no functions are provided. */ - public GQuery scroll(Function f) { - return bind(Event.ONSCROLL, null, f); - } - + public GQuery scroll(Function...f) { + return bindOrFire(Event.ONSCROLL, null, f); + } + /** * When a value is passed in, the scroll left offset is set to that value on * all matched elements. This method works for both visible and hidden @@ -1886,8 +1858,7 @@ public class GQuery implements Lazy { } public GQuery select() { - return trigger(document.createHtmlEvent("select", false, false), - null); + return as(Events).triggerHtmlEvent("select"); } /** @@ -1973,8 +1944,7 @@ public class GQuery implements Lazy { } public GQuery submit() { - return trigger(document.createHtmlEvent("submit", false, false), - null); + return as(Events).triggerHtmlEvent("submit"); } /** @@ -2071,11 +2041,18 @@ public class GQuery implements Lazy { return r; } + /** - * Trigger an event of type eventbits on every matched element. + * Trigger a set of events on each matched element. + * + * For keyboard events you can pass a second parameter which represents + * the key-code of the pushed key. + * + * Example: fire(Event.ONCLICK | Event.ONFOCUS) + * Example: fire(Event.ONKEYDOWN. 'a'); */ public GQuery trigger(int eventbits, int... keys) { - return as(Events).fire(eventbits, keys); + return as(Events).trigger(eventbits, keys); } /** @@ -2619,13 +2596,6 @@ public class GQuery implements Lazy { } } - private GQuery trigger(NativeEvent event, Object o) { - for (Element e : elements()) { - e.dispatchEvent(event); - } - return this; - } - private static native Element window() /*-{ return $wnd; }-*/; diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GwtEventsTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GwtEventsTest.java index 8e8ecbc2..d6509b23 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GwtEventsTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GwtEventsTest.java @@ -18,7 +18,8 @@ package com.google.gwt.query.client; import static com.google.gwt.query.client.GQuery.$; import com.google.gwt.dom.client.Element; -import com.google.gwt.dom.client.InputElement; +import com.google.gwt.junit.DoNotRunWith; +import com.google.gwt.junit.Platform; import com.google.gwt.junit.client.GWTTestCase; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.ui.HTML; @@ -48,7 +49,6 @@ public class GwtEventsTest extends GWTTestCase { } } - // FIXME: this test is broken in IE, and in chrome ONKEYPRESS does not work public void testEventsPlugin() { $(e).html("

Content

"); @@ -57,14 +57,19 @@ public class GwtEventsTest extends GWTTestCase { public void f(Element elem) { $(elem).css("color", "red"); } + }, new Function() { + public void f(Element elem) { + $(elem).css("background", "green"); + } }); - $("p", e, Events.Events).fire(Event.ONCLICK); + $("p", e, Events.Events).trigger(Event.ONCLICK); assertEquals("red", $("p", e).css("color")); + assertEquals("green", $("p", e).css("background-color")); // unbind $("p", e).css("color", "white"); $("p", e).unbind(Event.ONCLICK); - $("p", e).trigger(Event.ONCLICK); + $("p", e).click(); assertEquals("white", $("p", e).css("color")); // toggle @@ -78,9 +83,9 @@ public class GwtEventsTest extends GWTTestCase { $(elem).css("color", "blue"); } }); - $("p", e, Events.Events).fire(Event.ONCLICK); + $("p", e).click(); assertEquals("red", $("p", e).css("color")); - $("p", e, Events.Events).fire(Event.ONCLICK); + $("p", e).click(); assertEquals("blue", $("p", e).css("color")); // one @@ -90,10 +95,10 @@ public class GwtEventsTest extends GWTTestCase { $(elem).css("color", "red"); } }); - $("p", e).trigger(Event.ONCLICK); + $("p", e).click(); assertEquals("red", $("p", e).css("color")); $("p", e).css("color", "white"); - $("p", e).trigger(Event.ONCLICK); + $("p", e).click(); assertEquals("white", $("p", e).css("color")); // hover (mouseover, mouseout) @@ -117,32 +122,52 @@ public class GwtEventsTest extends GWTTestCase { $(elem).css("border", "1px dotted black"); } }); - $("p", e).trigger(Event.ONFOCUS); - assertEquals("1px dotted black", $("p", e).css("border")); - + $("p", e).focus(); + assertEquals("black", $("p", e).css("border-top-color")); + assertEquals("dotted", $("p", e).css("border-top-style")); + assertEquals("1px", $("p", e).css("border-top-width")); + // blur $("p", e).blur(new Function() { public void f(Element elem) { $(elem).css("border", ""); } }); - $("p", e).trigger(Event.ONBLUR); + $("p", e).blur(); assertEquals("", $("p", e).css("border")); - // keypressed + // key events $(e).html(""); - $("input", e).keypressed(new Function() { + Function keyEventAction = new Function() { public boolean f(Event evnt) { - Element elem = evnt.getCurrentEventTarget().cast(); - InputElement input = InputElement.as(elem); - input.setValue( - input.getValue() + Character.toString((char) evnt.getKeyCode())); + GQuery gq = $(evnt); + gq.val(gq.val() + Character.toString((char) evnt.getKeyCode())); return false; } + }; + $("input", e).keypress(keyEventAction); + $("input", e).keydown(keyEventAction); + $("input", e).keyup(keyEventAction); + $("input", e).focus(); + $("input", e).keydown('a'); + $("input", e).keypress('b'); + $("input", e).keyup('c'); + assertEquals("abc", $("input", e).val()); + } + + /** + * TODO: DblClick doesn't work with HtmlUnit, investigate and report. + */ + @DoNotRunWith(Platform.HtmlUnit) + public void testEventsDblClick() { + $(e).html("

Content

"); + $("p", e).css("color", "white"); + $("p", e).dblclick(new Function() { + public void f(Element elem) { + $(elem).css("color", "yellow"); + } }); - $("input", e).trigger(Event.ONFOCUS); - $("input", e).trigger(Event.ONKEYPRESS, 'a'); - assertEquals("a", InputElement.as($("input", e).get(0)).getValue()); + $("p", e).dblclick(); + assertEquals("yellow", $("p", e).css("color")); } - }