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: import java.util.Map;
044:
045: import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
046: import com.gargoylesoftware.htmlunit.MockWebConnection;
047: import com.gargoylesoftware.htmlunit.WebClient;
048: import com.gargoylesoftware.htmlunit.WebTestCase;
049: import com.gargoylesoftware.htmlunit.WebWindow;
050:
051: /**
052: * Tests for {@link HtmlFrameSet}.
053: *
054: * @version $Revision: 2132 $
055: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
056: * @author Marc Guillemot
057: * @author Hans Donner
058: * @author Ahmed Ashour
059: */
060: public class HtmlFrameSetTest extends WebTestCase {
061:
062: /**
063: * Create an instance
064: *
065: * @param name Name of the test
066: */
067: public HtmlFrameSetTest(final String name) {
068: super (name);
069: }
070:
071: /**
072: * @throws Exception if the test fails
073: */
074: public void testLoadingFrameSet() throws Exception {
075: final String firstContent = "<html><head><title>First</title></head>\n"
076: + "<frameset cols='130,*'>\n"
077: + " <frame scrolling='no' name='left' src='"
078: + URL_SECOND
079: + "' frameborder='1' />\n"
080: + " <frame scrolling='auto' name='right' src='"
081: + URL_THIRD
082: + "' frameborder='1' />\n"
083: + " <noframes>\n"
084: + " <body>Frames not supported</body>\n"
085: + " </noframes>\n" + "</frameset>\n" + "</html>";
086: final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
087: final String thirdContent = "<html><head><title>Third</title></head><body></body></html>";
088:
089: final WebClient webClient = new WebClient();
090:
091: final MockWebConnection webConnection = new MockWebConnection(
092: webClient);
093: webConnection.setResponse(URL_FIRST, firstContent);
094: webConnection.setResponse(URL_SECOND, secondContent);
095: webConnection.setResponse(URL_THIRD, thirdContent);
096:
097: webClient.setWebConnection(webConnection);
098:
099: final HtmlPage firstPage = (HtmlPage) webClient
100: .getPage(URL_FIRST);
101: assertEquals("First", firstPage.getTitleText());
102:
103: final WebWindow secondWebWindow = webClient
104: .getWebWindowByName("left");
105: assertSame(firstPage, ((FrameWindow) secondWebWindow)
106: .getEnclosingPage());
107: assertEquals("Second", ((HtmlPage) secondWebWindow
108: .getEnclosedPage()).getTitleText());
109:
110: final WebWindow thirdWebWindow = webClient
111: .getWebWindowByName("right");
112: assertInstanceOf(thirdWebWindow, FrameWindow.class);
113: assertSame(firstPage, ((FrameWindow) thirdWebWindow)
114: .getEnclosingPage());
115: assertEquals("Third", ((HtmlPage) thirdWebWindow
116: .getEnclosedPage()).getTitleText());
117: }
118:
119: /**
120: * @throws Exception if the test fails
121: */
122: public void testLoadingIFrames() throws Exception {
123: final String firstContent = "<html><head><title>First</title></head>\n"
124: + "<body>\n"
125: + " <iframe name='left' src='"
126: + URL_SECOND + "' />\n" + " some stuff" + "</html>";
127: final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
128:
129: final WebClient webClient = new WebClient();
130:
131: final MockWebConnection webConnection = new MockWebConnection(
132: webClient);
133: webConnection.setResponse(URL_FIRST, firstContent);
134: webConnection.setResponse(URL_SECOND, secondContent);
135:
136: webClient.setWebConnection(webConnection);
137:
138: final HtmlPage firstPage = (HtmlPage) webClient
139: .getPage(URL_FIRST);
140: assertEquals("First", firstPage.getTitleText());
141:
142: final WebWindow secondWebWindow = webClient
143: .getWebWindowByName("left");
144: assertInstanceOf(secondWebWindow, FrameWindow.class);
145: assertSame(firstPage, ((FrameWindow) secondWebWindow)
146: .getEnclosingPage());
147: assertEquals("Second", ((HtmlPage) secondWebWindow
148: .getEnclosedPage()).getTitleText());
149: }
150:
151: /**
152: * <a href="http://sourceforge.net/tracker/index.php?func=detail&aid=1101525&group_id=47038&atid=448266">
153: * Bug report 1101525 </a>
154: *
155: * @throws Exception if the test fails
156: */
157: public void testLoadingFrameSetWithRelativePaths() throws Exception {
158: final String framesContent = "<html><head><title>Frames</title></head>\n"
159: + "<frameset rows='110,*'>\n"
160: + " <frame src='subdir1/menu.html' name='menu' scrolling='no' border='0' noresize>\n"
161: + " <frame src='subdir2/first.html' name='test' border='0' auto>\n"
162: + "</frameset>\n"
163: + "<noframes>\n"
164: + " <body>Frames not supported</body>\n"
165: + "</noframes>\n" + "</html>";
166: final String menuContent = "<html><head><title>Menu</title></head>\n"
167: + "<body>\n"
168: + " <script language='javascript'>\n"
169: + " function changeEditPage() {parent.test.location='../second.html';}\n"
170: + " </script>\n"
171: + " <a name ='changePage' onClick='javascript:changeEditPage();' href='#'>Click</a>."
172: + "</body>\n" + "</html>";
173: final String firstContent = "<html><head><title>First</title></head>\n"
174: + "<body>First/body>\n" + "</html>";
175: final String secondContent = "<html><head><title>Second</title></head>\n"
176: + "<body>Second</body>\n" + "</html>";
177: final String baseUrl = "http://framestest";
178:
179: final URL framesURL = new URL(baseUrl + "/frames.html");
180: final URL menuURL = new URL(baseUrl + "/subdir1/menu.html");
181: final URL firstURL = new URL(baseUrl + "/subdir2/first.html");
182: final URL secondURL = new URL(baseUrl + "/second.html");
183:
184: final WebClient webClient = new WebClient();
185:
186: final MockWebConnection webConnection = new MockWebConnection(
187: webClient);
188: webConnection.setResponse(framesURL, framesContent);
189: webConnection.setResponse(menuURL, menuContent);
190: webConnection.setResponse(firstURL, firstContent);
191: webConnection.setResponse(secondURL, secondContent);
192:
193: webClient.setWebConnection(webConnection);
194:
195: final HtmlPage framesPage = (HtmlPage) webClient
196: .getPage(framesURL);
197: assertEquals("Frames", framesPage.getTitleText());
198:
199: final WebWindow menuWebWindow = webClient
200: .getWebWindowByName("menu");
201: final HtmlPage menuPage = (HtmlPage) menuWebWindow
202: .getEnclosedPage();
203: assertEquals("Menu", menuPage.getTitleText());
204:
205: final WebWindow testWebWindow = webClient
206: .getWebWindowByName("test");
207: assertEquals("First", ((HtmlPage) testWebWindow
208: .getEnclosedPage()).getTitleText());
209:
210: final HtmlAnchor changePage = menuPage
211: .getAnchorByName("changePage");
212: changePage.click();
213: assertEquals("Second", ((HtmlPage) testWebWindow
214: .getEnclosedPage()).getTitleText());
215: }
216:
217: /**
218: * Forward referencing issue in FrameSet.
219: * Test for bug 1239285
220: * https://sourceforge.net/tracker/index.php?func=detail&aid=1239285&group_id=47038&atid=448266
221: * @throws Exception if the test fails
222: */
223: public void testFrameOnloadAccessOtherFrame() throws Exception {
224:
225: final String framesContent = "<html><head><title>Main</title>\n"
226: + "</head>\n"
227: + " <frameset cols='18%,*'>\n"
228: + " <frame name='menu' src='"
229: + URL_SECOND
230: + "'>\n"
231: + " <frame name='button_pallete' src='about:blank'>\n"
232: + " </frameset>\n" + "</html>";
233:
234: final String menuContent = "<html><head><title>Menu</title>\n"
235: + " <script>\n"
236: + " function init()\n"
237: + " {\n"
238: + " var oFrame = top.button_pallete;\n"
239: + " alert((oFrame == null) ? 'Failure' : 'Success');\n"
240: + " }\n" + " </script>\n" + "</head>\n"
241: + "<body onload='init()'></body></html>";
242:
243: final WebClient webClient = new WebClient();
244:
245: final MockWebConnection webConnection = new MockWebConnection(
246: webClient);
247:
248: final List collectedAlerts = new ArrayList();
249: final String[] expectedAlerts = { "Success" };
250: webClient.setAlertHandler(new CollectingAlertHandler(
251: collectedAlerts));
252:
253: webConnection.setResponse(URL_FIRST, framesContent);
254: webConnection.setResponse(URL_SECOND, menuContent);
255:
256: webClient.setWebConnection(webConnection);
257:
258: final HtmlPage framesPage = (HtmlPage) webClient
259: .getPage(URL_FIRST);
260: assertEquals("Main", framesPage.getTitleText());
261:
262: assertEquals(expectedAlerts, collectedAlerts);
263: }
264:
265: /**
266: * @throws Exception if the test fails
267: */
268: public void testRefererHeader() throws Exception {
269:
270: final String firstContent = "<html><head><title>First</title></head>\n"
271: + "<frameset cols='130,*'>\n"
272: + " <frame scrolling='no' name='left' src='"
273: + URL_SECOND
274: + "' frameborder='1' />\n"
275: + " <noframes>\n"
276: + " <body>Frames not supported</body>\n"
277: + " </noframes>\n" + "</frameset>\n" + "</html>";
278: final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
279:
280: final WebClient webClient = new WebClient();
281:
282: final MockWebConnection webConnection = new MockWebConnection(
283: webClient);
284: webConnection.setResponse(URL_FIRST, firstContent);
285: webConnection.setResponse(URL_SECOND, secondContent);
286:
287: webClient.setWebConnection(webConnection);
288:
289: final HtmlPage firstPage = (HtmlPage) webClient
290: .getPage(URL_FIRST);
291: assertEquals("First", firstPage.getTitleText());
292:
293: final Map lastAdditionalHeaders = webConnection
294: .getLastAdditionalHeaders();
295: assertEquals(URL_FIRST.toString(), lastAdditionalHeaders
296: .get("Referer"));
297: }
298:
299: /**
300: * @throws Exception if the test fails
301: */
302: public void testScriptUnderNoFrames() throws Exception {
303: final String firstContent = "<html><head><title>first</title></head>\n"
304: + "<frameset cols='100%'>\n"
305: + " <frame src='"
306: + URL_SECOND
307: + "'' id='frame1'/>\n"
308: + " <noframes>\n"
309: + " <div><script>alert(1);</script></div>\n"
310: + " <script src='"
311: + URL_THIRD
312: + "'></script>\n"
313: + " </noframes>\n" + "</frameset></html>";
314: final String secondContent = "<html><body><script>alert(2);</script></body></html>";
315: final String thirdContent = "alert('3');\n";
316: final WebClient client = new WebClient();
317:
318: final MockWebConnection webConnection = new MockWebConnection(
319: client);
320: webConnection.setResponse(URL_FIRST, firstContent);
321: webConnection.setResponse(URL_SECOND, secondContent);
322: webConnection.setResponse(URL_THIRD, thirdContent,
323: "text/javascript");
324:
325: client.setWebConnection(webConnection);
326:
327: final String[] expectedAlerts = { "2" };
328: final ArrayList collectedAlerts = new ArrayList();
329: client.setAlertHandler(new CollectingAlertHandler(
330: collectedAlerts));
331:
332: client.getPage(URL_FIRST);
333: assertEquals(expectedAlerts, collectedAlerts);
334: }
335: }
|