Test that close() stops all threads. This wasn't the case as of HtmlUnit-2.7-SNAPSHOT 11.12.2009. @throws Exception if test fails
()
| 2246 | * @throws Exception if test fails |
| 2247 | */ |
| 2248 | @Test |
| 2249 | public void close() throws Exception { |
| 2250 | final String html = DOCTYPE_HTML |
| 2251 | + "<html><head></head>\n" |
| 2252 | + "<body onload='setInterval(addFrame, 1)'>\n" |
| 2253 | + "<iframe src='second.html'></iframe>\n" |
| 2254 | + "<script>\n" |
| 2255 | + "function addFrame() {\n" |
| 2256 | + " var f = document.createElement('iframe');\n" |
| 2257 | + " f.src = 'second.html';\n" |
| 2258 | + " document.body.appendChild(f);\n" |
| 2259 | + "}\n" |
| 2260 | + "</script>\n" |
| 2261 | + "</body></html>"; |
| 2262 | |
| 2263 | final String html2 = DOCTYPE_HTML |
| 2264 | + "<html><head><script>\n" |
| 2265 | + "function doSomething() {}\n" |
| 2266 | + "setInterval(doSomething, 100);\n" |
| 2267 | + "</script>\n" |
| 2268 | + "</head><body></body></html>"; |
| 2269 | |
| 2270 | getMockWebConnection().setResponse(URL_FIRST, html); |
| 2271 | getMockWebConnection().setDefaultResponse(html2); |
| 2272 | |
| 2273 | @SuppressWarnings("resource") |
| 2274 | final WebClient webClient = getWebClient(); |
| 2275 | final int initialJSThreads = getJavaScriptThreads().size(); |
| 2276 | webClient.setWebConnection(getMockWebConnection()); |
| 2277 | webClient.getPage(URL_FIRST); |
| 2278 | |
| 2279 | int nbJSThreads = getJavaScriptThreads().size(); |
| 2280 | final int nbNewJSThreads = nbJSThreads - initialJSThreads; |
| 2281 | assertTrue(nbNewJSThreads + " threads", nbNewJSThreads > 0); |
| 2282 | |
| 2283 | // close and verify that the WebClient is clean |
| 2284 | webClient.close(); |
| 2285 | |
| 2286 | assertEquals(0, webClient.getWebWindows().size()); |
| 2287 | assertEquals(0, webClient.getTopLevelWindows().size()); |
| 2288 | assertNull(webClient.getCurrentWindow()); |
| 2289 | |
| 2290 | nbJSThreads = getJavaScriptThreads().size(); |
| 2291 | assertEquals(initialJSThreads, nbJSThreads); |
| 2292 | } |
| 2293 | |
| 2294 | /** |
| 2295 | * Tests that setThrowExceptionOnScriptError also works, |
nothing calls this directly
no test coverage detected