KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.List;
44
45 import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
46 import com.gargoylesoftware.htmlunit.MockWebConnection;
47 import com.gargoylesoftware.htmlunit.WebClient;
48 import com.gargoylesoftware.htmlunit.WebTestCase;
49 import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
50 import com.gargoylesoftware.htmlunit.html.HtmlElement;
51 import com.gargoylesoftware.htmlunit.html.HtmlPage;
52
53 /**
54  * Tests for NodeImpl
55  *
56  * @author yourgod
57  * @author Chris Erskine
58  * @author David D. Kilzer
59  * @author Daniel Gredler
60  * @author Marc Guillemot
61  * @author Hans Donner
62  * @author <a HREF="mailto:george@murnock.com">George Murnock</a>
63  * @version $Revision: 1.28 $
64  */

65 public class HTMLElementTest extends WebTestCase {
66
67     /**
68      * @param name The name of the test case
69      */

70     public HTMLElementTest(final String name) {
71         super(name);
72     }
73
74     /**
75      * @throws Exception on test failure
76      */

77     public void testGetAttribute() throws Exception {
78         final String content = "<html>\n" +
79                 "<head>\n" +
80                 " <title>test</title>\n" +
81                 " <script>\n" +
82                 " function doTest(){\n" +
83                 " var myNode = document.getElementById('myNode');\n" +
84                 " alert(myNode.title);\n" +
85                 " alert(myNode.getAttribute('title'));\n" +
86                 " alert(myNode.Title);\n" +
87                 " alert(myNode.getAttribute('class'));\n" +
88                 " }\n" +
89                 " </script>\n" +
90                 "</head>\n" +
91                 "<body onload='doTest()'>\n" +
92                 "<p id='myNode' title='a'>\n" +
93                 "</p>\n" +
94                 "</body>\n" +
95                 "</html>\n" +
96                 "";
97         final List expectedAlerts = Arrays.asList(new String[]{
98             "a", "a", "undefined", "null"
99         });
100         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
101
102         final List collectedAlerts = new ArrayList();
103         final HtmlPage page = loadPage(content, collectedAlerts);
104         assertEquals("test", page.getTitleText());
105
106
107         assertEquals(expectedAlerts, collectedAlerts);
108     }
109
110     /**
111      * @throws Exception on test failure
112      */

113     public void testSetAttribute() throws Exception {
114         final String content = "<html>\n" +
115                 "<head>\n" +
116                 " <title>test</title>\n" +
117                 " <script>\n" +
118                 " function doTest(){\n" +
119                 " var myNode = document.getElementById('myNode');\n" +
120                 " alert(myNode.title);\n" +
121                 " myNode.setAttribute('title', 'b');\n" +
122                 " alert(myNode.title);\n" +
123                 " alert(myNode.Title);\n" +
124                 " myNode.Title = 'foo';\n" +
125                 " alert(myNode.Title);\n" +
126                 " }\n" +
127                 " </script>\n" +
128                 "</head>\n" +
129                 "<body onload='doTest()'>\n" +
130                 "<p id='myNode' title='a'>\n" +
131                 "</p>\n" +
132                 "</body>\n" +
133                 "</html>\n" +
134                 "";
135         final List collectedAlerts = new ArrayList();
136         final HtmlPage page = loadPage(content, collectedAlerts);
137         assertEquals("test", page.getTitleText());
138
139         final List expectedAlerts = Arrays.asList(new String[]{
140             "a", "b", "undefined", "foo"
141         });
142
143         assertEquals(expectedAlerts, collectedAlerts);
144     }
145
146     /**
147      * @throws Exception on test failure
148      */

149     public void testGetAttributeNode() throws Exception {
150         final String content =
151               "<html>\n"
152             + "<head>\n"
153             + " <title>test</title>\n"
154             + " <script>\n"
155             + " function test() {\n"
156             + " var div = document.getElementById('div2');\n"
157             + " var customAtt = div.getAttributeNode('customAttribute');\n"
158             + " alertAttributeProperties(customAtt);\n"
159             + " }\n"
160             + " function alertAttributeProperties(att) {\n"
161             + " alert('expando=' + att.expando);\n"
162             + " alert('firstChild=' + att.firstChild);\n"
163             + " alert('lastChild=' + att.lastChild);\n"
164             + " alert('name=' + att.name);\n"
165             + " alert('nextSibling=' + att.nextSibling);\n"
166             + " alert('nodeName=' + att.nodeName);\n"
167             + " alert('nodeType=' + att.nodeType);\n"
168             + " alert('nodeValue=' + att.nodeValue);\n"
169             + " alert('(ownerDocument==document)=' + (att.ownerDocument==document));\n"
170             + " alert('parentNode=' + att.parentNode);\n"
171             + " alert('previousSibling=' + att.previousSibling);\n"
172             + " alert('specified=' + att.specified);\n"
173             + " alert('value=' + att.value);\n"
174             + " }\n"
175             + " </script>\n"
176             + "</head>\n"
177             + "<body onload='test()'>\n"
178             + " <div id='div1'></div>\n"
179             + " <div id='div2' name='blah' customAttribute='bleh'></div>\n"
180             + " <div id='div3'></div>\n"
181             + "</body>\n"
182             + "</html>";
183         final List collectedAlerts = new ArrayList();
184         final HtmlPage page = loadPage(content, collectedAlerts);
185         assertEquals("test", page.getTitleText());
186
187         final List expectedAlerts = Arrays.asList(new String[]{
188             "expando=true",
189             "firstChild=null",
190             "lastChild=null",
191             "name=customAttribute",
192             "nextSibling=null",
193             "nodeName=customAttribute",
194             "nodeType=2",
195             "nodeValue=bleh",
196             "(ownerDocument==document)=true",
197             "parentNode=null",
198             "previousSibling=null",
199             "specified=true",
200             "value=bleh",
201         });
202
203         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
204         assertEquals(expectedAlerts, collectedAlerts);
205     }
206
207     /**
208      * @throws Exception on test failure
209      */

210     public void testSetAttributeNode() throws Exception {
211         final String content =
212               "<html>\n"
213             + "<head>\n"
214             + " <title>test</title>\n"
215             + " <script>\n"
216             + " function test() {\n"
217             + " // Get the old alignment.\n"
218             + " var div1 = document.getElementById('div1');\n"
219             + " var a1 = div1.getAttributeNode('align');\n"
220             + " alert(a1.value);\n"
221             + " // Set the new alignment.\n"
222             + " var a2 = document.createAttribute('align');\n"
223             + " a2.value = 'right';\n"
224             + " a1 = div1.setAttributeNode(a2);\n"
225             + " alert(a1.value);\n"
226             + " alert(div1.getAttributeNode('align').value);\n"
227             + " alert(div1.getAttribute('align'));\n"
228             + " }\n"
229             + " </script>\n"
230             + "</head>\n"
231             + "<body onload='test()'>\n"
232             + " <div id='div1' align='left'></div>\n"
233             + "</body>\n"
234             + "</html>";
235         final List collectedAlerts = new ArrayList();
236         final HtmlPage page = loadPage(content, collectedAlerts);
237         assertEquals("test", page.getTitleText());
238
239         final List expectedAlerts = Arrays.asList(new String[]{
240             "left", "left", "right", "right"
241         });
242
243         assertEquals(expectedAlerts, collectedAlerts);
244     }
245
246     /**
247      * Test for getElementsByTagName
248      * @throws Exception if the test fails
249      */

250     public void testGetElementsByTagName() throws Exception {
251         final String content
252             = "<html><head><title>First</title><script>\n"
253             + "function doTest() {\n"
254             + "var a1 = document.getElementsByTagName('td');\n"
255             + "alert('all = ' + a1.length);\n"
256             + "var firstRow = document.getElementById('r1');\n"
257             + "var rowOnly = firstRow.getElementsByTagName('td');\n"
258             + "alert('row = ' + rowOnly.length);\n"
259             + "}\n"
260             + "</script></head><body onload='doTest()'>\n"
261             + "<table>\n"
262             + "<tr id='r1'><td>1</td><td>2</td></tr>\n"
263             + "<tr id='r2'><td>3</td><td>4</td></tr>\n"
264             + "</table>\n"
265             + "</body></html>\n";
266         final List collectedAlerts = new ArrayList();
267         loadPage(content, collectedAlerts);
268
269         final List expectedAlerts = Arrays.asList(new String[]{
270             "all = 4", "row = 2"
271         });
272
273         assertEquals(expectedAlerts, collectedAlerts);
274     }
275     
276     /**
277      * Test getting the class for the element
278      * @throws Exception if the test fails
279      */

280     public void testGetClassName() throws Exception {
281         final String content
282             = "<html><head><style>.x { font: 8pt Arial bold; }</style>\n"
283             + "<script>\n"
284             + "function doTest() {\n"
285             + " var ele = document.getElementById('pid');\n"
286             + " var aClass = ele.className;\n"
287             + " alert('the class is ' + aClass);\n"
288             + "}\n"
289             + "</script></head><body onload='doTest()'>\n"
290             + "<p id='pid' class='x'>text</p>\n"
291             + "</body></html>\n";
292           
293         final List collectedAlerts = new ArrayList();
294         loadPage(content, collectedAlerts);
295
296         final List expectedAlerts = Arrays.asList(new String[]{
297             "the class is x"
298         });
299
300         assertEquals(expectedAlerts, collectedAlerts);
301     }
302
303     /**
304      * Test getting the class for the element
305      * @throws Exception if the test fails
306      */

307     public void testSetClassName() throws Exception {
308         final String content
309             = "<html><head><style>.x { font: 8pt Arial bold; }</style>\n"
310             + "<script>\n"
311             + "function doTest() {\n"
312             + " var ele = document.getElementById('pid');\n"
313             + " ele.className = 'z';"
314             + " var aClass = ele.className;\n"
315             + " alert('the class is ' + aClass);\n"
316             + "}\n"
317             + "</script></head><body onload='doTest()'>\n"
318             + "<p id='pid' class='x'>text</p>\n"
319             + "</body></html>\n";
320           
321         final List collectedAlerts = new ArrayList();
322         loadPage(content, collectedAlerts);
323
324         final List expectedAlerts = Arrays.asList(new String[]{
325             "the class is z"
326         });
327
328         assertEquals(expectedAlerts, collectedAlerts);
329     }
330     
331     /**
332      *
333      * @throws Exception if the test fails
334      */

335     public void testGetSetInnerHTMLSimple() throws Exception {
336         final String content = "<html>\n" +
337                 "<head>\n" +
338                 " <title>test</title>\n" +
339                 " <script>\n" +
340                 " function doTest(){\n" +
341                 " var myNode = document.getElementById('myNode');\n" +
342                 " alert('Old = ' + myNode.innerHTML);\n" +
343                 " myNode.innerHTML = 'New cell value';\n" +
344                 " alert('New = ' + myNode.innerHTML);\n" +
345                 " }\n" +
346                 " </script>\n" +
347                 "</head>\n" +
348                 "<body onload='doTest()'>\n" +
349                 "<p id='myNode'><b>Old innerHTML</b></p>\n" +
350                 "</body>\n" +
351                 "</html>\n" +
352                 "";
353         final List collectedAlerts = new ArrayList();
354         loadPage(content, collectedAlerts);
355
356         final List expectedAlerts = Arrays.asList(new String[]{
357             "Old = <b>Old innerHTML</b>",
358             "New = New cell value"
359         });
360         assertEquals(expectedAlerts, collectedAlerts);
361     }
362
363     /**
364      * Test the use of innerHTML to set new html code
365      * @throws Exception if the test fails
366      */

367     public void testGetSetInnerHTMLComplex() throws Exception {
368         final String content = "<html>\n" +
369                 "<head>\n" +
370                 " <title>test</title>\n" +
371                 " <script>\n" +
372                 " function doTest(){\n" +
373                 " var myNode = document.getElementById('myNode');\n" +
374                 " alert('Old = ' + myNode.innerHTML);\n" +
375                 " myNode.innerHTML = ' <b><i id=\"newElt\">New cell value</i></b>';\n" +
376                 " alert('New = ' + myNode.innerHTML);\n" +
377                 " alert(document.getElementById('newElt').tagName);\n" +
378                 " }\n" +
379                 " </script>\n" +
380                 "</head>\n" +
381                 "<body onload='doTest()'>\n" +
382                 "<p id='myNode'><b>Old innerHTML</b></p>\n" +
383                 "</body>\n" +
384                 "</html>\n" +
385                 "";
386         final List collectedAlerts = new ArrayList();
387         final HtmlPage page = loadPage(content, collectedAlerts);
388
389         final List expectedAlerts = Arrays.asList(new String[]{
390             "Old = <b>Old innerHTML</b>",
391             "New = <b><i id=\"newElt\">New cell value</i></b>",
392             "I"
393         });
394         assertEquals(expectedAlerts, collectedAlerts);
395
396         final HtmlElement pElt = page.getHtmlElementById("myNode");
397         assertEquals("p", pElt.getNodeName());
398         final HtmlElement elt = page.getHtmlElementById("newElt");
399         assertEquals("New cell value", elt.asText());
400     }
401
402     /**
403      * Test the use of innerHTML to set a new input
404      * @throws Exception if the test fails
405      */

406     public void testGetSetInnerHTMLNewInput() throws Exception {
407         final String content = "<html>\n" +
408                 "<head>\n" +
409                 " <title>test</title>\n" +
410                 " <script>\n" +
411                 " function doTest(){\n" +
412                 " var myNode = document.getElementById('myNode');\n" +
413                 " myNode.innerHTML = '<input type=\"checkbox\" name=\"myCb\" checked>';\n" +
414                 " alert(myNode.myCb.checked);\n" +
415                 " }\n" +
416                 " </script>\n" +
417                 "</head>\n" +
418                 "<body onload='doTest()'>\n" +
419                 "<form id='myNode'></form>\n" +
420                 "</body>\n" +
421                 "</html>\n" +
422                 "";
423         final List collectedAlerts = new ArrayList();
424         final List expectedAlerts = Arrays.asList(new String[]{ "true" });
425         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
426         loadPage(content, collectedAlerts);
427
428         assertEquals(expectedAlerts, collectedAlerts);
429     }
430
431     /**
432      *
433      * @throws Exception if the test fails
434      */

435     public void testGetSetOuterHTMLSimple() throws Exception {
436         final String content = "<html>\n" +
437                 "<head>\n" +
438                 " <title>test</title>\n" +
439                 " <script>\n" +
440                 " function doTest(){\n" +
441                 " var myNode = document.getElementById('myNode');\n" +
442                 " var innerNode = document.getElementById('innerNode');\n" +
443                 " alert('Old = ' + innerNode.outerHTML);\n" +
444                 " innerNode.outerHTML = 'New cell value';\n" +
445                 " alert('New = ' + myNode.innerHTML);\n" +
446                 " }\n" +
447                 " </script>\n" +
448                 "</head>\n" +
449                 "<body onload='doTest()'>\n" +
450                 "<p id='myNode'><b id='innerNode'>Old outerHTML</b></p>\n" +
451                 "</body>\n" +
452                 "</html>\n" +
453                 "";
454         final List collectedAlerts = new ArrayList();
455         final List expectedAlerts = Arrays.asList(new String[]{
456             "Old = <b id=\"innerNode\">Old outerHTML</b>",
457             "New = New cell value"
458         });
459         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
460         
461         loadPage(content, collectedAlerts);
462         assertEquals(expectedAlerts, collectedAlerts);
463     }
464
465     /**
466      * Test the use of outerHTML to set new html code
467      * @throws Exception if the test fails
468      */

469     public void testGetSetOuterHTMLComplex() throws Exception {
470         final String content = "<html>\n" +
471                 "<head>\n" +
472                 " <title>test</title>\n" +
473                 " <script>\n" +
474                 " function doTest(){\n" +
475                 " var myNode = document.getElementById('myNode');\n" +
476                 " var innerNode = document.getElementById('innerNode');\n" +
477                 " alert('Old = ' + innerNode.outerHTML);\n" +
478                 " innerNode.outerHTML = ' <b><i id=\"newElt\">New cell value</i></b>';\n" +
479                 " alert('New = ' + myNode.innerHTML);\n" +
480                 " alert(document.getElementById('newElt').tagName);\n" +
481                 " }\n" +
482                 " </script>\n" +
483                 "</head>\n" +
484                 "<body onload='doTest()'>\n" +
485                 "<p id='myNode'><b id='innerNode'>Old outerHTML</b></p>\n" +
486                 "</body>\n" +
487                 "</html>\n" +
488                 "";
489
490         final List expectedAlerts = Arrays.asList(new String[]{
491             "Old = <b id=\"innerNode\">Old outerHTML</b>",
492             "New = <b><i id=\"newElt\">New cell value</i></b>",
493             "I"
494         });
495         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
496
497         final List collectedAlerts = new ArrayList();
498         final HtmlPage page = loadPage(content, collectedAlerts);
499         assertEquals(expectedAlerts, collectedAlerts);
500
501         final HtmlElement pElt = page.getHtmlElementById("myNode");
502         assertEquals("p", pElt.getNodeName());
503         final HtmlElement elt = page.getHtmlElementById("newElt");
504         assertEquals("New cell value", elt.asText());
505     }
506
507     /**
508      * @throws Exception if the test fails
509      */

510     public void testInsertAdjacentHTML() throws Exception {
511         final String content = "<html><head><title>First</title>\n"
512                 + "<script>\n"
513                 + "function test()\n"
514                 + "{\n"
515                 + " var oDiv = document.getElementById('middle');\n"
516                 + " oDiv.insertAdjacentHTML('beforeEnd', ' <div id=3>before end</div> ');\n"
517                 + " oDiv.insertAdjacentHTML('afterEnd', ' <div id=4>after end</div> ');\n"
518                 + " oDiv.insertAdjacentHTML('beforeBegin', ' <div id=1>before begin</div> ');\n"
519                 + " oDiv.insertAdjacentHTML('afterBegin', ' <div id=2>after begin</div> ');\n"
520                 + " var coll = document.getElementsByTagName('DIV');\n"
521                 + " for (var i=0; i<coll.length; ++i) {\n"
522                 + " alert(coll[i].id);\n"
523                 + " }\n"
524                 + "}\n"
525                 + "</script>\n"
526                 + "</head>"
527                 + "<body onload='test()'>\n"
528                 + "<div id='outside' style='color: #00ff00'>\n"
529                 + "<div id='middle' style='color: #ff0000'>\n"
530                 + "inside\n"
531                 + "</div>\n"
532                 + "</div>\n"
533                 + "</body></html>";
534         final List collectedAlerts = new ArrayList();
535         final HtmlPage page = loadPage(content, collectedAlerts);
536
537         final List expectedAlerts = Arrays.asList(new String[]{
538             "outside", "1", "middle", "2", "3", "4"
539         });
540         assertEquals(expectedAlerts, collectedAlerts);
541
542         final HtmlElement elt = page.getHtmlElementById("outside");
543         assertEquals("before begin after begin inside before end after end", elt.asText());
544     }
545
546     /**
547      * Test the <tt>#default#clientCaps</tt> default IE behavior.
548      *
549      * @throws Exception if the test fails
550      */

551     public void testAddBehaviorDefaultClientCaps() throws Exception {
552         final String content = "<html>\n" +
553                 "<head>\n" +
554                 " <title>Test</title>\n" +
555                 " <script>\n" +
556                 " function doTest() {\n" +
557                 " var body = document.body;\n" +
558                 " alert('body.cpuClass = ' + body.cpuClass);\n" +
559                 " var id = body.addBehavior('#default#clientCaps');\n" +
560                 " alert('body.cpuClass = ' + body.cpuClass);\n" +
561                 " body.removeBehavior(id);\n" +
562                 " alert('body.cpuClass = ' + body.cpuClass);\n" +
563                 " }\n" +
564                 " </script>\n" +
565                 "</head>\n" +
566                 "<body onload='doTest()'>Test</body>\n" +
567                 "</html>";
568         final List collectedAlerts = new ArrayList();
569         final HtmlPage page = loadPage(content, collectedAlerts);
570
571         final List expectedAlerts = Arrays.asList(new String[]{
572             "body.cpuClass = undefined",
573             "body.cpuClass = " + page.getWebClient().getBrowserVersion().getCpuClass(),
574             "body.cpuClass = undefined"
575         });
576         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
577         assertEquals(expectedAlerts, collectedAlerts);
578     }
579
580     /**
581      * Test the <tt>#default#homePage</tt> default IE behavior.
582      *
583      * @throws Exception if the test fails
584      */

585     public void testAddBehaviorDefaultHomePage() throws Exception {
586
587         final URL content1Url = new URL("http://www.domain1.com/");
588         final URL content2Url = new URL("http://www.domain2.com/");
589
590         final String content1 =
591               "<html>\n"
592             + " <head>\n"
593             + " <title>Test</title>\n"
594             + " <script>\n"
595             + " function doTest() {\n"
596             + " // Test adding the behavior via script. Note that the URL\n"
597             + " // used to test must be part of the SAME domain as this\n"
598             + " // document, otherwise isHomePage() always returns false.\n"
599             + " var body = document.body;\n"
600             + " body.addBehavior('#default#homePage');\n"
601             + " var url = '" + content1Url.toExternalForm() + "';\n"
602             + " alert('isHomePage = ' + body.isHomePage(url));\n"
603             + " body.setHomePage(url);\n"
604             + " alert('isHomePage = ' + body.isHomePage(url));\n"
605             + " // Test behavior added via style attribute.\n"
606             + " // Also test case-insensitivity of default behavior names.\n"
607             + " alert('isHomePage = ' + hp.isHomePage(url));\n"
608             + " // Make sure that (as mentioned above) isHomePage() always\n"
609             + " // returns false when the url specified is the actual\n"
610             + " // homepage, but the document checking is on a DIFFERENT domain.\n"
611             + " hp.setHomePage('" + content2Url.toExternalForm() + "');\n"
612             + " alert('isHomePage = ' + hp.isHomePage(url));\n"
613             + " // Test navigation to homepage.\n"
614             + " body.navigateHomePage();\n"
615             + " }\n"
616             + " </script>\n"
617             + " </head>\n"
618             + " <body onload='doTest()'>\n"
619             + " <span id='hp' style='behavior:url(#default#homepage)'></span>\n"
620             + " </body>\n"
621             + "</html>";
622         final String content2 = "<html></html>";
623         
624         final WebClient client = new WebClient();
625         final List collectedAlerts = new ArrayList();
626         client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
627
628         final MockWebConnection webConnection = new MockWebConnection( client );
629         webConnection.setResponse(content1Url, content1);
630         webConnection.setResponse(content2Url, content2);
631         client.setWebConnection( webConnection );
632
633         final HtmlPage page = (HtmlPage) client.getPage(content1Url);
634
635         final List expectedAlerts = Arrays.asList(new String[]{
636             "isHomePage = false",
637             "isHomePage = true",
638             "isHomePage = true",
639             "isHomePage = false"
640         });
641         assertEquals(expectedAlerts, collectedAlerts);
642         assertEquals(content2Url.toExternalForm(), page.getWebResponse().getUrl().toExternalForm());
643     }
644
645     /**
646      * Test the removal of behaviors.
647      *
648      * @throws Exception if the test fails
649      */

650     public void testRemoveBehavior() throws Exception {
651         final String content = "<html>\n" +
652                 "<head>\n" +
653                 " <title>Test</title>\n" +
654                 " <script>\n" +
655                 " function doTest() {\n" +
656                 " var body = document.body;\n" +
657                 " alert('body.isHomePage = ' + body.isHomePage);\n" +
658                 " var id = body.addBehavior('#default#homePage');\n" +
659                 " alert('body.isHomePage = ' + body.isHomePage('not the home page'));\n" +
660                 " body.removeBehavior(id);\n" +
661                 " alert('body.isHomePage = ' + body.isHomePage);\n" +
662                 " }\n" +
663                 " </script>\n" +
664                 "</head>\n" +
665                 "<body onload='doTest()'>Test</body>\n" +
666                 "</html>";
667         final List collectedAlerts = new ArrayList();
668         loadPage(content, collectedAlerts);
669
670         final List expectedAlerts = Arrays.asList(new String[]{
671             "body.isHomePage = undefined",
672             "body.isHomePage = false",
673             "body.isHomePage = undefined"
674         });
675         assertEquals(expectedAlerts, collectedAlerts);
676     }
677
678     /**
679      * @throws Exception if the test fails
680      */

681     public void testChildren() throws Exception {
682         final String content = "<html>\n"
683             + "<head>\n"
684             + "<script>\n"
685             + "function test()\n"
686             + "{\n"
687             + " var oDiv = document.getElementById('myDiv');\n"
688             + " for (var i=0; i<oDiv.children.length; ++i) \n"
689             + " alert(oDiv.children(i).tagName);\n"
690             + " var oCol = oDiv.children;\n"
691             + " alert(oCol.length);\n"
692             + " oDiv.insertAdjacentHTML('beforeEnd', '<br>');\n"
693             + " alert(oCol.length);\n"
694             + "}\n"
695             + "</script>\n"
696             + "</head>\n"
697             + "<body onload='test()'>\n"
698             + "<div id='myDiv'><br/><div><span>test</span></div></div>\n"
699             + "</body>\n"
700             + "</html>";
701         final List collectedAlerts = new ArrayList();
702         final List expectedAlerts = Arrays.asList(new String[]{ "BR", "DIV", "2", "3" });
703         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
704         loadPage(content, collectedAlerts);
705
706         assertEquals(expectedAlerts, collectedAlerts);
707     }
708
709
710     /**
711      * @throws Exception if the test fails
712      */

713     public void testSetOnclick() throws Exception {
714         eventHandlerSetterGetterTest("onclick");
715     }
716
717     /**
718      * @throws Exception if the test fails
719      */

720     public void testSetOndblclick() throws Exception {
721         eventHandlerSetterGetterTest("ondblclick");
722     }
723
724     /**
725      * @throws Exception if the test fails
726      */

727     public void testSetOnblur() throws Exception {
728         eventHandlerSetterGetterTest("onblur");
729     }
730
731     /**
732      * @throws Exception if the test fails
733      */

734     public void testSetOnfocus() throws Exception {
735         eventHandlerSetterGetterTest("onfocus");
736     }
737
738     /**
739      * @throws Exception if the test fails
740      */

741     public void testSetOnkeydown() throws Exception {
742         eventHandlerSetterGetterTest("onkeydown");
743     }
744
745     /**
746      * @throws Exception if the test fails
747      */

748     public void testSetOnkeypress() throws Exception {
749         eventHandlerSetterGetterTest("onkeypress");
750     }
751
752     /**
753      * @throws Exception if the test fails
754      */

755     public void testSetOnkeyup() throws Exception {
756         eventHandlerSetterGetterTest("onkeyup");
757     }
758
759     /**
760      * @throws Exception if the test fails
761      */

762     public void testSetOnmousedown() throws Exception {
763         eventHandlerSetterGetterTest("onmousedown");
764     }
765
766     /**
767      * @throws Exception if the test fails
768      */

769     public void testSetOnmouseup() throws Exception {
770         eventHandlerSetterGetterTest("onmouseup");
771     }
772     /**
773      * @throws Exception if the test fails
774      */

775     public void testSetOnmouseover() throws Exception {
776         eventHandlerSetterGetterTest("onmouseover");
777     }
778     /**
779      * @throws Exception if the test fails
780      */

781     public void testSetOnmouseout() throws Exception {
782         eventHandlerSetterGetterTest("onmouseout");
783     }
784     /**
785      * @throws Exception if the test fails
786      */

787     public void testSetOnmousemove() throws Exception {
788         eventHandlerSetterGetterTest("onmousemove");
789     }
790
791     /**
792      * @throws Exception if the test fails
793      */

794     public void testSetOnresize() throws Exception {
795         eventHandlerSetterGetterTest("onresize");
796     }
797
798     /**
799      * @param eventName The name of the event
800      * @throws Exception if the test fails
801      */

802     private void eventHandlerSetterGetterTest(final String eventName) throws Exception {
803         final String content = "<html>\n"
804             + "<head>\n"
805             + "<script>\n"
806             + "function handler(event) {}"
807             + "function test()\n"
808             + "{\n"
809             + " var oDiv = document.getElementById('myDiv');\n"
810             + " oDiv." + eventName + " = handler;\n"
811             + " if (oDiv." + eventName + " == handler) {\n"
812             + " alert('success');\n"
813             + " } else {\n"
814             + " alert('fail');\n"
815             + " }\n"
816             + "}\n"
817             + "</script>\n"
818             + "</head>\n"
819             + "<body onload='test()'>\n"
820             + "<div id='myDiv'><br/><div><span>test</span></div></div>\n"
821             + "</body>\n"
822             + "</html>";
823         final List collectedAlerts = new ArrayList();
824         final List expectedAlerts = Arrays.asList(new String[]{ "success" });
825         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
826         final HtmlPage page = loadPage(content, collectedAlerts);
827         final HtmlElement div = page.getHtmlElementById("myDiv");
828         
829         assertNotNull("Event handler was not set", div.getEventHandler(eventName));
830         
831         assertEquals(expectedAlerts, collectedAlerts);
832     }
833
834     /**
835      *
836      * @throws Exception if the test fails
837      */

838     public void testGetSetInnerTextSimple() throws Exception {
839         final String content = "<html>\n" +
840                 "<head>\n" +
841                 " <title>test</title>\n" +
842                 " <script>\n" +
843                 " function doTest(){\n" +
844                 " var myNode = document.getElementById('myNode');\n" +
845                 " alert('Old = ' + myNode.innerText);\n" +
846                 " myNode.innerText = 'New cell value';\n" +
847                 " alert('New = ' + myNode.innerText);\n" +
848                 " }\n" +
849                 " </script>\n" +
850                 "</head>\n" +
851                 "<body onload='doTest()'>\n" +
852                 "<div id='myNode'><b>Old <p>innerText</p></b></div>\n" +
853                 "</body>\n" +
854                 "</html>\n" +
855                 "";
856         final List expectedAlerts = Arrays.asList(new String[]{
857             "Old = Old \r\ninnerText",
858             "New = New cell value"
859         });
860         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
861
862         final List collectedAlerts = new ArrayList();
863         loadPage(content, collectedAlerts);
864         assertEquals(expectedAlerts, collectedAlerts);
865     }
866
867     /**
868      *
869      * @throws Exception if the test fails
870      */

871     public void testClickHashAnchor() throws Exception {
872         final String content
873             = "<html><head><title>HashAnchor</title></head>"
874             + "<body>"
875             + " <script language='javascript'>"
876             + " function test() {alert('test hash');}"
877             + " </script>"
878             + " <a onClick='javascript:test();' HREF='#' name='hash'>Click</a>"
879             + "</body>"
880             + "</html>";
881         final List expectedAlerts = Arrays.asList(new String[]{"test hash"});
882
883         // first use direct load
884
final List loadCollectedAlerts = new ArrayList();
885         final HtmlPage loadPage = loadPage(content, loadCollectedAlerts);
886
887         final HtmlAnchor loadHashAnchor = loadPage.getAnchorByName("hash");
888         loadHashAnchor.click();
889         
890         assertEquals(expectedAlerts, loadCollectedAlerts);
891         
892         // finally try via the client
893

894         final WebClient webClient = new WebClient();
895         final MockWebConnection webConnection = new MockWebConnection( webClient );
896         webConnection.setResponse(URL_FIRST, content);
897         webClient.setWebConnection( webConnection );
898
899         final CollectingAlertHandler clientCollectedAlertsHandler = new CollectingAlertHandler();
900         webClient.setAlertHandler(clientCollectedAlertsHandler);
901
902         final HtmlPage clientPage = (HtmlPage) webClient.getPage(URL_FIRST);
903         final HtmlAnchor clientHashAnchor = clientPage.getAnchorByName("hash");
904         clientHashAnchor.click();
905         
906         assertEquals(expectedAlerts, clientCollectedAlertsHandler.getCollectedAlerts());
907     }
908
909     /**
910      * Test the removal of attributes from HTMLElements.
911      *
912      * @throws Exception if the test fails
913      */

914     public void testRemoveAttribute() throws Exception {
915         final String content = "<html>\n" +
916                 "<head>\n" +
917                 " <title>Test</title>\n" +
918                 " <script>\n" +
919                 " function doTest() {\n" +
920                 " var myDiv = document.getElementById('aDiv');\n" +
921                 " alert(myDiv.getAttribute('name'));\n" +
922                 " myDiv.removeAttribute('name');\n" +
923                " alert(myDiv.getAttribute('name'));\n" +
924                 " }\n" +
925                 " </script>\n" +
926                 "</head>\n" +
927                 "<body onload='doTest()'><div id='aDiv' name='removeMe'>" +
928                "</div></body>\n" +
929                 "</html>";
930         final List expectedAlerts = Arrays.asList(new String[]{"removeMe", "null"});
931         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
932
933         final List collectedAlerts = new ArrayList();
934         loadPage(content, collectedAlerts);
935
936         assertEquals(expectedAlerts, collectedAlerts);
937     }
938 }
939
Popular Tags