KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > SimpleScriptableTest


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;
39
40 import java.io.File;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.HashSet;
44 import java.util.List;
45 import java.util.Map;
46 import java.util.Set;
47 import java.util.TreeSet;
48
49 import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
50 import com.gargoylesoftware.htmlunit.MockWebConnection;
51 import com.gargoylesoftware.htmlunit.ScriptException;
52 import com.gargoylesoftware.htmlunit.WebClient;
53 import com.gargoylesoftware.htmlunit.WebTestCase;
54 import com.gargoylesoftware.htmlunit.html.HtmlPage;
55
56 /**
57  * Tests for SimpleScriptable.
58  *
59  * @version $Revision: 1.25 $
60  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
61  * @author <a HREF="mailto:BarnabyCourt@users.sourceforge.net">Barnaby Court</a>
62  * @author David K. Taylor
63  * @author <a HREF="mailto:bcurren@esomnie.com">Ben Curren</a>
64  * @author Marc Guillemot
65  */

66 public class SimpleScriptableTest extends WebTestCase {
67     /**
68      * Create an instance
69      * @param name The name of the test
70      */

71     public SimpleScriptableTest( final String name ) {
72         super(name);
73     }
74
75
76     /**
77      * @throws Exception if the test fails
78      */

79     public void testCallInheritedFunction() throws Exception {
80         final WebClient client = new WebClient();
81         final MockWebConnection webConnection = new MockWebConnection( client );
82
83         final String content
84             = "<html><head><title>foo</title><script>"
85             + "function doTest() {\n"
86             + " document.form1.textfield1.focus();\n"
87             + " alert('past focus');\n"
88             + "}\n"
89             + "</script></head><body onload='doTest()'>"
90             + "<p>hello world</p>"
91             + "<form name='form1'>"
92             + " <input type='text' name='textfield1' id='textfield1' value='foo' />"
93             + "</form>"
94             + "</body></html>";
95
96         webConnection.setDefaultResponse( content );
97         client.setWebConnection( webConnection );
98
99         final List expectedAlerts = Collections.singletonList("past focus");
100         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
101
102         final List collectedAlerts = new ArrayList();
103         client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
104
105         final HtmlPage page = ( HtmlPage )client.getPage(URL_GARGOYLE);
106         assertEquals("foo", page.getTitleText());
107         assertEquals("focus not changed to textfield1",
108                      page.getFormByName("form1").getInputByName("textfield1"),
109                      page.getWebClient().getElementWithFocus());
110         assertEquals( expectedAlerts, collectedAlerts );
111         
112     }
113
114     /**
115      */

116     public void testHtmlJavaScriptMapping_AllJavaScriptClassesArePresent() {
117         final Map map = SimpleScriptable.getHtmlJavaScriptMapping();
118         final String directoryName = "../../src/java/com/gargoylesoftware/htmlunit/javascript/host";
119         final Set names = getFileNames(directoryName.replace('/', File.separatorChar));
120
121         // Now pull out those names that we know don't have html equivalents
122
names.remove("CharacterDataImpl");
123         names.remove("Document");
124         names.remove("Event");
125         names.remove("EventHandler");
126         names.remove("History");
127         names.remove("Location");
128         names.remove("Navigator");
129         names.remove("NodeImpl");
130         names.remove("Screen");
131         names.remove("Style");
132         names.remove("ActiveXObject");
133         names.remove("Window");
134         names.remove("Attribute");
135         names.remove("ScoperFunctionObject");
136         names.remove("RowContainer");
137         names.remove("FormField");
138
139         assertEquals( new TreeSet(names), new TreeSet(map.values()) );
140     }
141
142     private Set getFileNames( final String directoryName ) {
143         File directory = new File("."+File.separatorChar+directoryName);
144         if( directory.exists() == false ) {
145             directory = new File("./src/java/".replace('/', File.separatorChar)+directoryName );
146         }
147         assertTrue("directory exists", directory.exists() );
148         assertTrue("is a directory", directory.isDirectory() );
149
150         final String fileNames[] = directory.list();
151         final Set collection = new HashSet();
152
153         for( int i=0; i<fileNames.length; i++ ) {
154             final String name = fileNames[i];
155             if( name.endsWith(".java") ) {
156                 collection.add( name.substring(0, name.length()-5) );
157             }
158         }
159         return collection;
160     }
161
162     /**
163      * @throws Exception if the test fails
164      */

165     public void testSetNonWritableProperty() throws Exception {
166         final String content
167             = "<html><head><title>foo</title></head><body onload='document.body=123456'>"
168             + "</body></html>";
169
170         try {
171             loadPage(content);
172             fail("Exception should have been thrown");
173         }
174         catch (final ScriptException e) {
175             // it's ok
176
}
177     }
178 }
179
Popular Tags