001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.ui;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019: import com.google.gwt.user.client.DOM;
020: import com.google.gwt.user.client.Element;
021:
022: /**
023: * Tests the DockPanel widget.
024: */
025: public class DockPanelTest extends GWTTestCase {
026:
027: static class Adder implements HasWidgetsTester.WidgetAdder {
028: public void addChild(HasWidgets container, Widget child) {
029: ((DockPanel) container).add(child, DockPanel.NORTH);
030: }
031: }
032:
033: private static class OnLoadTestLabel extends Label {
034: boolean attachedWhenLoaded;
035:
036: public OnLoadTestLabel(String text) {
037: super (text);
038: }
039:
040: protected void onLoad() {
041: // Crawl up the DOM, looking for the body.
042: Element curElem = getElement();
043: Element body = RootPanel.getBodyElement();
044: while (curElem != null) {
045: if (DOM.compare(curElem, body)) {
046: attachedWhenLoaded = true;
047: }
048: curElem = DOM.getParent(curElem);
049: }
050: }
051: }
052:
053: public String getModuleName() {
054: return "com.google.gwt.user.User";
055: }
056:
057: public void testAddRemove() {
058: final DockPanel dock = new DockPanel();
059:
060: OnLoadTestLabel l0 = new OnLoadTestLabel("l0");
061: OnLoadTestLabel l1 = new OnLoadTestLabel("l1");
062: OnLoadTestLabel l2 = new OnLoadTestLabel("l2");
063: OnLoadTestLabel l3 = new OnLoadTestLabel("l3");
064: OnLoadTestLabel l4 = new OnLoadTestLabel("l4");
065: OnLoadTestLabel l5 = new OnLoadTestLabel("l5");
066:
067: dock.add(l0, DockPanel.NORTH);
068: dock.add(l1, DockPanel.NORTH);
069: dock.add(l2, DockPanel.WEST);
070:
071: // Add the dock halfway through to make sure onLoad is called for its
072: // children correctly in both cases.
073: RootPanel.get().add(dock);
074: dock.add(l3, DockPanel.EAST);
075: dock.add(l5, DockPanel.CENTER);
076: dock.add(l4, DockPanel.SOUTH);
077:
078: // Ensure that the CENTER element can be added only once.
079: try {
080: dock.add(new Label("garbage"), DockPanel.CENTER);
081: fail("Expecting IllegalArgumentException (from adding CENTER twice)");
082: } catch (IllegalArgumentException e) {
083: }
084:
085: // Test the structure.
086: Element table = dock.getElement();
087: Element tbody = DOM.getFirstChild(table);
088:
089: assertEquals(DOM.getChildCount(tbody), 4);
090:
091: Element tr0 = DOM.getChild(tbody, 0);
092: Element tr1 = DOM.getChild(tbody, 1);
093: Element tr2 = DOM.getChild(tbody, 2);
094: Element tr3 = DOM.getChild(tbody, 3);
095:
096: assertEquals(DOM.getChildCount(tr0), 1);
097: assertEquals(DOM.getChildCount(tr1), 1);
098: assertEquals(DOM.getChildCount(tr2), 3);
099: assertEquals(DOM.getChildCount(tr3), 1);
100:
101: assertTrue(l0.attachedWhenLoaded);
102: assertTrue(l1.attachedWhenLoaded);
103: assertTrue(l2.attachedWhenLoaded);
104: assertTrue(l3.attachedWhenLoaded);
105: assertTrue(l4.attachedWhenLoaded);
106: assertTrue(l5.attachedWhenLoaded);
107:
108: // Ensure that adding an existing child again moves it to the new slot.
109: // (move l4 from SOUTH to NORTH)
110: dock.add(l4, DockPanel.NORTH);
111: assertTrue(((DockPanel.LayoutData) l4.getLayoutData()).direction == DockPanel.NORTH);
112: }
113:
114: public void testAttachDetachOrder() {
115: HasWidgetsTester.testAll(new DockPanel(), new Adder());
116: }
117: }
|