use of com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager in project htmlunit by HtmlUnit.
the class TopLevelWindowTest method useCustomJobManager.
/**
* Tests the use of a custom job manager.
* @throws Exception if an error occurs
*/
@Test
public void useCustomJobManager() throws Exception {
final MutableInt jobCount = new MutableInt(0);
final JavaScriptJobManager mgr = new JavaScriptJobManager() {
/**
* {@inheritDoc}
*/
@Override
public int waitForJobsStartingBefore(final long delayMillis) {
return jobCount.intValue();
}
/**
* {@inheritDoc}
*/
@Override
public int waitForJobsStartingBefore(final long delayMillis, final JavaScriptJobFilter filter) {
return jobCount.intValue();
}
/**
* {@inheritDoc}
*/
@Override
public int waitForJobs(final long timeoutMillis) {
return jobCount.intValue();
}
/**
* {@inheritDoc}
*/
@Override
public void stopJob(final int id) {
// Empty.
}
/**
* {@inheritDoc}
*/
@Override
public void shutdown() {
// Empty.
}
/**
* {@inheritDoc}
*/
@Override
public void removeJob(final int id) {
// Empty.
}
/**
* {@inheritDoc}
*/
@Override
public void removeAllJobs() {
// Empty.
}
/**
* {@inheritDoc}
*/
@Override
public int getJobCount() {
return jobCount.intValue();
}
/**
* {@inheritDoc}
*/
@Override
public int getJobCount(final JavaScriptJobFilter filter) {
return jobCount.intValue();
}
/**
* {@inheritDoc}
*/
@Override
public int addJob(final JavaScriptJob job, final Page page) {
jobCount.increment();
return jobCount.intValue();
}
/**
* {@inheritDoc}
*/
@Override
public JavaScriptJob getEarliestJob() {
return null;
}
/**
* {@inheritDoc}
*/
@Override
public JavaScriptJob getEarliestJob(final JavaScriptJobFilter filter) {
return null;
}
/**
* {@inheritDoc}
*/
@Override
public boolean runSingleJob(final JavaScriptJob job) {
// Empty
return false;
}
@Override
public String jobStatusDump(final JavaScriptJobFilter filter) {
return null;
}
};
final WebWindowListener listener = new WebWindowListener() {
/**
* {@inheritDoc}
*/
@Override
public void webWindowOpened(final WebWindowEvent event) {
((WebWindowImpl) event.getWebWindow()).setJobManager(mgr);
}
/**
* {@inheritDoc}
*/
@Override
public void webWindowContentChanged(final WebWindowEvent event) {
// Empty.
}
/**
* {@inheritDoc}
*/
@Override
public void webWindowClosed(final WebWindowEvent event) {
// Empty.
}
};
final WebClient client = getWebClientWithMockWebConnection();
client.addWebWindowListener(listener);
final TopLevelWindow window = (TopLevelWindow) client.getCurrentWindow();
window.setJobManager(mgr);
final MockWebConnection conn = getMockWebConnection();
conn.setDefaultResponse("<html><body><script>window.setTimeout('', 500);</script></body></html>");
client.getPage(URL_FIRST);
assertEquals(1, jobCount.intValue());
client.getPage(URL_FIRST);
assertEquals(2, jobCount.intValue());
}
use of com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager in project htmlunit by HtmlUnit.
the class HtmlInlineFrameTest method frameSetInnerHtmlDoesLoadFrameContentTimeout.
/**
* Verifies that frames added via element.innerHtml() are resolved.
* @throws Exception if an error occurs
*/
@Test
public void frameSetInnerHtmlDoesLoadFrameContentTimeout() throws Exception {
final String html1 = "<html><body>\n" + "<iframe id='myFrame' src='" + URL_THIRD + "'></iframe>';\n" + "<span id='A'></span>\n" + "<script>\n" + " function createIframe() {\n" + " var frame='<iframe id=\"f\" src=\"" + URL_SECOND + "\"></iframe>';\n" + " document.getElementById('A').innerHTML = frame;\n" + " }\n" + " setTimeout('createIframe()', 100);\n" + "</script>\n" + "</body></html>";
final String html2 = "<html><body>iframe content</body></html>";
final String html3 = "<html><head></head><body>Third content</body></html>";
final WebClient client = getWebClientWithMockWebConnection();
final MockWebConnection conn = getMockWebConnection();
conn.setResponse(URL_FIRST, html1);
conn.setResponse(URL_SECOND, html2);
conn.setResponse(URL_THIRD, html3);
final HtmlPage page = client.getPage(URL_FIRST);
final HtmlElement myFrame = page.getHtmlElementById("myFrame");
assertEquals("iframe", myFrame.getTagName());
HtmlPage enclosedPage = (HtmlPage) ((HtmlInlineFrame) myFrame).getEnclosedPage();
assertEquals("Third content", enclosedPage.asNormalizedText());
// wait for the timer
final JavaScriptJobManager jobManager = page.getEnclosingWindow().getJobManager();
jobManager.waitForJobs(1000);
final HtmlElement iFrame = page.getHtmlElementById("f");
assertEquals("iframe", iFrame.getTagName());
enclosedPage = (HtmlPage) ((HtmlInlineFrame) iFrame).getEnclosedPage();
assertEquals("iframe content", enclosedPage.asNormalizedText());
assertEquals(3, conn.getRequestCount());
}
use of com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager 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.javascript.background.JavaScriptJobManager 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);
}
use of com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager in project htmlunit by HtmlUnit.
the class WebClient method waitForBackgroundJavaScriptStartingBefore.
/**
* <p><span style="color:red">Experimental API: May be changed in next release
* and may not yet work perfectly!</span></p>
*
* <p>This method blocks until all background JavaScript tasks scheduled to start executing before
* <tt>(now + delayMillis)</tt> have finished executing. Background JavaScript tasks are JavaScript
* tasks scheduled for execution via <tt>window.setTimeout</tt>, <tt>window.setInterval</tt> or
* asynchronous <tt>XMLHttpRequest</tt>.</p>
*
* <p>If there is no background JavaScript task currently executing, and there is no background JavaScript
* task scheduled to start executing within the specified time, this method returns immediately -- even
* if there are tasks scheduled to be executed after <tt>(now + delayMillis)</tt>.</p>
*
* <p>Note that the total time spent executing a background JavaScript task is never known ahead of
* time, so this method makes no guarantees as to how long it will block.</p>
*
* <p>Use this method instead of {@link #waitForBackgroundJavaScript(long)} if you know roughly when
* your background JavaScript is supposed to start executing, but you're not necessarily sure how long
* it will take to execute.</p>
*
* @param delayMillis the delay which determines the background tasks to wait for (in milliseconds)
* @return the number of background JavaScript jobs still executing or waiting to be executed when this
* method returns; will be <tt>0</tt> if there are no jobs left to execute
*/
public int waitForBackgroundJavaScriptStartingBefore(final long delayMillis) {
int count = 0;
final long endTime = System.currentTimeMillis() + delayMillis;
for (Iterator<WeakReference<JavaScriptJobManager>> i = jobManagers_.iterator(); i.hasNext(); ) {
final JavaScriptJobManager jobManager;
final WeakReference<JavaScriptJobManager> reference;
try {
reference = i.next();
jobManager = reference.get();
if (jobManager == null) {
i.remove();
continue;
}
} catch (final ConcurrentModificationException e) {
i = jobManagers_.iterator();
count = 0;
continue;
}
final long newDelay = endTime - System.currentTimeMillis();
count += jobManager.waitForJobsStartingBefore(newDelay);
}
if (count != getAggregateJobCount()) {
final long newDelay = endTime - System.currentTimeMillis();
return waitForBackgroundJavaScriptStartingBefore(newDelay);
}
return count;
}
Aggregations