use of com.gargoylesoftware.htmlunit.TopLevelWindow in project htmlunit by HtmlUnit.
the class HtmlPage method initialize.
/**
* Initialize this page.
* @throws IOException if an IO problem occurs
* @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
* {@link com.gargoylesoftware.htmlunit.WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is set
* to true.
*/
@Override
public void initialize() throws IOException, FailingHttpStatusCodeException {
final WebWindow enclosingWindow = getEnclosingWindow();
final boolean isAboutBlank = getUrl() == UrlUtils.URL_ABOUT_BLANK;
if (isAboutBlank) {
// a frame contains first a faked "about:blank" before its real content specified by src gets loaded
if (enclosingWindow instanceof FrameWindow && !((FrameWindow) enclosingWindow).getFrameElement().isContentLoaded()) {
return;
}
// save the URL that should be used to resolve relative URLs in this page
if (enclosingWindow instanceof TopLevelWindow) {
final TopLevelWindow topWindow = (TopLevelWindow) enclosingWindow;
final WebWindow openerWindow = topWindow.getOpener();
if (openerWindow != null && openerWindow.getEnclosedPage() != null) {
baseUrl_ = openerWindow.getEnclosedPage().getWebResponse().getWebRequest().getUrl();
}
}
}
if (!isAboutBlank) {
setReadyState(READY_STATE_INTERACTIVE);
getDocumentElement().setReadyState(READY_STATE_INTERACTIVE);
}
executeDeferredScriptsIfNeeded();
executeEventHandlersIfNeeded(Event.TYPE_DOM_DOCUMENT_LOADED);
loadFrames();
// see Node.initInlineFrameIfNeeded()
if (!isAboutBlank) {
if (hasFeature(FOCUS_BODY_ELEMENT_AT_START)) {
setElementWithFocus(getBody());
}
setReadyState(READY_STATE_COMPLETE);
getDocumentElement().setReadyState(READY_STATE_COMPLETE);
}
// frame initialization has a different order
boolean isFrameWindow = enclosingWindow instanceof FrameWindow;
boolean isFirstPageInFrameWindow = false;
if (isFrameWindow) {
isFrameWindow = ((FrameWindow) enclosingWindow).getFrameElement() instanceof HtmlFrame;
final History hist = enclosingWindow.getHistory();
if (hist.getLength() > 0 && UrlUtils.URL_ABOUT_BLANK == hist.getUrl(0)) {
isFirstPageInFrameWindow = hist.getLength() <= 2;
} else {
isFirstPageInFrameWindow = enclosingWindow.getHistory().getLength() < 2;
}
}
if (isFrameWindow && !isFirstPageInFrameWindow) {
executeEventHandlersIfNeeded(Event.TYPE_LOAD);
}
for (final FrameWindow frameWindow : getFrames()) {
if (frameWindow.getFrameElement() instanceof HtmlFrame) {
final Page page = frameWindow.getEnclosedPage();
if (page != null && page.isHtmlPage()) {
((HtmlPage) page).executeEventHandlersIfNeeded(Event.TYPE_LOAD);
}
}
}
if (!isFrameWindow) {
executeEventHandlersIfNeeded(Event.TYPE_LOAD);
if (!isAboutBlank && enclosingWindow.getWebClient().isJavaScriptEnabled() && hasFeature(EVENT_FOCUS_ON_LOAD)) {
final HtmlElement body = getBody();
if (body != null) {
final Event event = new Event((Window) enclosingWindow.getScriptableObject(), Event.TYPE_FOCUS);
body.fireEvent(event);
}
}
}
try {
while (!afterLoadActions_.isEmpty()) {
final PostponedAction action = afterLoadActions_.remove(0);
action.execute();
}
} catch (final IOException e) {
throw e;
} catch (final Exception e) {
throw new RuntimeException(e);
}
executeRefreshIfNeeded();
}
use of com.gargoylesoftware.htmlunit.TopLevelWindow in project htmlunit by HtmlUnit.
the class HTMLCollectionFrames method initialize.
/**
* Initializes this window.
* @param webWindow the web window corresponding to this window
* @param pageToEnclose the page that will become the enclosing page
*/
public void initialize(final WebWindow webWindow, final Page pageToEnclose) {
webWindow_ = webWindow;
webWindow_.setScriptableObject(this);
windowProxy_ = new WindowProxy(webWindow_);
if (pageToEnclose instanceof XmlPage) {
document_ = new XMLDocument();
} else {
document_ = new HTMLDocument();
}
document_.setParentScope(this);
document_.setPrototype(getPrototype(document_.getClass()));
document_.setWindow(this);
if (pageToEnclose instanceof SgmlPage) {
final SgmlPage page = (SgmlPage) pageToEnclose;
document_.setDomNode(page);
if (page.isHtmlPage()) {
final HtmlPage htmlPage = (HtmlPage) page;
htmlPage.addAutoCloseable(this);
}
}
documentProxy_ = new DocumentProxy(webWindow_);
navigator_ = new Navigator();
navigator_.setParentScope(this);
navigator_.setPrototype(getPrototype(navigator_.getClass()));
screen_ = new Screen(getWebWindow().getScreen());
screen_.setParentScope(this);
screen_.setPrototype(getPrototype(screen_.getClass()));
history_ = new History();
history_.setParentScope(this);
history_.setPrototype(getPrototype(history_.getClass()));
location_ = new Location();
location_.setParentScope(this);
location_.setPrototype(getPrototype(location_.getClass()));
location_.initialize(this, pageToEnclose);
final Console console = new Console();
console.setWebWindow(webWindow_);
console.setParentScope(this);
console.setPrototype(getPrototype(console.getClass()));
console_ = console;
applicationCache_ = new ApplicationCache();
applicationCache_.setParentScope(this);
applicationCache_.setPrototype(getPrototype(applicationCache_.getClass()));
// like a JS new Object()
final Context ctx = Context.getCurrentContext();
controllers_ = ctx.newObject(this);
if (webWindow_ instanceof TopLevelWindow) {
final WebWindow opener = ((TopLevelWindow) webWindow_).getOpener();
if (opener != null) {
opener_ = opener.getScriptableObject();
}
}
}
use of com.gargoylesoftware.htmlunit.TopLevelWindow in project htmlunit by HtmlUnit.
the class HistoryTest method go.
/**
* @throws Exception if an error occurs
*/
@Test
public void go() throws Exception {
final WebClient client = getWebClient();
final TopLevelWindow window = (TopLevelWindow) client.getCurrentWindow();
final History history = window.getHistory();
final String urlA = URL_FIRST + "HistoryTest_a.html";
final String urlB = URL_FIRST + "HistoryTest_b.html";
final String urlBX = URL_FIRST + "HistoryTest_b.html#x";
final String urlC = URL_FIRST + "HistoryTest_c.html";
HtmlPage page = client.getPage(urlA);
assertEquals(1, history.getLength());
assertEquals(0, history.getIndex());
assertEquals(urlA, page.getUrl());
page = page.getAnchorByName("b").click();
assertEquals(2, history.getLength());
assertEquals(1, history.getIndex());
assertEquals(urlB, page.getUrl());
page = page.getAnchorByName("x").click();
assertEquals(3, history.getLength());
assertEquals(2, history.getIndex());
assertEquals(urlBX, page.getUrl());
page = page.getAnchorByName("minusTwo").click();
assertEquals(3, history.getLength());
assertEquals(0, history.getIndex());
assertEquals(urlA, page.getUrl());
page = page.getAnchorByName("plusOne").click();
assertEquals(3, history.getLength());
assertEquals(1, history.getIndex());
assertEquals(urlB, page.getUrl());
page = page.getAnchorByName("c").click();
assertEquals(3, history.getLength());
assertEquals(2, history.getIndex());
assertEquals(urlC, page.getUrl());
page = page.getAnchorByName("minusOne").click();
assertEquals(3, history.getLength());
assertEquals(1, history.getIndex());
assertEquals(urlB, page.getUrl());
page = page.getAnchorByName("plusTwo").click();
assertEquals(3, history.getLength());
assertEquals(1, history.getIndex());
assertEquals(urlB, page.getUrl());
}
use of com.gargoylesoftware.htmlunit.TopLevelWindow in project htmlunit by HtmlUnit.
the class HistoryTest method backAndForward.
/**
* @throws Exception if an error occurs
*/
@Test
public void backAndForward() throws Exception {
final WebClient client = getWebClient();
final TopLevelWindow window = (TopLevelWindow) client.getCurrentWindow();
final History history = window.getHistory();
final String urlA = URL_FIRST + "HistoryTest_a.html";
final String urlB = URL_FIRST + "HistoryTest_b.html";
final String urlBX = URL_FIRST + "HistoryTest_b.html#x";
final String urlC = URL_FIRST + "HistoryTest_c.html";
HtmlPage page = client.getPage(urlA);
assertEquals(1, history.getLength());
assertEquals(0, history.getIndex());
assertEquals(urlA, page.getUrl());
page = page.getAnchorByName("b").click();
assertEquals(2, history.getLength());
assertEquals(1, history.getIndex());
assertEquals(urlB, page.getUrl());
page = page.getAnchorByName("x").click();
assertEquals(3, history.getLength());
assertEquals(2, history.getIndex());
assertEquals(urlBX, page.getUrl());
page = page.getAnchorByName("back").click();
assertEquals(3, history.getLength());
assertEquals(1, history.getIndex());
assertEquals(urlB, page.getUrl());
page = page.getAnchorByName("back").click();
assertEquals(3, history.getLength());
assertEquals(0, history.getIndex());
assertEquals(urlA, page.getUrl());
page = page.getAnchorByName("forward").click();
assertEquals(3, history.getLength());
assertEquals(1, history.getIndex());
assertEquals(urlB, page.getUrl());
page = page.getAnchorByName("c").click();
assertEquals(3, history.getLength());
assertEquals(2, history.getIndex());
assertEquals(urlC, page.getUrl());
page = page.getAnchorByName("back").click();
assertEquals(3, history.getLength());
assertEquals(1, history.getIndex());
assertEquals(urlB, page.getUrl());
page = page.getAnchorByName("forward").click();
assertEquals(3, history.getLength());
assertEquals(2, history.getIndex());
assertEquals(urlC, page.getUrl());
}
use of com.gargoylesoftware.htmlunit.TopLevelWindow in project htmlunit by HtmlUnit.
the class JavaScriptJobManagerTest method interruptAllWithRecursiveSetTimeout.
/**
* Test for Bug #487 that makes sure closing a window prevents a
* recursive setTimeout from continuing forever.
*
* @throws Exception if the test fails
*/
@Test
public void interruptAllWithRecursiveSetTimeout() throws Exception {
final String content = "<html>\n" + "<head>\n" + " <title>test</title>\n" + " <script>\n" + " var threadID;\n" + " function test() {\n" + " alert('ping');\n" + " threadID = setTimeout(test, 5);\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);
// Not perfect, but 100 chances to start should be enough for a loaded system
Thread.sleep(500);
assertFalse("At least one alert should have fired by now", collectedAlerts.isEmpty());
((TopLevelWindow) page.getEnclosingWindow()).close();
// 100 chances to stop
jobManager.waitForJobs(500);
final int finalValue = collectedAlerts.size();
// 100 chances to fail
jobManager.waitForJobs(500);
assertEquals("No new alerts should have happened", finalValue, collectedAlerts.size());
}
Aggregations