Fixes a backward compatibility bug in DeckPanel where the offsetWidth and offsetHeight of a child widget weren't defined in the child widget's onLoad method. By rearranging the order in which the container element is initialized and the child widget is adopted, we made the offsetWidth/Height available. Patch by: ecc, fredsa, jlabanca Review by: jlabanca, ecc (pair programming) Issue: 2321 git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@2510 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/client/ui/DeckPanel.java b/user/src/com/google/gwt/user/client/ui/DeckPanel.java index 8b2337b..efac341 100644 --- a/user/src/com/google/gwt/user/client/ui/DeckPanel.java +++ b/user/src/com/google/gwt/user/client/ui/DeckPanel.java
@@ -203,11 +203,20 @@ */ @Override public void add(Widget w) { - Element container = DOM.createDiv(); + Element container = createWidgetContainer(); DOM.appendChild(getElement(), container); - initChildWidget(w); - initWidgetContainer(container); + + // The order of these methods is very important. In order to preserve + // backward compatibility, the offsetWidth and offsetHeight of the child + // widget should be defined (greater than zero) when w.onLoad() is called. + // As a result, we first initialize the container with a height of 0px, then + // we attach the child widget to the container. See Issue 2321 for more + // details. super.add(w, container); + + // After w.onLoad is called, it is safe to make the container invisible and + // set the height of the container and widget to 100%. + finishWidgetInitialization(container, w); } /** @@ -228,11 +237,12 @@ * range */ public void insert(Widget w, int beforeIndex) { - Element container = DOM.createDiv(); + Element container = createWidgetContainer(); DOM.insertChild(getElement(), container, beforeIndex); - initChildWidget(w); - initWidgetContainer(container); + + // See add(Widget) for important comments super.insert(w, container, beforeIndex, true); + finishWidgetInitialization(container, w); } /** @@ -285,22 +295,25 @@ } /** - * Set the widget's width and height to full. - */ - private void initChildWidget(Widget w) { - w.setSize("100%", "100%"); - } - - /** * Setup the container around the widget. */ - private void initWidgetContainer(Element container) { + private Element createWidgetContainer() { + Element container = DOM.createDiv(); DOM.setStyleAttribute(container, "width", "100%"); - DOM.setStyleAttribute(container, "height", "100%"); + DOM.setStyleAttribute(container, "height", "0px"); DOM.setStyleAttribute(container, "overflow", "hidden"); DOM.setStyleAttribute(container, "padding", "0px"); DOM.setStyleAttribute(container, "margin", "0px"); + return container; + } + + /** + * Setup the container around the widget. + */ + private void finishWidgetInitialization(Element container, Widget w) { UIObject.setVisible(container, false); + DOM.setStyleAttribute(container, "height", "100%"); + w.setSize("100%", "100%"); } /**
diff --git a/user/test/com/google/gwt/user/UISuite.java b/user/test/com/google/gwt/user/UISuite.java index e5fb785..1106526 100644 --- a/user/test/com/google/gwt/user/UISuite.java +++ b/user/test/com/google/gwt/user/UISuite.java
@@ -25,6 +25,7 @@ import com.google.gwt.user.client.ui.CompositeTest; import com.google.gwt.user.client.ui.CustomButtonTest; import com.google.gwt.user.client.ui.DOMTest; +import com.google.gwt.user.client.ui.DeckPanelTest; import com.google.gwt.user.client.ui.DecoratorPanelTest; import com.google.gwt.user.client.ui.DelegatingKeyboardListenerCollectionTest; import com.google.gwt.user.client.ui.DialogBoxTest; @@ -83,6 +84,7 @@ suite.addTestSuite(CompositeTest.class); suite.addTestSuite(CookieTest.class); suite.addTestSuite(CustomButtonTest.class); + suite.addTestSuite(DeckPanelTest.class); suite.addTestSuite(DecoratorPanelTest.class); suite.addTestSuite(DelegatingKeyboardListenerCollectionTest.class); suite.addTestSuite(DialogBoxTest.class);
diff --git a/user/test/com/google/gwt/user/client/ui/DeckPanelTest.java b/user/test/com/google/gwt/user/client/ui/DeckPanelTest.java new file mode 100644 index 0000000..c3f32e9 --- /dev/null +++ b/user/test/com/google/gwt/user/client/ui/DeckPanelTest.java
@@ -0,0 +1,53 @@ +/* + * 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.junit.client.GWTTestCase; + +/** + * Test for {@link DeckPanel}. + */ +public class DeckPanelTest extends GWTTestCase { + @Override + public String getModuleName() { + return "com.google.gwt.user.User"; + } + + /** + * Test that the offsetHeight/Width of a widget are defined when the widget is + * added to the DeckPanel. + */ + public void testWidgetOffsetDimensionsOnload() { + DeckPanel deck = new DeckPanel(); + RootPanel.get().add(deck); + + // Add a widget to the DeckPanel + Label content = new Label("detached") { + @Override + public void onLoad() { + // Verify that the offsetWidth/Height are greater than zero + assertTrue(this.getOffsetHeight() > 0); + assertTrue(this.getOffsetWidth() > 0); + setText("attached"); + } + }; + deck.add(content); + + // Verify content.onLoad was actually called + assertEquals("attached", content.getText()); + } +}