Adds a lot of missing IsWidget support, especially for UiBinder. Addresses http://code.google.com/p/google-web-toolkit/issues/detail?id=5879 Review by rjrjr@google.com http://gwt-code-reviews.appspot.com/1295806 Review by: sbrubaker@google.com git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@9744 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/uibinder/elementparsers/HasTreeItemsParser.java b/user/src/com/google/gwt/uibinder/elementparsers/HasTreeItemsParser.java index 6151afa..18bca99 100644 --- a/user/src/com/google/gwt/uibinder/elementparsers/HasTreeItemsParser.java +++ b/user/src/com/google/gwt/uibinder/elementparsers/HasTreeItemsParser.java
@@ -19,6 +19,7 @@ import com.google.gwt.core.ext.typeinfo.JClassType; import com.google.gwt.uibinder.rebind.UiBinderWriter; import com.google.gwt.uibinder.rebind.XMLElement; +import com.google.gwt.user.client.ui.IsWidget; import com.google.gwt.user.client.ui.TreeItem; import com.google.gwt.user.client.ui.Widget; @@ -34,6 +35,7 @@ // Prepare base types. JClassType itemType = writer.getOracle().findType(TreeItem.class.getName()); JClassType widgetType = writer.getOracle().findType(Widget.class.getName()); + JClassType isWidgetType = writer.getOracle().findType(IsWidget.class.getName()); // Parse children. for (XMLElement child : elem.consumeChildElements()) { @@ -46,8 +48,8 @@ continue; } - // Widget+ - if (widgetType.isAssignableFrom(childType)) { + // Widget+ or IsWidget+ + if (widgetType.isAssignableFrom(childType) || isWidgetType.isAssignableFrom(childType)) { String childFieldName = writer.parseElementToField(child); writer.addStatement("%1$s.addItem(%2$s);", fieldName, childFieldName); continue;
diff --git a/user/src/com/google/gwt/user/client/ui/AbsolutePanel.java b/user/src/com/google/gwt/user/client/ui/AbsolutePanel.java index 66bb445..9fe27ca 100644 --- a/user/src/com/google/gwt/user/client/ui/AbsolutePanel.java +++ b/user/src/com/google/gwt/user/client/ui/AbsolutePanel.java
@@ -117,6 +117,15 @@ insert(w, beforeIndex); verifyPositionNotStatic(w); } + + /** + * Overloaded version for IsWidget. + * + * @see #add(Widget,int,int) + */ + public void add(IsWidget w, int left, int top) { + this.add(w.asWidget(),left,top); + } /** * Gets the position of the left outer border edge of the widget relative to
diff --git a/user/src/com/google/gwt/user/client/ui/CaptionPanel.java b/user/src/com/google/gwt/user/client/ui/CaptionPanel.java index 5fda0ff..647bc79 100644 --- a/user/src/com/google/gwt/user/client/ui/CaptionPanel.java +++ b/user/src/com/google/gwt/user/client/ui/CaptionPanel.java
@@ -31,7 +31,7 @@ * the upper left corner of the border. This is an implementation of the * fieldset HTML element. */ -public class CaptionPanel extends Composite implements HasWidgets { +public class CaptionPanel extends Composite implements HasWidgets.ForIsWidget { /** * Implementation class without browser-specific hacks. */ @@ -165,6 +165,15 @@ public void add(Widget w) { ((SimplePanel) getWidget()).add(w); } + + /** + * Overloaded version for IsWidget. + * + * @see #add(Widget) + */ + public void add(IsWidget w) { + this.add(asWidgetOrNull(w)); + } /** * Removes the content widget. @@ -220,6 +229,15 @@ public boolean remove(Widget w) { return ((SimplePanel) getWidget()).remove(w); } + + /** + * Overloaded version for IsWidget. + * + * @see #remove(Widget) + */ + public boolean remove(IsWidget w) { + return this.remove(asWidgetOrNull(w)); + } /** * Sets the caption for the panel using an HTML fragment. Pass in empty string
diff --git a/user/src/com/google/gwt/user/client/ui/CellPanel.java b/user/src/com/google/gwt/user/client/ui/CellPanel.java index 4c238fc..43be0b1 100644 --- a/user/src/com/google/gwt/user/client/ui/CellPanel.java +++ b/user/src/com/google/gwt/user/client/ui/CellPanel.java
@@ -110,6 +110,15 @@ td.setPropertyString("height", height); } } + + /** + * Overloaded version for IsWidget. + * + * @see #setCellHeight(Widget,String) + */ + public void setCellHeight(IsWidget w, String height) { + this.setCellHeight(w.asWidget(), height); + } /** * Sets the horizontal alignment of the given widget within its cell. @@ -125,6 +134,16 @@ setCellHorizontalAlignment(td, align); } } + + /** + * Overloaded version for IsWidget. + * + * @see #setCellHorizontalAlignment(Widget,HorizontalAlignmentConstant) + */ + public void setCellHorizontalAlignment(IsWidget w, + HorizontalAlignmentConstant align) { + this.setCellHorizontalAlignment(w.asWidget(), align); + } /** * Sets the vertical alignment of the given widget within its cell. @@ -139,6 +158,15 @@ setCellVerticalAlignment(td, align); } } + + /** + * Overloaded version for IsWidget. + * + * @see #setCellVerticalAlignment(Widget,VerticalAlignmentConstant) + */ + public void setCellVerticalAlignment(IsWidget w, VerticalAlignmentConstant align) { + this.setCellVerticalAlignment(w.asWidget(),align); + } /** * Sets the width of the cell associated with the given widget, related to the @@ -153,6 +181,15 @@ td.setPropertyString("width", width); } } + + /** + * Overloaded version for IsWidget. + * + * @see #setCellWidth(Widget,String) + */ + public void setCellWidth(IsWidget w, String width) { + this.setCellWidth(w.asWidget(), width); + } /** * Sets the amount of spacing between this panel's cells.
diff --git a/user/src/com/google/gwt/user/client/ui/DisclosurePanel.java b/user/src/com/google/gwt/user/client/ui/DisclosurePanel.java index a140939..506ef09 100644 --- a/user/src/com/google/gwt/user/client/ui/DisclosurePanel.java +++ b/user/src/com/google/gwt/user/client/ui/DisclosurePanel.java
@@ -81,7 +81,7 @@ */ @SuppressWarnings("deprecation") public final class DisclosurePanel extends Composite implements - FiresDisclosureEvents, HasWidgets, HasAnimation, + FiresDisclosureEvents, HasWidgets.ForIsWidget, HasAnimation, HasOpenHandlers<DisclosurePanel>, HasCloseHandlers<DisclosurePanel> { interface DefaultImages extends ClientBundle { @ImageOptions(flipRtl = true) @@ -456,6 +456,15 @@ "A DisclosurePanel can only contain two Widgets."); } } + + /** + * Overloaded version for IsWidget. + * + * @see #add(Widget) + */ + public void add(IsWidget w) { + this.add(asWidgetOrNull(w)); + } public HandlerRegistration addCloseHandler( CloseHandler<DisclosurePanel> handler) { @@ -538,6 +547,15 @@ } return false; } + + /** + * Overloaded version for IsWidget. + * + * @see #remove(Widget) + */ + public boolean remove(IsWidget w) { + return this.remove(asWidgetOrNull(w)); + } /** * Removes an event handler from the panel.
diff --git a/user/src/com/google/gwt/user/client/ui/DockLayoutPanel.java b/user/src/com/google/gwt/user/client/ui/DockLayoutPanel.java index a07d933..fcf6dab 100644 --- a/user/src/com/google/gwt/user/client/ui/DockLayoutPanel.java +++ b/user/src/com/google/gwt/user/client/ui/DockLayoutPanel.java
@@ -161,6 +161,15 @@ } /** + * Overloaded version for IsWidget. + * + * @see #addEast(Widget,double) + */ + public void addEast(IsWidget widget, double size) { + this.addEast(widget.asWidget(), size); + } + + /** * Adds a widget to the end of the line. In LTR mode, the widget is added to * the east. In RTL mode, the widget is added to the west. * @@ -191,6 +200,15 @@ public void addNorth(Widget widget, double size) { insert(widget, Direction.NORTH, size, null); } + + /** + * Overloaded version for IsWidget. + * + * @see #addNorth(Widget,double) + */ + public void addNorth(IsWidget widget, double size) { + this.addNorth(widget.asWidget(), size); + } /** * Adds a widget to the south edge of the dock. @@ -201,6 +219,15 @@ public void addSouth(Widget widget, double size) { insert(widget, Direction.SOUTH, size, null); } + + /** + * Overloaded version for IsWidget. + * + * @see #addSouth(Widget,double) + */ + public void addSouth(IsWidget widget, double size) { + this.addSouth(widget.asWidget(), size); + } /** * Adds a widget to the west edge of the dock. @@ -211,6 +238,15 @@ public void addWest(Widget widget, double size) { insert(widget, Direction.WEST, size, null); } + + /** + * Overloaded version for IsWidget. + * + * @see #addWest(Widget,double) + */ + public void addWest(IsWidget widget, double size) { + this.addWest(widget.asWidget(), size); + } public void animate(int duration) { animate(duration, null);
diff --git a/user/src/com/google/gwt/user/client/ui/DockPanel.java b/user/src/com/google/gwt/user/client/ui/DockPanel.java index f6e1cce..952d60e 100644 --- a/user/src/com/google/gwt/user/client/ui/DockPanel.java +++ b/user/src/com/google/gwt/user/client/ui/DockPanel.java
@@ -196,7 +196,16 @@ // Adopt. adopt(widget); } - + + /** + * Overloaded version for IsWidget. + * + * @see #add(Widget,DockLayoutConstant) + */ + public void add(IsWidget widget, DockLayoutConstant direction) { + this.add(widget.asWidget(), direction); + } + public HorizontalAlignmentConstant getHorizontalAlignment() { return horzAlign; }
diff --git a/user/src/com/google/gwt/user/client/ui/HTMLPanel.java b/user/src/com/google/gwt/user/client/ui/HTMLPanel.java index 2240ac7..e2a5091 100644 --- a/user/src/com/google/gwt/user/client/ui/HTMLPanel.java +++ b/user/src/com/google/gwt/user/client/ui/HTMLPanel.java
@@ -201,6 +201,16 @@ } /** + * Overloaded version for IsWidget. + * + * @see #addAndReplaceElement(Widget,Element) + */ + public void addAndReplaceElement(IsWidget widget, + com.google.gwt.user.client.Element toReplace) { + this.addAndReplaceElement(widget.asWidget(),toReplace); + } + + /** * Adds a child widget to the panel, replacing the HTML element specified by a * given id. * @@ -218,6 +228,15 @@ } /** + * Overloaded version for IsWidget. + * + * @see #addAndReplaceElement(Widget,String) + */ + public void addAndReplaceElement(IsWidget widget, String id) { + this.addAndReplaceElement(widget.asWidget(), id); + } + + /** * Finds an {@link Element element} within this panel by its id. * * This method uses
diff --git a/user/src/com/google/gwt/user/client/ui/HTMLTable.java b/user/src/com/google/gwt/user/client/ui/HTMLTable.java index 5155ffe..6a0d499 100644 --- a/user/src/com/google/gwt/user/client/ui/HTMLTable.java +++ b/user/src/com/google/gwt/user/client/ui/HTMLTable.java
@@ -1114,6 +1114,15 @@ adopt(widget); } } + + /** + * Overloaded version for IsWidget. + * + * @see #setWidget(int,int,Widget) + */ + public void setWidget(int row, int column, IsWidget widget) { + this.setWidget(row, column, asWidgetOrNull(widget)); + } /** * Bounds checks that the cell exists at the specified location.
diff --git a/user/src/com/google/gwt/user/client/ui/HasTreeItems.java b/user/src/com/google/gwt/user/client/ui/HasTreeItems.java index d8caf16..cb7ba1e 100644 --- a/user/src/com/google/gwt/user/client/ui/HasTreeItems.java +++ b/user/src/com/google/gwt/user/client/ui/HasTreeItems.java
@@ -22,7 +22,14 @@ * {@link com.google.gwt.user.client.ui.TreeItem items} and can operate them. */ public interface HasTreeItems { - + + /** + * Extends this interface with convenience methods to handle {@link IsWidget}. + */ + interface ForIsWidget extends HasTreeItems { + TreeItem addItem(IsWidget w); + } + /** * Adds a simple tree item containing the specified html. * @@ -37,7 +44,7 @@ * @param item the item to be added */ void addItem(TreeItem item); - + /** * Adds an item wrapped by specified {@link IsTreeItem}. * @@ -52,7 +59,7 @@ * @return the new item */ TreeItem addItem(Widget widget); - + /** * Adds a simple tree item containing the specified text. * @@ -67,7 +74,7 @@ * @param item the item to be removed */ void removeItem(TreeItem item); - + /** * Removes an item. *
diff --git a/user/src/com/google/gwt/user/client/ui/LayoutPanel.java b/user/src/com/google/gwt/user/client/ui/LayoutPanel.java index 52f2ebb..4662c7a 100644 --- a/user/src/com/google/gwt/user/client/ui/LayoutPanel.java +++ b/user/src/com/google/gwt/user/client/ui/LayoutPanel.java
@@ -231,6 +231,16 @@ } /** + * Overloaded version for IsWidget. + * + * @see #setWidgetBottomHeight(Widget,double, Unit, double, Unit) + */ + public void setWidgetBottomHeight(IsWidget child, double bottom, + Unit bottomUnit, double height, Unit heightUnit) { + this.setWidgetBottomHeight(child.asWidget(), bottom, bottomUnit, height, heightUnit); + } + + /** * Sets the child widget's horizontal position within its layer. * * @param child @@ -257,6 +267,16 @@ getLayer(child).setLeftRight(left, leftUnit, right, rightUnit); animate(0); } + + /** + * Overloaded version for IsWidget. + * + * @see #setWidgetLeftRight(Widget,double, Unit, double, Unit) + */ + public void setWidgetLeftRight(IsWidget child, double left, Unit leftUnit, + double right, Unit rightUnit) { + this.setWidgetLeftRight(child.asWidget(), left, leftUnit, right, rightUnit); + } /** * Sets the child widget's left and width values. @@ -273,6 +293,16 @@ getLayer(child).setLeftWidth(left, leftUnit, width, widthUnit); animate(0); } + + /** + * Overloaded version for IsWidget. + * + * @see #setWidgetLeftWidth(Widget,double, Unit, double, Unit) + */ + public void setWidgetLeftWidth(IsWidget child, double left, Unit leftUnit, + double width, Unit widthUnit) { + this.setWidgetLeftWidth(child.asWidget(), left, leftUnit, width, widthUnit); + } /** * Sets the child widget's right and width values. @@ -289,6 +319,16 @@ getLayer(child).setRightWidth(right, rightUnit, width, widthUnit); animate(0); } + + /** + * Overloaded version for IsWidget. + * + * @see #setWidgetRightWidth(Widget,double, Unit, double, Unit) + */ + public void setWidgetRightWidth(IsWidget child, double right, Unit rightUnit, + double width, Unit widthUnit) { + this.setWidgetRightWidth(child.asWidget(), right, rightUnit, width, widthUnit); + } /** * Sets the child widget's top and bottom values. @@ -305,6 +345,16 @@ getLayer(child).setTopBottom(top, topUnit, bottom, bottomUnit); animate(0); } + + /** + * Overloaded version for IsWidget. + * + * @see #setWidgetTopBottom(Widget,double, Unit, double, Unit) + */ + public void setWidgetTopBottom(IsWidget child, double top, Unit topUnit, + double bottom, Unit bottomUnit) { + this.setWidgetTopBottom(child.asWidget(), top, topUnit, bottom, bottomUnit); + } /** * Sets the child widget's top and height values. @@ -321,6 +371,16 @@ getLayer(child).setTopHeight(top, topUnit, height, heightUnit); animate(0); } + + /** + * Overloaded version for IsWidget. + * + * @see #setWidgetTopHeight(Widget,double, Unit, double, Unit) + */ + public void setWidgetTopHeight(IsWidget child, double top, Unit topUnit, + double height, Unit heightUnit) { + this.setWidgetTopHeight(child.asWidget(), top, topUnit, height, heightUnit); + } /** * Sets the child widget's vertical position within its layer.
diff --git a/user/src/com/google/gwt/user/client/ui/StackLayoutPanel.java b/user/src/com/google/gwt/user/client/ui/StackLayoutPanel.java index 9ea1e6a..1f03c78 100644 --- a/user/src/com/google/gwt/user/client/ui/StackLayoutPanel.java +++ b/user/src/com/google/gwt/user/client/ui/StackLayoutPanel.java
@@ -179,6 +179,15 @@ public void add(final Widget widget, String header, boolean asHtml, double headerSize) { insert(widget, header, asHtml, headerSize, getWidgetCount()); } + + /** + * Overloaded version for IsWidget. + * + * @see #add(Widget,String,boolean,double) + */ + public void add(final IsWidget widget, String header, boolean asHtml, double headerSize) { + this.add(widget.asWidget(), header, asHtml, headerSize); + } /** * Adds a child widget to this stack, along with a widget representing the @@ -204,6 +213,15 @@ insert(widget, header, headerSize, getWidgetCount()); } + /** + * Overloaded version for IsWidget. + * + * @see #add(Widget,Widget,double) + */ + public void add(final IsWidget widget, IsWidget header, double headerSize) { + this.add(widget.asWidget(), header.asWidget(), headerSize); + } + public HandlerRegistration addBeforeSelectionHandler( BeforeSelectionHandler<Integer> handler) { return addHandler(handler, BeforeSelectionEvent.getType());
diff --git a/user/src/com/google/gwt/user/client/ui/Tree.java b/user/src/com/google/gwt/user/client/ui/Tree.java index 5745374..a032a38 100644 --- a/user/src/com/google/gwt/user/client/ui/Tree.java +++ b/user/src/com/google/gwt/user/client/ui/Tree.java
@@ -89,7 +89,7 @@ * </p> */ @SuppressWarnings("deprecation") -public class Tree extends Widget implements HasTreeItems, HasWidgets, +public class Tree extends Widget implements HasTreeItems.ForIsWidget, HasWidgets.ForIsWidget, SourcesTreeEvents, HasFocus, HasAnimation, HasAllKeyHandlers, HasAllFocusHandlers, HasSelectionHandlers<TreeItem>, HasOpenHandlers<TreeItem>, HasCloseHandlers<TreeItem>, SourcesMouseEvents, @@ -303,6 +303,15 @@ public void add(Widget widget) { addItem(widget); } + + /** + * Overloaded version for IsWidget. + * + * @see #add(Widget) + */ + public void add(IsWidget w) { + this.add(asWidgetOrNull(w)); + } public HandlerRegistration addBlurHandler(BlurHandler handler) { return addDomHandler(handler, BlurEvent.getType()); @@ -371,6 +380,15 @@ public TreeItem addItem(Widget widget) { return root.addItem(widget); } + + /** + * Overloaded version for IsWidget. + * + * @see #addItem(Widget) + */ + public TreeItem addItem(IsWidget w) { + return this.addItem(asWidgetOrNull(w)); + } /** * @deprecated Use {@link #addKeyDownHandler}, {@link #addKeyUpHandler} and @@ -679,6 +697,15 @@ item.setWidget(null); return true; } + + /** + * Overloaded version for IsWidget. + * + * @see #remove(Widget) + */ + public boolean remove(IsWidget w) { + return this.remove(w.asWidget()); + } /** * @deprecated Use the {@link HandlerRegistration#removeHandler} method on the
diff --git a/user/test/com/google/gwt/layout/client/LayerFriend.java b/user/test/com/google/gwt/layout/client/LayerFriend.java new file mode 100644 index 0000000..6090a3b --- /dev/null +++ b/user/test/com/google/gwt/layout/client/LayerFriend.java
@@ -0,0 +1,82 @@ +/* + * Copyright 2011 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.layout.client; + +import com.google.gwt.dom.client.Style.Unit; +import com.google.gwt.layout.client.Layout.Layer; + +/** + * A helper class to gain access to the package protected internals of a + * {@link Layout.Layer} from other packages. For testing purposes. + * + */ +public class LayerFriend { + + private Layer layer; + + public LayerFriend(Layer layer) { + this.layer = layer; + } + + public double getTop() { + return layer.top; + } + + public Unit getTopUnit() { + return layer.topUnit; + } + + public double getBottom() { + return layer.bottom; + } + + public Unit getBottomUnit() { + return layer.bottomUnit; + } + + public double getRight() { + return layer.right; + } + + public Unit getRightUnit() { + return layer.rightUnit; + } + + public double getLeft() { + return layer.left; + } + + public Unit getLeftUnit() { + return layer.leftUnit; + } + + public double getHeight() { + return layer.height; + } + + public Unit getHeightUnit() { + return layer.heightUnit; + } + + public double getWidth() { + return layer.width; + } + + public Unit getWidthUnit() { + return layer.widthUnit; + } + +}
diff --git a/user/test/com/google/gwt/uibinder/elementparsers/HasTreeItemsParserTest.java b/user/test/com/google/gwt/uibinder/elementparsers/HasTreeItemsParserTest.java index 10c311f..692e1a2 100644 --- a/user/test/com/google/gwt/uibinder/elementparsers/HasTreeItemsParserTest.java +++ b/user/test/com/google/gwt/uibinder/elementparsers/HasTreeItemsParserTest.java
@@ -85,6 +85,18 @@ assertStatements("fieldName.addItem(<g:Button text='1'>);", "fieldName.addItem(<g:Button text='2'>);"); } + + public void test_IsWidget() throws Exception { + StringBuffer b = new StringBuffer(); + b.append("<g:Tree>"); + b.append(" <g:IsWidget />"); + b.append(" <g:IsWidget />"); + b.append("</g:Tree>"); + // parse + tester.parse(b.toString()); + assertStatements("fieldName.addItem(<g:IsWidget>);", + "fieldName.addItem(<g:IsWidget>);"); + } public void test_WidgetItemMix() throws Exception { StringBuffer b = new StringBuffer();
diff --git a/user/test/com/google/gwt/uibinder/test/UiJavaResources.java b/user/test/com/google/gwt/uibinder/test/UiJavaResources.java index 5d218e9..22678eb 100644 --- a/user/test/com/google/gwt/uibinder/test/UiJavaResources.java +++ b/user/test/com/google/gwt/uibinder/test/UiJavaResources.java
@@ -300,6 +300,18 @@ return code; } }; + public static final MockJavaResource IS_WIDGET = new MockJavaResource( + "com.google.gwt.user.client.ui.IsWidget") { + @Override + protected CharSequence getContent() { + StringBuffer code = new StringBuffer(); + code.append("package com.google.gwt.user.client.ui;\n"); + code.append("public interface IsWidget {\n"); + code.append(" Widget asWidget();\n"); + code.append("}\n"); + return code; + } + }; public static final MockJavaResource LABEL = new MockJavaResource( "com.google.gwt.user.client.ui.Label") { @Override @@ -635,6 +647,7 @@ rtn.add(GWT_EVENT); rtn.add(IMAGE); rtn.add(IMAGE_RESOURCE); + rtn.add(IS_WIDGET); rtn.add(HANDLER_REGISTRATION); rtn.add(HAS_CLICK_HANDLERS); rtn.add(HAS_HORIZONTAL_ALIGNMENT);
diff --git a/user/test/com/google/gwt/user/client/ui/AbsolutePanelTest.java b/user/test/com/google/gwt/user/client/ui/AbsolutePanelTest.java index 966fcda..fb07d8f 100644 --- a/user/test/com/google/gwt/user/client/ui/AbsolutePanelTest.java +++ b/user/test/com/google/gwt/user/client/ui/AbsolutePanelTest.java
@@ -16,6 +16,7 @@ package com.google.gwt.user.client.ui; import com.google.gwt.dom.client.Document; +import com.google.gwt.dom.client.Element; import com.google.gwt.junit.DoNotRunWith; import com.google.gwt.junit.Platform; import com.google.gwt.user.client.Window; @@ -31,7 +32,7 @@ * would throw an {@link IndexOutOfBoundsException}. */ public void testDoubleAdd() { - AbsolutePanel absolutePanel = new AbsolutePanel(); + AbsolutePanel absolutePanel = createPanel(); Label label = new Label("label"); absolutePanel.add(label, 10, 10); @@ -39,13 +40,40 @@ } /** - * Failed in all modes with absolute positioning. - * TODO: (flin) File a new HtmlUnit bug. + * Ensures that add(Widget, int, int) adds the Widget as its child. + */ + public void testAdd() { + AbsolutePanel absolutePanel = createPanel(); + Label label = new Label("foo"); + + absolutePanel.add(label, 10, 10); + + assertLogicalPaternity(absolutePanel, label); + assertPhysicalPaternity(absolutePanel, label); + } + + /** + * Ensures that add(IsWidget, int, int) adds the Widget as its child. + */ + public void testAddAsIsWidget() { + AbsolutePanel absolutePanel = createPanel(); + Label label = new Label("foo"); + + // IsWidget cast to call the overloaded version + absolutePanel.add((IsWidget) label, 10, 10); + + assertLogicalPaternity(absolutePanel, label); + assertPhysicalPaternity(absolutePanel, label); + } + + /** + * Failed in all modes with absolute positioning. TODO: (flin) File a new + * HtmlUnit bug. */ @DoNotRunWith(Platform.HtmlUnitBug) public void testPositioning() { // Make an absolute panel with a label at (3, 7). - AbsolutePanel abs = new AbsolutePanel(); + AbsolutePanel abs = createPanel(); abs.setSize("128px", "128px"); Label lbl = new Label("foo"); abs.add(lbl, 3, 7); @@ -73,7 +101,8 @@ int absY = lbl.getAbsoluteTop() - Document.get().getBodyOffsetTop(); assertEquals(3, x); assertEquals(7, y); - assertEquals("absX should be 103. This will fail in WebKit if run headless", + assertEquals( + "absX should be 103. This will fail in WebKit if run headless", 3 + 100, absX); assertEquals(7 + 200, absY); } @@ -82,4 +111,35 @@ protected AbsolutePanel createPanel() { return new AbsolutePanel(); } + + /** + * Asserts that <b>panel</b> is the logical parent of <b>expectedChild</b>. + * + * @param panel the parent panel + * @param expectedChild the expected child of <b>panel</b> + */ + private void assertLogicalPaternity(ComplexPanel panel, Widget expectedChild) { + assertSame("The parent and the panel must be the same", panel, + expectedChild.getParent()); + assertTrue("The child must be in the childen collection of the panel", + panel.getChildren().contains(expectedChild)); + } + + /** + * Asserts that <b>expectedFirstChild</b> is the first physical child of + * <b>panel</b>. + * + * @param panel the parent panel + * @param expectedFirstChild the expected first child of <b>panel</b> + */ + private void assertPhysicalPaternity(ComplexPanel panel, + Widget expectedFirstChild) { + Element panelElement = panel.getElement(); + Element childElement = expectedFirstChild.getElement(); + assertSame("The parent's Element of the child must be the panel's Element", + panelElement, childElement.getParentElement()); + assertSame( + "The child's Element must be first child of the panel's Element", + childElement, panelElement.getFirstChildElement()); + } }
diff --git a/user/test/com/google/gwt/user/client/ui/AbstractCellPanelTest.java b/user/test/com/google/gwt/user/client/ui/AbstractCellPanelTest.java index 55f5ebe..f98c5c5 100644 --- a/user/test/com/google/gwt/user/client/ui/AbstractCellPanelTest.java +++ b/user/test/com/google/gwt/user/client/ui/AbstractCellPanelTest.java
@@ -22,19 +22,30 @@ * * @param <T> the panel type */ -public abstract class AbstractCellPanelTest<T extends CellPanel> extends PanelTestBase<T> { +public abstract class AbstractCellPanelTest<T extends CellPanel> extends + PanelTestBase<T> { @Override public String getModuleName() { return "com.google.gwt.user.DebugTest"; } + /** + * Test {@link CellPanel#getWidgetTd(Widget)}. + */ public void testGetWidgetTd() { CellPanel panel = createCellPanel(); Widget w = panel.getWidget(0); assertEquals(w.getElement().getParentElement(), panel.getWidgetTd(w)); } + /** + * Tests + * {@link CellPanel#setCellVerticalAlignment(Widget, com.google.gwt.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant)} + * and + * {@link CellPanel#setCellHorizontalAlignment(Widget, com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant)} + * . + */ public void testSetCellAlignment() { CellPanel panel = createCellPanel(); Widget w = panel.getWidget(0); @@ -49,6 +60,39 @@ assertEquals("right", td.getPropertyString("align")); } + /** + * Tests + * {@link CellPanel#setCellVerticalAlignment(IsWidget, com.google.gwt.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant)} + * and + * {@link CellPanel#setCellHorizontalAlignment(IsWidget, com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant)} + * . + */ + public void testSetCellAlignmentAsIsWidget() { + CellPanel panel = createCellPanel(); + Widget w = panel.getWidget(0); + Element td = panel.getWidgetTd(w); + + // setCellVerticalAlignment + // IsWidget cast to call the overloaded version + panel.setCellVerticalAlignment((IsWidget) w, + HasVerticalAlignment.ALIGN_BOTTOM); + assertEquals("bottom", td.getStyle().getProperty("verticalAlign")); + + // setCellHorizontalAlignment + // IsWidget reference to call the overloaded version + panel.setCellHorizontalAlignment((IsWidget) w, + HasHorizontalAlignment.ALIGN_RIGHT); + assertEquals("right", td.getPropertyString("align")); + } + + /** + * Ensures that + * {@link CellPanel#setCellVerticalAlignment(Widget, com.google.gwt.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant)} + * and + * {@link CellPanel#setCellHorizontalAlignment(Widget, com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant)} + * don't throw an Exception when the Widget argument is not a child of the + * panel. + */ public void testSetCellAlignmentForNonChildWidget() { CellPanel panel = createCellPanel(); Widget w = new Label("Not a chid"); @@ -60,6 +104,30 @@ panel.setCellHorizontalAlignment(w, HasHorizontalAlignment.ALIGN_RIGHT); } + /** + * Ensures that + * {@link CellPanel#setCellVerticalAlignment(IsWidget, com.google.gwt.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant)} + * and + * {@link CellPanel#setCellHorizontalAlignment(IsWidget, com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant)} + * don't throw an Exception when the IsWidget argument is not a child of the + * panel. + */ + public void testSetCellAlignmentForNonChildWidgetAsIsWidget() { + CellPanel panel = createCellPanel(); + // IsWidget reference to call the overloaded version + IsWidget w = new Label("Not a chid"); + + // setCellVerticalAlignment should not throw an error + panel.setCellVerticalAlignment(w, HasVerticalAlignment.ALIGN_BOTTOM); + + // setCellHorizontalAlignment should not throw an error + panel.setCellHorizontalAlignment(w, HasHorizontalAlignment.ALIGN_RIGHT); + } + + /** + * Tests {@link CellPanel#setCellHeight(Widget, String)} and + * {@link CellPanel#setCellWidth(Widget, String)}. + */ public void testSetCellSize() { CellPanel panel = createCellPanel(); Widget w = panel.getWidget(0); @@ -74,6 +142,31 @@ assertEquals(200, td.getPropertyInt("width")); } + /** + * Tests {@link CellPanel#setCellHeight(IsWidget, String)} and + * {@link CellPanel#setCellWidth(IsWidget, String)}. + */ + public void testSetCellSizeAsIsWidget() { + CellPanel panel = createCellPanel(); + Widget w = panel.getWidget(0); + Element td = panel.getWidgetTd(w); + + // setCellHeight + // IsWidget cast to call the overloaded version + panel.setCellHeight((IsWidget) w, "100px"); + assertEquals(100, td.getPropertyInt("height")); + + // setCellWidth + // IsWidget cast to call the overloaded version + panel.setCellWidth((IsWidget) w, "200px"); + assertEquals(200, td.getPropertyInt("width")); + } + + /** + * Ensures that {@link CellPanel#setCellHeight(Widget, String)} and + * {@link CellPanel#setCellWidth(Widget, String)} don't throw an Exception + * when the Widget argument is not a child of the panel. + */ public void testSetCellSizeForNonChildWidget() { CellPanel panel = createCellPanel(); Widget w = new Label("Not a chid"); @@ -86,6 +179,24 @@ } /** + * Ensures that {@link CellPanel#setCellHeight(IsWidget, String)} and + * {@link CellPanel#setCellWidth(IsWidget, String)} don't throw an Exception + * when the IsWidget argument is not a child of the panel. + */ + public void testSetCellSizeForNonChildWidgetAsIsWidget() { + CellPanel panel = createCellPanel(); + Widget w = new Label("Not a chid"); + + // setCellHeight should not throw an error + // IsWidget cast to call the overloaded version + panel.setCellHeight((IsWidget) w, "100px"); + + // setCellWidth should not throw an error + // IsWidget cast to call the overloaded version + panel.setCellWidth((IsWidget) w, "200px"); + } + + /** * Create a populated {@link CellPanel}. * * @return the {@link CellPanel}
diff --git a/user/test/com/google/gwt/user/client/ui/CaptionPanelTest.java b/user/test/com/google/gwt/user/client/ui/CaptionPanelTest.java index 13d195b..357b7a8 100644 --- a/user/test/com/google/gwt/user/client/ui/CaptionPanelTest.java +++ b/user/test/com/google/gwt/user/client/ui/CaptionPanelTest.java
@@ -32,11 +32,38 @@ return "com.google.gwt.user.User"; } + /** + * Tests {@link CaptionPanel#add(IsWidget)}. + */ + public void testAddAsIsWidget() { + CaptionPanel panel = createEmptyCaptionPanel(); + Widget widget = new Label("foo"); + + // IsWidget cast to call the overloaded version + panel.add((IsWidget) widget); + + assertSame(widget,panel.getContentWidget()); + } + + /** + * Ensures that {@link CaptionPanel#add(IsWidget)} does <b>NOT</b> fail + * when the IsWidget argument is <code>null</code>. Not that this is + * a great thing, but it works in the add(Widget) case so... + */ + public void testAddNullAsIsWidget() { + CaptionPanel panel = createEmptyCaptionPanel(); + // IsWidget reference to call the overloaded version + IsWidget widget = null; + + panel.add(widget); + // ta da... + } + public void testHasWidgets() { WidgetAdder adder = new HasWidgetsTester.DefaultWidgetAdder(); // With no caption. - HasWidgetsTester.testAll(new CaptionPanel(), adder, false); + HasWidgetsTester.testAll(createEmptyCaptionPanel(), adder, false); // With a text caption. HasWidgetsTester.testAll(new CaptionPanel("some text"), adder, false); @@ -75,45 +102,47 @@ } } -// TODO(bruce): re-active when we ensure that assertions are enabled during unit test runs -// /** -// * When the caption is null, it needs to be actually removed from the DOM (to -// * compensate for browser bugs). This formulation requires no widget to have -// * been set first. -// */ -// public void testCaptionAssertsAgainstNull() { -// // Ctor. -// { -// try { -// new CaptionPanel(null); -// fail("Should've asserted!"); -// } catch (AssertionError e) { -// // good to make it here -// } -// } -// -// // Setter/HTML. -// { -// try { -// CaptionPanel panel = new CaptionPanel("stuff"); -// panel.setCaptionHTML(null); -// fail("Should've asserted!"); -// } catch (AssertionError e) { -// // good to make it here -// } -// } -// -// // Setter/Text. -// { -// try { -// CaptionPanel panel = new CaptionPanel("stuff"); -// panel.setCaptionText(null); -// fail("Should've asserted!"); -// } catch (AssertionError e) { -// // good to make it here -// } -// } -// } + // TODO(bruce): re-active when we ensure that assertions are enabled during + // unit test runs + // /** + // * When the caption is null, it needs to be actually removed from the DOM + // (to + // * compensate for browser bugs). This formulation requires no widget to have + // * been set first. + // */ + // public void testCaptionAssertsAgainstNull() { + // // Ctor. + // { + // try { + // new CaptionPanel(null); + // fail("Should've asserted!"); + // } catch (AssertionError e) { + // // good to make it here + // } + // } + // + // // Setter/HTML. + // { + // try { + // CaptionPanel panel = new CaptionPanel("stuff"); + // panel.setCaptionHTML(null); + // fail("Should've asserted!"); + // } catch (AssertionError e) { + // // good to make it here + // } + // } + // + // // Setter/Text. + // { + // try { + // CaptionPanel panel = new CaptionPanel("stuff"); + // panel.setCaptionText(null); + // fail("Should've asserted!"); + // } catch (AssertionError e) { + // // good to make it here + // } + // } + // } public void testCtorAsHtmlFlag() { String s = "this is <b>not</b> null"; @@ -152,7 +181,7 @@ } public void testDefaultCaptionIsEmptyString() { - CaptionPanel panel = new CaptionPanel(); + CaptionPanel panel = createEmptyCaptionPanel(); assertEquals("", panel.getCaptionText()); assertEquals("", panel.getCaptionHTML()); // Wigets may be supported in the future. @@ -161,7 +190,7 @@ } public void testGetSetHTMLCaption() { - CaptionPanel panel = new CaptionPanel(); + CaptionPanel panel = createEmptyCaptionPanel(); panel.setCaptionHTML("<b>bold</b>"); assertEqualsIgnoreCase("<b>bold</b>", panel.getCaptionHTML()); assertEquals("bold", panel.getCaptionText()); @@ -169,7 +198,7 @@ public void testGetSetTextCaption() { String s = "this is <b>not</b> null"; - CaptionPanel panel = new CaptionPanel(); + CaptionPanel panel = createEmptyCaptionPanel(); panel.setCaptionText(s); assertEquals(s, panel.getCaptionText()); assertNotEquals(s, panel.getCaptionHTML()); @@ -297,14 +326,18 @@ public void testSafeHtmlConstructor() { CaptionPanel panel = new CaptionPanel(SafeHtmlUtils.fromSafeConstant(html)); - + assertEquals(html, panel.getCaptionHTML().toLowerCase()); } public void testSetCaptionSafeHtml() { CaptionPanel panel = new CaptionPanel("hiworld"); panel.setCaptionHTML(SafeHtmlUtils.fromSafeConstant(html)); - + assertEquals(html, panel.getCaptionHTML().toLowerCase()); } + + private CaptionPanel createEmptyCaptionPanel() { + return new CaptionPanel(); + } }
diff --git a/user/test/com/google/gwt/user/client/ui/DisclosurePanelTest.java b/user/test/com/google/gwt/user/client/ui/DisclosurePanelTest.java index 106ec14..aac9f35 100644 --- a/user/test/com/google/gwt/user/client/ui/DisclosurePanelTest.java +++ b/user/test/com/google/gwt/user/client/ui/DisclosurePanelTest.java
@@ -61,6 +61,32 @@ }; t.schedule(450); } + + /** + * Tests {@link DisclosurePanel#add(IsWidget)}. + */ + public void testAddAsIsWidget() { + DisclosurePanel panel = createEmptyDisclourePanel(); + Widget widget = new Label("foo"); + + // IsWidget cast to call the overloaded version + panel.add((IsWidget) widget); + + assertSame(widget, panel.getContent()); + } + + /** + * Ensures that {@link DisclosurePanel#add(IsWidget)} does <b>NOT</b> throws a + * {@link NullPointerException} when the IsWidget argument is + * <code>null</code>. Stupid, but it's what add(Widget) does. + */ + public void testAddNullAsIsWidget() { + DisclosurePanel panel = createEmptyDisclourePanel(); + IsWidget widget = null; + + panel.add(widget); + // ta da... + } public void testAttachDetachOrder() { HasWidgetsTester.testAll(new DisclosurePanel(), @@ -82,20 +108,21 @@ public void testEvents() { final DisclosurePanel panel = createTestPanel(); - assertEquals(1, panel.getHandlerManager().getHandlerCount(CloseEvent.getType())); + assertEquals(1, + panel.getHandlerManager().getHandlerCount(CloseEvent.getType())); panel.addCloseHandler(new CloseHandler<DisclosurePanel>() { public void onClose(CloseEvent<DisclosurePanel> event) { // for now nothing. } }); - assertEquals(2, panel.getHandlerManager().getHandlerCount(CloseEvent.getType())); + assertEquals(2, + panel.getHandlerManager().getHandlerCount(CloseEvent.getType())); } /** * Test to ensure that event handler dispatch function appropriately. */ - @SuppressWarnings("deprecation") public void testEventHandlers() { final boolean[] aDidFire = new boolean[2]; @@ -127,8 +154,10 @@ panel.addEventHandler(handleA); panel.addEventHandler(handleB); // There is one to begin with. - assertEquals(3, panel.getHandlerManager().getHandlerCount(CloseEvent.getType())); - assertEquals(3, panel.getHandlerManager().getHandlerCount(OpenEvent.getType())); + assertEquals(3, + panel.getHandlerManager().getHandlerCount(CloseEvent.getType())); + assertEquals(3, + panel.getHandlerManager().getHandlerCount(OpenEvent.getType())); panel.setOpen(true); // We expect onOpen to fire and onClose to not fire. @@ -145,8 +174,10 @@ aDidFire[OPEN] = bDidFire[CLOSE] = false; panel.removeEventHandler(handleB); - assertEquals(2, panel.getHandlerManager().getHandlerCount(OpenEvent.getType())); - assertEquals(2, panel.getHandlerManager().getHandlerCount(CloseEvent.getType())); + assertEquals(2, + panel.getHandlerManager().getHandlerCount(OpenEvent.getType())); + assertEquals(2, + panel.getHandlerManager().getHandlerCount(CloseEvent.getType())); panel.setOpen(true); panel.setOpen(false); @@ -169,6 +200,39 @@ content.removeFromParent(); assertNull(panel.getContent()); } + + /** + * Tests {@link DisclosurePanel#remove(IsWidget)}. + */ + public void testRemoveAsIsWidget() { + DisclosurePanel panel = createEmptyDisclourePanel(); + Widget widget = new Label("foo"); + panel.setContent(widget); + assertSame(widget,panel.getContent()); + + boolean wasPresent = panel.remove((IsWidget) widget); + + assertTrue(wasPresent); + assertNull(panel.getContent()); + } + + /** + * Ensures that {@link DisclosurePanel#remove(IsWidget)} does <b>NOT</b> throws a + * {@link NullPointerException} when the IsWidget argument is + * <code>null</code>, for consistency with remove(Widget) brain damage. + */ + public void testRemoveNullAsIsWidget() { + DisclosurePanel panel = createEmptyDisclourePanel(); + // IsWidget reference to call the overload version + IsWidget widget = null; + + panel.remove(widget); + // ta da... + } + + private DisclosurePanel createEmptyDisclourePanel() { + return new DisclosurePanel(); + } private DisclosurePanel createTestPanel() { DisclosurePanel panel = new DisclosurePanel("Test Subject", false);
diff --git a/user/test/com/google/gwt/user/client/ui/DockLayoutPanelTest.java b/user/test/com/google/gwt/user/client/ui/DockLayoutPanelTest.java index d53e7db..a751b7a 100644 --- a/user/test/com/google/gwt/user/client/ui/DockLayoutPanelTest.java +++ b/user/test/com/google/gwt/user/client/ui/DockLayoutPanelTest.java
@@ -1,12 +1,12 @@ /* * Copyright 2010 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 @@ -25,10 +25,9 @@ public void testGetResolvedDirection() { DockLayoutPanel panel = createDockLayoutPanel(); - assertEquals( - Direction.WEST, panel.getResolvedDirection(Direction.LINE_START)); - assertEquals( - Direction.EAST, panel.getResolvedDirection(Direction.LINE_END)); + assertEquals(Direction.WEST, + panel.getResolvedDirection(Direction.LINE_START)); + assertEquals(Direction.EAST, panel.getResolvedDirection(Direction.LINE_END)); } public void testAddLineEnd() { @@ -45,6 +44,122 @@ assertEquals(Direction.LINE_START, panel.getWidgetDirection(widget)); } + /** + * Tests {@link DockLayoutPanel#addEast(Widget, double)}. + */ + public void testAddEast() { + DockLayoutPanel panel = createDockLayoutPanel(); + Widget widget = new Label(); + + panel.addEast(widget, 10); + + assertWidgetDirection(panel, widget, Direction.EAST); + assertLogicalPaternity(panel, widget); + assertPhysicalPaternity(panel, widget); + } + + /** + * Tests {@link DockLayoutPanel#addEast(IsWidget, double)}. + */ + public void testAddEastAsIsWidget() { + DockLayoutPanel panel = createDockLayoutPanel(); + Widget widget = new Label(); + + // IsWidget cast to call the overloaded version + panel.addEast((IsWidget) widget, 10); + + assertWidgetDirection(panel, widget, Direction.EAST); + assertLogicalPaternity(panel, widget); + assertPhysicalPaternity(panel, widget); + } + + /** + * Tests {@link DockLayoutPanel#addNorth(Widget, double)}. + */ + public void testAddNorth() { + DockLayoutPanel panel = createDockLayoutPanel(); + Widget widget = new Label(); + + panel.addNorth(widget, 10); + + assertWidgetDirection(panel, widget, Direction.NORTH); + assertLogicalPaternity(panel, widget); + assertPhysicalPaternity(panel, widget); + } + + /** + * Tests {@link DockLayoutPanel#addNorth(IsWidget, double)}. + */ + public void testAddNorthAsIsWidget() { + DockLayoutPanel panel = createDockLayoutPanel(); + Widget widget = new Label(); + + // IsWidget cast to call the overloaded version + panel.addNorth((IsWidget) widget, 10); + + assertWidgetDirection(panel, widget, Direction.NORTH); + assertLogicalPaternity(panel, widget); + assertPhysicalPaternity(panel, widget); + } + + /** + * Tests {@link DockLayoutPanel#addSouth(Widget, double)}. + */ + public void testAddSouth() { + DockLayoutPanel panel = createDockLayoutPanel(); + Widget widget = new Label(); + + panel.addSouth(widget, 10); + + assertWidgetDirection(panel, widget, Direction.SOUTH); + assertLogicalPaternity(panel, widget); + assertPhysicalPaternity(panel, widget); + } + + /** + * Tests {@link DockLayoutPanel#addSouth(IsWidget, double)}. + */ + public void testAddSouthAsIsWidget() { + DockLayoutPanel panel = createDockLayoutPanel(); + Widget widget = new Label(); + + // IsWidget cast to call the overloaded version + panel.addSouth((IsWidget) widget, 10); + + assertWidgetDirection(panel, widget, Direction.SOUTH); + assertLogicalPaternity(panel, widget); + assertPhysicalPaternity(panel, widget); + } + + /** + * Tests {@link DockLayoutPanel#addWest(Widget, double)}. + */ + public void testAddWest() { + DockLayoutPanel panel = createDockLayoutPanel(); + Widget widget = new Label(); + + panel.addWest(widget, 10); + + assertWidgetDirection(panel, widget, Direction.WEST); + assertLogicalPaternity(panel, widget); + assertPhysicalPaternity(panel, widget); + } + + /** + * Tests {@link DockLayoutPanel#addWest(IsWidget, double)}. + */ + public void testAddWestAsIsWidget() { + DockLayoutPanel panel = createDockLayoutPanel(); + Widget widget = new Label(); + + // IsWidget cast to call the overloaded version + panel.addWest((IsWidget) widget, 10); + + assertWidgetDirection(panel, widget, Direction.WEST); + assertLogicalPaternity(panel, widget); + assertPhysicalPaternity(panel, widget); + } + public void testInsertLineEnd() { DockLayoutPanel panel = createDockLayoutPanel(); Widget widget = new Label(); @@ -62,4 +177,52 @@ protected DockLayoutPanel createDockLayoutPanel() { return new DockLayoutPanel(Unit.PX); } + + /** + * Asserts that <b>panel</b> is the logical parent of <b>expectedChild</b>. + * + * @param panel the parent panel + * @param expectedChild the expected child of <b>panel</b> + */ + private void assertLogicalPaternity(ComplexPanel panel, Widget expectedChild) { + assertSame("The parent and the panel must be the same", panel, + expectedChild.getParent()); + assertTrue("The child must be in the childen collection of the panel", + panel.getChildren().contains(expectedChild)); + } + + /** + * Asserts that <b>expectedFirstChild</b> is the first physical child of + * <b>panel</b>. + * + * @param panel the parent panel + * @param expectedFirstChild the expected first child of <b>panel</b> + */ + private void assertPhysicalPaternity(DockLayoutPanel panel, + Widget expectedFirstChild) { + assertSame( + "The parent's Element of the child must be the panel's container Element", + expectedFirstChild.getElement().getParentElement(), + panel.getWidgetContainerElement(expectedFirstChild)); + assertSame( + "The child's Element must be first child of the panel's container Element", + panel.getWidgetContainerElement(expectedFirstChild).getFirstChildElement(), + expectedFirstChild.getElement()); + } + + /** + * Asserts that the {@link DockLayoutPanel.Direction} of <b>widget</b> in + * <b>panel</b> is <b>expectedDirection</b>. + * + * @param panel the panel containing <b>widget</b> + * @param widget the widget being tested + * @param expectedDirection the expected direction + */ + private void assertWidgetDirection(DockLayoutPanel panel, Widget widget, + DockLayoutPanel.Direction expectedDirection) { + assertEquals("The direction of the widget in the panel should be " + + expectedDirection, expectedDirection, + panel.getWidgetDirection(widget)); + } + }
diff --git a/user/test/com/google/gwt/user/client/ui/DockPanelTest.java b/user/test/com/google/gwt/user/client/ui/DockPanelTest.java index 85bf22b..b271f60 100644 --- a/user/test/com/google/gwt/user/client/ui/DockPanelTest.java +++ b/user/test/com/google/gwt/user/client/ui/DockPanelTest.java
@@ -22,6 +22,7 @@ /** * Tests the DockPanel widget. */ +@SuppressWarnings("deprecation") public class DockPanelTest extends GWTTestCase { static class Adder implements HasWidgetsTester.WidgetAdder { @@ -113,6 +114,16 @@ assertTrue(((DockPanel.LayoutData) l4.getLayoutData()).direction == DockPanel.NORTH); } + public void testAddAsIsWidget() { + DockPanel panel = createDockPanel(); + Widget widget = new Label("foo"); + + panel.add(widget, DockPanel.NORTH); + + assertLogicalPaternity(panel,widget); + assertPhysicalPaternity(panel,widget); + } + public void testAttachDetachOrder() { HasWidgetsTester.testAll(new DockPanel(), new Adder(), true); } @@ -160,4 +171,35 @@ UIObjectTest.assertDebugId("myDock-center", DOM.getParent(center.getElement())); } + + /** + * Asserts that <b>panel</b> is the logical parent of <b>expectedChild</b>. + * + * @param panel the parent panel + * @param expectedChild the expected child of <b>panel</b> + */ + private void assertLogicalPaternity(DockPanel panel, Widget expectedChild) { + assertSame("The parent and the panel must be the same", panel, + expectedChild.getParent()); + assertTrue("The child must be in the childen collection of the panel", + panel.getChildren().contains(expectedChild)); + } + + /** + * Asserts that <b>expectedChild</b> is the first physical child of + * <b>parent</b>. + * + * @param parent the parent panel + * @param expectedChild the expected child of <b>panel</b> + */ + private void assertPhysicalPaternity(Widget parent, + Widget expectedChild) { + Element panelElement = parent.getElement(); + Element childElement = expectedChild.getElement(); + assertTrue("The parent's Element of the child must be the panel's Element", DOM.isOrHasChild(panelElement, childElement)); + } + + private DockPanel createDockPanel() { + return new DockPanel(); + } }
diff --git a/user/test/com/google/gwt/user/client/ui/HTMLPanelTest.java b/user/test/com/google/gwt/user/client/ui/HTMLPanelTest.java index 94f63ae..6df56dc 100644 --- a/user/test/com/google/gwt/user/client/ui/HTMLPanelTest.java +++ b/user/test/com/google/gwt/user/client/ui/HTMLPanelTest.java
@@ -1,12 +1,12 @@ /* * 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 @@ -79,10 +79,11 @@ */ public void testAddToElement() { Label labelA = new Label("A"), labelB = new Label("B"); - HTMLPanel p = new HTMLPanel("<div class=\"a\"></div><div class=\"b\"></div>"); + HTMLPanel p = new HTMLPanel( + "<div class=\"a\"></div><div class=\"b\"></div>"); Element first = p.getElement().getFirstChildElement(); Element second = first.getNextSiblingElement(); - + p.add(labelA, first); p.add(labelB, second); // Ensure that both Label's have the correct parent. @@ -92,10 +93,10 @@ /** * This is meant to catch an issue created by a faulty optimization. To - * optimize add() when the HTMLPanel is unattached, we would originally - * move its element to a hidden div so that getElementById() would work. - * Unfortunately, we didn't move it back to its original parent, causing - * a problem in the case described in this test. + * optimize add() when the HTMLPanel is unattached, we would originally move + * its element to a hidden div so that getElementById() would work. + * Unfortunately, we didn't move it back to its original parent, causing a + * problem in the case described in this test. */ public void testAddPartiallyAttached() { SimplePanel sp = new SimplePanel(); @@ -145,68 +146,108 @@ } /** - * Ensures that addAndReplaceChild() puts the widget in exactly the right place in the DOM. + * Ensures that {@link HTMLPanel#addAndReplaceChild(Widget,String)} puts the + * widget in exactly the right place in the DOM. */ public void testAddAndReplaceElement() { - HTMLPanel hp = new HTMLPanel("<div id='parent'>foo<span id='placeholder'></span>bar</div>"); - + HTMLPanel hp = new HTMLPanel( + "<div id='parent'>foo<span id='placeholder'></span>bar</div>"); Button button = new Button("my button"); + hp.addAndReplaceElement(button, "placeholder"); - assertEquals("parent", button.getElement().getParentElement().getId()); - - Node prev = button.getElement().getPreviousSibling(); - assertEquals("foo", prev.getNodeValue()); - - Node next = button.getElement().getNextSibling(); - assertEquals("bar", next.getNodeValue()); + assertParentId(button, "parent"); + assertIsBetweenSiblings(button, "foo", "bar"); } /** - * Ensures that addAndReplaceChild() puts the widget in exactly the right place in the DOM. + * Ensures that + * {@link HTMLPanel#addAndReplaceElement(Widget, com.google.gwt.user.client.Element)} + * puts the widget in exactly the right place in the DOM. */ @SuppressWarnings("deprecation") public void testAddAndReplaceElementForUserElement() { - - HTMLPanel hp = new HTMLPanel("<div id='parent'>foo<span id='placeholder'></span>bar</div>"); - + HTMLPanel hp = new HTMLPanel( + "<div id='parent'>foo<span id='placeholder'></span>bar</div>"); RootPanel.get().add(hp); com.google.gwt.user.client.Element placeholder = hp.getElementById("placeholder"); - Button button = new Button("my button"); + hp.addAndReplaceElement(button, placeholder); - assertEquals("parent", button.getElement().getParentElement().getId()); - - Node prev = button.getElement().getPreviousSibling(); - assertEquals("foo", prev.getNodeValue()); - - Node next = button.getElement().getNextSibling(); - assertEquals("bar", next.getNodeValue()); + assertParentId(button, "parent"); + assertIsBetweenSiblings(button, "foo", "bar"); } /** - * Ensures that addAndReplaceChild() puts the widget in exactly the right place in the DOM. + * Ensures that overloaded version of + * {@link HTMLPanel#addAndReplaceElement(IsWidget, com.google.gwt.user.client.Element)} + * for IsWidget puts the widget in exactly the right place in the DOM. */ - public void testAddAndReplaceElementForElement() { - HTMLPanel hp = new HTMLPanel("<div id='parent'>foo<span id='placeholder'></span>bar</div>"); - + public void testAddAndReplaceElementForUserElementAsIsWidget() { + HTMLPanel hp = new HTMLPanel( + "<div id='parent'>foo<span id='placeholder'></span>bar</div>"); RootPanel.get().add(hp); - Element placeholder = hp.getElementById("placeholder"); - + com.google.gwt.user.client.Element placeholder = hp.getElementById("placeholder"); Button button = new Button("my button"); - hp.addAndReplaceElement(button, placeholder); + // IsWidget reference to call the overloaded version + IsWidget isWidget = button; - assertEquals("parent", button.getElement().getParentElement().getId()); + hp.addAndReplaceElement(isWidget, placeholder); - Node prev = button.getElement().getPreviousSibling(); - assertEquals("foo", prev.getNodeValue()); - - Node next = button.getElement().getNextSibling(); - assertEquals("bar", next.getNodeValue()); + assertParentId(button, "parent"); + assertIsBetweenSiblings(button, "foo", "bar"); } /** + * Ensures that {@link HTMLPanel#addAndReplaceElement(Widget, Element)} puts + * the widget in exactly the right place in the DOM. + */ + public void testAddAndReplaceElementForElement() { + HTMLPanel hp = new HTMLPanel( + "<div id='parent'>foo<span id='placeholder'></span>bar</div>"); + RootPanel.get().add(hp); + Element placeholder = hp.getElementById("placeholder"); + Button button = new Button("my button"); + + hp.addAndReplaceElement(button, placeholder); + + assertParentId(button, "parent"); + assertIsBetweenSiblings(button, "foo", "bar"); + } + + /** + * Tests {@link HTMLPanel#addAndReplaceElement(Widget, String)}. + */ + public void testAddAndReplaceElementById() { + HTMLPanel hp = new HTMLPanel( + "<div id='parent'>foo<span id='placeholder'></span>bar</div>"); + RootPanel.get().add(hp); + Button button = new Button("my button"); + + hp.addAndReplaceElement(button, "placeholder"); + + assertParentId(button, "parent"); + assertIsBetweenSiblings(button, "foo", "bar"); + } + + /** + * Tests {@link HTMLPanel#addAndReplaceElement(IsWidget, String)}. + */ + public void testAddAndReplaceElementByIdAsIsWidget() { + HTMLPanel hp = new HTMLPanel( + "<div id='parent'>foo<span id='placeholder'></span>bar</div>"); + RootPanel.get().add(hp); + Button button = new Button("my button"); + + // IsWidget cast to call the overloaded version + hp.addAndReplaceElement((IsWidget) button, "placeholder"); + + assertParentId(button, "parent"); + assertIsBetweenSiblings(button, "foo", "bar"); + } + + /** * Tests table root tag. */ public void testCustomRootTagAsTable() { @@ -274,7 +315,7 @@ public void testSafeHtml() { TestTemplates templates = GWT.create(TestTemplates.class); SafeHtml table = templates.tableTemplate("Hello"); - + HTMLPanel hp = new HTMLPanel(table); InlineLabel label = new InlineLabel("World"); hp.addAndReplaceElement(label, "labelHere"); @@ -296,4 +337,30 @@ assertEquals("div", parent.getTagName().toLowerCase()); assertEquals("table", firstChild.getTagName().toLowerCase()); } + + /** + * Asserts that the widget w is between the given previous and next nodes. + * + * @param w the widget + * @param previous the expected previous node string representation of the + * widget + * @param next the expected next node string representation of the widget + */ + private void assertIsBetweenSiblings(Widget w, String previous, String next) { + Node prevNode = w.getElement().getPreviousSibling(); + assertEquals(previous, prevNode.getNodeValue()); + + Node nextNode = w.getElement().getNextSibling(); + assertEquals(next, nextNode.getNodeValue()); + } + + /** + * Asserts that the parent id of the widget is the given one. + * + * @param w the widget + * @param expected the expected parent id of w + */ + private void assertParentId(Widget w, String expected) { + assertEquals(expected, w.getElement().getParentElement().getId()); + } }
diff --git a/user/test/com/google/gwt/user/client/ui/HTMLTableTestBase.java b/user/test/com/google/gwt/user/client/ui/HTMLTableTestBase.java index ef021e8..ce44b5a 100644 --- a/user/test/com/google/gwt/user/client/ui/HTMLTableTestBase.java +++ b/user/test/com/google/gwt/user/client/ui/HTMLTableTestBase.java
@@ -229,6 +229,47 @@ assertEquals(columnGroup, formatter.columnGroup); } + /** + * Tests {@link HTMLTable#setWidget(int, int, Widget)}. + */ + public void testSetWidget() { + HTMLTable t = getTable(2, 2); + Widget widget = new Label("foo"); + + t.setWidget(1, 1, widget); + + assertLogicalPaternity(t, widget); + assertPhysicalPaternityInPosition(t, widget, 1, 1); + } + + /** + * Tests {@link HTMLTable#setWidget(int, int, IsWidget)}. + */ + public void testSetWidgetAsIsWidget() { + HTMLTable t = getTable(2, 2); + Widget widget = new Label("foo"); + + // IsWidget cast to call the overloaded version + t.setWidget(1, 1, (IsWidget) widget); + + assertLogicalPaternity(t, widget); + assertPhysicalPaternityInPosition(t, widget, 1, 1); + } + + /** + * Ensures that {@link HTMLTable#setWidget(int, int, IsWidget)} does + * <b>NOT</b> throws a {@link NullPointerException} when the Widget argument + * is <code>null</code>, for compatibility with setWidget(Widget) foolishness + */ + public void testSetNullWidgetAsIsWidget() { + HTMLTable t = getTable(2, 2); + // IsWidget reference to call the overloaded version + IsWidget widget = null; + + t.setWidget(1, 1, widget); + // ta da... + } + public void testSettingCellAttributes() { // These tests simple test for errors while setting these fields. The // Patient sample under the survey project has the visual part of the test. @@ -292,4 +333,16 @@ t.getRowFormatter().setStyleName(3, "newStyle"); assertEquals("newStyle", t.getRowFormatter().getStyleName(3)); } + + private void assertPhysicalPaternityInPosition(HTMLTable parent, + Widget child, int row, int column) { + assertSame("The child should be in te given position", child, + parent.getWidget(row, column)); + } + + private void assertLogicalPaternity(HTMLTable parent, Widget child) { + Iterator<Widget> iterator = parent.iterator(); + assertTrue(iterator.hasNext()); + assertSame(child, iterator.next()); + } }
diff --git a/user/test/com/google/gwt/user/client/ui/LayoutPanelTest.java b/user/test/com/google/gwt/user/client/ui/LayoutPanelTest.java index 6ac4e96..a8e63ce 100644 --- a/user/test/com/google/gwt/user/client/ui/LayoutPanelTest.java +++ b/user/test/com/google/gwt/user/client/ui/LayoutPanelTest.java
@@ -15,12 +15,15 @@ */ package com.google.gwt.user.client.ui; +import com.google.gwt.core.client.Scheduler; +import com.google.gwt.core.client.Scheduler.ScheduledCommand; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Style.Unit; +import com.google.gwt.layout.client.LayerFriend; +import com.google.gwt.layout.client.Layout; import com.google.gwt.layout.client.Layout.AnimationCallback; import com.google.gwt.layout.client.Layout.Layer; import com.google.gwt.user.client.Command; -import com.google.gwt.user.client.DeferredCommand; /** * Tests for {@link LayoutPanel}. Note that this only tests LayoutPanel-specific @@ -33,6 +36,7 @@ * Tests for a bug in LayoutCommand, which caused an animate() call, just * before an unnecessary forceLayout(), to get stuck. See issue 4360. */ + @SuppressWarnings("deprecation") public void testRedundantForceLayout() { final LayoutPanel p = new LayoutPanel(); Label l = new Label("foo"); @@ -42,7 +46,8 @@ p.forceLayout(); delayTestFinish(5000); - DeferredCommand.addCommand(new Command() { + // Fully qualified to avoid the deprecation warning in the import section + com.google.gwt.user.client.DeferredCommand.addCommand(new Command() { public void execute() { p.animate(100, new AnimationCallback() { public void onLayout(Layer layer, double progress) { @@ -62,6 +67,7 @@ * cropped up on IE7 as a result of CSS expressions used in PopupImplIE6, as * described in issue 4532. */ + @SuppressWarnings("deprecation") public void testWeirdPopupInteraction() { assertTrue(Document.get().isCSS1Compat()); @@ -73,7 +79,8 @@ popup.center(); delayTestFinish(2000); - DeferredCommand.addCommand(new Command() { + // Fully qualified to avoid the deprecation warning in the import section + com.google.gwt.user.client.DeferredCommand.addCommand(new Command() { public void execute() { int offsetWidth = lp.getOffsetWidth(); int offsetHeight = lp.getOffsetHeight(); @@ -83,4 +90,415 @@ } }); } + + /** + * Tests + * {@link LayoutPanel#setWidgetBottomHeight(Widget, double, Unit, double, Unit)} + * . + */ + public void testSetWidgetBottomHeight() { + LayoutPanel layoutPanel = createLayoutPanel(); + Widget label = createLabelInPanel(layoutPanel); + + layoutPanel.setWidgetBottomHeight(label, 1.5, Unit.EM, 2.0, Unit.EM); + + assertLayerProperties( + label, + new LayerProperties().bottom(1.5).bottomUnit(Unit.EM).height(2.0).heightUnit( + Unit.EM)); + } + + /** + * Tests + * {@link LayoutPanel#setWidgetBottomHeight(IsWidget, double, Unit, double, Unit)} + * . + */ + public void testSetWidgetBottomHeightAsIsWidget() { + LayoutPanel layoutPanel = createLayoutPanel(); + Widget label = createLabelInPanel(layoutPanel); + + // IsWidget cast to call the overloaded version + layoutPanel.setWidgetBottomHeight((IsWidget) label, 10.0, Unit.PX, 15.0, + Unit.PX); + + assertLayerProperties( + label, + new LayerProperties().bottom(10.0).bottomUnit(Unit.PX).height(15.0).heightUnit( + Unit.PX)); + } + + /** + * Tests + * {@link LayoutPanel#setWidgetLeftRight(Widget, double, Unit, double, Unit)} + * . + */ + public void testSetWidgetLeftRight() { + LayoutPanel layoutPanel = createLayoutPanel(); + Widget label = createLabelInPanel(layoutPanel); + + layoutPanel.setWidgetLeftRight(label, 10.0, Unit.PX, 20.0, Unit.PX); + + assertLayerProperties( + label, + new LayerProperties().left(10.0).leftUnit(Unit.PX).right(20.0).rightUnit( + Unit.PX)); + } + + /** + * Tests + * {@link LayoutPanel#setWidgetLeftRight(IsWidget, double, Unit, double, Unit)} + * . + */ + public void testSetWidgetLeftRightAsIsWidget() { + LayoutPanel layoutPanel = createLayoutPanel(); + Widget label = createLabelInPanel(layoutPanel); + + // IsWidget cast to call the overloaded version + layoutPanel.setWidgetLeftRight((IsWidget) label, 10.0, Unit.PX, 15.0, + Unit.PX); + + assertLayerProperties( + label, + new LayerProperties().left(10.0).leftUnit(Unit.PX).right(15.0).rightUnit( + Unit.PX)); + } + + /** + * Tests + * {@link LayoutPanel#setWidgetLeftWidth(Widget, double, Unit, double, Unit)} + * . + */ + public void testSetWidgetLeftWidth() { + LayoutPanel layoutPanel = createLayoutPanel(); + Widget label = createLabelInPanel(layoutPanel); + + layoutPanel.setWidgetLeftWidth(label, 10.0, Unit.PX, 20.0, Unit.PX); + + assertLayerProperties( + label, + new LayerProperties().left(10.0).leftUnit(Unit.PX).width(20.0).widthUnit( + Unit.PX)); + } + + /** + * Tests + * {@link LayoutPanel#setWidgetLeftWidth(IsWidget, double, Unit, double, Unit)} + * . + */ + public void testSetWidgetLeftWidthAsIsWidget() { + LayoutPanel layoutPanel = createLayoutPanel(); + Widget label = createLabelInPanel(layoutPanel); + + // IsWidget cast to call the overloaded version + layoutPanel.setWidgetLeftWidth((IsWidget) label, 10.0, Unit.PX, 15.0, + Unit.PX); + + assertLayerProperties( + label, + new LayerProperties().left(10.0).leftUnit(Unit.PX).width(15.0).widthUnit( + Unit.PX)); + } + + /** + * Tests + * {@link LayoutPanel#setWidgetRightWidth(Widget, double, Unit, double, Unit)} + * . + */ + public void testSetWidgetRightWidth() { + LayoutPanel layoutPanel = createLayoutPanel(); + Widget label = createLabelInPanel(layoutPanel); + + layoutPanel.setWidgetRightWidth(label, 10.0, Unit.PX, 20.0, Unit.PX); + + assertLayerProperties( + label, + new LayerProperties().right(10.0).rightUnit(Unit.PX).width(20.0).widthUnit( + Unit.PX)); + } + + /** + * Tests + * {@link LayoutPanel#setWidgetRightWidth(IsWidget, double, Unit, double, Unit)} + * . + */ + public void testSetWidgetRightWidthAsIsWidget() { + LayoutPanel layoutPanel = createLayoutPanel(); + Widget label = createLabelInPanel(layoutPanel); + + // IsWidget cast to call the overloaded version + layoutPanel.setWidgetRightWidth((IsWidget) label, 10.0, Unit.PX, 15.0, + Unit.PX); + + assertLayerProperties( + label, + new LayerProperties().right(10.0).rightUnit(Unit.PX).width(15.0).widthUnit( + Unit.PX)); + } + + /** + * Tests + * {@link LayoutPanel#setWidgetTopBottom(Widget, double, Unit, double, Unit)} + * . + */ + public void testSetWidgetTopBottom() { + LayoutPanel layoutPanel = createLayoutPanel(); + Widget label = createLabelInPanel(layoutPanel); + + layoutPanel.setWidgetTopBottom(label, 10.0, Unit.PX, 20.0, Unit.PX); + + assertLayerProperties( + label, + new LayerProperties().top(10.0).topUnit(Unit.PX).bottom(20.0).bottomUnit( + Unit.PX)); + } + + /** + * Tests + * {@link LayoutPanel#setWidgetTopBottom(IsWidget, double, Unit, double, Unit)} + * . + */ + public void testSetWidgetTopBottomAsIsWidget() { + LayoutPanel layoutPanel = createLayoutPanel(); + Widget label = createLabelInPanel(layoutPanel); + + // IsWidget cast to call the overloaded version + layoutPanel.setWidgetTopBottom((IsWidget) label, 10.0, Unit.PX, 15.0, + Unit.PX); + + assertLayerProperties( + label, + new LayerProperties().top(10.0).topUnit(Unit.PX).bottom(15.0).bottomUnit( + Unit.PX)); + } + + /** + * Tests + * {@link LayoutPanel#setWidgetTopHeight(Widget, double, Unit, double, Unit)} + * . + */ + public void testSetWidgetTopHeight() { + LayoutPanel layoutPanel = createLayoutPanel(); + Widget label = createLabelInPanel(layoutPanel); + + layoutPanel.setWidgetTopHeight(label, 10.0, Unit.PX, 20.0, Unit.PX); + + assertLayerProperties( + label, + new LayerProperties().top(10.0).topUnit(Unit.PX).height(20.0).heightUnit( + Unit.PX)); + } + + /** + * Tests + * {@link LayoutPanel#setWidgetTopHeight(IsWidget, double, Unit, double, Unit)} + * . + */ + public void testSetWidgetTopHeightAsIsWidget() { + LayoutPanel layoutPanel = createLayoutPanel(); + Widget label = createLabelInPanel(layoutPanel); + + // IsWidget cast to call the overloaded version + layoutPanel.setWidgetTopHeight((IsWidget) label, 10.0, Unit.PX, 15.0, + Unit.PX); + + assertLayerProperties( + label, + new LayerProperties().top(10.0).topUnit(Unit.PX).height(15.0).heightUnit( + Unit.PX)); + } + + protected LayoutPanel createLayoutPanel() { + return new LayoutPanel(); + } + + protected Widget createLabelInPanel(LayoutPanel layoutPanel) { + Widget label = new Label("foo"); + layoutPanel.add(label); + return label; + } + + /** + * <p> + * Parameter class that hold a set of properties of a {@link Layout.Layer}. The + * default value for all the six properties (top, right, bottom, left, height + * and width) is 0.0, value that matches the default values for this + * properties in the actual {@link Layout.Layer}. The default values for the + * units also match the default values in {@link Layout.Layer}: <br> + * <code>topUnit = rightUnit = bottomUnit = leftUnit = {@link Unit#PX}</code> + * <br> + * <code>heigthUnit = widthUnit = null</code> + * </p> + * This class must be used as a parameter of + * {@link LayoutPanelTest#assertLayerProperties(Widget, LayerProperties)}, and + * was thought to be used as a builder in order to improve readability and + * reduce errors with the parameters of + * {@link LayoutPanelTest#assertLayerProperties(Widget, LayerProperties)}. + * </p> + * + * <p> + * <h3>Example:</h3> + * <code>assertLayerProperties(label, + * new LayerProperties().bottom(10.0).bottomUnit(Unit.PX) + * .height(15.0).heightUnit(Unit.PX);</code> + * </p> + */ + static class LayerProperties { + private double top, right, bottom, left, height, width; + private Unit topUnit = Unit.PX, rightUnit = Unit.PX, bottomUnit = Unit.PX, + leftUnit = Unit.PX, heightUnit, widthUnit; + + LayerProperties top(double top) { + this.top = top; + return this; + } + + double getTop() { + return top; + } + + LayerProperties topUnit(Unit topUnit) { + this.topUnit = topUnit; + return this; + } + + Unit getTopUnit() { + return topUnit; + } + + LayerProperties right(double right) { + this.right = right; + return this; + } + + double getRight() { + return right; + } + + LayerProperties rightUnit(Unit rightUnit) { + this.rightUnit = rightUnit; + return this; + } + + Unit getRightUnit() { + return rightUnit; + } + + LayerProperties bottom(double bottom) { + this.bottom = bottom; + return this; + } + + double getBottom() { + return bottom; + } + + LayerProperties bottomUnit(Unit bottomUnit) { + this.bottomUnit = bottomUnit; + return this; + } + + Unit getBottomUnit() { + return bottomUnit; + } + + LayerProperties left(double left) { + this.left = left; + return this; + } + + double getLeft() { + return left; + } + + LayerProperties leftUnit(Unit leftUnit) { + this.leftUnit = leftUnit; + return this; + } + + Unit getLeftUnit() { + return leftUnit; + } + + LayerProperties height(double height) { + this.height = height; + return this; + } + + double getHeight() { + return height; + } + + LayerProperties heightUnit(Unit heightUnit) { + this.heightUnit = heightUnit; + return this; + } + + Unit getHeightUnit() { + return heightUnit; + } + + LayerProperties width(double width) { + this.width = width; + return this; + } + + double getWidth() { + return width; + } + + LayerProperties widthUnit(Unit widthUnit) { + this.widthUnit = widthUnit; + return this; + } + + Unit getWidthUnit() { + return widthUnit; + } + } + + /** + * <p> + * Asserts that the layer properties of the <b>widget</b> are the given ones. + * </p> + * + * @param widget the widget being tested + * @param expectedLayerProperties the expected properties of <b>widget</b> + */ + private void assertLayerProperties(final Widget widget, + final LayerProperties expectedLayerProperties) { + delayTestFinish(2000); + Scheduler.get().scheduleDeferred(new ScheduledCommand() { + + public void execute() { + Layer layer = (Layout.Layer) widget.getLayoutData(); + LayerFriend helper = new LayerFriend(layer); + + assertEquals("Top", expectedLayerProperties.getTop(), helper.getTop()); + assertEquals("Right", expectedLayerProperties.getRight(), + helper.getRight()); + assertEquals("Bottom", expectedLayerProperties.getBottom(), + helper.getBottom()); + assertEquals("Left", expectedLayerProperties.getLeft(), + helper.getLeft()); + assertEquals("Height", expectedLayerProperties.getHeight(), + helper.getHeight()); + assertEquals("Width", expectedLayerProperties.getWidth(), + helper.getWidth()); + + assertEquals("Top Units", expectedLayerProperties.getTopUnit(), + helper.getTopUnit()); + assertEquals("Right Units", expectedLayerProperties.getRightUnit(), + helper.getRightUnit()); + assertEquals("Bottom Units", expectedLayerProperties.getBottomUnit(), + helper.getBottomUnit()); + assertEquals("Left Units", expectedLayerProperties.getLeftUnit(), + helper.getLeftUnit()); + assertEquals("Height Units", expectedLayerProperties.getHeightUnit(), + helper.getHeightUnit()); + assertEquals("Width Units", expectedLayerProperties.getWidthUnit(), + helper.getWidthUnit()); + finishTest(); + } + }); + } }
diff --git a/user/test/com/google/gwt/user/client/ui/StackLayoutPanelTest.java b/user/test/com/google/gwt/user/client/ui/StackLayoutPanelTest.java index 3cb288b..8acbeb0 100644 --- a/user/test/com/google/gwt/user/client/ui/StackLayoutPanelTest.java +++ b/user/test/com/google/gwt/user/client/ui/StackLayoutPanelTest.java
@@ -62,12 +62,66 @@ } } + /** + * Tests {@link StackLayoutPanel#add(Widget, String, boolean, double)}. + */ + public void testAddWithTextHeader() { + StackLayoutPanel panel = createStackLayoutPanel(Unit.EM); + Widget widget = new Label("foo"); + + panel.add(widget, "header", false, 1); + + assertLogicalPaternityOfChild(panel, widget); + } + + /** + * Tests {@link StackLayoutPanel#add(IsWidget, String, boolean, double)}. + */ + public void testAddWithTextHeaderAsIsWidget() { + StackLayoutPanel panel = createStackLayoutPanel(Unit.EM); + Widget widget = new Label("foo"); + + // IsWidget cast to call the overloaded version + panel.add((IsWidget) widget, "header", false, 1); + + assertLogicalPaternityOfChild(panel, widget); + } + + /** + * Tests {@link StackLayoutPanel#add(Widget, Widget, double)}. + */ + public void testAddWithWidgetHeader() { + StackLayoutPanel panel = createStackLayoutPanel(Unit.EM); + Widget header = new Label("foo"); + Widget widget = new Label("bar"); + + panel.add(widget, header, 1); + + assertLogicalPaternityOfChild(panel, widget); + assertLogicalPaternityOfHeader(panel, widget, header); + } + + /** + * Tests {@link StackLayoutPanel#add(IsWidget, IsWidget, double)}. + */ + public void testAddWithWidgetHeaderAsIsWidget() { + StackLayoutPanel panel = createStackLayoutPanel(Unit.EM); + Widget header = new Label("foo"); + Widget widget = new Label("bar"); + + // IsWidget casts to call the overloaded version + panel.add((IsWidget) widget, (IsWidget) header, 1); + + assertLogicalPaternityOfChild(panel, widget); + assertLogicalPaternityOfHeader(panel, widget, header); + } + public void testAddWithSafeHtml() { StackLayoutPanel panel = new StackLayoutPanel(Unit.EM); panel.add(new HTML("foo"), SafeHtmlUtils.fromSafeConstant(html), 1.0); - + assertEquals(1, panel.getWidgetCount()); - assertEquals(html, + assertEquals(html, panel.getHeaderWidget(0).getElement().getInnerHTML().toLowerCase()); } @@ -123,9 +177,9 @@ public void testInsertSafeHtml() { StackLayoutPanel panel = new StackLayoutPanel(Unit.EM); panel.insert(new HTML("foo"), SafeHtmlUtils.fromSafeConstant(html), 1.0, 0); - + assertEquals(1, panel.getWidgetCount()); - assertEquals(html, + assertEquals(html, panel.getHeaderWidget(0).getElement().getInnerHTML().toLowerCase()); } @@ -227,7 +281,7 @@ panel.add(new HTML("bar"), "foo", 1.0); panel.setHeaderHTML(0, SafeHtmlUtils.fromSafeConstant(html)); Widget header = panel.getHeaderWidget(0); - + assertEquals(html, header.getElement().getInnerHTML().toLowerCase()); } @@ -287,4 +341,35 @@ p.showWidget(l2); assertEquals(l2, p.getVisibleWidget()); } + + /** + * Asserts that <b>widget</b> is attached to <b>panel</b> as a child in the + * logical representation of <b>panel</b>. + * + * @param panel the parent panel + * @param child a expected child of <b>panel</b> + */ + private void assertLogicalPaternityOfChild(StackLayoutPanel panel, + Widget child) { + assertTrue("The widget should be a child of the panel", + panel.getWidgetIndex(child) >= 0); + } + + /** + * Asserts that <b>header</b> is attached to <b>panel</b> as a header of + * <b>widget</b> in the logical representation of <b>panel</b>. + * + * @param panel the parent panel + * @param child the child whose header is being tested + * @param header the expected header of <b>child</b> + */ + private void assertLogicalPaternityOfHeader(StackLayoutPanel panel, + Widget child, Widget header) { + assertSame("The header should be attached to the panel.", header, + panel.getHeaderWidget(child)); + } + + private StackLayoutPanel createStackLayoutPanel(Unit unit) { + return new StackLayoutPanel(unit); + } }
diff --git a/user/test/com/google/gwt/user/client/ui/TreeTest.java b/user/test/com/google/gwt/user/client/ui/TreeTest.java index 168f747..fdbf29f 100644 --- a/user/test/com/google/gwt/user/client/ui/TreeTest.java +++ b/user/test/com/google/gwt/user/client/ui/TreeTest.java
@@ -39,12 +39,40 @@ public String getModuleName() { return "com.google.gwt.user.DebugTest"; } + + /** + * Test for {@link Tree#add(IsWidget)}. + */ + public void testAddAsIsWidget() { + Tree t = createTree(); + Widget widget = new Label("foo"); + + // IsWidget cast to call the overloaded version + t.add((IsWidget) widget); + + assertEquals("The number of items should be 1", 1, t.getItemCount()); + assertSame(widget, t.getItem(0).getWidget()); + } /** + * Ensures that {@link Tree#add(Widget)} does <b>NOT</b> throws a + * {@link NullPointerException} when the Widget argument is <code>null</code>, + * for stupidity consistency with add(Widget). + */ + public void testAddNullAsIsWidget() { + Tree t = createTree(); + // IsWidget reference to call the overload version + IsWidget widget = null; + + t.add(widget); + // ta da... + } + + /** * Test for {@link Tree#addItem(IsTreeItem)}. */ public void testAddItemIsTreeItem() { - Tree t = new Tree(); + Tree t = createTree(); TreeItem item = new TreeItem("hello"); t.addItem((IsTreeItem) item); assertEquals(1, t.getItemCount()); @@ -52,16 +80,16 @@ } public void testAddItemSafeHtml() { - Tree t = new Tree(); + Tree t = createTree(); TreeItem item = t.addItem(SafeHtmlUtils.fromSafeConstant(html)); assertEquals(html, item.getHTML().toLowerCase()); } - + /** * Test for {@link Tree#addTextItem(String)}. */ public void testAddTextItem() { - Tree t = new Tree(); + Tree t = createTree(); String text = "Some<br>text"; TreeItem item = t.addTextItem(text); assertEquals(text, item.getText()); @@ -71,11 +99,11 @@ } public void testAttachDetachOrder() { - HasWidgetsTester.testAll(new Tree(), new Adder(), true); + HasWidgetsTester.testAll(createTree(), new Adder(), true); } public void testClear() { - Tree t = new Tree(); + Tree t = createTree(); // Adding widget to end of tree, widgets still have their parents set // correctly. TreeItem a = new TreeItem("a"); @@ -99,7 +127,7 @@ } public void testDebugId() { - Tree tree = new Tree(); + Tree tree = createTree(); TreeItem top0 = tree.addItem("top0"); TreeItem top1 = tree.addItem("top1"); TreeItem top2 = tree.addItem("top2"); @@ -131,7 +159,7 @@ } public void testInsertSameItemRepeatedly() { - Tree t = new Tree(); + Tree t = createTree(); TreeItem ti = new TreeItem(); TreeItem wti = new TreeItem(); wti.setWidget(new Label("label")); @@ -145,13 +173,13 @@ } public void testInsertItemSafeHtml() { - Tree t = new Tree(); + Tree t = createTree(); TreeItem item = t.insertItem(0, SafeHtmlUtils.fromSafeConstant(html)); assertEquals(html, item.getHTML().toLowerCase()); } public void testIterator() { - Tree tree = new Tree(); + Tree tree = createTree(); Iterator<TreeItem> iter = tree.treeItemIterator(); assertFalse(iter.hasNext()); TreeItem a = tree.addItem("a"); @@ -180,7 +208,7 @@ public void testNulls() { // Checking for setting the widget null then clearing the tree. - Tree t = new Tree(); + Tree t = createTree(); TreeItem item = new TreeItem(); item.setWidget(null); t.clear(); @@ -192,7 +220,7 @@ } public void testRemove() { - Tree t = new Tree(); + Tree t = createTree(); TreeItem item = t.addItem("a"); TreeItem itemb = t.addItem("b"); t.setSelectedItem(item); @@ -208,12 +236,12 @@ Iterator<TreeItem> iter2 = t.treeItemIterator(); assertFalse(iter2.hasNext()); } - + /** * Test for {@link Tree#removeItem(IsTreeItem)}. */ public void testRemoveIsTreeItem() { - Tree t = new Tree(); + Tree t = createTree(); TreeItem itemA = t.addItem("a"); TreeItem itemB = t.addItem("b"); // initial state @@ -227,12 +255,12 @@ // ignore null t.removeItem((IsTreeItem) null); } - + /** * Test for {@link Tree#removeItems()}. */ public void testRemoveItems() { - Tree t = new Tree(); + Tree t = createTree(); TreeItem itemA = t.addItem("a"); TreeItem itemB = t.addItem("b"); // initial state @@ -245,14 +273,14 @@ } public void testRootAdd() { - Tree t = new Tree(); + Tree t = createTree(); Label l = new Label("hello"); t.add(l); assertEquals(t, l.getParent()); } public void testRootInsert() { - Tree t = new Tree(); + Tree t = createTree(); TreeItem b = t.addItem("b"); assertEquals(1, t.getItemCount()); assertEquals(b, t.getItem(0)); @@ -284,7 +312,7 @@ } public void testRootInsertInvalidIndex() { - Tree t = new Tree(); + Tree t = createTree(); t.addItem("a"); t.addItem("b"); t.addItem("c"); @@ -307,7 +335,7 @@ } public void testSwap() { - Tree t = new Tree(); + Tree t = createTree(); // Start with text. TreeItem item = t.addItem("hello"); @@ -342,7 +370,7 @@ public void testTree() { // Simple widget - Tree t = new Tree(); + Tree t = createTree(); Label l = new Label("simple widget"); TreeItem simple = new TreeItem(l); t.addItem(simple); @@ -385,7 +413,7 @@ assertEquals(t, eLabel.getParent()); // Tree inside of Tree. - Tree childTree = new Tree(); + Tree childTree = createTree(); t.addItem(new TreeItem(childTree)); // Swap TreeItems to new Tree. @@ -399,4 +427,8 @@ assertNull(eLabel.getParent()); assertFalse(childTree.getChildWidgets().containsKey(eLabel.getParent())); } + + private Tree createTree() { + return new Tree(); + } }