001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.html;
039:
040: import java.util.ArrayList;
041: import java.util.List;
042:
043: import com.gargoylesoftware.htmlunit.WebTestCase;
044:
045: /**
046: * Tests for {@link HtmlTable}.
047: *
048: * @version $Revision: 2132 $
049: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
050: * @author Ahmed Ashour
051: */
052: public class HtmlTableTest extends WebTestCase {
053: /**
054: * Create an instance
055: *
056: * @param name The name of the test
057: */
058: public HtmlTableTest(final String name) {
059: super (name);
060: }
061:
062: /**
063: * Test getTableCell(int,int)
064: *
065: * @exception Exception If the test fails
066: */
067: public void testGetTableCell() throws Exception {
068: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
069: + "<table id='table1' summary='Test table'>\n"
070: + "<tr><td>cell1</td><td>cell2</td><td rowspan='2'>cell4</td></tr>\n"
071: + "<tr><td colspan='2'>cell3</td></tr>\n"
072: + "</table>\n" + "</body></html>";
073: final HtmlPage page = loadPage(htmlContent);
074:
075: final HtmlTable table = (HtmlTable) page
076: .getHtmlElementById("table1");
077:
078: final HtmlTableCell cell1 = table.getCellAt(0, 0);
079: assertEquals("cell1 contents", "cell1", cell1.asText());
080:
081: final HtmlTableCell cell2 = table.getCellAt(0, 1);
082: assertEquals("cell2 contents", "cell2", cell2.asText());
083:
084: final HtmlTableCell cell3 = table.getCellAt(1, 0);
085: assertEquals("cell3 contents", "cell3", cell3.asText());
086: assertSame("cells (1,0) and (1,1)", cell3, table
087: .getCellAt(1, 1));
088:
089: final HtmlTableCell cell4 = table.getCellAt(0, 2);
090: assertEquals("cell4 contents", "cell4", cell4.asText());
091: assertSame("cells (0,2) and (1,2)", cell4, table
092: .getCellAt(1, 2));
093: }
094:
095: /**
096: * Test getCellAt(int,int) with colspan
097: *
098: * @exception Exception If the test fails
099: */
100: public void testGetCellAt() throws Exception {
101: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
102: + "<table id='table1'>\n"
103: + "<tr><td>row 1 col 1</td></tr>\n"
104: + "<tr><td>row 2 col 1</td><td>row 2 col 2</td></tr>\n"
105: + "<tr><td colspan='1'>row 3 col 1&2</td></tr>\n"
106: + "</table>\n" + "</body></html>";
107: final HtmlPage page = loadPage(htmlContent);
108:
109: final HtmlTable table = (HtmlTable) page
110: .getHtmlElementById("table1");
111:
112: final HtmlTableCell cell1 = table.getCellAt(0, 0);
113: assertEquals("cell (0,0) contents", "row 1 col 1", cell1
114: .asText());
115:
116: final HtmlTableCell cell2 = table.getCellAt(0, 1);
117: assertEquals("cell (0,1) contents", null, cell2);
118:
119: final HtmlTableCell cell3 = table.getCellAt(1, 0);
120: assertEquals("cell (1,0) contents", "row 2 col 1", cell3
121: .asText());
122:
123: final HtmlTableCell cell4 = table.getCellAt(1, 1);
124: assertEquals("cell (1,1) contents", "row 2 col 2", cell4
125: .asText());
126:
127: final HtmlTableCell cell5 = table.getCellAt(2, 0);
128: assertEquals("cell (2, 0) contents", "row 3 col 1&2", cell5
129: .asText());
130: final HtmlTableCell cell6 = table.getCellAt(2, 1);
131: assertEquals("cell (2, 1) contents", null, cell6);
132: }
133:
134: /**
135: * Test getTableCell(int,int) for a cell that doesn't exist
136: *
137: * @exception Exception If the test fails
138: */
139: public void testGetTableCell_NotFound() throws Exception {
140: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
141: + "<table id='table1' summary='Test table'>\n"
142: + "<tr><td>cell1</td><td>cell2</td><td rowspan='2'>cell4</td></tr>\n"
143: + "<tr><td colspan='2'>cell3</td></tr>\n"
144: + "</table>\n" + "</body></html>";
145: final HtmlPage page = loadPage(htmlContent);
146:
147: final HtmlTable table = (HtmlTable) page
148: .getHtmlElementById("table1");
149:
150: final HtmlTableCell cell = table.getCellAt(99, 0);
151: assertNull("cell", cell);
152: }
153:
154: /**
155: * @throws Exception if the test fails
156: */
157: public void testGetTableRows() throws Exception {
158: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
159: + "<table id='table1'>\n"
160: + "<tr id='row1'><td>cell1</td></tr>\n"
161: + "<tr id='row2'><td>cell2</td></tr>\n"
162: + "<tr id='row3'><td>cell3</td></tr>\n"
163: + "<tr id='row4'><td>cell4</td></tr>\n"
164: + "<tr id='row5'><td>cell5</td></tr>\n"
165: + "<tr id='row6'><td>cell6</td></tr>\n"
166: + "</table>\n"
167: + "</body></html>";
168: final HtmlPage page = loadPage(htmlContent);
169:
170: final HtmlTable table = (HtmlTable) page
171: .getHtmlElementById("table1");
172:
173: final List expectedRows = new ArrayList();
174: expectedRows.add(table.getRowById("row1"));
175: expectedRows.add(table.getRowById("row2"));
176: expectedRows.add(table.getRowById("row3"));
177: expectedRows.add(table.getRowById("row4"));
178: expectedRows.add(table.getRowById("row5"));
179: expectedRows.add(table.getRowById("row6"));
180:
181: assertEquals(expectedRows, table.getRows());
182: }
183:
184: /**
185: * @throws Exception if the test fails
186: */
187: public void testGetTableRows_WithHeadBodyFoot() throws Exception {
188: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
189: + "<table id='table1'>\n"
190: + "<thead>\n"
191: + " <tr id='row1'><td>cell1</td></tr>\n"
192: + " <tr id='row2'><td>cell2</td></tr>\n"
193: + "</thead>\n"
194: + "<tbody>\n"
195: + " <tr id='row3'><td>cell3</td></tr>\n"
196: + " <tr id='row4'><td>cell4</td></tr>\n"
197: + "</tbody>\n"
198: + "<tfoot>\n"
199: + " <tr id='row5'><td>cell5</td></tr>\n"
200: + " <tr id='row6'><td>cell6</td></tr>\n"
201: + "</tfoot>\n" + "</table>\n" + "</body></html>";
202: final HtmlPage page = loadPage(htmlContent);
203:
204: final HtmlTable table = (HtmlTable) page
205: .getHtmlElementById("table1");
206:
207: final List expectedRows = new ArrayList();
208: expectedRows.add(table.getRowById("row1"));
209: expectedRows.add(table.getRowById("row2"));
210: expectedRows.add(table.getRowById("row3"));
211: expectedRows.add(table.getRowById("row4"));
212: expectedRows.add(table.getRowById("row5"));
213: expectedRows.add(table.getRowById("row6"));
214:
215: assertEquals(expectedRows, table.getRows());
216: }
217:
218: /**
219: * @throws Exception if the test fails
220: */
221: public void testRowGroupings_AllDefined() throws Exception {
222: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
223: + "<table id='table1'>\n"
224: + "<thead>\n"
225: + " <tr id='row1'><td>cell1</td></tr>\n"
226: + " <tr id='row2'><td>cell2</td></tr>\n"
227: + "</thead>\n"
228: + "<tbody>\n"
229: + " <tr id='row3'><td>cell3</td></tr>\n"
230: + "</tbody>\n"
231: + "<tbody>\n"
232: + " <tr id='row4'><td>cell4</td></tr>\n"
233: + "</tbody>\n"
234: + "<tfoot>\n"
235: + " <tr id='row5'><td>cell5</td></tr>\n"
236: + " <tr id='row6'><td>cell6</td></tr>\n"
237: + "</tfoot>\n" + "</table>\n" + "</body></html>";
238: final HtmlPage page = loadPage(htmlContent);
239:
240: final HtmlTable table = (HtmlTable) page
241: .getHtmlElementById("table1");
242:
243: assertNotNull(table.getHeader());
244: assertNotNull(table.getFooter());
245: assertEquals(2, table.getBodies().size());
246: }
247:
248: /**
249: * Check to ensure that the proper numbers of tags show up. Note that an extra tbody
250: * will be inserted to be in compliance with the common browsers.
251: * @throws Exception if the test fails
252: */
253: public void testRowGroupings_NoneDefined() throws Exception {
254:
255: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
256: + "<table id='table1'>\n"
257: + " <tr id='row1'><td>cell1</td></tr>\n"
258: + " <tr id='row2'><td>cell2</td></tr>\n"
259: + " <tr id='row3'><td>cell3</td></tr>\n"
260: + " <tr id='row4'><td>cell4</td></tr>\n"
261: + " <tr id='row5'><td>cell5</td></tr>\n"
262: + " <tr id='row6'><td>cell6</td></tr>\n"
263: + "</table>\n" + "</body></html>";
264: final HtmlPage page = loadPage(htmlContent);
265:
266: final HtmlTable table = (HtmlTable) page
267: .getHtmlElementById("table1");
268:
269: assertEquals(null, table.getHeader());
270: assertEquals(null, table.getFooter());
271: assertEquals(1, table.getBodies().size());
272: }
273:
274: /**
275: * @throws Exception if the test fails
276: */
277: public void testGetCaptionText() throws Exception {
278: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
279: + "<table id='table1' summary='Test table'>\n"
280: + "<caption>MyCaption</caption>\n"
281: + "<tr><td>cell1</td><td>cell2</td><td rowspan='2'>cell4</td></tr>\n"
282: + "<tr><td colspan='2'>cell3</td></tr>\n"
283: + "</table>\n" + "</body></html>";
284: final HtmlPage page = loadPage(htmlContent);
285:
286: final HtmlTable table = (HtmlTable) page
287: .getHtmlElementById("table1");
288:
289: assertEquals("MyCaption", table.getCaptionText());
290: }
291:
292: /**
293: * The common browsers will automatically insert tbody tags around the table rows if
294: * one wasn't specified. Ensure that we do this too. Also ensure that extra ones
295: * aren't inserted if a tbody was already there.
296: * @throws Exception if the test fails
297: */
298: public void testInsertionOfTbodyTags() throws Exception {
299: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
300: + "<table>\n"
301: + "<tr><td id='cell1'>cell1</td></tr>\n"
302: + "</table>\n"
303: + "<table><tbody>\n"
304: + "<tr><td id='cell2'>cell1</td></tr>\n"
305: + "</tbody></table>\n" + "</body></html>";
306: final HtmlPage page = loadPage(htmlContent);
307:
308: // Check that a <tbody> was inserted properly
309: final HtmlTableDataCell cell1 = (HtmlTableDataCell) page
310: .getHtmlElementById("cell1");
311: assertInstanceOf(cell1.getParentDomNode(), HtmlTableRow.class);
312: assertInstanceOf(cell1.getParentDomNode().getParentDomNode(),
313: HtmlTableBody.class);
314: assertInstanceOf(cell1.getParentDomNode().getParentDomNode()
315: .getParentDomNode(), HtmlTable.class);
316:
317: // Check that the existing <tbody> wasn't messed up.
318: final HtmlTableDataCell cell2 = (HtmlTableDataCell) page
319: .getHtmlElementById("cell2");
320: assertInstanceOf(cell2.getParentDomNode(), HtmlTableRow.class);
321: assertInstanceOf(cell2.getParentDomNode().getParentDomNode(),
322: HtmlTableBody.class);
323: assertInstanceOf(cell2.getParentDomNode().getParentDomNode()
324: .getParentDomNode(), HtmlTable.class);
325: }
326:
327: /**
328: * Regression test for bug 1210751: JavaScript inside <table> run twice.
329: * @throws Exception if the test fails
330: */
331: public void testJSInTable() throws Exception {
332: final String content = "<html><head><title>foo</title></head><body>\n"
333: + "<table>\n"
334: + "<tr><td>cell1</td></tr>\n"
335: + "<script>alert('foo');</script>\n"
336: + "<tr><td>cell1</td></tr>\n"
337: + "</table>\n"
338: + "<div id='div1'>foo</div>\n"
339: + "<script>alert(document.getElementById('div1').parentNode.tagName);</script>\n"
340: + "</body></html>";
341:
342: final String[] expectedAlerts = { "foo", "BODY" };
343: createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
344:
345: final List collectedAlerts = new ArrayList();
346: loadPage(content, collectedAlerts);
347:
348: assertEquals(expectedAlerts, collectedAlerts);
349: }
350: }
|