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:
020: import java.util.Iterator;
021:
022: /**
023: * Tests the TabPanel.
024: */
025: public class TabPanelTest extends GWTTestCase {
026:
027: static class Adder implements HasWidgetsTester.WidgetAdder {
028: public void addChild(HasWidgets container, Widget child) {
029: ((TabPanel) container).add(child, "foo");
030: }
031: }
032:
033: public String getModuleName() {
034: return "com.google.gwt.user.User";
035: }
036:
037: public void testAttachDetachOrder() {
038: HasWidgetsTester.testAll(new TabPanel(), new Adder());
039: }
040:
041: public void testInsertMultipleTimes() {
042: TabPanel p = new TabPanel();
043:
044: TextBox tb = new TextBox();
045: p.add(tb, "Title");
046: p.add(tb, "Title");
047: p.add(tb, "Title3");
048:
049: assertEquals(1, p.getWidgetCount());
050: assertEquals(0, p.getWidgetIndex(tb));
051: Iterator i = p.iterator();
052: assertTrue(i.hasNext());
053: assertTrue(tb.equals(i.next()));
054: assertFalse(i.hasNext());
055:
056: Label l = new Label();
057: p.add(l, "Title");
058: p.add(l, "Title");
059: p.add(l, "Title3");
060: assertEquals(2, p.getWidgetCount());
061: assertEquals(0, p.getWidgetIndex(tb));
062: assertEquals(1, p.getWidgetIndex(l));
063:
064: p.insert(l, "Title", 0);
065: assertEquals(2, p.getWidgetCount());
066: assertEquals(0, p.getWidgetIndex(l));
067: assertEquals(1, p.getWidgetIndex(tb));
068:
069: p.insert(l, "Title", 1);
070: assertEquals(2, p.getWidgetCount());
071: assertEquals(0, p.getWidgetIndex(l));
072: assertEquals(1, p.getWidgetIndex(tb));
073:
074: p.insert(l, "Title", 2);
075: assertEquals(2, p.getWidgetCount());
076: assertEquals(0, p.getWidgetIndex(tb));
077: assertEquals(1, p.getWidgetIndex(l));
078: }
079:
080: public void testInsertWithHTML() {
081: TabPanel p = new TabPanel();
082: Label l = new Label();
083: p.add(l, "three");
084: p.insert(new HTML("<b>hello</b>"), "two", true, 0);
085: p.insert(new HTML("goodbye"), "one", false, 0);
086: assertEquals(3, p.getWidgetCount());
087: }
088:
089: /**
090: * Tests to ensure that arbitrary widgets can be added/inserted effectively.
091: */
092: public void testInsertWithWidgets() {
093: TabPanel p = new TabPanel();
094:
095: TextBox wa = new TextBox();
096: CheckBox wb = new CheckBox();
097: VerticalPanel wc = new VerticalPanel();
098: wc.add(new Label("First"));
099: wc.add(new Label("Second"));
100:
101: p.add(new Label("Content C"), wc);
102: p.insert(new Label("Content B"), wb, 0);
103: p.insert(new Label("Content A"), wa, 0);
104:
105: // Call these to ensure we don't throw an exception.
106: assertTrue(p.getTabBar().getTabHTML(0).length() > 0);
107: assertTrue(p.getTabBar().getTabHTML(1).length() > 0);
108: assertTrue(p.getTabBar().getTabHTML(2).length() > 0);
109: assertEquals(3, p.getWidgetCount());
110: }
111:
112: public void testIterator() {
113: TabPanel p = new TabPanel();
114: HTML foo = new HTML("foo");
115: HTML bar = new HTML("bar");
116: HTML baz = new HTML("baz");
117: p.add(foo, "foo");
118: p.add(bar, "bar");
119: p.add(baz, "baz");
120:
121: // Iterate over the entire set and make sure it stops correctly.
122: Iterator it = p.iterator();
123: assertTrue(it.hasNext());
124: assertTrue(it.next() == foo);
125: assertTrue(it.hasNext());
126: assertTrue(it.next() == bar);
127: assertTrue(it.hasNext());
128: assertTrue(it.next() == baz);
129: assertFalse(it.hasNext());
130:
131: // Test removing using the iterator.
132: it = p.iterator();
133: it.next();
134: it.remove();
135: assertTrue(it.next() == bar);
136: assertTrue(p.getWidgetCount() == 2);
137: assertTrue(p.getWidget(0) == bar);
138: assertTrue(p.getWidget(1) == baz);
139: }
140:
141: public void testSelectionEvents() {
142: TabPanel p = new TabPanel();
143: RootPanel.get().add(p);
144:
145: p.add(new Button("foo"), "foo");
146: p.add(new Button("bar"), "bar");
147:
148: // Make sure selecting a tab fires both events in the right order.
149: p.addTabListener(new TabListener() {
150: private boolean onBeforeFired;
151:
152: public boolean onBeforeTabSelected(SourcesTabEvents sender,
153: int tabIndex) {
154: onBeforeFired = true;
155: return true;
156: }
157:
158: public void onTabSelected(SourcesTabEvents sender,
159: int tabIndex) {
160: assertTrue(onBeforeFired);
161: finishTest();
162: }
163: });
164:
165: this .delayTestFinish(1000);
166: p.selectTab(1);
167: }
168:
169: public void testUnmodifiableDeckPanelSubclasses() {
170: TabPanel p = new TabPanel();
171: DeckPanel d = p.getDeckPanel();
172:
173: try {
174: d.add(new Label("No"));
175: fail("Internal DeckPanel should not allow add() method");
176: } catch (UnsupportedOperationException e) {
177: // Expected behavior
178: }
179:
180: try {
181: d.insert(new Label("No"), 0);
182: fail("Internal DeckPanel should not allow insert() method");
183: } catch (UnsupportedOperationException e) {
184: // Expected behavior
185: }
186:
187: try {
188: d.clear();
189: fail("Internal DeckPanel should not allow clear() method");
190: } catch (UnsupportedOperationException e) {
191: // Expected behavior
192: }
193: }
194:
195: public void testUnmodifiableTabBarSubclasses() {
196: TabPanel p = new TabPanel();
197: TabBar b = p.getTabBar();
198:
199: try {
200: b.addTab("no");
201: fail("Internal TabBar should not allow addTab() method");
202: } catch (UnsupportedOperationException e) {
203: // Expected behavior
204: }
205:
206: try {
207: b.addTab("no", true);
208: fail("Internal TabBar should not allow addTab() method");
209: } catch (UnsupportedOperationException e) {
210: // Expected behavior
211: }
212:
213: try {
214: b.addTab(new Label("no"));
215: fail("Internal TabBar should not allow addTab() method");
216: } catch (UnsupportedOperationException e) {
217: // Expected behavior
218: }
219:
220: try {
221: b.insertTab("no", 0);
222: fail("Internal TabBar should not allow insertTab() method");
223: } catch (UnsupportedOperationException e) {
224: // Expected behavior
225: }
226:
227: try {
228: b.insertTab("no", true, 0);
229: fail("Internal TabBar should not allow insertTab() method");
230: } catch (UnsupportedOperationException e) {
231: // Expected behavior
232: }
233:
234: try {
235: b.insertTab(new Label("no"), 0);
236: fail("Internal TabBar should not allow insertTab() method");
237: } catch (UnsupportedOperationException e) {
238: // Expected behavior
239: }
240:
241: try {
242: b.removeTab(0);
243: fail("Internal TabBar should not allow removeTab() method");
244: } catch (UnsupportedOperationException e) {
245: // Expected behavior
246: }
247: }
248: }
|