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.net.URL;
041: import java.util.ArrayList;
042: import java.util.List;
043:
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 HtmlFrame}.
052: *
053: * @version $Revision: 2132 $
054: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
055: * @author Ahmed Ashour
056: */
057: public class HtmlFrameTest extends WebTestCase {
058:
059: /**
060: * Create an instance
061: *
062: * @param name Name of the test
063: */
064: public HtmlFrameTest(final String name) {
065: super (name);
066: }
067:
068: /**
069: * @throws Exception if the test fails
070: */
071: public void testSrcOfBlankAndEmpty() throws Exception {
072: final String firstContent = "<html><head><title>first</title></head>\n"
073: + "<frameset cols='20%,80%'>\n"
074: + " <frame src='' id='frame1'>\n"
075: + " <frame src='about:blank' id='frame2'>\n"
076: + "</frameset></html>";
077: final HtmlPage page = loadPage(firstContent);
078:
079: final HtmlFrame frame1 = (HtmlFrame) page
080: .getHtmlElementById("frame1");
081: assertEquals("frame1", "",
082: ((HtmlPage) frame1.getEnclosedPage()).getTitleText());
083:
084: final HtmlFrame frame2 = (HtmlFrame) page
085: .getHtmlElementById("frame2");
086: assertEquals("frame2", "",
087: ((HtmlPage) frame2.getEnclosedPage()).getTitleText());
088: }
089:
090: /**
091: * @throws Exception if the test fails
092: */
093: public void testOnLoadHandler() throws Exception {
094: final WebClient webClient = new WebClient();
095: final MockWebConnection webConnection = new MockWebConnection(
096: webClient);
097: final List collectedAlerts = new ArrayList();
098:
099: webClient.setAlertHandler(new CollectingAlertHandler(
100: collectedAlerts));
101:
102: final String firstContent = "<html><head><title>first</title></head>\n"
103: + "<frameset cols='20%,80%'>\n"
104: + " <frame id='frame1'>\n"
105: + " <frame onload='alert(this.tagName)' id='frame2'>\n"
106: + "</frameset></html>";
107: final String[] expectedAlerts = { "FRAME" };
108:
109: webConnection.setResponse(URL_FIRST, firstContent);
110: webClient.setWebConnection(webConnection);
111:
112: final HtmlPage page = (HtmlPage) webClient.getPage(URL_FIRST);
113: assertEquals("first", page.getTitleText());
114:
115: final HtmlFrame frame1 = (HtmlFrame) page
116: .getHtmlElementById("frame1");
117: assertEquals("frame1", "",
118: ((HtmlPage) frame1.getEnclosedPage()).getTitleText());
119:
120: final HtmlFrame frame2 = (HtmlFrame) page
121: .getHtmlElementById("frame2");
122: assertEquals("frame2", "",
123: ((HtmlPage) frame2.getEnclosedPage()).getTitleText());
124:
125: assertEquals(expectedAlerts, collectedAlerts);
126: }
127:
128: /**
129: * @throws Exception if the test fails
130: */
131: public void testDocumentWrite() throws Exception {
132: final String firstContent = "<html><head><title>first</title></head>\n"
133: + "<frameset cols='20%,80%'>\n"
134: + " <frame src='' name='frame1' id='frame1'>\n"
135: + " <frame onload=\"frame1.document.open();frame1.document.write("
136: + "'<html><head><title>generated</title></head><body>generated</body></html>');"
137: + "frame1.document.close()\" id='frame2'>\n"
138: + "</frameset></html>";
139: final HtmlPage page = loadPage(firstContent);
140:
141: assertEquals("first", page.getTitleText());
142:
143: final HtmlFrame frame1 = (HtmlFrame) page
144: .getHtmlElementById("frame1");
145: assertEquals("frame1", "generated", ((HtmlPage) frame1
146: .getEnclosedPage()).getTitleText());
147:
148: final HtmlFrame frame2 = (HtmlFrame) page
149: .getHtmlElementById("frame2");
150: assertEquals("frame2", "",
151: ((HtmlPage) frame2.getEnclosedPage()).getTitleText());
152: }
153:
154: /**
155: * Test that frames are correctly deregistered even if not html
156: * @throws Exception if the test fails
157: */
158: public void testDeregisterNonHtmlFrame() throws Exception {
159: final WebClient webClient = new WebClient();
160: final MockWebConnection webConnection = new MockWebConnection(
161: webClient);
162:
163: final String firstContent = "<html><head><title>first</title></head>\n"
164: + "<frameset cols='100%'>\n"
165: + " <frame src='foo.txt'>\n" + "</frameset></html>";
166: webConnection
167: .setDefaultResponse("foo", 200, "OK", "text/plain");
168: webConnection.setResponse(URL_FIRST, firstContent);
169: webClient.setWebConnection(webConnection);
170:
171: final HtmlPage page = (HtmlPage) webClient.getPage(URL_FIRST);
172: assertEquals("first", page.getTitleText());
173:
174: // loads something else to trigger frame de-registration
175: webClient.getPage(URL_SECOND);
176: }
177:
178: /**
179: * @throws Exception if the test fails
180: */
181: public void testFailingHttpStatusCodeException() throws Exception {
182: final String failingContent = "<html><head><body>Not found</body></html>";
183:
184: final String firstContent = "<html><head><title>First</title></head>\n"
185: + "<frameset cols='130,*'>\n"
186: + " <frame scrolling='no' name='left' src='"
187: + "failing_url"
188: + "' frameborder='1' />\n"
189: + " <frame scrolling='auto' name='right' src='"
190: + URL_THIRD
191: + "' frameborder='1' />\n"
192: + " <noframes>\n"
193: + " <body>Frames not supported</body>\n"
194: + " </noframes>\n" + "</frameset>\n" + "</html>";
195:
196: final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
197: final String thirdContent = "<html><head><title>Third</title></head><body></body></html>";
198:
199: final WebClient webClient = new WebClient();
200:
201: final MockWebConnection webConnection = new MockWebConnection(
202: webClient);
203: webConnection.setDefaultResponse(failingContent, 404,
204: "No Found", "text/html");
205: webConnection.setResponse(URL_FIRST, firstContent);
206: webConnection.setResponse(URL_SECOND, secondContent);
207: webConnection.setResponse(URL_THIRD, thirdContent);
208:
209: webClient.setWebConnection(webConnection);
210:
211: try {
212: webClient.getPage(URL_FIRST);
213: fail("Expected FailingHttpStatusCodeException");
214: } catch (final FailingHttpStatusCodeException e) {
215: assertEquals(404, e.getStatusCode());
216: }
217: }
218:
219: /**
220: * Regression test for bug 1518195
221: * http://sourceforge.net/tracker/index.php?func=detail&aid=1518195&group_id=47038&atid=448266
222: * @throws Exception if the test fails
223: */
224: public void testFrameScriptReplaceOtherFrame() throws Exception {
225: final String mainContent = "<html><head><title>frames</title></head>"
226: + "<frameset cols='180,*'>"
227: + "<frame name='f1' src='1.html'/>"
228: + "<frame name='f2' src='2.html'/>"
229: + "</frameset>"
230: + "</html>";
231:
232: final String frame1 = "<html><head><title>1</title></head>"
233: + "<body>1" + "<script>"
234: + " parent.frames['f2'].location.href = '3.html';\n"
235: + "</script>" + "</body></html>";
236:
237: final String frame3 = "<html><head><title>page 3</title></head><body></body></html>";
238:
239: final WebClient webClient = new WebClient();
240: final MockWebConnection conn = new MockWebConnection(webClient);
241: webClient.setWebConnection(conn);
242:
243: conn
244: .setDefaultResponse("<html><head><title>default</title></head><body></body></html>");
245: conn.setResponse(URL_FIRST, mainContent);
246: conn.setResponse(new URL(URL_FIRST, "1.html"), frame1);
247: conn.setResponse(new URL(URL_FIRST, "3.html"), frame3);
248:
249: final HtmlPage page = (HtmlPage) webClient.getPage(URL_FIRST);
250:
251: assertEquals("page 3", ((HtmlPage) page.getFrameByName("f2")
252: .getEnclosedPage()).getTitleText());
253: }
254: }
|