001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.html;
039:
040: import java.util.ArrayList;
041: import java.util.List;
042:
043: import com.gargoylesoftware.htmlunit.BrowserVersion;
044: import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
045: import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
046: import com.gargoylesoftware.htmlunit.MockWebConnection;
047: import com.gargoylesoftware.htmlunit.WebClient;
048: import com.gargoylesoftware.htmlunit.WebTestCase;
049:
050: /**
051: * Tests for {@link HtmlScript}.
052: *
053: * @version $Revision: 2132 $
054: * @author Marc Guillemot
055: * @author Daniel Gredler
056: */
057: public class HtmlScriptTest extends WebTestCase {
058:
059: /**
060: * Creates an instance.
061: * @param name The name of the test.
062: */
063: public HtmlScriptTest(final String name) {
064: super (name);
065: }
066:
067: /**
068: * Verifies that a failing HTTP status code for a JavaScript file request (like a 404 response)
069: * results in a {@link FailingHttpStatusCodeException}, depending on how the client has been
070: * configured.
071: *
072: * @see HtmlPage#loadJavaScriptFromUrl(java.net.URL, String)
073: * @see WebClient#isThrowExceptionOnFailingStatusCode()
074: * @throws Exception if an error occurs
075: */
076: public void testBadExternalScriptReference() throws Exception {
077:
078: final String html = "<html><head><title>foo</title>\n"
079: + "<script src='inexistent.js'></script>\n"
080: + "</head><body></body></html>";
081:
082: final WebClient client = new WebClient();
083:
084: final MockWebConnection webConnection = new MockWebConnection(
085: client);
086: webConnection.setDefaultResponse("inexistent", 404,
087: "Not Found", "text/html");
088: webConnection.setResponse(URL_FIRST, html);
089: client.setWebConnection(webConnection);
090:
091: try {
092: client.getPage(URL_FIRST);
093: fail("Should throw.");
094: } catch (final FailingHttpStatusCodeException e) {
095: final String url = URL_FIRST.toExternalForm();
096: assertTrue("exception contains url of failing script", e
097: .getMessage().indexOf(url) > -1);
098: assertEquals(404, e.getStatusCode());
099: assertEquals("Not Found", e.getStatusMessage());
100: }
101:
102: client.setThrowExceptionOnFailingStatusCode(false);
103:
104: try {
105: client.getPage(URL_FIRST);
106: } catch (final FailingHttpStatusCodeException e) {
107: fail("Should not throw.");
108: }
109: }
110:
111: /**
112: * @throws Exception If an error occurs.
113: */
114: public void testAsText() throws Exception {
115: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
116: + "<script id='script1'>\n"
117: + " var foo = 132;\n"
118: + "</script></body></html>";
119:
120: final HtmlPage page = loadPage(htmlContent);
121:
122: final HtmlScript script = (HtmlScript) page
123: .getHtmlElementById("script1");
124: assertEquals("", script.asText());
125: }
126:
127: /**
128: * Verifies that the weird script src attribute used by the jQuery JavaScript library is
129: * ignored silently (bug 1695279).
130: * @throws Exception If the test fails.
131: */
132: public void testInvalidJQuerySrcAttribute() throws Exception {
133: loadPage("<html><body><script src='//:'></script></body></html>");
134: }
135:
136: /**
137: * Verifies that if a script element executes "window.location.href=someotherpage", then subsequent
138: * script tags, and any onload handler for the original page do not run.
139: * @throws Exception If the test fails
140: */
141: public void testChangingLocationSkipsFurtherScriptsOnPage()
142: throws Exception {
143: final String firstPage = "<html><head></head>\n"
144: + "<body onload='alert(\"body onload executing but should be skipped\")'>\n"
145: + "<script>alert('First script executes')</script>\n"
146: + "<script>window.location.href='"
147: + URL_SECOND
148: + "'</script>\n"
149: + "<script>alert('Third script executing but should be skipped')</script>\n"
150: + "</body></html>";
151:
152: final String secondPage = "<html><head></head><body>\n"
153: + "<script>alert('Second page loading')</script>\n"
154: + "</body></html>";
155:
156: final WebClient client = new WebClient();
157:
158: final MockWebConnection webConnection = new MockWebConnection(
159: client);
160: webConnection.setResponse(URL_FIRST, firstPage);
161: webConnection.setResponse(URL_SECOND, secondPage);
162: client.setWebConnection(webConnection);
163:
164: final List collectedAlerts = new ArrayList();
165: client.setAlertHandler(new CollectingAlertHandler(
166: collectedAlerts));
167:
168: client.getPage(URL_FIRST);
169: final String[] expectedAlerts = { "First script executes",
170: "Second page loading" };
171: assertEquals(expectedAlerts, collectedAlerts);
172: }
173:
174: /**
175: * Verifies that a script element is not run when it is cloned.
176: * See bug 1707788.
177: * @throws Exception If an error occurs.
178: */
179: public void testScriptIsNotRunWhenCloned() throws Exception {
180: final String html = "<html><body onload='document.body.cloneNode(true)'>\n"
181: + "<script>alert('a')</script></body></html>";
182: final List collectedAlerts = new ArrayList();
183: loadPage(html, collectedAlerts);
184:
185: final String[] expectedAlerts = { "a" };
186: assertEquals(expectedAlerts, collectedAlerts);
187: }
188:
189: /**
190: * @throws Exception if an error occurs
191: */
192: public void testDefer() throws Exception {
193: final String html = "<html><head>\n"
194: + "<script defer>alert('deferred')</script>\n"
195: + "<script>alert('normal')</script>\n" + "</head>\n"
196: + "<body onload='alert(\"onload\")'>test</body>\n"
197: + "</html>";
198:
199: final List actualFF = new ArrayList();
200: loadPage(BrowserVersion.FIREFOX_2, html, actualFF);
201: final String[] expectedFF = new String[] { "deferred",
202: "normal", "onload" };
203: assertEquals(expectedFF, actualFF);
204:
205: final List actualIE = new ArrayList();
206: loadPage(BrowserVersion.INTERNET_EXPLORER_7_0, html, actualIE);
207: final String[] expectedIE = new String[] { "normal",
208: "deferred", "onload" };
209: assertEquals(expectedIE, actualIE);
210: }
211: }
|