2
* Copyright (c) 2002-2008 Gargoyle Software Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
* http://www.apache.org/licenses/LICENSE-2.0
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
* See the License for the specific language governing permissions and
13
* limitations under the License.
15
package com.gargoylesoftware.htmlunit.html;
17
import static org.junit.Assert.assertNotNull;
19
import java.util.ArrayList;
20
import java.util.List;
22
import org.junit.Test;
24
import com.gargoylesoftware.htmlunit.BrowserVersion;
25
import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
26
import com.gargoylesoftware.htmlunit.MockWebConnection;
27
import com.gargoylesoftware.htmlunit.WebClient;
28
import com.gargoylesoftware.htmlunit.WebTestCase;
31
* Unit tests for {@link HtmlInlineFrame}.
33
* @version $Revision: 3519 $
34
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
35
* @author Ahmed Ashour
36
* @author Marc Guillemot
37
* @author Daniel Gredler
39
public class HtmlInlineFrameTest extends WebTestCase {
42
* @throws Exception if the test fails
45
public void testSetSrcAttribute() throws Exception {
46
final String firstContent
47
= "<html><head><title>First</title></head><body>\n"
48
+ "<iframe id='iframe1' src='" + URL_SECOND + "'>\n"
50
final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
51
final String thirdContent = "<html><head><title>Third</title></head><body></body></html>";
52
final WebClient client = new WebClient();
54
final MockWebConnection webConnection = new MockWebConnection();
55
webConnection.setResponse(URL_FIRST, firstContent);
56
webConnection.setResponse(URL_SECOND, secondContent);
57
webConnection.setResponse(URL_THIRD, thirdContent);
59
client.setWebConnection(webConnection);
61
final HtmlPage page = client.getPage(URL_FIRST);
62
assertEquals("First", page.getTitleText());
64
final HtmlInlineFrame iframe = page.getHtmlElementById("iframe1");
65
assertEquals(URL_SECOND.toExternalForm(), iframe.getSrcAttribute());
66
assertEquals("Second", ((HtmlPage) iframe.getEnclosedPage()).getTitleText());
68
iframe.setSrcAttribute(URL_THIRD.toExternalForm());
69
assertEquals(URL_THIRD.toExternalForm(), iframe.getSrcAttribute());
70
assertEquals("Third", ((HtmlPage) iframe.getEnclosedPage()).getTitleText());
74
* @throws Exception if the test fails
77
public void testSetSrcAttributeWithWhiteSpaces() throws Exception {
78
final String firstContent
79
= "<html><head><title>First</title></head><body>\n"
80
+ "<iframe id='iframe1' src='\n" + URL_SECOND + "\n'>\n"
82
final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
83
final String thirdContent = "<html><head><title>Third</title></head><body></body></html>";
84
final WebClient client = new WebClient();
86
final MockWebConnection webConnection = new MockWebConnection();
87
webConnection.setResponse(URL_FIRST, firstContent);
88
webConnection.setResponse(URL_SECOND, secondContent);
89
webConnection.setResponse(URL_THIRD, thirdContent);
91
client.setWebConnection(webConnection);
93
final HtmlPage page = client.getPage(URL_FIRST);
94
assertEquals("First", page.getTitleText());
96
final HtmlInlineFrame iframe = page.getHtmlElementById("iframe1");
97
assertEquals(URL_SECOND.toExternalForm(), iframe.getSrcAttribute());
98
assertEquals("Second", ((HtmlPage) iframe.getEnclosedPage()).getTitleText());
100
iframe.setSrcAttribute(URL_THIRD.toExternalForm());
101
assertEquals(URL_THIRD.toExternalForm(), iframe.getSrcAttribute());
102
assertEquals("Third", ((HtmlPage) iframe.getEnclosedPage()).getTitleText());
106
* Tests that a recursive src attribute (i.e. src="#xyz") doesn't result in an
107
* infinite loop (bug 1699125).
109
* @throws Exception if an error occurs
112
public void testRecursiveSrcAttribute() throws Exception {
113
final String html = "<html><body><iframe id='a' src='#abc'></body></html>";
114
final HtmlPage page = loadPage(html);
115
final HtmlInlineFrame iframe = page.getHtmlElementById("a");
116
assertNotNull(iframe.getEnclosedPage());
120
* Tests that a recursive src is prevented.
121
* @throws Exception if an error occurs
124
public void testRecursiveNestedFrames() throws Exception {
125
final String firstContent
126
= "<html><head><title>First</title></head><body>\n"
127
+ "<iframe id='iframe1' src='" + URL_SECOND + "'>\n"
129
final String secondContent = "<html><head><title>Second</title></head>\n"
130
+ "<body><iframe id='iframe2_1' src='" + URL_FIRST + "'></iframe></body></html>";
131
final WebClient client = new WebClient();
133
final MockWebConnection webConnection = new MockWebConnection();
134
webConnection.setResponse(URL_FIRST, firstContent);
135
webConnection.setResponse(URL_SECOND, secondContent);
137
client.setWebConnection(webConnection);
139
final HtmlPage page = client.getPage(URL_FIRST);
140
assertEquals("First", page.getTitleText());
142
final HtmlInlineFrame iframe = page.getHtmlElementById("iframe1");
143
assertEquals(URL_SECOND.toExternalForm(), iframe.getSrcAttribute());
144
final HtmlPage iframePage = (HtmlPage) iframe.getEnclosedPage();
145
assertEquals("Second", iframePage.getTitleText());
147
// the nested frame should not have been loaded
148
final HtmlInlineFrame iframeIn2 = iframePage.getHtmlElementById("iframe2_1");
149
assertEquals(URL_FIRST.toExternalForm(), iframeIn2.getSrcAttribute());
150
assertEquals("about:blank", iframeIn2.getEnclosedPage().getWebResponse().getRequestUrl());
154
* Tests that an invalid src attribute (i.e. src="foo://bar") doesn't result
155
* in a NPE (bug 1699119).
157
* @throws Exception if an error occurs
160
public void testInvalidSrcAttribute() throws Exception {
161
final String html = "<html><body><iframe id='a' src='foo://bar'></body></html>";
162
final HtmlPage page = loadPage(html);
163
final HtmlInlineFrame iframe = page.getHtmlElementById("a");
164
assertNotNull(iframe.getEnclosedPage());
168
* @throws Exception if the test fails
171
public void testSetSrcAttribute_ViaJavaScript() throws Exception {
172
final String firstContent
173
= "<html><head><title>First</title></head><body>\n"
174
+ "<iframe id='iframe1' src='" + URL_SECOND + "'></iframe>\n"
175
+ "<script type='text/javascript'>document.getElementById('iframe1').src = '" + URL_THIRD + "';\n"
176
+ "</script></body></html>";
177
final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
178
final String thirdContent = "<html><head><title>Third</title></head><body></body></html>";
179
final WebClient client = new WebClient();
181
final MockWebConnection webConnection = new MockWebConnection();
182
webConnection.setResponse(URL_FIRST, firstContent);
183
webConnection.setResponse(URL_SECOND, secondContent);
184
webConnection.setResponse(URL_THIRD, thirdContent);
186
client.setWebConnection(webConnection);
188
final HtmlPage page = client.getPage(URL_FIRST);
189
assertEquals("First", page.getTitleText());
191
final HtmlInlineFrame iframe = page.getHtmlElementById("iframe1");
192
assertEquals(URL_THIRD.toExternalForm(), iframe.getSrcAttribute());
193
assertEquals("Third", ((HtmlPage) iframe.getEnclosedPage()).getTitleText());
197
* @throws Exception if the test fails
200
public void testScriptUnderIFrame() throws Exception {
201
final String firstContent
203
+ "<iframe src='" + URL_SECOND + "'>\n"
204
+ " <div><script>alert(1);</script></div>\n"
205
+ " <script src='" + URL_THIRD + "'></script>\n"
208
final String secondContent
209
= "<html><body><script>alert(2);</script></body></html>";
210
final String thirdContent
212
final WebClient client = new WebClient();
214
final MockWebConnection webConnection = new MockWebConnection();
215
webConnection.setResponse(URL_FIRST, firstContent);
216
webConnection.setResponse(URL_SECOND, secondContent);
217
webConnection.setResponse(URL_THIRD, thirdContent, "text/javascript");
219
client.setWebConnection(webConnection);
221
final String[] expectedAlerts = {"2"};
222
final List<String> collectedAlerts = new ArrayList<String>();
223
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
225
client.getPage(URL_FIRST);
226
assertEquals(expectedAlerts, collectedAlerts);
230
* @throws Exception if the test fails
233
public void testSimpleScriptable() throws Exception {
234
final String html = "<html><head>\n"
236
+ " function test() {\n"
237
+ " alert(document.getElementById('myId'));\n"
240
+ "</head><body onload='test()'>\n"
241
+ " <iframe id='myId'>\n"
244
final String[] expectedAlerts = {"[object HTMLIFrameElement]"};
245
final List<String> collectedAlerts = new ArrayList<String>();
246
final HtmlPage page = loadPage(BrowserVersion.FIREFOX_2, html, collectedAlerts);
247
assertTrue(HtmlInlineFrame.class.isInstance(page.getHtmlElementById("myId")));
248
assertEquals(expectedAlerts, collectedAlerts);
252
* Verifies that cloned frames do no reload their content (bug 1954869).
253
* @throws Exception if an error occurs
256
public void testFrameCloneDoesNotReloadFrame() throws Exception {
257
final String html1 = "<html><body><iframe src='" + URL_SECOND + "'></iframe></body></html>";
258
final String html2 = "<html><body>abc</body></html>";
260
final WebClient client = new WebClient();
262
final MockWebConnection conn = new MockWebConnection();
263
conn.setResponse(URL_FIRST, html1);
264
conn.setResponse(URL_SECOND, html2);
265
client.setWebConnection(conn);
267
final HtmlPage page = client.getPage(URL_FIRST);
268
assertEquals(2, conn.getRequestCount());
270
page.cloneNode(true);
271
assertEquals(2, conn.getRequestCount());
275
* Verifies that frames added via document.write() don't get their contents loaded twice (bug 1156009).
276
* @throws Exception if an error occurs
279
public void testFrameWriteDoesNotReloadFrame() throws Exception {
282
+ "<script>document.write('<iframe id=\"f\" src=\"" + URL_SECOND + "\"></iframe>')</script>\n"
284
final String html2 = "<html><body>abc</body></html>";
286
final WebClient client = new WebClient();
288
final MockWebConnection conn = new MockWebConnection();
289
conn.setResponse(URL_FIRST, html1);
290
conn.setResponse(URL_SECOND, html2);
291
client.setWebConnection(conn);
293
final HtmlPage page = client.getPage(URL_FIRST);
294
assertEquals("iframe", page.getElementById("f").getTagName());
296
page.getEnclosingWindow().getThreadManager().joinAll(3000);
297
assertEquals(2, conn.getRequestCount());