|
14 | 14 | */ |
15 | 15 | package org.htmlunit.javascript.host.html; |
16 | 16 |
|
| 17 | +import java.io.InputStream; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.apache.commons.io.IOUtils; |
| 23 | +import org.htmlunit.CollectingAlertHandler; |
17 | 24 | import org.htmlunit.MockWebConnection; |
18 | 25 | import org.htmlunit.SimpleWebTestCase; |
19 | 26 | import org.htmlunit.WebClient; |
| 27 | +import org.htmlunit.html.HtmlPage; |
20 | 28 | import org.htmlunit.junit.BrowserRunner; |
21 | 29 | import org.htmlunit.util.MimeType; |
| 30 | +import org.htmlunit.util.NameValuePair; |
22 | 31 | import org.junit.Test; |
23 | 32 | import org.junit.runner.RunWith; |
24 | 33 |
|
@@ -51,4 +60,103 @@ public void onLoad_notDownloadedWhenJavascriptDisabled() throws Exception { |
51 | 60 | loadPageWithAlerts(html); |
52 | 61 | assertEquals(URL_FIRST, conn.getLastWebRequest().getUrl()); |
53 | 62 | } |
| 63 | + |
| 64 | + /** |
| 65 | + * Make sure this works without an exception. |
| 66 | + * |
| 67 | + * @throws Exception on test failure |
| 68 | + */ |
| 69 | + @Test |
| 70 | + public void onload_complex_JavascriptDisabled() throws Exception { |
| 71 | + try (InputStream is = getClass().getClassLoader().getResourceAsStream("testfiles/tiny-jpg.img")) { |
| 72 | + final byte[] directBytes = IOUtils.toByteArray(is); |
| 73 | + |
| 74 | + final List<NameValuePair> emptyList = Collections.emptyList(); |
| 75 | + getMockWebConnection().setResponse(URL_SECOND, directBytes, 200, "ok", MimeType.IMAGE_JPEG, emptyList); |
| 76 | + } |
| 77 | + |
| 78 | + final String html = |
| 79 | + "<html>\n" |
| 80 | + + "<head>\n" |
| 81 | + + "<script>\n" |
| 82 | + + " function test(i) {\n" |
| 83 | + + " alert('in');\n" |
| 84 | + + " var image = new Image();\n" |
| 85 | + + " image.onload = function () { alert(\"Image.onload(\" + i + \")\") };\n" |
| 86 | + + " image.src = '" + URL_SECOND + "';\n" |
| 87 | + + " alert('out');\n" |
| 88 | + + " }\n" |
| 89 | + + "</script>\n" |
| 90 | + + "</head>\n" |
| 91 | + + "<body onload='test(0)'>\n" |
| 92 | + + "<button id='myId'" |
| 93 | + + "onmousedown=\"alert('mousedown'); test(1)\" " |
| 94 | + + "onmouseup=\"alert('mouseup'); test(2)\" " |
| 95 | + + "onclick=\"alert('click'); test(3)\"></button>\n" |
| 96 | + + "</body>\n" |
| 97 | + + "</html>\n"; |
| 98 | + |
| 99 | + final WebClient client = getWebClientWithMockWebConnection(); |
| 100 | + client.getOptions().setJavaScriptEnabled(false); |
| 101 | + |
| 102 | + final List<String> collectedAlerts = new ArrayList<>(); |
| 103 | + client.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); |
| 104 | + |
| 105 | + final HtmlPage page = loadPage(html); |
| 106 | + page.getElementById("myId").click(); |
| 107 | + assertEquals(getExpectedAlerts(), collectedAlerts); |
| 108 | + assertEquals(URL_FIRST, getMockWebConnection().getLastWebRequest().getUrl()); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Make sure this works without an exception. |
| 113 | + * |
| 114 | + * @throws Exception on test failure |
| 115 | + */ |
| 116 | + @Test |
| 117 | + public void onload_complex_JavascriptEngineDisabled() throws Exception { |
| 118 | + try (InputStream is = getClass().getClassLoader().getResourceAsStream("testfiles/tiny-jpg.img")) { |
| 119 | + final byte[] directBytes = IOUtils.toByteArray(is); |
| 120 | + |
| 121 | + final List<NameValuePair> emptyList = Collections.emptyList(); |
| 122 | + getMockWebConnection().setResponse(URL_SECOND, directBytes, 200, "ok", MimeType.IMAGE_JPEG, emptyList); |
| 123 | + } |
| 124 | + |
| 125 | + final String html = |
| 126 | + "<html>\n" |
| 127 | + + "<head>\n" |
| 128 | + + "<script>\n" |
| 129 | + + " function test(i) {\n" |
| 130 | + + " alert('in');\n" |
| 131 | + + " var image = new Image();\n" |
| 132 | + + " image.onload = function () { alert(\"Image.onload(\" + i + \")\") };\n" |
| 133 | + + " image.src = '" + URL_SECOND + "';\n" |
| 134 | + + " alert('out');\n" |
| 135 | + + " }\n" |
| 136 | + + "</script>\n" |
| 137 | + + "</head>\n" |
| 138 | + + "<body onload='test(0)'>\n" |
| 139 | + + "<button id='myId'" |
| 140 | + + "onmousedown=\"alert('mousedown'); test(1)\" " |
| 141 | + + "onmouseup=\"alert('mouseup'); test(2)\" " |
| 142 | + + "onclick=\"alert('click'); test(3)\"></button>\n" |
| 143 | + + "</body>\n" |
| 144 | + + "</html>\n"; |
| 145 | + |
| 146 | + try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) { |
| 147 | + final List<String> collectedAlerts = new ArrayList<>(); |
| 148 | + webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); |
| 149 | + |
| 150 | + final MockWebConnection webConnection = getMockWebConnection(); |
| 151 | + webConnection.setResponse(URL_FIRST, html); |
| 152 | + |
| 153 | + webClient.setWebConnection(webConnection); |
| 154 | + |
| 155 | + final HtmlPage page = loadPage(html); |
| 156 | + page.getElementById("myId").click(); |
| 157 | + assertEquals(getExpectedAlerts(), collectedAlerts); |
| 158 | + assertEquals(URL_SECOND, webConnection.getLastWebRequest().getUrl()); |
| 159 | + } |
| 160 | + } |
| 161 | + |
54 | 162 | } |
0 commit comments