use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Tries in project htmlunit by HtmlUnit.
the class ThreadSynchronizer method waitCalledDuringJobExecution.
/**
* When waitForBackgroundJavaScriptStartingBefore is called while a job is being executed, it has
* to wait for this job to finish, even if this clearXXX has been called for it.
* @throws Exception if the test fails
*/
@Test
@Tries(3)
public void waitCalledDuringJobExecution() throws Exception {
final String html = "<html>\n" + "<head>\n" + " <title>test</title>\n" + " <script>\n" + " var intervalId;\n" + " function test() {\n" + " intervalId = setTimeout(doWork, 100);\n" + " }\n" + " function doWork() {\n" + " clearTimeout(intervalId);\n" + " // waitForBackgroundJavaScriptStartingBefore should be called when JS execution is here\n" + " var request = new XMLHttpRequest();\n" + " request.open('GET', 'wait', false);\n" + " request.send('');\n" + " alert('end work');\n" + " }\n" + " </script>\n" + "</head>\n" + "<body onload='test()'>\n" + "</body>\n" + "</html>";
final ThreadSynchronizer threadSynchronizer = new ThreadSynchronizer();
final MockWebConnection webConnection = new MockWebConnection() {
@Override
public WebResponse getResponse(final WebRequest request) throws IOException {
if (request.getUrl().toExternalForm().endsWith("/wait")) {
threadSynchronizer.waitForState("just before waitForBackgroundJavaScriptStartingBefore");
// main thread need to be able to process next instruction
threadSynchronizer.sleep(400);
}
return super.getResponse(request);
}
};
webConnection.setResponse(URL_FIRST, html);
webConnection.setDefaultResponse("");
final WebClient client = getWebClient();
client.setWebConnection(webConnection);
final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final HtmlPage page = client.getPage(URL_FIRST);
final JavaScriptJobManager jobManager = page.getEnclosingWindow().getJobManager();
assertNotNull(jobManager);
assertEquals(1, jobManager.getJobCount());
startTimedTest();
threadSynchronizer.setState("just before waitForBackgroundJavaScriptStartingBefore");
assertEquals(0, client.waitForBackgroundJavaScriptStartingBefore(20_000));
assertMaxTestRunTime(600);
assertEquals(0, jobManager.getJobCount());
final String[] expectedAlerts = { "end work" };
assertEquals(expectedAlerts, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Tries in project htmlunit by HtmlUnit.
the class ThreadSynchronizer method newJobStartedAfterWait.
/**
* Test the case where a job is being executed at the time where waitForBackgroundJavaScriptStartingBefore
* and where this jobs schedules a new job after call to waitForBackgroundJavaScriptStartingBefore,
* .
* @throws Exception if the test fails
*/
@Test
@Tries(3)
public void newJobStartedAfterWait() throws Exception {
final String html = "<html>\n" + "<head>\n" + " <title>test</title>\n" + " <script>\n" + " var request;\n" + " function onReadyStateChange() {\n" + " if (request.readyState == 4) {\n" + " alert('xhr onchange');\n" + " setTimeout(doWork1, 200);\n" + " }\n" + " }\n" + " function test() {\n" + " request = new XMLHttpRequest();\n" + " request.open('GET', 'wait', true);\n" + " request.onreadystatechange = onReadyStateChange;\n" + " // waitForBackgroundJavaScriptStartingBefore should be called when JS execution is in send()\n" + " request.send('');\n" + " }\n" + " function doWork1() {\n" + " alert('work1');\n" + " }\n" + " </script>\n" + "</head>\n" + "<body onload='test()'>\n" + "</body>\n" + "</html>";
final ThreadSynchronizer threadSynchronizer = new ThreadSynchronizer();
final MockWebConnection webConnection = new MockWebConnection() {
@Override
public WebResponse getResponse(final WebRequest request) throws IOException {
if (request.getUrl().toExternalForm().endsWith("/wait")) {
threadSynchronizer.waitForState("just before waitForBackgroundJavaScriptStartingBefore");
// main thread need to be able to process next instruction
threadSynchronizer.sleep(400);
}
return super.getResponse(request);
}
};
webConnection.setResponse(URL_FIRST, html);
webConnection.setDefaultResponse("");
final WebClient client = getWebClient();
client.setWebConnection(webConnection);
final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final HtmlPage page = client.getPage(URL_FIRST);
final JavaScriptJobManager jobManager = page.getEnclosingWindow().getJobManager();
assertNotNull(jobManager);
assertEquals(1, jobManager.getJobCount());
startTimedTest();
threadSynchronizer.setState("just before waitForBackgroundJavaScriptStartingBefore");
assertEquals(0, client.waitForBackgroundJavaScriptStartingBefore(20_000));
assertMaxTestRunTime(1000);
assertEquals(0, jobManager.getJobCount());
final String[] expectedAlerts = { "xhr onchange", "work1" };
assertEquals(expectedAlerts, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Tries in project htmlunit by HtmlUnit.
the class ThreadSynchronizer method waitForBackgroundJavaScriptStartingBefore_hangs.
/**
* HtmlUnit-2.7-SNAPSHOT (as of 29.10.09) had bug with
* WebClient.waitForBackgroundJavaScriptStartingBefore: it could be totally blocking
* under some circumstances. This test reproduces the problem but ensures
* that the test terminates (calling clearInterval when waitForBackgroundJavaScriptStartingBefore
* has not done its job correctly).
* @throws Exception if the test fails
*/
@Test
@Tries(3)
public void waitForBackgroundJavaScriptStartingBefore_hangs() throws Exception {
final String html = "<html>\n" + "<head>\n" + " <title>test</title>\n" + " <script>\n" + " var start = new Date().getTime();\n" + " var id = setInterval(doWork1, 35);\n" + " function stopTimer() {\n" + " clearInterval(id);\n" + " }\n" + " function doWork1() {\n" + " if (start + 8000 < new Date().getTime()) {\n" + " document.title = 'failed';\n" + " clearInterval(id);\n" + " }\n" + " }\n" + " </script>\n" + "</head>\n" + "<body>\n" + "<button onclick='stopTimer()'>stop</button>\n" + "</body>\n" + "</html>";
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setDefaultResponse(html);
client.setWebConnection(webConnection);
final HtmlPage page = client.getPage(URL_FIRST);
int noOfJobs = client.waitForBackgroundJavaScriptStartingBefore(500);
// maybe one is running
assertTrue(noOfJobs == 1 || noOfJobs == 2);
assertEquals("test", page.getTitleText());
noOfJobs = client.waitForBackgroundJavaScriptStartingBefore(500);
// maybe one is running
assertTrue(noOfJobs == 1 || noOfJobs == 2);
}
use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Tries in project htmlunit by HtmlUnit.
the class ThreadSynchronizer method waitWithsubWindows.
/**
* When waitForBackgroundJavaScriptStartingBefore is called and a new job is scheduled after the one that
* is first found as the last one within the delay (a job starts a new one or simply a setInterval),
* a few retries should be done to see if new jobs exists.
* @throws Exception if the test fails
*/
@Test
@Tries(3)
public void waitWithsubWindows() throws Exception {
final String html = "<html>\n" + "<head>\n" + " <title>test</title>\n" + "</head>\n" + "<body>\n" + "<iframe src='nested.html'></iframe>\n" + "</body>\n" + "</html>";
final String nested = "<html>\n" + "<head>\n" + " <title>nested</title>\n" + " <script>\n" + " function test() {\n" + " setTimeout(doWork1, 200);\n" + " }\n" + " function doWork1() {\n" + " alert('work1');\n" + " }\n" + " </script>\n" + "</head>\n" + "<body onload='test()'>\n" + "</body>\n" + "</html>";
final WebClient client = getWebClient();
final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setResponse(URL_FIRST, html);
webConnection.setDefaultResponse(nested);
client.setWebConnection(webConnection);
final HtmlPage page = client.getPage(URL_FIRST);
final JavaScriptJobManager jobManager = page.getEnclosingWindow().getJobManager();
assertNotNull(jobManager);
assertEquals(0, jobManager.getJobCount());
startTimedTest();
assertEquals(0, client.waitForBackgroundJavaScriptStartingBefore(20_000));
assertMaxTestRunTime(300);
assertEquals(0, jobManager.getJobCount());
final String[] expectedAlerts = { "work1" };
assertEquals(expectedAlerts, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Tries in project htmlunit by HtmlUnit.
the class ThreadSynchronizer method dontWaitWhenUnnecessary_jobRemovesOtherJob.
/**
* @throws Exception if the test fails
*/
@Test
@Tries(3)
public void dontWaitWhenUnnecessary_jobRemovesOtherJob() throws Exception {
final String content = "<html>\n" + "<head>\n" + " <title>test</title>\n" + " <script>\n" + " var longTimeoutID;\n" + " function test() {\n" + " longTimeoutID = setTimeout(doAlert('late'), 10000);\n" + " setTimeout(clearLongTimeout, 100);\n" + " setTimeout(doAlert('hello'), 300);\n" + " }\n" + " function clearLongTimeout() {\n" + " alert('clearLongTimeout');\n" + " clearTimeout(longTimeoutID);\n" + " }\n" + " function doAlert(_text) {\n" + " return function doAlert() {\n" + " alert(_text);\n" + " }\n" + " }\n" + " </script>\n" + "</head>\n" + "<body onload='test()'>\n" + "</body>\n" + "</html>";
final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
final HtmlPage page = loadPage(content, collectedAlerts);
final JavaScriptJobManager jobManager = page.getEnclosingWindow().getJobManager();
assertNotNull(jobManager);
assertEquals(3, jobManager.getJobCount());
startTimedTest();
assertEquals(0, page.getWebClient().waitForBackgroundJavaScriptStartingBefore(20_000));
assertMaxTestRunTime(400);
assertEquals(0, jobManager.getJobCount());
final String[] expectedAlerts = { "clearLongTimeout", "hello" };
assertEquals(expectedAlerts, collectedAlerts);
}
Aggregations