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: import java.util.NoSuchElementException;
022:
023: /**
024: * Tests {@link WidgetIterators}.
025: */
026: public class WidgetIteratorsTest extends GWTTestCase {
027:
028: /**
029: * Provides a class from which to construct mock containers in this test.
030: */
031: private static class MockWidget implements HasWidgets {
032: public void add(Widget w) {
033: fail("Unexpected call to add(Widget)");
034: }
035:
036: public void clear() {
037: fail("Unexpected call to clear()");
038: }
039:
040: public Iterator iterator() {
041: fail("Unexpected call to iterator()");
042: return null;
043: }
044:
045: public boolean remove(Widget w) {
046: fail("Unexpected call to remove(Widget)");
047: return false;
048: }
049: }
050:
051: public String getModuleName() {
052: return "com.google.gwt.user.User";
053: }
054:
055: /**
056: * Tests that empty arrays operate properly.
057: */
058: public void testEmptyArray() {
059: final Iterator subject = WidgetIterators.createWidgetIterator(
060: new MockWidget(), new Widget[] { null, null });
061: assertFalse(subject.hasNext());
062: assertNextFails(subject);
063: assertRemoveFails(subject);
064: }
065:
066: /**
067: * Tests that remove operates properly on an array of widgets that includes
068: * <code>null</code>.
069: */
070: public void testRemove() {
071: final int[] expectedRemoveIndex = new int[1];
072:
073: final Widget[] widgets = new Widget[] { null,
074: createTestWidget(), null, createTestWidget(), null,
075: null };
076:
077: final MockWidget mock = new MockWidget() {
078: public boolean remove(Widget w) {
079: assertEquals(widgets[expectedRemoveIndex[0]], w);
080: return true;
081: }
082: };
083:
084: final Iterator subject = WidgetIterators.createWidgetIterator(
085: mock, widgets);
086:
087: expectedRemoveIndex[0] = 1;
088: assertTrue(subject.hasNext());
089: assertEquals(widgets[1], subject.next());
090: subject.remove();
091: assertRemoveFails(subject);
092:
093: expectedRemoveIndex[0] = 3;
094: assertTrue(subject.hasNext());
095: assertEquals(widgets[3], subject.next());
096: subject.remove();
097: assertRemoveFails(subject);
098:
099: assertFalse(subject.hasNext());
100: assertNextFails(subject);
101: }
102:
103: /**
104: * Tests that the common iteration pattern works on an array of widgets that
105: * contains <code>null</code>.
106: */
107: public void testStandardIteration() {
108: final MockWidget mock = new MockWidget();
109: final Widget[] widgets = new Widget[] { null,
110: createTestWidget(), null, createTestWidget(), null,
111: null };
112: final Iterator subject = WidgetIterators.createWidgetIterator(
113: mock, widgets);
114:
115: assertTrue(subject.hasNext());
116: assertEquals(widgets[1], subject.next());
117:
118: assertTrue(subject.hasNext());
119: assertEquals(widgets[3], subject.next());
120:
121: assertFalse(subject.hasNext());
122: assertNextFails(subject);
123: }
124:
125: private void assertNextFails(Iterator iterator) {
126: try {
127: iterator.next();
128: fail("Expected NoSuchElementException.");
129: } catch (NoSuchElementException e) {
130: // This page left intentionally blank.
131: }
132: }
133:
134: private void assertRemoveFails(Iterator iterator) {
135: try {
136: iterator.remove();
137: fail("Expected IllegalStateException.");
138: } catch (IllegalStateException e) {
139: // The page left intentionally blank.
140: }
141: }
142:
143: private Widget createTestWidget() {
144: return new Label("Shazam!");
145: }
146: }
|