From: <rb...@us...> - 2013-03-21 21:35:20
|
Revision: 8187 http://sourceforge.net/p/htmlunit/code/8187 Author: rbri Date: 2013-03-21 21:35:14 +0000 (Thu, 21 Mar 2013) Log Message: ----------- Removing frame tag removes the associated FrameWindow also. Issue 1497 Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebWindowImpl.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/BaseFrameElement.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/DomNode.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/FrameWindow.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLIFrameElementTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2013-03-21 18:59:07 UTC (rev 8186) +++ trunk/htmlunit/src/changes/changes.xml 2013-03-21 21:35:14 UTC (rev 8187) @@ -8,6 +8,9 @@ <body> <release version="2.13" date="???" description="Bugfixes"> + <action type="fix" dev="rbri" issue="1497"> + JavaScript: Removing frame tag removes the associated FrameWindow also. + </action> <action type="fix" dev="mguillem"> HtmlPage.getElementById: always return the first element in document order (and not the first parsed one). </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebWindowImpl.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebWindowImpl.java 2013-03-21 18:59:07 UTC (rev 8186) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebWindowImpl.java 2013-03-21 21:35:14 UTC (rev 8187) @@ -165,7 +165,10 @@ childWindows_.add(child); } - void destroyChildren() { + /** + * Destroy our childs. + */ + protected void destroyChildren() { if (LOG.isDebugEnabled()) { LOG.debug("destroyChildren"); } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/BaseFrameElement.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/BaseFrameElement.java 2013-03-21 18:59:07 UTC (rev 8186) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/BaseFrameElement.java 2013-03-21 21:35:14 UTC (rev 8187) @@ -408,4 +408,14 @@ ((BaseFrameElement) node).init(); return node; } + + /** + * Remove our window also. + * {@inheritDoc} + */ + @Override + protected void basicRemove() { + super.basicRemove(); + ((FrameWindow) getEnclosedWindow()).close(); + } } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/DomNode.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/DomNode.java 2013-03-21 18:59:07 UTC (rev 8186) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/DomNode.java 2013-03-21 21:35:14 UTC (rev 8187) @@ -1118,7 +1118,7 @@ /** * Cuts off all relationships this node has with siblings and parents. */ - private void basicRemove() { + protected void basicRemove() { if (parent_ != null && parent_.firstChild_ == this) { parent_.firstChild_ = nextSibling_; } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/FrameWindow.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/FrameWindow.java 2013-03-21 18:59:07 UTC (rev 8186) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/FrameWindow.java 2013-03-21 21:35:14 UTC (rev 8187) @@ -131,4 +131,18 @@ public String toString() { return "FrameWindow[name=\"" + getName() + "\"]"; } + + /** + * Closes this frame window. + */ + public void close() { + setClosed(); + final Page page = getEnclosedPage(); + if (page != null) { + page.cleanUp(); + } + getJobManager().shutdown(); + destroyChildren(); + getWebClient().deregisterWebWindow(this); + } } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLIFrameElementTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLIFrameElementTest.java 2013-03-21 18:59:07 UTC (rev 8186) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLIFrameElementTest.java 2013-03-21 21:35:14 UTC (rev 8187) @@ -32,6 +32,8 @@ import com.gargoylesoftware.htmlunit.MockWebConnection; import com.gargoylesoftware.htmlunit.SimpleWebTestCase; import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.html.FrameWindow; +import com.gargoylesoftware.htmlunit.html.HtmlElement; import com.gargoylesoftware.htmlunit.html.HtmlInlineFrame; import com.gargoylesoftware.htmlunit.html.HtmlPage; @@ -121,7 +123,7 @@ + "</body></html>"; final WebClient webClient = getWebClient(); - final MockWebConnection webConnection = new MockWebConnection(); + final MockWebConnection webConnection = getMockWebConnection(); webConnection.setDefaultResponse(frameContent); webConnection.setResponse(URL_FIRST, firstContent); @@ -229,7 +231,7 @@ final String frame2 = "<html><head><script>alert(window.foo);</script></head></html>"; final WebClient webClient = getWebClient(); - final MockWebConnection webConnection = new MockWebConnection(); + final MockWebConnection webConnection = getMockWebConnection(); webConnection.setResponse(URL_FIRST, html); webConnection.setResponse(new URL(URL_FIRST, "1.html"), frame1); @@ -263,7 +265,7 @@ final String thirdContent = "<html><head><title>Third</title></head><body></body></html>"; final WebClient client = getWebClient(); - final MockWebConnection webConnection = new MockWebConnection(); + final MockWebConnection webConnection = getMockWebConnection(); webConnection.setResponse(URL_FIRST, firstContent); webConnection.setResponse(URL_SECOND, secondContent); webConnection.setResponse(URL_THIRD, thirdContent); @@ -313,7 +315,7 @@ final WebClient client = getWebClient(); client.getOptions().setJavaScriptEnabled(false); - final MockWebConnection conn = new MockWebConnection(); + final MockWebConnection conn = getMockWebConnection(); conn.setDefaultResponse(html); client.setWebConnection(conn); @@ -463,7 +465,7 @@ + "<body><script>alert(document.body);</script></html>"; final WebClient webClient = getWebClient(); - final MockWebConnection webConnection = new MockWebConnection(); + final MockWebConnection webConnection = getMockWebConnection(); webConnection.setResponse(URL_FIRST, html); webConnection.setDefaultResponse(frame); @@ -540,7 +542,7 @@ + "</script></body></html>"; final String html2 = "<html><body>foo</body></html>"; - final MockWebConnection conn = new MockWebConnection(); + final MockWebConnection conn = getMockWebConnection(); conn.setResponse(URL_FIRST, html); conn.setResponse(new URL(URL_FIRST, "frame.html"), html2); @@ -594,4 +596,44 @@ assertFalse(((HtmlPage) page.getFrames().get(0).getEnclosedPage()).asText().isEmpty()); } + /** + * @throws Exception if the test fails + */ + @Test + public void testRemoveFrameWindow() throws Exception { + final String index = "<html><head></head><body>" + + "<div id='content'>" + + " <iframe name='content' src='second/'></iframe>" + + "</div>" + + "<button id='clickId' " + + "onClick=\"document.getElementById('content').innerHTML = 'new content';\">Item</button>\n" + + "</body></html>"; + + final String frame1 = "<html><head></head><body>" + + "<p>frame content</p>" + + "</body></html>"; + + final WebClient webClient = getWebClient(); + final MockWebConnection webConnection = getMockWebConnection(); + + webConnection.setResponse(URL_SECOND, frame1); + webClient.setWebConnection(webConnection); + + final HtmlPage page = loadPage(index); + + assertEquals("", page.getElementById("content").asText()); + // check frame on page + List<FrameWindow> frames = page.getFrames(); + assertEquals(1, frames.size()); + assertEquals("frame content", ((HtmlPage) page.getFrameByName("content").getEnclosedPage()).asText()); + + // replace frame tag with javascript + ((HtmlElement) page.getElementById("clickId")).click(); + + assertEquals("new content", page.getElementById("content").asText()); + + // frame is still there + frames = page.getFrames(); + assertEquals(0, frames.size()); + } } |