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.CollectingAlertHandler;
044: import com.gargoylesoftware.htmlunit.MockWebConnection;
045: import com.gargoylesoftware.htmlunit.WebClient;
046: import com.gargoylesoftware.htmlunit.WebTestCase;
047:
048: /**
049: * Unit tests for {@link HtmlInlineFrame}.
050: *
051: * @version $Revision: 2132 $
052: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
053: * @author Ahmed Ashour
054: * @author Marc Guillemot
055: */
056: public class HtmlInlineFrameTest extends WebTestCase {
057:
058: /**
059: * Creates an instance.
060: *
061: * @param name The name of the test
062: */
063: public HtmlInlineFrameTest(final String name) {
064: super (name);
065: }
066:
067: /**
068: * @throws Exception if the test fails
069: */
070: public void testSetSrcAttribute() throws Exception {
071: final String firstContent = "<html><head><title>First</title></head><body>\n"
072: + "<iframe id='iframe1' src='"
073: + URL_SECOND
074: + "'>\n"
075: + "</body></html>";
076: final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
077: final String thirdContent = "<html><head><title>Third</title></head><body></body></html>";
078: final WebClient client = new WebClient();
079:
080: final MockWebConnection webConnection = new MockWebConnection(
081: client);
082: webConnection.setResponse(URL_FIRST, firstContent);
083: webConnection.setResponse(URL_SECOND, secondContent);
084: webConnection.setResponse(URL_THIRD, thirdContent);
085:
086: client.setWebConnection(webConnection);
087:
088: final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
089: assertEquals("First", page.getTitleText());
090:
091: final HtmlInlineFrame iframe = (HtmlInlineFrame) page
092: .getHtmlElementById("iframe1");
093: assertEquals(URL_SECOND.toExternalForm(), iframe
094: .getSrcAttribute());
095: assertEquals("Second", ((HtmlPage) iframe.getEnclosedPage())
096: .getTitleText());
097:
098: iframe.setSrcAttribute(URL_THIRD.toExternalForm());
099: assertEquals(URL_THIRD.toExternalForm(), iframe
100: .getSrcAttribute());
101: assertEquals("Third", ((HtmlPage) iframe.getEnclosedPage())
102: .getTitleText());
103: }
104:
105: /**
106: * @throws Exception if the test fails
107: */
108: public void testSetSrcAttributeWithWhiteSpaces() throws Exception {
109: final String firstContent = "<html><head><title>First</title></head><body>\n"
110: + "<iframe id='iframe1' src='\n"
111: + URL_SECOND
112: + "\n'>\n" + "</body></html>";
113: final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
114: final String thirdContent = "<html><head><title>Third</title></head><body></body></html>";
115: final WebClient client = new WebClient();
116:
117: final MockWebConnection webConnection = new MockWebConnection(
118: client);
119: webConnection.setResponse(URL_FIRST, firstContent);
120: webConnection.setResponse(URL_SECOND, secondContent);
121: webConnection.setResponse(URL_THIRD, thirdContent);
122:
123: client.setWebConnection(webConnection);
124:
125: final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
126: assertEquals("First", page.getTitleText());
127:
128: final HtmlInlineFrame iframe = (HtmlInlineFrame) page
129: .getHtmlElementById("iframe1");
130: assertEquals(URL_SECOND.toExternalForm(), iframe
131: .getSrcAttribute());
132: assertEquals("Second", ((HtmlPage) iframe.getEnclosedPage())
133: .getTitleText());
134:
135: iframe.setSrcAttribute(URL_THIRD.toExternalForm());
136: assertEquals(URL_THIRD.toExternalForm(), iframe
137: .getSrcAttribute());
138: assertEquals("Third", ((HtmlPage) iframe.getEnclosedPage())
139: .getTitleText());
140: }
141:
142: /**
143: * Tests that a recursive src attribute (i.e. src="#xyz") doesn't result in an
144: * infinite loop (bug 1699125).
145: *
146: * @throws Exception if an error occurs
147: */
148: public void testRecursiveSrcAttribute() throws Exception {
149: final String html = "<html><body><iframe id='a' src='#abc'></body></html>";
150: final HtmlPage page = loadPage(html);
151: final HtmlInlineFrame iframe = (HtmlInlineFrame) page
152: .getHtmlElementById("a");
153: assertNotNull(iframe.getEnclosedPage());
154: }
155:
156: /**
157: * Tests that a recursive src is prevented.
158: * @throws Exception if an error occurs
159: */
160: public void testRecursiveNestedFrames() throws Exception {
161: final String firstContent = "<html><head><title>First</title></head><body>\n"
162: + "<iframe id='iframe1' src='"
163: + URL_SECOND
164: + "'>\n"
165: + "</body></html>";
166: final String secondContent = "<html><head><title>Second</title></head>"
167: + "<body><iframe id='iframe2_1' src='"
168: + URL_FIRST
169: + "'></iframe></body></html>";
170: final WebClient client = new WebClient();
171:
172: final MockWebConnection webConnection = new MockWebConnection(
173: client);
174: webConnection.setResponse(URL_FIRST, firstContent);
175: webConnection.setResponse(URL_SECOND, secondContent);
176:
177: client.setWebConnection(webConnection);
178:
179: final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
180: assertEquals("First", page.getTitleText());
181:
182: final HtmlInlineFrame iframe = (HtmlInlineFrame) page
183: .getHtmlElementById("iframe1");
184: assertEquals(URL_SECOND.toExternalForm(), iframe
185: .getSrcAttribute());
186: final HtmlPage iframePage = (HtmlPage) iframe.getEnclosedPage();
187: assertEquals("Second", iframePage.getTitleText());
188:
189: // the nested frame should not have been loaded
190: final HtmlInlineFrame iframeIn2 = (HtmlInlineFrame) iframePage
191: .getHtmlElementById("iframe2_1");
192: assertEquals(URL_FIRST.toExternalForm(), iframeIn2
193: .getSrcAttribute());
194: assertEquals("about:blank", iframeIn2.getEnclosedPage()
195: .getWebResponse().getUrl());
196: }
197:
198: /**
199: * Tests that an invalid src attribute (i.e. src="foo://bar") doesn't result
200: * in a NPE (bug 1699119).
201: *
202: * @throws Exception if an error occurs
203: */
204: public void testInvalidSrcAttribute() throws Exception {
205: final String html = "<html><body><iframe id='a' src='foo://bar'></body></html>";
206: final HtmlPage page = loadPage(html);
207: final HtmlInlineFrame iframe = (HtmlInlineFrame) page
208: .getHtmlElementById("a");
209: assertNotNull(iframe.getEnclosedPage());
210: }
211:
212: /**
213: * @throws Exception if the test fails
214: */
215: public void testSetSrcAttribute_ViaJavaScript() throws Exception {
216: final String firstContent = "<html><head><title>First</title></head><body>\n"
217: + "<iframe id='iframe1' src='"
218: + URL_SECOND
219: + "'></iframe>\n"
220: + "<script type='text/javascript'>document.getElementById('iframe1').src = '"
221: + URL_THIRD + "';\n" + "</script></body></html>";
222: final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
223: final String thirdContent = "<html><head><title>Third</title></head><body></body></html>";
224: final WebClient client = new WebClient();
225:
226: final MockWebConnection webConnection = new MockWebConnection(
227: client);
228: webConnection.setResponse(URL_FIRST, firstContent);
229: webConnection.setResponse(URL_SECOND, secondContent);
230: webConnection.setResponse(URL_THIRD, thirdContent);
231:
232: client.setWebConnection(webConnection);
233:
234: final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
235: assertEquals("First", page.getTitleText());
236:
237: final HtmlInlineFrame iframe = (HtmlInlineFrame) page
238: .getHtmlElementById("iframe1");
239: assertEquals(URL_THIRD.toExternalForm(), iframe
240: .getSrcAttribute());
241: assertEquals("Third", ((HtmlPage) iframe.getEnclosedPage())
242: .getTitleText());
243: }
244:
245: /**
246: *
247: * @throws Exception if the test fails
248: */
249: public void testScriptUnderIFrame() throws Exception {
250: final String firstContent = "<html><body>\n" + "<iframe src='"
251: + URL_SECOND + "'>\n"
252: + " <div><script>alert(1);</script></div>\n"
253: + " <script src='" + URL_THIRD + "'></script>\n"
254: + "</iframe>\n" + "</body></html>";
255: final String secondContent = "<html><body><script>alert(2);</script></body></html>";
256: final String thirdContent = "alert('3');";
257: final WebClient client = new WebClient();
258:
259: final MockWebConnection webConnection = new MockWebConnection(
260: client);
261: webConnection.setResponse(URL_FIRST, firstContent);
262: webConnection.setResponse(URL_SECOND, secondContent);
263: webConnection.setResponse(URL_THIRD, thirdContent,
264: "text/javascript");
265:
266: client.setWebConnection(webConnection);
267:
268: final String[] expectedAlerts = { "2" };
269: final List collectedAlerts = new ArrayList();
270: client.setAlertHandler(new CollectingAlertHandler(
271: collectedAlerts));
272:
273: client.getPage(URL_FIRST);
274: assertEquals(expectedAlerts, collectedAlerts);
275: }
276:
277: }
|