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: /**
021: * Tests {@link ListBox}. Needs many, many more tests.
022: */
023: public class ListBoxTest extends GWTTestCase {
024:
025: public String getModuleName() {
026: return "com.google.gwt.user.User";
027: }
028:
029: public void testClear() {
030: ListBox lb = new ListBox();
031: lb.addItem("a");
032: lb.addItem("b");
033: lb.addItem("c");
034: lb.clear();
035: assertEquals(0, lb.getItemCount());
036: }
037:
038: public void testInsert() {
039:
040: // Insert in the middle
041: {
042: ListBox lb = new ListBox();
043: lb.addItem("a");
044: lb.addItem("c");
045: lb.insertItem("b", 1);
046: assertEquals("a", lb.getItemText(0));
047: assertEquals("b", lb.getItemText(1));
048: assertEquals("c", lb.getItemText(2));
049: }
050:
051: // Insert at the front
052: {
053: ListBox lb = new ListBox();
054: lb.addItem("b");
055: lb.addItem("c");
056: lb.insertItem("a", 0);
057: assertEquals("a", lb.getItemText(0));
058: assertEquals("b", lb.getItemText(1));
059: assertEquals("c", lb.getItemText(2));
060: }
061:
062: // Insert at the end by using a negative index
063: {
064: ListBox lb = new ListBox();
065: lb.addItem("a");
066: lb.addItem("b");
067: lb.insertItem("c", -1);
068: assertEquals("a", lb.getItemText(0));
069: assertEquals("b", lb.getItemText(1));
070: assertEquals("c", lb.getItemText(2));
071: }
072:
073: // Insert at the end by using an index greater than the length
074: // of the list
075: {
076: ListBox lb = new ListBox();
077: lb.addItem("a");
078: lb.addItem("b");
079: lb.insertItem("c", 2);
080: assertEquals("a", lb.getItemText(0));
081: assertEquals("b", lb.getItemText(1));
082: assertEquals("c", lb.getItemText(2));
083: }
084: }
085:
086: public void testRemove() {
087: ListBox lb = new ListBox();
088: lb.addItem("a");
089: lb.addItem("b");
090: lb.addItem("c");
091: lb.removeItem(1);
092: assertEquals("a", lb.getItemText(0));
093: assertEquals("c", lb.getItemText(1));
094: }
095:
096: public void testSelected() {
097: ListBox lb = new ListBox();
098: lb.clear();
099: for (int i = 0; i < 3; i++) {
100: lb.addItem(Integer.toString(i), Integer.toString(i));
101: }
102: lb.setSelectedIndex(2);
103: assertEquals(2, lb.getSelectedIndex());
104: }
105:
106: public void testSelection() {
107: {
108: ListBox box = new ListBox();
109: box.addItem("a");
110: box.setSelectedIndex(-1);
111: assertEquals(-1, box.getSelectedIndex());
112: box.setSelectedIndex(0);
113: assertEquals("a", box.getItemText(box.getSelectedIndex()));
114: }
115:
116: // Testing multiple selection
117: {
118: ListBox box = new ListBox(true);
119: box.setMultipleSelect(true);
120: box.addItem("a");
121: box.addItem("b");
122: box.addItem("c");
123:
124: for (int j = 0; j < box.getItemCount(); j++) {
125: box.setItemSelected(j, true);
126: }
127:
128: for (int j = 0; j < box.getItemCount(); j++) {
129: assertTrue(box.isItemSelected(j));
130: }
131:
132: // Setting the selected index should de-select all other items, except
133: // the item at the index.
134: box.setSelectedIndex(1);
135: assertFalse(box.isItemSelected(0));
136: assertTrue(box.isItemSelected(1));
137:
138: // Make sure that setting the selected index to -1 de-selects the
139: // selected item.
140: box.setSelectedIndex(-1);
141: assertFalse(box.isItemSelected(1));
142: }
143: }
144:
145: public void testSetStyleNames() {
146: ListBox box = new ListBox();
147:
148: // Check subset problems.
149: box.addStyleName("superset");
150: box.addStyleName("super");
151: assertEquals("gwt-ListBox superset super", box.getStyleName());
152:
153: // Remove a style that doesn't exist.
154: box.removeStyleName("sup");
155: assertEquals("gwt-ListBox superset super", box.getStyleName());
156: box.removeStyleName("super");
157: assertEquals("gwt-ListBox superset", box.getStyleName());
158: box.addStyleName("two styles");
159: assertEquals("gwt-ListBox superset two styles", box
160: .getStyleName());
161: box.removeStyleName("superset");
162: assertEquals("gwt-ListBox two styles", box.getStyleName());
163: box.removeStyleName("two styles");
164: try {
165: box.addStyleName("");
166: fail("Should have thrown illegal argument exception");
167: } catch (IllegalArgumentException e) {
168: // expected
169: }
170: box.addStyleName("superset");
171: box.addStyleName("two");
172: box.addStyleName("styles");
173: assertEquals("gwt-ListBox superset two styles", box
174: .getStyleName());
175: }
176:
177: public void testText() {
178: ListBox box = new ListBox();
179: box.addItem("a");
180: box.addItem("b", "B");
181: assertEquals(2, box.getItemCount());
182: box.addItem("c", "C");
183: assertEquals("B", box.getValue(1));
184: assertEquals("a", box.getItemText(0));
185: assertEquals("b", box.getItemText(1));
186: assertEquals("c", box.getItemText(2));
187: box.setItemText(1, "bb");
188: assertEquals("bb", box.getItemText(1));
189: box.setItemText(1, "bc");
190: assertEquals("bc", box.getItemText(1));
191: box.setItemText(0, "");
192: assertEquals("", box.getItemText(0));
193: try {
194: box.setItemText(0, null);
195: fail("Should have thrown Null Pointer");
196: } catch (NullPointerException e) {
197: // expected;
198: }
199: }
200:
201: public void testValues() {
202: ListBox box = new ListBox();
203:
204: // Test adding an item without a value then modifying it later.
205: {
206: String text = "no-value item added";
207:
208: box.addItem(text);
209: assertEquals(text, box.getValue(0));
210:
211: box.setValue(0, "foo");
212: assertEquals("foo", box.getValue(0));
213: }
214:
215: // Test inserting an item without a value then modifying it later.
216: {
217: String text = "no-value item inserted";
218:
219: box.insertItem(text, 0);
220: assertEquals(text, box.getValue(0));
221:
222: box.setValue(0, "bar");
223: assertEquals("bar", box.getValue(0));
224: }
225:
226: // Test inserting an item with a value then modifying it later.
227: {
228: String text = "value item inserted";
229: String value = "value";
230:
231: box.insertItem(text, value, 1);
232: assertEquals(text, box.getItemText(1));
233: assertEquals(value, box.getValue(1));
234:
235: box.setValue(1, "bar");
236: box.setItemText(1, "item text");
237: assertEquals("bar", box.getValue(1));
238: assertEquals("item text", box.getItemText(1));
239: }
240: }
241: }
|