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.user.client.DOM;
019: import com.google.gwt.user.client.Element;
020:
021: /**
022: * TODO: document me.
023: */
024: public class GridTest extends HTMLTableTestBase {
025:
026: public HTMLTable getTable(int row, int column) {
027: return new Grid(row, column);
028: }
029:
030: public void testBounds() {
031: HTMLTable t = getTable(3, 3);
032: try {
033: t.setText(-1, 0, "hello");
034: fail("IndexOutOfBoundsException should have been thrown");
035: } catch (IndexOutOfBoundsException e) {
036: assertEquals(
037: "Cannot access a row with a negative index: -1", e
038: .getMessage());
039: }
040: try {
041: t.setText(0, -1, "hello");
042: fail("IndexOutOfBoundsException should have been thrown");
043: } catch (IndexOutOfBoundsException e) {
044: assertEquals(
045: "Cannot access a column with a negative index: -1",
046: e.getMessage());
047: }
048: try {
049: t.clearCell(3, 3);
050: fail("IndexOutOfBoundsException should have been thrown");
051: } catch (IndexOutOfBoundsException e) {
052: assertEquals("Row index: 3, Row size: 3", e.getMessage());
053: }
054:
055: try {
056: t.getText(0, 5);
057: fail("IndexOutOfBoundsException should have been thrown");
058: } catch (Exception e) {
059: // Success.
060: }
061: }
062:
063: public void testColumnFormatter() {
064: Grid r = new Grid(4, 5);
065: Grid.ColumnFormatter columns = r.getColumnFormatter();
066: columns.setStyleName(0, "base");
067: columns.addStyleName(0, "a");
068: assertEquals("base a", columns.getStyleName(0));
069: columns.addStyleName(0, "b");
070: assertEquals("base a b", columns.getStyleName(0));
071: columns.addStyleName(0, "c");
072: assertEquals("base a b c", columns.getStyleName(0));
073: // Remove first.
074: columns.removeStyleName(0, "a");
075: assertEquals("base b c", columns.getStyleName(0));
076:
077: // Remove last.
078: columns.removeStyleName(0, "c");
079: assertEquals("base b", columns.getStyleName(0));
080:
081: // Only one column should be created.
082: Element e = DOM.getChild(r.getElement(), 0);
083: assertEquals(1, DOM.getChildCount(e));
084:
085: columns.addStyleName(3, "a");
086: // Now there shoud be three such columns .
087: e = DOM.getChild(r.getElement(), 0);
088: assertEquals(4, DOM.getChildCount(e));
089: }
090:
091: public void testColumnMessage() {
092: Grid r = new Grid(1, 1);
093:
094: try {
095: r.setWidget(0, 2, new Label("hello"));
096: fail("Expected IndexOutOfBoundsException");
097: } catch (IndexOutOfBoundsException e) {
098: assertEquals("Column index: 2, Column size: 1", e
099: .getMessage());
100: }
101:
102: try {
103: r.setWidget(2, 0, new Label("hello"));
104: fail("Expected IndexOutOfBoundsException");
105: } catch (IndexOutOfBoundsException e) {
106: assertEquals("Row index: 2, Row size: 1", e.getMessage());
107: }
108: }
109:
110: /**
111: * Ensures row and column counts stay in sync during resizing.
112: */
113: public void testResizing() {
114: Grid r = new Grid(4, 1);
115: assertEquals(4, r.getRowCount());
116: assertEquals(1, r.getColumnCount());
117: r.resizeRows(0);
118: assertEquals(0, r.getRowCount());
119: r.resizeColumns(0);
120: assertEquals(0, r.getColumnCount());
121: r.resize(3, 2);
122: assertEquals(3, r.getRowCount());
123: assertEquals(2, r.getColumnCount());
124: }
125: }
|