KickJava   Java API By Example, From Geeks To Geeks.

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


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
50 import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
51 import com.gargoylesoftware.htmlunit.html.HtmlPage;
52
53 /**
54  * Tests for Anchor.
55  *
56  * @version $Revision: 1.7 $
57  * @author <a HREF="mailto:gousseff@netscape.net">Alexei Goussev</a>
58  */

59 public class AnchorTest extends WebTestCase {
60
61     /**
62      * Create an instance
63      * @param name The name of the test
64      */

65     public AnchorTest( final String name ) {
66         super(name);
67     }
68
69
70     /**
71      * @throws Exception if the test fails
72      */

73     public void testAnchor_getAttribute_and_href() throws Exception {
74         final WebClient client = new WebClient();
75         final MockWebConnection webConnection = new MockWebConnection( client );
76
77         final String content
78             = "<html><head><title>AnchorTest</title><script>\n"
79             + "function doTest(anchorElement) {\n"
80             + " alert(anchorElement.href);\n"
81             + " alert(anchorElement.getAttribute('href'));\n"
82             + " anchorElement.href='testsite2.html';\n"
83             + " alert(anchorElement.href);\n"
84             + " alert(anchorElement.getAttribute('href'));\n"
85             + " alert(anchorElement.getAttribute('id'));\n"
86             + " alert(anchorElement.getAttribute('name'));\n"
87             + "}\n</script></head>"
88             + "<body>"
89             + "<a HREF='testsite1.html' id='13' name='testanchor' onClick='doTest(this);return false'>"
90             + "</body></html>";
91         
92         webConnection.setDefaultResponse( content );
93         client.setWebConnection( webConnection );
94         
95         final List collectedAlerts = new ArrayList();
96         client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
97
98         final HtmlPage page = (HtmlPage)(client.getPage( new URL("http://x")));
99                 
100         final HtmlAnchor anchor = page.getAnchorByName("testanchor");
101         
102         anchor.click();
103         
104         final List expectedAlerts = Arrays.asList( new String[]{
105             "testsite1.html", "testsite1.html", "testsite2.html", "testsite2.html", "13", "testanchor"
106         } );
107         assertEquals( expectedAlerts, collectedAlerts );
108     }
109
110     /**
111      * @throws Exception if the test fails
112      */

113     public void testOnclickToString() throws Exception {
114         final String content
115             = "<html><head><title>AnchorTest</title><script>\n"
116             + "function test() {\n"
117             + " for (var i=0; i<document.links.length; ++i)"
118             + " {\n"
119             + " var onclick = document.links[i].onclick;\n"
120             + " alert(onclick ? (onclick.toString().indexOf('alert(') != -1) : 'not defined');\n"
121             + " }\n"
122             + "}\n</script></head>"
123             + "<body onload='test()'>"
124             + "<a HREF='foo.html' onClick='alert(\"on click\")'>"
125             + "<a HREF='foo2.html'>"
126             + "</body></html>";
127         
128         
129         final List collectedAlerts = new ArrayList();
130         final List expectedAlerts = Arrays.asList( new String[]{ "true", "not defined" } );
131         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
132         loadPage(content, collectedAlerts);
133
134         
135         assertEquals( expectedAlerts, collectedAlerts );
136     }
137
138     /**
139      * @throws Exception if the test fails
140      */

141     public void testDefaultConversionToString() throws Exception {
142         final String content
143             = "<html><head><title>AnchorTest</title><script>\n"
144             + "function test() {\n"
145             + " alert(document.getElementById('myAnchor'));\n"
146             + " for (var i=0; i<document.links.length; ++i)"
147             + " {\n"
148             + " alert(document.links[i]);\n"
149             + " }\n"
150             + "}</script></head>"
151             + "<body onload='test()'>"
152             + "<a name='start' id='myAnchor'/>"
153             + "<a HREF='foo.html'>foo</a>"
154             + "<a HREF='javascript:void(0)'>void</a>"
155             + "<a HREF='#'>#</a>"
156             + "</body></html>";
157         
158         
159         final List collectedAlerts = new ArrayList();
160         final List expectedAlerts = Arrays.asList( new String[]{ "",
161             "http://www.gargoylesoftware.com/foo.html",
162             "javascript:void(0)",
163             "http://www.gargoylesoftware.com/#"} );
164         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
165         loadPage(content, collectedAlerts);
166
167         assertEquals( expectedAlerts, collectedAlerts );
168     }
169 }
170
Popular Tags