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: import java.util.Iterator;
023:
024: /**
025: * Tests the Tree widget.
026: */
027: public class TreeTest extends GWTTestCase {
028:
029: static class Adder implements HasWidgetsTester.WidgetAdder {
030: public void addChild(HasWidgets container, Widget child) {
031: ((Tree) container).addItem(child);
032: }
033: }
034:
035: public String getModuleName() {
036: return "com.google.gwt.user.User";
037: }
038:
039: public void testAttachDetachOrder() {
040: HasWidgetsTester.testAll(new Tree(), new Adder());
041: }
042:
043: public void testClear() {
044: Tree t = new Tree();
045: // Adding widget to end of tree, widgets still have their parents set
046: // correctly.
047: TreeItem a = new TreeItem("a");
048: TreeItem b = new TreeItem("b");
049: TreeItem c = new TreeItem("c");
050: TreeItem d = new TreeItem();
051: TreeItem e = new TreeItem();
052: Label dLabel = new Label("d");
053: Label eLabel = new Label("e");
054: d.setWidget(dLabel);
055: d.addItem(e);
056: c.addItem(d);
057: b.addItem(c);
058: a.addItem(b);
059: t.addItem(a);
060: t.addItem("hello");
061: t.addItem(eLabel);
062: t.clear();
063: assertFalse(t.treeItemIterator().hasNext());
064: assertEquals(0, t.getChildWidgets().size());
065: }
066:
067: public void testInsertSameItemRepeatedly() {
068: Tree t = new Tree();
069: TreeItem ti = new TreeItem();
070: TreeItem wti = new TreeItem();
071: wti.setWidget(new Label("label"));
072: for (int i = 0; i < 10; i++) {
073: t.addItem(ti);
074: t.addItem(wti);
075: }
076: assertEquals(2, t.getItemCount());
077: assertEquals(ti, t.getItem(0));
078: assertEquals(wti, t.getItem(1));
079: }
080:
081: public void testIterator() {
082: Tree tree = new Tree();
083: Iterator iter = tree.treeItemIterator();
084: assertFalse(iter.hasNext());
085: TreeItem a = tree.addItem("a");
086: TreeItem b = tree.addItem("b");
087: TreeItem c = tree.addItem("c");
088:
089: Iterator iter2 = tree.treeItemIterator();
090: assertEquals(a, iter2.next());
091: assertEquals(b, iter2.next());
092: assertEquals(c, iter2.next());
093: assertFalse(iter2.hasNext());
094:
095: TreeItem a_a = a.addItem("a_a");
096: TreeItem a_a_a = a_a.addItem("a_a_a");
097: TreeItem a_a_b = a_a.addItem("a_a_b");
098:
099: Iterator iter3 = tree.treeItemIterator();
100: assertEquals(a, iter3.next());
101: assertEquals(a_a, iter3.next());
102: assertEquals(a_a_a, iter3.next());
103: assertEquals(a_a_b, iter3.next());
104: assertEquals(b, iter3.next());
105: assertEquals(c, iter3.next());
106: assertFalse(iter3.hasNext());
107: }
108:
109: public void testNulls() {
110: // Checking for setting the widget null then clearing the tree.
111: Tree t = new Tree();
112: TreeItem item = new TreeItem();
113: item.setWidget(null);
114: t.clear();
115:
116: TreeItem a = t.addItem("");
117: TreeItem b = t.addItem(new Label("b"));
118: a.setWidget(null);
119: b.setWidget(null);
120: }
121:
122: public void testRemove() {
123: Tree t = new Tree();
124: TreeItem item = t.addItem("a");
125: TreeItem itemb = t.addItem("b");
126: t.setSelectedItem(item);
127: assertEquals(item, t.getSelectedItem());
128: item.remove();
129: assertNull(t.getSelectedItem());
130: Iterator iter = t.treeItemIterator();
131: assertTrue(iter.hasNext());
132: iter.next();
133: assertFalse(iter.hasNext());
134: t.removeItem(itemb);
135: assertNull(t.getSelectedItem());
136: Iterator iter2 = t.treeItemIterator();
137: assertFalse(iter2.hasNext());
138: }
139:
140: public void testRootAdd() {
141: Tree t = new Tree();
142: Label l = new Label("hello");
143: t.add(l);
144: assertEquals(t, l.getParent());
145: }
146:
147: public void testSwap() {
148: Tree t = new Tree();
149:
150: // Start with text.
151: TreeItem item = t.addItem("hello");
152: String inner = DOM.getInnerHTML(item.getContentElem());
153: assertTrue(inner.indexOf("hello") >= 0);
154: t.addItem(item);
155: Widget goodbyeWidget = new Label("goodbye");
156: item.setWidget(goodbyeWidget);
157: String innerWidget = DOM.getInnerHTML(item.getContentElem());
158: assertFalse(innerWidget.indexOf("hello") >= 0);
159:
160: // Start with widget.
161: Widget hello = new Label("hello");
162: TreeItem widgetItem = t.addItem(hello);
163: assertTrue(DOM.getInnerHTML(widgetItem.getContentElem())
164: .indexOf("hello") >= 0);
165: widgetItem.setText("goodbye");
166: assertFalse(DOM.getInnerHTML(widgetItem.getContentElem())
167: .indexOf("hello") >= 0);
168: assertTrue(DOM.getInnerHTML(widgetItem.getContentElem())
169: .indexOf("goodbye") >= 0);
170: assertNull(hello.getParent());
171:
172: // Move widget.
173: widgetItem.addItem(item);
174: assertEquals(t, goodbyeWidget.getParent());
175: assertEquals(goodbyeWidget, item.getWidget());
176:
177: // Set back to text.
178: item.setText("aloha");
179: assertEquals("aloha", DOM.getInnerHTML(item.getContentElem()));
180: assertNull(goodbyeWidget.getParent());
181: assertNull(item.getWidget());
182: }
183:
184: public void testTree() {
185: // Simple widget
186: Tree t = new Tree();
187: Label l = new Label("simple widget");
188: TreeItem simple = new TreeItem(l);
189: t.addItem(simple);
190:
191: // Adding test and widget
192: TreeItem item = new TreeItem();
193: t.addItem(item);
194: item.setWidget(new Label("now widget"));
195: Element elem = item.getContentElem();
196: assertEquals(1, DOM.getChildCount(elem));
197:
198: // Add widget to existing tree.
199: Label l2 = new Label("next widget");
200: simple.setWidget(l2);
201: assertEquals(t, l2.getParent());
202:
203: // Remove a tree item, make sure widget is removed from tree, but not tree
204: // item.
205: simple.remove();
206: assertEquals(l2, simple.getWidget());
207: assertNull(l2.getParent());
208:
209: // Adding widget to end of tree, widgets still have their parents set
210: // correctly.
211: TreeItem a = new TreeItem("a");
212: TreeItem b = new TreeItem("b");
213: TreeItem c = new TreeItem("c");
214: TreeItem d = new TreeItem();
215: TreeItem e = new TreeItem();
216: Label dLabel = new Label("d");
217: Label eLabel = new Label("e");
218: d.setWidget(dLabel);
219: d.addItem(e);
220: c.addItem(d);
221: b.addItem(c);
222: a.addItem(b);
223: t.addItem(a);
224: assertEquals(t, dLabel.getParent());
225: e.setWidget(eLabel);
226: assertEquals(t, eLabel.getParent());
227:
228: // Tree inside of Tree.
229: Tree childTree = new Tree();
230: t.addItem(new TreeItem(childTree));
231:
232: // Swap TreeItems to new Tree.
233: childTree.addItem(c);
234: assertEquals(childTree, dLabel.getParent());
235: assertEquals(childTree, eLabel.getParent());
236:
237: // Make sure remove clears.
238: d.remove();
239: assertNull(dLabel.getParent());
240: assertNull(eLabel.getParent());
241: assertFalse(childTree.getChildWidgets().containsKey(
242: eLabel.getParent()));
243: }
244: }
|