~testplan-team/testplan/source-collection

« back to all changes in this revision

Viewing changes to htmlunit-2.4/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlInlineFrameTest.java

  • Committer: edA-qa mort-ora-y
  • Date: 2010-04-07 10:54:57 UTC
  • Revision ID: eda-qa@disemia.com-20100407105457-g46bvbsrjqtjujab
updating hmltunit src

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (c) 2002-2008 Gargoyle Software Inc.
3
 
 *
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
8
 
 *
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.
14
 
 */
15
 
package com.gargoylesoftware.htmlunit.html;
16
 
 
17
 
import static org.junit.Assert.assertNotNull;
18
 
 
19
 
import java.util.ArrayList;
20
 
import java.util.List;
21
 
 
22
 
import org.junit.Test;
23
 
 
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;
29
 
 
30
 
/**
31
 
 * Unit tests for {@link HtmlInlineFrame}.
32
 
 *
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
38
 
 */
39
 
public class HtmlInlineFrameTest extends WebTestCase {
40
 
 
41
 
    /**
42
 
     * @throws Exception if the test fails
43
 
     */
44
 
    @Test
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"
49
 
            + "</body></html>";
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();
53
 
 
54
 
        final MockWebConnection webConnection = new MockWebConnection();
55
 
        webConnection.setResponse(URL_FIRST, firstContent);
56
 
        webConnection.setResponse(URL_SECOND, secondContent);
57
 
        webConnection.setResponse(URL_THIRD, thirdContent);
58
 
 
59
 
        client.setWebConnection(webConnection);
60
 
 
61
 
        final HtmlPage page = client.getPage(URL_FIRST);
62
 
        assertEquals("First", page.getTitleText());
63
 
 
64
 
        final HtmlInlineFrame iframe = page.getHtmlElementById("iframe1");
65
 
        assertEquals(URL_SECOND.toExternalForm(), iframe.getSrcAttribute());
66
 
        assertEquals("Second", ((HtmlPage) iframe.getEnclosedPage()).getTitleText());
67
 
 
68
 
        iframe.setSrcAttribute(URL_THIRD.toExternalForm());
69
 
        assertEquals(URL_THIRD.toExternalForm(), iframe.getSrcAttribute());
70
 
        assertEquals("Third", ((HtmlPage) iframe.getEnclosedPage()).getTitleText());
71
 
    }
72
 
 
73
 
    /**
74
 
     * @throws Exception if the test fails
75
 
     */
76
 
    @Test
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"
81
 
            + "</body></html>";
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();
85
 
 
86
 
        final MockWebConnection webConnection = new MockWebConnection();
87
 
        webConnection.setResponse(URL_FIRST, firstContent);
88
 
        webConnection.setResponse(URL_SECOND, secondContent);
89
 
        webConnection.setResponse(URL_THIRD, thirdContent);
90
 
 
91
 
        client.setWebConnection(webConnection);
92
 
 
93
 
        final HtmlPage page = client.getPage(URL_FIRST);
94
 
        assertEquals("First", page.getTitleText());
95
 
 
96
 
        final HtmlInlineFrame iframe = page.getHtmlElementById("iframe1");
97
 
        assertEquals(URL_SECOND.toExternalForm(), iframe.getSrcAttribute());
98
 
        assertEquals("Second", ((HtmlPage) iframe.getEnclosedPage()).getTitleText());
99
 
 
100
 
        iframe.setSrcAttribute(URL_THIRD.toExternalForm());
101
 
        assertEquals(URL_THIRD.toExternalForm(), iframe.getSrcAttribute());
102
 
        assertEquals("Third", ((HtmlPage) iframe.getEnclosedPage()).getTitleText());
103
 
    }
104
 
 
105
 
    /**
106
 
     * Tests that a recursive src attribute (i.e. src="#xyz") doesn't result in an
107
 
     * infinite loop (bug 1699125).
108
 
     *
109
 
     * @throws Exception if an error occurs
110
 
     */
111
 
    @Test
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());
117
 
    }
118
 
 
119
 
    /**
120
 
     * Tests that a recursive src is prevented.
121
 
     * @throws Exception if an error occurs
122
 
     */
123
 
    @Test
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"
128
 
            + "</body></html>";
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();
132
 
 
133
 
        final MockWebConnection webConnection = new MockWebConnection();
134
 
        webConnection.setResponse(URL_FIRST, firstContent);
135
 
        webConnection.setResponse(URL_SECOND, secondContent);
136
 
 
137
 
        client.setWebConnection(webConnection);
138
 
 
139
 
        final HtmlPage page = client.getPage(URL_FIRST);
140
 
        assertEquals("First", page.getTitleText());
141
 
 
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());
146
 
 
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());
151
 
    }
152
 
 
153
 
    /**
154
 
     * Tests that an invalid src attribute (i.e. src="foo://bar") doesn't result
155
 
     * in a NPE (bug 1699119).
156
 
     *
157
 
     * @throws Exception if an error occurs
158
 
     */
159
 
    @Test
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());
165
 
    }
166
 
 
167
 
    /**
168
 
     * @throws Exception if the test fails
169
 
     */
170
 
    @Test
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();
180
 
 
181
 
        final MockWebConnection webConnection = new MockWebConnection();
182
 
        webConnection.setResponse(URL_FIRST, firstContent);
183
 
        webConnection.setResponse(URL_SECOND, secondContent);
184
 
        webConnection.setResponse(URL_THIRD, thirdContent);
185
 
 
186
 
        client.setWebConnection(webConnection);
187
 
 
188
 
        final HtmlPage page = client.getPage(URL_FIRST);
189
 
        assertEquals("First", page.getTitleText());
190
 
 
191
 
        final HtmlInlineFrame iframe = page.getHtmlElementById("iframe1");
192
 
        assertEquals(URL_THIRD.toExternalForm(), iframe.getSrcAttribute());
193
 
        assertEquals("Third", ((HtmlPage) iframe.getEnclosedPage()).getTitleText());
194
 
    }
195
 
 
196
 
    /**
197
 
     * @throws Exception if the test fails
198
 
     */
199
 
    @Test
200
 
    public void testScriptUnderIFrame() throws Exception {
201
 
        final String firstContent
202
 
            = "<html><body>\n"
203
 
            + "<iframe src='" + URL_SECOND + "'>\n"
204
 
            + "  <div><script>alert(1);</script></div>\n"
205
 
            + "  <script src='" + URL_THIRD + "'></script>\n"
206
 
            + "</iframe>\n"
207
 
            + "</body></html>";
208
 
        final String secondContent
209
 
            = "<html><body><script>alert(2);</script></body></html>";
210
 
        final String thirdContent
211
 
            = "alert('3');";
212
 
        final WebClient client = new WebClient();
213
 
 
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");
218
 
 
219
 
        client.setWebConnection(webConnection);
220
 
 
221
 
        final String[] expectedAlerts = {"2"};
222
 
        final List<String> collectedAlerts = new ArrayList<String>();
223
 
        client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
224
 
 
225
 
        client.getPage(URL_FIRST);
226
 
        assertEquals(expectedAlerts, collectedAlerts);
227
 
    }
228
 
 
229
 
    /**
230
 
     * @throws Exception if the test fails
231
 
     */
232
 
    @Test
233
 
    public void testSimpleScriptable() throws Exception {
234
 
        final String html = "<html><head>\n"
235
 
            + "<script>\n"
236
 
            + "  function test() {\n"
237
 
            + "    alert(document.getElementById('myId'));\n"
238
 
            + "  }\n"
239
 
            + "</script>\n"
240
 
            + "</head><body onload='test()'>\n"
241
 
            + "  <iframe id='myId'>\n"
242
 
            + "</body></html>";
243
 
 
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);
249
 
    }
250
 
 
251
 
    /**
252
 
     * Verifies that cloned frames do no reload their content (bug 1954869).
253
 
     * @throws Exception if an error occurs
254
 
     */
255
 
    @Test
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>";
259
 
 
260
 
        final WebClient client = new WebClient();
261
 
 
262
 
        final MockWebConnection conn = new MockWebConnection();
263
 
        conn.setResponse(URL_FIRST, html1);
264
 
        conn.setResponse(URL_SECOND, html2);
265
 
        client.setWebConnection(conn);
266
 
 
267
 
        final HtmlPage page = client.getPage(URL_FIRST);
268
 
        assertEquals(2, conn.getRequestCount());
269
 
 
270
 
        page.cloneNode(true);
271
 
        assertEquals(2, conn.getRequestCount());
272
 
    }
273
 
 
274
 
    /**
275
 
     * Verifies that frames added via document.write() don't get their contents loaded twice (bug 1156009).
276
 
     * @throws Exception if an error occurs
277
 
     */
278
 
    @Test
279
 
    public void testFrameWriteDoesNotReloadFrame() throws Exception {
280
 
        final String html1 =
281
 
              "<html><body>\n"
282
 
            + "<script>document.write('<iframe id=\"f\" src=\"" + URL_SECOND + "\"></iframe>')</script>\n"
283
 
            + "</body></html>";
284
 
        final String html2 = "<html><body>abc</body></html>";
285
 
 
286
 
        final WebClient client = new WebClient();
287
 
 
288
 
        final MockWebConnection conn = new MockWebConnection();
289
 
        conn.setResponse(URL_FIRST, html1);
290
 
        conn.setResponse(URL_SECOND, html2);
291
 
        client.setWebConnection(conn);
292
 
 
293
 
        final HtmlPage page = client.getPage(URL_FIRST);
294
 
        assertEquals("iframe", page.getElementById("f").getTagName());
295
 
 
296
 
        page.getEnclosingWindow().getThreadManager().joinAll(3000);
297
 
        assertEquals(2, conn.getRequestCount());
298
 
    }
299
 
 
300
 
}