Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Feb 13, 2025
1 parent 07a16ff commit 7c9c338
Showing 1 changed file with 177 additions and 9 deletions.
186 changes: 177 additions & 9 deletions src/test/java/org/htmlunit/javascript/host/html/HTMLElementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3941,6 +3941,7 @@ public void appendChildExecuteJavaScript() throws Exception {
+ "</script></head><body onload='test()'>\n"
+ " <div id='myNode'></div>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

Expand Down Expand Up @@ -3971,6 +3972,7 @@ public void appendChildExecuteNestedJavaScript() throws Exception {
+ "</script></head><body onload='test()'>\n"
+ " <div id='myNode'></div>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

Expand Down Expand Up @@ -4004,7 +4006,8 @@ public void appendChildExecuteNestedExternalJavaScript() throws Exception {
+ " <div id='myNode'></div>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
loadPage2(html);
verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
}

/**
Expand Down Expand Up @@ -4615,7 +4618,7 @@ public void insertBeforeDeclareJavaScript() throws Exception {
* @throws Exception if the test fails
*/
@Test
@Alerts("executed")
@Alerts({"replaceChild start", "executed", "replaceChild done"})
public void replaceChildExecuteJavaScript() throws Exception {
final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
+ "<html><head><script>\n"
Expand All @@ -4625,7 +4628,9 @@ public void replaceChildExecuteJavaScript() throws Exception {
+ " try {\n"
+ " newnode.appendChild(document.createTextNode('alerter();'));\n"
+ " var outernode = document.getElementById('myNode');\n"
+ " log('replaceChild start');\n"
+ " outernode.replaceChild(newnode, document.getElementById('inner'));\n"
+ " log('replaceChild done');\n"
+ " } catch(e) { logEx(e); }\n"
+ " }\n"
+ " function alerter() {\n"
Expand All @@ -4634,14 +4639,47 @@ public void replaceChildExecuteJavaScript() throws Exception {
+ "</script></head><body onload='test()'>\n"
+ " <div id='myNode'><div id='inner'></div></div>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts("executed")
@Alerts({"replaceChild start", "replaceChild done", "executed"})
public void replaceChildExecuteExternalJavaScript() throws Exception {
getMockWebConnection().setDefaultResponse("alerter();", MimeType.TEXT_JAVASCRIPT);

final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
+ "<html><head><script>\n"
+ LOG_TITLE_FUNCTION
+ " function test() {\n"
+ " var newnode = document.createElement('script');\n"
+ " try {\n"
+ " newnode.setAttribute('src', 'script.js');\n"
+ " var outernode = document.getElementById('myNode');\n"
+ " log('replaceChild start');\n"
+ " outernode.replaceChild(newnode, document.getElementById('inner'));\n"
+ " log('replaceChild done');\n"
+ " } catch(e) { logEx(e); }\n"
+ " }\n"
+ " function alerter() {\n"
+ " log('executed');\n"
+ " }\n"
+ "</script></head><body onload='test()'>\n"
+ " <div id='myNode'><div id='inner'></div></div>\n"
+ "</body></html>";

loadPage2(html);
verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts({"replaceChild start", "executed", "replaceChild done"})
public void replaceChildExecuteNestedJavaScript() throws Exception {
final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
+ "<html><head><script>\n"
Expand All @@ -4653,7 +4691,9 @@ public void replaceChildExecuteNestedJavaScript() throws Exception {
+ " try {\n"
+ " newscript.appendChild(document.createTextNode('alerter();'));\n"
+ " var outernode = document.getElementById('myNode');\n"
+ " log('replaceChild start');\n"
+ " outernode.replaceChild(newnode, document.getElementById('inner'));\n"
+ " log('replaceChild done');\n"
+ " } catch(e) { logEx(e); }\n"
+ " }\n"
+ " function alerter() {\n"
Expand All @@ -4662,16 +4702,51 @@ public void replaceChildExecuteNestedJavaScript() throws Exception {
+ "</script></head><body onload='test()'>\n"
+ " <div id='myNode'><div id='inner'></div></div>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = {"executed", "replaceChild done"},
FF = "replaceChild done",
FF_ESR = "replaceChild done")
@Alerts({"replaceChild start", "replaceChild done", "executed"})
public void replaceChildExecuteNestedExternalJavaScript() throws Exception {
getMockWebConnection().setDefaultResponse("alerter();", MimeType.TEXT_JAVASCRIPT);

final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
+ "<html><head><script>\n"
+ LOG_TITLE_FUNCTION
+ " function test() {\n"
+ " var newnode = document.createElement('div');\n"
+ " var newscript = document.createElement('script');\n"
+ " newnode.appendChild(newscript);\n"
+ " try {\n"
+ " newscript.setAttribute('src', 'script.js');\n"
+ " var outernode = document.getElementById('myNode');\n"
+ " log('replaceChild start');\n"
+ " outernode.replaceChild(newnode, document.getElementById('inner'));\n"
+ " log('replaceChild done');\n"
+ " } catch(e) { logEx(e); }\n"
+ " }\n"
+ " function alerter() {\n"
+ " log('executed');\n"
+ " }\n"
+ "</script></head><body onload='test()'>\n"
+ " <div id='myNode'><div id='inner'></div></div>\n"
+ "</body></html>";

loadPage2(html);
verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = {"replaceChild start", "executed", "replaceChild done"},
FF = {"replaceChild start", "replaceChild done"},
FF_ESR = {"replaceChild start", "replaceChild done"})
public void replaceChildExecuteTemplateChildJavaScript() throws Exception {
final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
+ "<html><head><script>\n"
Expand All @@ -4685,6 +4760,7 @@ public void replaceChildExecuteTemplateChildJavaScript() throws Exception {
+ " var scriptElem = template.content.firstChild;\n"
+ " try {\n"
+ " var outernode = document.getElementById('myNode');\n"
+ " log('replaceChild start');\n"
+ " outernode.replaceChild(scriptElem, document.getElementById('inner'));\n"
+ " log('replaceChild done');\n"
+ " } catch(e) { logEx(e); }\n"
Expand All @@ -4696,16 +4772,57 @@ public void replaceChildExecuteTemplateChildJavaScript() throws Exception {
+ "<body onload='test()'>\n"
+ " <div id='myNode'><div id='inner'></div></div>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = {"executed", "replaceChild done1", "replaceChild done2"},
FF = {"replaceChild done1", "replaceChild done2"},
FF_ESR = {"replaceChild done1", "replaceChild done2"})
@Alerts(DEFAULT = {"replaceChild start", "replaceChild done", "executed"},
FF = {"replaceChild start", "replaceChild done"},
FF_ESR = {"replaceChild start", "replaceChild done"})
public void replaceChildExecuteTemplateChildExternalJavaScript() throws Exception {
getMockWebConnection().setDefaultResponse("alerter();", MimeType.TEXT_JAVASCRIPT);

final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
+ "<html><head><script>\n"
+ LOG_TITLE_FUNCTION
+ " function test() {\n"
+ " var html = '<body><template><script src=\"script.js\"><' + '/script></template></body>';\n"
+ " var parser = new DOMParser();\n"
+ " var responseDoc = parser.parseFromString(html, 'text/html');\n"
+ " var responseBody = responseDoc.body;\n"
+ " var template = responseBody.querySelector('template');\n"
+ " var scriptElem = template.content.firstChild;\n"
+ " try {\n"
+ " var outernode = document.getElementById('myNode');\n"
+ " log('replaceChild start');\n"
+ " outernode.replaceChild(scriptElem, document.getElementById('inner'));\n"
+ " log('replaceChild done');\n"
+ " } catch(e) { logEx(e); }\n"
+ " }\n"
+ " function alerter() {\n"
+ " log('executed');\n"
+ " }\n"
+ "</script></head>\n"
+ "<body onload='test()'>\n"
+ " <div id='myNode'><div id='inner'></div></div>\n"
+ "</body></html>";

loadPage2(html);
verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = {"replaceChild start1", "executed", "replaceChild done1",
"replaceChild start2", "replaceChild done2"},
FF = {"replaceChild start1", "replaceChild done1", "replaceChild start2", "replaceChild done2"},
FF_ESR = {"replaceChild start1", "replaceChild done1", "replaceChild start2", "replaceChild done2"})
public void replaceChildExecuteTemplateFragmentJavaScript() throws Exception {
final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
+ "<html><head><script>\n"
Expand All @@ -4720,12 +4837,14 @@ public void replaceChildExecuteTemplateFragmentJavaScript() throws Exception {

+ " try {\n"
+ " var outernode = document.getElementById('myNode');\n"
+ " log('replaceChild start1');\n"
+ " outernode.replaceChild(scriptElem, document.getElementById('inner'));\n"
+ " log('replaceChild done1');\n"
+ " } catch(e) { logEx(e); }\n"

+ " try {\n"
+ " var outernode = document.getElementById('secondNode');\n"
+ " log('replaceChild start2');\n"
+ " outernode.replaceChild(scriptElem, document.getElementById('inner2'));\n"
+ " log('replaceChild done2');\n"
+ " } catch(e) { logEx(e); }\n"
Expand All @@ -4741,6 +4860,55 @@ public void replaceChildExecuteTemplateFragmentJavaScript() throws Exception {
loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = {"replaceChild start1", "replaceChild done1",
"replaceChild start2", "replaceChild done2", "executed"},
FF = {"replaceChild start1", "replaceChild done1", "replaceChild start2", "replaceChild done2"},
FF_ESR = {"replaceChild start1", "replaceChild done1", "replaceChild start2", "replaceChild done2"})
public void replaceChildExecuteTemplateFragmentExternalJavaScript() throws Exception {
getMockWebConnection().setDefaultResponse("alerter();", MimeType.TEXT_JAVASCRIPT);

final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
+ "<html><head><script>\n"
+ LOG_TITLE_FUNCTION
+ " function test() {\n"
+ " var html = '<body><template><script src=\"script.js\"><' + '/script></template></body>';\n"
+ " var parser = new DOMParser();\n"
+ " var responseDoc = parser.parseFromString(html, 'text/html');\n"
+ " var responseBody = responseDoc.body;\n"
+ " var templateContent = responseBody.querySelector('template').content;\n"
+ " var scriptElem = templateContent.firstChild;\n"

+ " try {\n"
+ " var outernode = document.getElementById('myNode');\n"
+ " log('replaceChild start1');\n"
+ " outernode.replaceChild(scriptElem, document.getElementById('inner'));\n"
+ " log('replaceChild done1');\n"
+ " } catch(e) { logEx(e); }\n"

+ " try {\n"
+ " var outernode = document.getElementById('secondNode');\n"
+ " log('replaceChild start2');\n"
+ " outernode.replaceChild(scriptElem, document.getElementById('inner2'));\n"
+ " log('replaceChild done2');\n"
+ " } catch(e) { logEx(e); }\n"
+ " }\n"
+ " function alerter() {\n"
+ " log('executed');\n"
+ " }\n"
+ "</script></head>\n"
+ "<body onload='test()'>\n"
+ " <div id='myNode'><div id='inner'></div></div>\n"
+ " <div id='secondNode'><div id='inner2'></div></div>\n"
+ "</body></html>";

loadPage2(html);
verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
}

/**
* @throws Exception if the test fails
*/
Expand Down

0 comments on commit 7c9c338

Please sign in to comment.