Author: j...@google.com Date: Wed Feb 4 10:49:05 2009 New Revision: 4624 Added: branches/snapshot-2009.01.29/user/test/com/google/gwt/user/client/ui/ButtonTest.java Modified: branches/snapshot-2009.01.29/user/src/com/google/gwt/dom/client/ButtonElement.java branches/snapshot-2009.01.29/user/src/com/google/gwt/dom/client/DOMImpl.java branches/snapshot-2009.01.29/user/src/com/google/gwt/dom/client/DOMImplMozillaOld.java branches/snapshot-2009.01.29/user/src/com/google/gwt/user/client/ui/Button.java branches/snapshot-2009.01.29/user/test/com/google/gwt/user/UISuite.java
Log: Fixes obscure Old-Mozilla bug that caused events created by ButtonElement.click() to have the wrong target element. Patch by: jgw Review by: rjrjr (Desk check) Modified: branches/snapshot-2009.01.29/user/src/com/google/gwt/dom/client/ButtonElement.java ============================================================================== --- branches/snapshot-2009.01.29/user/src/com/google/gwt/dom/client/ButtonElement.java (original) +++ branches/snapshot-2009.01.29/user/src/com/google/gwt/dom/client/ButtonElement.java Wed Feb 4 10:49:05 2009 @@ -40,9 +40,9 @@ /** * Simulate a mouse-click. */ - public final native void click() /*-{ - this.click(); - }-*/; + public final void click() { + DOMImpl.impl.buttonClick(this); + } /** * A single character access key to give access to the form control. Modified: branches/snapshot-2009.01.29/user/src/com/google/gwt/dom/client/DOMImpl.java ============================================================================== --- branches/snapshot-2009.01.29/user/src/com/google/gwt/dom/client/DOMImpl.java (original) +++ branches/snapshot-2009.01.29/user/src/com/google/gwt/dom/client/DOMImpl.java Wed Feb 4 10:49:05 2009 @@ -21,6 +21,10 @@ static final DOMImpl impl = GWT.create(DOMImpl.class); + public native void buttonClick(ButtonElement button) /*-{ + button.click(); + }-*/; + public native Element createElement(String tag) /*-{ return $doc.createElement(tag); }-*/; Modified: branches/snapshot-2009.01.29/user/src/com/google/gwt/dom/client/DOMImplMozillaOld.java ============================================================================== --- branches/snapshot-2009.01.29/user/src/com/google/gwt/dom/client/DOMImplMozillaOld.java (original) +++ branches/snapshot-2009.01.29/user/src/com/google/gwt/dom/client/DOMImplMozillaOld.java Wed Feb 4 10:49:05 2009 @@ -24,6 +24,16 @@ */ class DOMImplMozillaOld extends DOMImplMozilla { + public native void buttonClick(ButtonElement button) /*-{ + var doc = button.ownerDocument; + if (doc != null) { + var evt = doc.createEvent('MouseEvents'); + evt.initMouseEvent('click', true, true, null, 0, 0, + 0, 0, 0, false, false, false, false, 0, null); + button.dispatchEvent(evt); + } + }-*/; + @Override public native int getAbsoluteLeft(Element elem) /*-{ var style = $doc.defaultView.getComputedStyle(elem, null); Modified: branches/snapshot-2009.01.29/user/src/com/google/gwt/user/client/ui/Button.java ============================================================================== --- branches/snapshot-2009.01.29/user/src/com/google/gwt/user/client/ui/Button.java (original) +++ branches/snapshot-2009.01.29/user/src/com/google/gwt/user/client/ui/Button.java Wed Feb 4 10:49:05 2009 @@ -18,6 +18,7 @@ import com.google.gwt.dom.client.ButtonElement; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.ButtonElement; import com.google.gwt.event.dom.client.ClickHandler; /** @@ -71,10 +72,6 @@ } }-*/; - static native void click(Element button) /*-{ - button.click(); - }-*/; - /** * Creates a button with no caption. */ @@ -133,6 +130,11 @@ * Programmatic equivalent of the user clicking the button. */ public void click() { - click(getElement()); + getButtonElement().click(); + } + + private ButtonElement getButtonElement() { + return getElement().cast(); } } + Modified: branches/snapshot-2009.01.29/user/test/com/google/gwt/user/UISuite.java ============================================================================== --- branches/snapshot-2009.01.29/user/test/com/google/gwt/user/UISuite.java (original) +++ branches/snapshot-2009.01.29/user/test/com/google/gwt/user/UISuite.java Wed Feb 4 10:49:05 2009 @@ -23,6 +23,7 @@ import com.google.gwt.user.client.WindowTest; import com.google.gwt.user.client.ui.AbsolutePanelTest; import com.google.gwt.user.client.ui.AnchorTest; +import com.google.gwt.user.client.ui.ButtonTest; import com.google.gwt.user.client.ui.CaptionPanelTest; import com.google.gwt.user.client.ui.CheckBoxTest; import com.google.gwt.user.client.ui.CompositeTest; @@ -95,6 +96,7 @@ suite.addTestSuite(AbsolutePanelTest.class); suite.addTestSuite(AnchorTest.class); suite.addTestSuite(AsyncProxyTest.class); + suite.addTestSuite(ButtonTest.class); suite.addTestSuite(CaptionPanelTest.class); suite.addTestSuite(CheckBoxTest.class); suite.addTestSuite(ClippedImagePrototypeTest.class); Added: branches/snapshot-2009.01.29/user/test/com/google/gwt/user/client/ui/ButtonTest.java ============================================================================== --- (empty file) +++ branches/snapshot-2009.01.29/user/test/com/google/gwt/user/client/ui/ButtonTest.java Wed Feb 4 10:49:05 2009 @@ -0,0 +1,57 @@ +/* + * Copyright 2008 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.user.client.ui; + +import com.google.gwt.dom.client.Element; +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.event.dom.client.ClickHandler; +import com.google.gwt.junit.client.GWTTestCase; + +/** + * Tests for {...@link Button}. + */ +public class ButtonTest extends GWTTestCase { + + public String getModuleName() { + return "com.google.gwt.user.User"; + } + + private class H implements ClickHandler { + boolean clicked; + Element target; + + public void onClick(ClickEvent event) { + target = event.getNativeEvent().getTarget(); + clicked = true; + } + } + + public void testClick() { + Button b = new Button(); + RootPanel.get().add(b); + + H h = new H(); + b.addClickHandler(h); + + b.click(); + assertTrue(h.clicked); + + // Old Mozilla browsers don't set up the event target properly for + // synthesized clicks. This tests the workaround in DOMImplMozillaOld. + assertEquals(b.getElement(), h.target); + } +} + --~--~---------~--~----~------------~-------~--~----~ http://groups.google.com/group/Google-Web-Toolkit-Contributors -~----------~----~----~----~------~----~------~--~---