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.ui.HTMLTable.CellFormatter;
020: import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;
021:
022: import java.util.Iterator;
023: import java.util.List;
024:
025: /**
026: * Base test for HTMLTable derived classes.
027: */
028: public abstract class HTMLTableTestBase extends GWTTestCase {
029:
030: static class Adder implements HasWidgetsTester.WidgetAdder {
031: public void addChild(HasWidgets container, Widget child) {
032: ((HTMLTable) container).setWidget(0, 0, child);
033: }
034: }
035:
036: public static void assertEquals(Object[] x, Object[] y) {
037: assertEquals(x.length, y.length);
038: for (int i = 0; i < y.length; i++) {
039: assertEquals(x[i], y[i]);
040: }
041: }
042:
043: /**
044: * Easy way to test what should be in a list.
045: */
046: protected static void assertEquals(Object[] array, List target) {
047: if (target.size() != array.length) {
048: fail(target + " should be the same length as" + array);
049: }
050: for (int i = 0; i < array.length; i++) {
051: assertEquals(target.get(i), array[i]);
052: }
053: }
054:
055: public String getModuleName() {
056: return "com.google.gwt.user.User";
057: }
058:
059: public abstract HTMLTable getTable(int row, int column);
060:
061: public void testAttachDetachOrder() {
062: HasWidgetsTester.testAll(getTable(1, 1), new Adder());
063: }
064:
065: public void testBoundsOnEmptyTable() {
066: HTMLTable t = getTable(0, 0);
067: try {
068: t.getCellFormatter().getElement(4, 5);
069: } catch (IndexOutOfBoundsException e) {
070: return;
071: }
072: fail("should have throw an index out of bounds");
073: }
074:
075: public void testDoubleSet() {
076: HTMLTable t = getTable(4, 4);
077: t.setWidget(0, 0, new Label());
078: Widget s = new Label();
079: t.setWidget(0, 0, s);
080: assertEquals(s, t.getWidget(0, 0));
081: }
082:
083: public void testIterator() {
084: // Check remove.
085: HTMLTable t = getTable(5, 5);
086: Label l = new Label("hello");
087: t.setWidget(0, 0, l);
088: Iterator iter = t.iterator();
089: assertEquals(l, iter.next());
090: iter.remove();
091: Iterator iter2 = t.iterator();
092: assertFalse(iter2.hasNext());
093:
094: // Check put after remove.
095: Widget w = new Label("bo");
096: t.setWidget(0, 0, w);
097: Iterator iter3 = t.iterator();
098: assertEquals(w, iter3.next());
099: assertFalse(iter3.hasNext());
100:
101: // Check swapping widgets.
102: Widget w2 = new Label("ba");
103: t.setWidget(0, 0, w2);
104: Iterator iter4 = t.iterator();
105: assertEquals(w2, iter4.next());
106: assertFalse(iter4.hasNext());
107:
108: // Check put after put.
109: Widget w3 = new Label("be");
110: t.setWidget(1, 1, w3);
111: Iterator iter5 = t.iterator();
112: assertEquals(w2, iter5.next());
113: assertEquals(w3, iter5.next());
114: assertFalse(iter5.hasNext());
115: }
116:
117: public void testSettingCellAttributes() {
118: // These tests simple test for errors while setting these fields. The
119: // Patient sample under the survey project has the visual part of the test.
120: HTMLTable t = getTable(4, 4);
121:
122: CellFormatter formatter = t.getCellFormatter();
123: formatter.setHeight(0, 0, "100%");
124: formatter.setVerticalAlignment(0, 1,
125: HasVerticalAlignment.ALIGN_BOTTOM);
126: formatter.setHorizontalAlignment(1, 1,
127: HasHorizontalAlignment.ALIGN_RIGHT);
128: formatter.setWidth(0, 2, "100%");
129: }
130:
131: public void testStyles() {
132: HTMLTable t = getTable(4, 4);
133: t.getCellFormatter().setStyleName(2, 2, "hello");
134: assertEquals("hello", t.getCellFormatter().getStyleName(2, 2));
135: t.getCellFormatter().setStyleName(2, 2, "goodbye");
136: t.getCellFormatter().addStyleName(2, 2, "hello");
137:
138: // Visable Styles.
139: t.getCellFormatter().setVisible(0, 0, false);
140: assertTrue(t.getCellFormatter().isVisible(2, 2));
141: assertFalse(t.getCellFormatter().isVisible(0, 0));
142: RowFormatter formatter = t.getRowFormatter();
143: formatter.setVisible(3, false);
144: assertFalse(formatter.isVisible(3));
145: assertTrue(formatter.isVisible(2));
146: assertTrue(t.getCellFormatter().isVisible(2, 0));
147:
148: // Style name.
149: assertEquals("goodbye hello", t.getCellFormatter()
150: .getStyleName(2, 2));
151: t.getRowFormatter().setStyleName(3, "newStyle");
152: assertEquals("newStyle", t.getRowFormatter().getStyleName(3));
153: }
154: }
|