KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > host > TableTest


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.javascript.host;
39
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.List;
43
44 import com.gargoylesoftware.htmlunit.WebTestCase;
45
46 /**
47  * Tests for Table.
48  *
49  * @author David D. Kilzer
50  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
51  * @author Daniel Gredler
52  * @author Marc Guillemot
53  * @version $Revision: 1.7 $
54  */

55 public class TableTest extends WebTestCase {
56
57     /**
58      * Create an instance
59      * @param name The name of the test
60      */

61     public TableTest( final String name ) {
62         super(name);
63     }
64
65     /**
66      * @throws Exception if the test fails
67      */

68     public void testTableCaptions() throws Exception {
69
70         final String htmlContent
71             = "<html><head><title>foo</title></head><body>\n"
72             + " <table id='table_1'><caption>caption1</caption><caption>caption2</caption>\n"
73             + " <tr><td>cell1</td><td>cell2</td><td rowspan='2'>cell4</td></tr>\n"
74             + " <tr><td colspan='2'>cell3</td></tr>\n"
75             + " </table>\n"
76             + " <script type='text/javascript' language='JavaScript'>\n"
77             + " <!--\n"
78             + " var table = document.getElementById('table_1');\n"
79             + " alert(table.caption.innerHTML);\n"
80             + " table.deleteCaption();\n"
81             + " alert(table.caption.innerHTML);\n"
82             + " table.deleteCaption();\n"
83             + " alert(table.caption);\n"
84             + " var newCaption = table.createCaption();\n"
85             + " newCaption.innerHTML = 'caption3';\n"
86             + " alert(table.caption.innerHTML);\n"
87             + " // -->\n"
88             + " </script>\n"
89             + "</body></html>\n";
90
91         final List collectedAlerts = new ArrayList();
92         loadPage(htmlContent, collectedAlerts);
93
94         final List expectedAlerts = Arrays.asList(new String[] { "caption1", "caption2", "null", "caption3" });
95         assertEquals(expectedAlerts, collectedAlerts);
96     }
97
98     /**
99      * @throws Exception if the test fails
100      */

101     public void testTableHeaders() throws Exception {
102
103         final String htmlContent
104             = "<html><head><title>foo</title></head><body>\n"
105             + " <table id='table_1'>\n"
106             + " <thead id='thead1'><tr><td>cell1</td><td>cell2</td><td>cell3</td></tr></thead>\n"
107             + " <thead id='thead2'><tr><td>cell7</td><td>cell8</td><td>cell9</td></tr></thead>\n"
108             + " <tr><td>cell1</td><td>cell2</td><td rowspan='2'>cell4</td></tr>\n"
109             + " <tr><td colspan='2'>cell3</td></tr>\n"
110             + " </table>\n"
111             + " <script type='text/javascript' language='JavaScript'>\n"
112             + " <!--\n"
113             + " var table = document.getElementById('table_1');\n"
114             + " alert(table.tHead.id);\n"
115             + " table.deleteTHead();\n"
116             + " alert(table.tHead.id);\n"
117             + " table.deleteTHead();\n"
118             + " alert(table.tHead);\n"
119             + " var newTHead = table.createTHead();\n"
120             + " newTHead.id = 'thead3';\n"
121             + " alert(table.tHead.id);\n"
122             + " // -->\n"
123             + " </script>\n"
124             + "</body></html>\n";
125
126         final List collectedAlerts = new ArrayList();
127         loadPage(htmlContent, collectedAlerts);
128
129         final List expectedAlerts = Arrays.asList(new String[] { "thead1", "thead2", "null", "thead3" });
130         assertEquals(expectedAlerts, collectedAlerts);
131     }
132
133     /**
134      * @throws Exception if the test fails
135      */

136     public void testTableBodies() throws Exception {
137
138         final String htmlContent
139             = "<html><head><title>foo</title></head><body>\n"
140             + " <table id='table_1'>\n"
141             + " <tbody id='tbody1'>\n"
142             + " <tr><td>cell1</td><td>cell2</td></tr>\n"
143             + " <tr><td>cell3</td><td>cell4</td></tr>\n"
144             + " </tbody>\n"
145             + " <tbody id='tbody2'>\n"
146             + " <tr><td>cell1</td><td>cell2</td></tr>\n"
147             + " <tr><td>cell3</td><td>cell4</td></tr>\n"
148             + " </tbody>\n"
149             + " </table>\n"
150             + " <script type='text/javascript' language='JavaScript'>\n"
151             + " var table = document.getElementById('table_1');\n"
152             + " var bodies = table.tBodies;\n"
153             + " alert(bodies.length);\n"
154             + " alert(bodies == table.tBodies);\n"
155             + " var body1 = table.tBodies[0];\n"
156             + " var body2 = table.tBodies[1];\n"
157             + " alert(table.rows.length + ' ' + body1.rows.length + ' ' + body2.rows.length);\n"
158             + " table.insertRow(-1); // Should add at end, to body2.\n"
159             + " body1.insertRow(-1); // Add one to body1, as well.\n"
160             + " alert(table.rows.length + ' ' + body1.rows.length + ' ' + body2.rows.length);\n"
161             + " </script>\n"
162             + "</body></html>\n";
163
164         final List expectedAlerts = Arrays.asList(new String[] { "2", "true", "4 2 2", "6 3 3" });
165         createTestPageForRealBrowserIfNeeded(htmlContent, expectedAlerts);
166
167         final List collectedAlerts = new ArrayList();
168         loadPage(htmlContent, collectedAlerts);
169
170         assertEquals(expectedAlerts, collectedAlerts);
171     }
172
173     /**
174      * @throws Exception if the test fails
175      */

176     public void testTableRows() throws Exception {
177
178         final String htmlContent
179             = "<html><head><title>foo</title></head><body>\n"
180             + " <table id='table_1'>\n"
181             + " <tr><td>cell1</td><td>cell2</td><td rowspan='2'>cell4</td></tr>\n"
182             + " <tr><td colspan='2'>cell3</td></tr>\n"
183             + " </table>\n"
184             + " <script type='text/javascript' language='JavaScript'>\n"
185             + " <!--\n"
186             + " var table = document.getElementById('table_1');\n"
187             + " var rows = table.rows;\n"
188             + " alert(rows.length);\n"
189             + " alert(rows == table.rows);\n"
190             + " table.insertRow(1);\n"
191             + " alert(rows.length);\n"
192             + " table.deleteRow(1);\n"
193             + " alert(rows.length);\n"
194             + " // -->\n"
195             + " </script>\n"
196             + "</body></html>\n";
197
198         final List collectedAlerts = new ArrayList();
199         loadPage(htmlContent, collectedAlerts);
200         
201         final List expectedAlerts = Arrays.asList(new String[] { "2", "true", "3", "2" });
202         assertEquals(expectedAlerts, collectedAlerts);
203     }
204
205     /**
206      * @throws Exception if the test fails
207      */

208     public void testTableRowsWithManySections() throws Exception {
209
210         final String htmlContent = "<html><head><title>foo</title></head><body>\n"
211             + " <table id='table_1'>\n"
212             + " <thead>\n"
213             + " <tr><td>cell1</td><td>cell2</td></tr>\n"
214             + " <tr><td>cell3</td><td>cell4</td></tr>\n"
215             + " </thead>\n"
216             + " <tbody id='tbody1'>\n"
217             + " <tr><td>cell1</td><td>cell2</td></tr>\n"
218             + " <tr><td>cell3</td><td>cell4</td></tr>\n"
219             + " </tbody>\n"
220             + " <tbody id='tbody2'>\n"
221             + " <tr><td>cell1</td><td>cell2</td></tr>\n"
222             + " <tr><td>cell3</td><td>cell4</td></tr>\n"
223             + " </tbody>\n"
224             + " <tfoot>\n"
225             + " <tr><td>cell1</td><td>cell2</td></tr>\n"
226             + " <tr><td>cell3</td><td>cell4</td></tr>\n"
227             + " </tfoot>\n"
228             + " </table>\n"
229             + " <script type='text/javascript' language='JavaScript'>\n"
230             + " <!--\n"
231             + " var table = document.getElementById('table_1');\n"
232             + " var bodies = table.tBodies;\n"
233             + " alert(bodies.length);\n"
234             + " alert(bodies == table.tBodies);\n"
235             + " var head = table.tHead;\n"
236             + " var body1 = table.tBodies.item(0);\n"
237             + " var body2 = table.tBodies.item(1);\n"
238             + " var foot = table.tFoot;\n"
239             + " alert(table.rows.length + ' ' + head.rows.length + ' ' + body1.rows.length "
240             + " + ' ' + body2.rows.length + ' ' + foot.rows.length);\n"
241             + " table.insertRow(6); // Insert a row in the footer.\n"
242             + " alert(table.rows.length + ' ' + head.rows.length + ' ' + body1.rows.length "
243             + " + ' ' + body2.rows.length + ' ' + foot.rows.length);\n"
244             + " table.deleteRow(5); // Delete a row from the second body.\n"
245             + " alert(table.rows.length + ' ' + head.rows.length + ' ' + body1.rows.length "
246             + " + ' ' + body2.rows.length + ' ' + foot.rows.length);\n"
247             + " table.insertRow(2); // Insert a row in the first body.\n"
248             + " alert(table.rows.length + ' ' + head.rows.length + ' ' + body1.rows.length "
249             + " + ' ' + body2.rows.length + ' ' + foot.rows.length);\n"
250             + " table.deleteRow(1); // Delete a row from the header.\n"
251             + " alert(table.rows.length + ' ' + head.rows.length + ' ' + body1.rows.length "
252             + " + ' ' + body2.rows.length + ' ' + foot.rows.length);\n"
253             + " // -->\n"
254             + " </script>\n"
255             + "</body></html>\n";
256
257         
258         final List collectedAlerts = new ArrayList();
259         loadPage(htmlContent, collectedAlerts);
260         
261         final List expectedAlerts = Arrays.asList(new String[] {"2", "true", "8 2 2 2 2",
262             "9 2 2 2 3", "8 2 2 1 3", "9 2 3 1 3", "8 1 3 1 3"});
263         assertEquals(expectedAlerts, collectedAlerts);
264     }
265
266     /**
267      * @throws Exception if the test fails
268      */

269     public void testTableFooters() throws Exception {
270
271         final String htmlContent
272             = "<html><head><title>foo</title></head><body>\n"
273             + " <table id='table_1'>\n"
274             + " <tr><td>cell1</td><td>cell2</td><td rowspan='2'>cell4</td></tr>\n"
275             + " <tr><td colspan='2'>cell3</td></tr>\n"
276             + " <tfoot id='tfoot1'><tr><td>cell1</td><td>cell2</td><td>cell3</td></tr></tfoot>\n"
277             + " <tfoot id='tfoot2'><tr><td>cell7</td><td>cell8</td><td>cell9</td></tr></tfoot>\n"
278             + " </table>\n"
279             + " <script type='text/javascript' language='JavaScript'>\n"
280             + " <!--\n"
281             + " var table = document.getElementById('table_1');\n"
282             + " alert(table.tFoot.id);\n"
283             + " table.deleteTFoot();\n"
284             + " alert(table.tFoot.id);\n"
285             + " table.deleteTFoot();\n"
286             + " alert(table.tFoot);\n"
287             + " var newTFoot = table.createTFoot();\n"
288             + " newTFoot.id = 'tfoot3';\n"
289             + " alert(table.tFoot.id);\n"
290             + " // -->\n"
291             + " </script>\n"
292             + "</body></html>\n";
293
294         final List collectedAlerts = new ArrayList();
295         loadPage(htmlContent, collectedAlerts);
296
297         final List expectedAlerts = Arrays.asList(new String[] { "tfoot1", "tfoot2", "null", "tfoot3" });
298         assertEquals(expectedAlerts, collectedAlerts);
299     }
300
301     /**
302      * @throws Exception if the test fails
303      */

304     public void testInsertRow() throws Exception {
305
306         final String htmlContent
307             = "<html><head><title>foo</title></head><body>\n"
308             + " <table id='table_1'>\n"
309             + " <tr><td>foo</td></tr>\n"
310             + " </table>\n"
311             + " <script type='text/javascript' language='JavaScript'>\n"
312             + " var table = document.getElementById('table_1');\n"
313             + " alert(table.rows.length);\n"
314             + " var newRow = table.insertRow(-1);\n"
315             + " alert(table.rows.length);\n"
316             + " alert(newRow.rowIndex);\n"
317             + " alert(newRow.cells.length);\n"
318             + " var newCell = newRow.insertCell(-1);\n"
319             + " alert(newCell.tagName);\n"
320             + " alert(newRow.cells.length);\n"
321             + " </script>\n"
322             + "</body></html>\n";
323
324         final List expectedAlerts = Arrays.asList(new String[] { "1", "2", "1", "0", "TD", "1" });
325         createTestPageForRealBrowserIfNeeded(htmlContent, expectedAlerts);
326
327         final List collectedAlerts = new ArrayList();
328         loadPage(htmlContent, collectedAlerts);
329
330         assertEquals(expectedAlerts, collectedAlerts);
331     }
332 }
333
Popular Tags