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: * TODO: document me.
024: */
025: public class WidgetCollectionTest extends GWTTestCase {
026:
027: public String getModuleName() {
028: return "com.google.gwt.user.User";
029: }
030:
031: private static class Container implements HasWidgets {
032:
033: public WidgetCollection collection = new WidgetCollection(this );
034:
035: public void add(Widget w) {
036: }
037:
038: public void clear() {
039: }
040:
041: public Iterator iterator() {
042: return null;
043: }
044:
045: public boolean remove(Widget w) {
046: if (!collection.contains(w)) {
047: return false;
048: }
049: collection.remove(w);
050: return true;
051: }
052: }
053:
054: public void testAddRemove() {
055: Container c = new Container();
056: WidgetCollection wc = c.collection;
057:
058: Label l0 = new Label("foo");
059: Label l1 = new Label("bar");
060: Label l2 = new Label("baz");
061:
062: wc.add(l0);
063: wc.add(l1);
064: wc.add(l2);
065:
066: assertEquals(wc.size(), 3);
067: assertEquals(wc.get(1), l1);
068:
069: wc.remove(l1);
070: assertEquals(wc.size(), 2);
071: assertEquals(wc.get(1), l2);
072: assertFalse(wc.contains(l1));
073:
074: wc.remove(0);
075: assertFalse(wc.contains(l0));
076: assertEquals(wc.indexOf(l2), 0);
077: }
078:
079: public void testIterator() {
080: Container c = new Container();
081: WidgetCollection wc = c.collection;
082:
083: Label l0 = new Label("foo");
084: Label l1 = new Label("bar");
085: Label l2 = new Label("baz");
086:
087: wc.add(l0);
088: wc.add(l1);
089: wc.add(l2);
090:
091: Iterator it = wc.iterator();
092: assertTrue(it.hasNext());
093: assertEquals(it.next(), l0);
094: it.remove();
095: assertTrue(it.hasNext());
096: assertEquals(it.next(), l1);
097: assertEquals(it.next(), l2);
098: assertFalse(it.hasNext());
099: }
100: }
|