KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > html > HtmlFrameSetTest


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.html;
39
40 import java.net.URL;
41
42 import com.gargoylesoftware.htmlunit.MockWebConnection;
43 import com.gargoylesoftware.htmlunit.WebClient;
44 import com.gargoylesoftware.htmlunit.WebTestCase;
45 import com.gargoylesoftware.htmlunit.WebWindow;
46
47 /**
48  * Tests for HtmlFrameSet
49  *
50  * @version $Revision: 1.12 $
51  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
52  * @author Marc Guillemot
53  * @author Hans Donner
54  */

55 public class HtmlFrameSetTest extends WebTestCase {
56
57     /**
58      * Create an instance
59      *
60      * @param name Name of the test
61      */

62     public HtmlFrameSetTest( final String name ) {
63         super( name );
64     }
65
66
67     /**
68      * @throws Exception if the test fails
69      */

70     public void testLoadingFrameSet()
71         throws Exception {
72
73         final String firstContent
74             = "<html><head><title>First</title></head>"
75             + "<frameset cols='130,*'>"
76             + " <frame scrolling='no' name='left' SRC='http://second' frameborder='1' />"
77             + " <frame scrolling='auto' name='right' SRC='http://third' frameborder='1' />"
78             + " <noframes>"
79             + " <body>Frames not supported</body>"
80             + " </noframes>"
81             + "</frameset>"
82             + "</html>";
83         final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
84         final String thirdContent = "<html><head><title>Third</title></head><body></body></html>";
85
86         final WebClient webClient = new WebClient();
87
88         final MockWebConnection webConnection = new MockWebConnection( webClient );
89         webConnection.setResponse(URL_FIRST, firstContent);
90         webConnection.setResponse(URL_SECOND, secondContent);
91         webConnection.setResponse(URL_THIRD, thirdContent);
92
93         webClient.setWebConnection( webConnection );
94
95         final HtmlPage firstPage = (HtmlPage) webClient.getPage(URL_FIRST);
96         assertEquals( "First", firstPage.getTitleText() );
97
98         final WebWindow secondWebWindow = webClient.getWebWindowByName("left");
99         assertSame( firstPage, ((BaseFrame.FrameWindow) secondWebWindow).getEnclosingPage() );
100         assertEquals( "Second", ((HtmlPage) secondWebWindow.getEnclosedPage()).getTitleText() );
101
102         final WebWindow thirdWebWindow = webClient.getWebWindowByName("right");
103         assertInstanceOf( thirdWebWindow, BaseFrame.FrameWindow.class );
104         assertSame( firstPage, ((BaseFrame.FrameWindow) thirdWebWindow).getEnclosingPage() );
105         assertEquals( "Third", ((HtmlPage)thirdWebWindow.getEnclosedPage()).getTitleText() );
106     }
107
108
109     /**
110      * @throws Exception if the test fails
111      */

112     public void testLoadingIFrames()
113         throws Exception {
114
115         final String firstContent
116             = "<html><head><title>First</title></head>"
117             + "<body>"
118             + " <iframe name='left' SRC='http://second' />"
119             + " some stuff"
120             + "</html>";
121         final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
122
123         final WebClient webClient = new WebClient();
124
125         final MockWebConnection webConnection = new MockWebConnection( webClient );
126         webConnection.setResponse(URL_FIRST, firstContent);
127         webConnection.setResponse(URL_SECOND, secondContent);
128
129         webClient.setWebConnection( webConnection );
130
131         final HtmlPage firstPage = (HtmlPage) webClient.getPage(URL_FIRST);
132         assertEquals( "First", firstPage.getTitleText() );
133
134         final WebWindow secondWebWindow = webClient.getWebWindowByName("left");
135         assertInstanceOf(secondWebWindow, BaseFrame.FrameWindow.class);
136         assertSame( firstPage, ((BaseFrame.FrameWindow) secondWebWindow).getEnclosingPage() );
137         assertEquals( "Second", ((HtmlPage)secondWebWindow.getEnclosedPage()).getTitleText() );
138     }
139
140     /**
141      * <a HREF="http://sourceforge.net/tracker/index.php?func=detail&aid=1101525&group_id=47038&atid=448266">
142      * Bug report 1101525 </a>
143      *
144      * @throws Exception if the test fails
145      */

146     public void testLoadingFrameSetWithRelativePaths()
147         throws Exception {
148
149         final String framesContent
150             = "<html><head><title>Frames</title></head>"
151             + "<frameset rows='110,*'>"
152             + " <frame SRC='subdir1/menu.html' name='menu' scrolling='no' border='0' noresize>"
153             + " <frame SRC='subdir2/first.html' name='test' border='0' auto>"
154             + "</frameset>"
155             + "<noframes>"
156             + " <body>Frames not supported</body>"
157             + "</noframes>"
158             + "</html>";
159         final String menuContent
160             = "<html><head><title>Menu</title></head>"
161             + "<body>"
162             + " <script language='javascript'>"
163             + " function changeEditPage() {parent.test.location='../second.html';}"
164             + " </script>"
165             + " <a name ='changePage' onClick='javascript:changeEditPage();' HREF='#'>Click</a>."
166             + "</body>"
167             + "</html>";
168         final String firstContent
169             = "<html><head><title>First</title></head>"
170             + "<body>First/body>"
171             + "</html>";
172         final String secondContent
173             = "<html><head><title>Second</title></head>"
174             + "<body>Second</body>"
175             + "</html>";
176         final String baseUrl = "http://framestest";
177         
178         final URL framesURL = new URL(baseUrl + "/frames.html");
179         final URL menuURL = new URL(baseUrl + "/subdir1/menu.html");
180         final URL firstURL = new URL(baseUrl + "/subdir2/first.html");
181         final URL secondURL = new URL(baseUrl + "/second.html");
182
183         final WebClient webClient = new WebClient();
184
185         final MockWebConnection webConnection = new MockWebConnection( webClient );
186         webConnection.setResponse(framesURL, framesContent);
187         webConnection.setResponse(menuURL, menuContent);
188         webConnection.setResponse(firstURL, firstContent);
189         webConnection.setResponse(secondURL, secondContent);
190
191         webClient.setWebConnection( webConnection );
192
193         final HtmlPage framesPage = (HtmlPage) webClient.getPage(framesURL);
194         assertEquals( "Frames", framesPage.getTitleText() );
195       
196         final WebWindow menuWebWindow = webClient.getWebWindowByName("menu");
197         final HtmlPage menuPage = (HtmlPage) menuWebWindow.getEnclosedPage();
198         assertEquals( "Menu", menuPage.getTitleText() );
199
200         final WebWindow testWebWindow = webClient.getWebWindowByName("test");
201         assertEquals( "First", ((HtmlPage) testWebWindow.getEnclosedPage()).getTitleText() );
202
203         final HtmlAnchor changePage = menuPage.getAnchorByName("changePage");
204         changePage.click();
205         assertEquals( "Second", ((HtmlPage) testWebWindow.getEnclosedPage()).getTitleText() );
206         
207     }
208
209 }
210
Popular Tags