KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > html > HtmlTableRowTest


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.html;
39
40 import org.mozilla.javascript.ScriptableObject;
41
42 import com.gargoylesoftware.htmlunit.WebClient;
43 import com.gargoylesoftware.htmlunit.WebTestCase;
44 import com.gargoylesoftware.htmlunit.javascript.host.HTMLElement;
45
46 /**
47  * Tests for HtmlTableRow
48  *
49  * @version $Revision: 1.5 $
50  * @author <a HREF="mailto:gallaherm@pragmatics.com">Mike Gallaher </a>
51  * @author Mike Bowler
52  */

53 public class HtmlTableRowTest extends WebTestCase {
54     /**
55      * Create an instance.
56      * @param name The name of the test
57      */

58     public HtmlTableRowTest(final String name) {
59         super(name);
60     }
61
62     private static final String htmlContent = "<html><head><title>foo</title></head><body>"
63             + "<table id='table'><tr id='row'>"
64             + "<td id='cell' width='20'><input type='text' id='foo'/></td>"
65             + "</tr></table>" + "</body></html>";
66
67     private WebClient client_;
68
69     private HtmlPage page_;
70     private HtmlTable table_;
71     private HtmlTableBody tbody_;
72     private HtmlTableRow row_;
73     private HtmlTableCell cell_;
74     private HtmlTableRow rowClone_;
75     private HtmlTableCell cellClone_;
76
77     /** @see junit.framework.TestCase#setUp() */
78     public void setUp() throws Exception {
79         page_ = loadPage(htmlContent);
80         client_ = page_.getWebClient();
81         
82         table_ = (HtmlTable) page_.getHtmlElementById("table");
83         tbody_ = (HtmlTableBody) table_.getFirstChild();
84         row_ = table_.getRow(0);
85         cell_ = row_.getCell(0);
86
87         rowClone_ = (HtmlTableRow) row_.cloneNode(true);
88         cellClone_ = rowClone_.getCell(0);
89     }
90
91     /**
92      * Ensure that cloneNode leaves the original node unchanged.
93      */

94     public void testClonePreservesOriginal() {
95         assertSame(tbody_, row_.getParentNode());
96         assertSame(row_, cell_.getParentNode());
97         assertSame(cell_, row_.getCell(0));
98         assertEquals("row", row_.getId());
99         assertEquals("cell", cell_.getId());
100     }
101
102     /**
103      * Ensure that the clones are not the originals.
104      * @throws Exception if the test fails.
105      */

106     public void testClonesAreDistinct() throws Exception {
107         assertNotSame(row_, rowClone_);
108         assertNotSame(cell_, cellClone_);
109     }
110
111     /**
112      * Ensure that the clones have the same page as the originals.
113      */

114     public void testCloneHasSamePage() {
115         assertSame(cell_.getPage(), cellClone_.getPage());
116         assertSame(row_.getPage(), rowClone_.getPage());
117     }
118
119     /**
120      * Ensure that the cloned row has no parent.
121      * @throws Exception if the test fails.
122      */

123     public void testClonedRowHasNullParent() throws Exception {
124         assertNull(rowClone_.getParentNode());
125     }
126
127     /**
128      * Ensure that the cloned row's children are not those of the original.
129      * @throws Exception if the test fails.
130      */

131     public void testClonedRowHasDifferentChildren() throws Exception {
132         assertEquals(row_.getCells().size(), rowClone_.getCells().size());
133         assertNotSame(row_.getFirstChild(), rowClone_.getFirstChild());
134     }
135
136     /**
137      * Ensure that the cloned cell's children are not those of the original.
138      */

139     public void testClonedCellHasDifferentChildren() {
140         assertNotSame(cell_.getParentNode(), cellClone_.getParentNode());
141         assertNotNull(cell_.getFirstChild());
142         assertNotSame(cell_.getFirstChild(), cellClone_.getFirstChild());
143     }
144
145     /**
146      * Ensure that the cloned cell has the cloned row as its parent.
147      * @throws Exception if the test fails.
148      */

149     public void testClonedCellHasClonedRowAsParent() throws Exception {
150         assertSame(rowClone_, cellClone_.getParentNode());
151     }
152
153     /**
154      * Ensure the cloned row has a different id than the original.
155      */

156     public void testClonedRowHasDifferentId() {
157         assertFalse(row_.getId().equals(rowClone_.getId()));
158         assertEquals("", rowClone_.getId());
159     }
160
161     /**
162      * Ensure the cloned cell has a different id than the original.
163      */

164     public void testClonedCellHasDifferentId() {
165         assertFalse(cell_.getId().equals(cellClone_.getId()));
166         assertEquals("", cellClone_.getId());
167     }
168
169     /**
170      * Ensure the cloned cell's child has a different id than the original's
171      * child.
172      */

173     public void testClonedCellChildHasDifferentId() {
174         final HtmlElement cellChild = (HtmlElement) cell_.getFirstChild();
175         final HtmlElement cellChildClone = (HtmlElement) cellClone_.getFirstChild();
176         assertFalse(cellChild.getId().equals(cellChildClone.getId()));
177         assertEquals("", cellChildClone.getId());
178     }
179
180     /**
181      * Ensure the cloned cell's attribute value is the same as the original.
182      */

183     public void testCloneAttributesCopiedFromOriginal() {
184         assertEquals("20", cell_.getAttributeValue("width"));
185         assertEquals("20", cellClone_.getAttributeValue("width"));
186     }
187
188     /**
189      * Ensure that changing the clone's attribute leaves the original's
190      * attribute unchanged.
191      */

192     public void testCloneAttributeIsIndependentOfOriginal() {
193         cellClone_.setAttributeValue("a", "one");
194         assertFalse("one".equals(cell_.getAttributeValue("a")));
195     }
196
197     /**
198      * Ensure that changing the original's attribute leaves the clone's
199      * attribute unchanged.
200      */

201     public void testOriginalAttributeIsIndependentOfClone() {
202         cell_.setAttributeValue("a", "one");
203         assertFalse("one".equals(cellClone_.getAttributeValue("a")));
204     }
205
206     /**
207      * Ensure that changing the clone's nodeValue leaves the original's
208      * unchanged.
209      */

210     public void testCloneValueIsIndependentOfOriginal() {
211         cellClone_.setNodeValue("one");
212         assertFalse("one".equals(cell_.getNodeValue()));
213     }
214
215     /**
216      * Ensure that changing the clone's id leaves the original's unchanged.
217      */

218     public void testCloneIdIsIndependentOfOriginal() {
219         cellClone_.setNodeValue("one");
220         assertFalse("one".equals(cell_.getNodeValue()));
221     }
222
223     // these next few test our assumptions about how scripts affect the DOM
224

225     /**
226      * Ensure that the JavaScript object returned by the script fragment really
227      * refers to the same DOM node we think it should.
228      */

229     public void testScriptCanGetOriginalCell() {
230         final String cmd = "document.getElementById('cell')";
231         final Object object = client_.getScriptEngine().execute(page_, cmd, "test");
232
233         final HtmlElement cellElement = ((HTMLElement) object).getHtmlElementOrDie();
234         assertSame(cell_, cellElement);
235     }
236
237     /**
238      * Ensure that the JavaScript object returned by the script fragment is the
239      * same one the DOM node thinks it's wrapped by.
240      */

241     public void testCellScriptObjectIsReturnedByScript() {
242         final String cmd = "document.getElementById('cell')";
243         final HTMLElement jselement = (HTMLElement) client_.getScriptEngine()
244                 .execute(page_, cmd, "test");
245
246         assertSame(jselement, cell_.getScriptObject());
247     }
248
249     /**
250      * Ensure that setting a property via script sets the property on the
251      * ScriptableObject that we think it should.
252      */

253     public void testScriptCanSetJsPropertyOnCell() {
254         final String cmd = "document.getElementById('cell').a='original';document.getElementById('cell')";
255         final Object object = client_.getScriptEngine().execute(page_, cmd, "test");
256
257         final HTMLElement jselement = ((HTMLElement) object);
258         assertEquals("original", ScriptableObject.getProperty(jselement, "a"));
259
260         assertSame(jselement, cell_.getScriptObject());
261     }
262
263     /**
264      * Ensure that a script can set the disabled property on a DOM node.
265      */

266     public void testCloneScriptCanSetDisabledOnCell() {
267         final String cmd = "document.getElementById('cell').disabled='true'";
268         client_.getScriptEngine().execute(page_, cmd, "test");
269         assertEquals("disabled", cell_.getAttributeValue("disabled"));
270     }
271
272     /**
273      * Ensure that a script can set an attribute on the DOM node.
274      */

275     public void testCloneScriptCanSetAttributeOnCell() {
276         final String cmd = "document.getElementById('cell').setAttribute('a','original')";
277         client_.getScriptEngine().execute(page_, cmd, "test");
278         assertEquals("original", cell_.getAttributeValue("a"));
279     }
280
281     // these next few check that scripts manipulate the clone independently
282

283     /**
284      * Ensure that a script setting an attribute on the original does not affect
285      * that same attribute on the clone.
286      */

287     public void testCloneScriptSetAttributeIndependentOfOriginal() {
288         final String cmd = "document.getElementById('cell').setAttribute('a','original')";
289         client_.getScriptEngine().execute(page_, cmd, "test");
290
291         assertEquals("original", cell_.getAttributeValue("a"));
292         assertFalse("original".equals(cellClone_.getAttributeValue("a")));
293     }
294
295     /**
296      * Ensure that a script setting disabled on the original does not affect
297      * that same attribute on the clone.
298      */

299     public void testCloneScriptSetDisabledIndependentOfOriginal() {
300         final String cmd = "document.getElementById('cell').disabled = 'true'";
301         client_.getScriptEngine().execute(page_, cmd, "test");
302
303         assertEquals("disabled", cell_.getAttributeValue("disabled"));
304         assertFalse("disabled".equals(cellClone_.getAttributeValue("disabled")));
305     }
306
307     /**
308      * Ensure that a script referencing an element causes only that DOM element
309      * to get a ScriptObject assigned, and does not cause one to be assigned to
310      * the clone.
311      */

312     public void testCloneHasDifferentScriptableObject() {
313
314         final String cmd = "document.getElementById('cell')"; // force it to have a
315
// scriptable object
316
client_.getScriptEngine().execute(page_, cmd, "test");
317
318         assertNotSame(cell_.getScriptObject(), cellClone_.getScriptObject());
319     }
320
321     /**
322      * Ensure that setting the value on a child of a table cell doesn't affect
323      * the cloned child.
324      */

325     public void testScriptDomOperations() {
326         final String cmd = "document.getElementById('foo').value = 'Input!';document.getElementById('foo')";
327         client_.getScriptEngine().execute(page_, cmd, "test");
328
329         final HtmlElement input = (HtmlElement) cell_.getFirstChild();
330         assertEquals("Input!", input.getAttributeValue("value"));
331
332         final HtmlElement inputClone = (HtmlElement) cellClone_.getFirstChild();
333         assertFalse("Input!".equals(inputClone.getAttributeValue("value")));
334     }
335 }
336
Popular Tags