View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.dom;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Tests for {@link DOMException}.
25   *
26   * @author Marc Guillemot
27   * @author Frank Danek
28   * @author Ronald Brill
29   */
30  @RunWith(BrowserRunner.class)
31  public class DOMExceptionTest extends WebDriverTestCase {
32  
33      /**
34       * @throws Exception if the test fails
35       */
36      @Test
37      @Alerts({"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"})
38      public void constants() throws Exception {
39          final String html = DOCTYPE_HTML
40              + "<html><head>\n"
41              + "<script>\n"
42              + LOG_TITLE_FUNCTION
43              + "  var properties = ['INDEX_SIZE_ERR', 'DOMSTRING_SIZE_ERR', 'HIERARCHY_REQUEST_ERR',"
44              + " 'WRONG_DOCUMENT_ERR', 'INVALID_CHARACTER_ERR', 'NO_DATA_ALLOWED_ERR', 'NO_MODIFICATION_ALLOWED_ERR',"
45              + " 'NOT_FOUND_ERR', 'NOT_SUPPORTED_ERR', 'INUSE_ATTRIBUTE_ERR', 'INVALID_STATE_ERR', 'SYNTAX_ERR',"
46              + " 'INVALID_MODIFICATION_ERR', 'NAMESPACE_ERR', 'INVALID_ACCESS_ERR'];\n"
47              + "  try {\n"
48              + "    for (var i = 0; i < properties.length; i++) {\n"
49              + "      log(DOMException[properties[i]]);\n"
50              + "    }\n"
51              + "  } catch(e) { logEx(e);}\n"
52              + "</script></head>\n"
53              + "<body></body></html>";
54  
55          loadPageVerifyTitle2(html);
56      }
57  
58      /**
59       * @throws Exception if the test fails
60       */
61      @Test
62      @Alerts({"IndexSizeError - 1 IndexSizeError",
63               "INDEX_SIZE_ERR - 0 INDEX_SIZE_ERR",
64               "HierarchyRequestError - 3 HierarchyRequestError",
65               "HIERARCHY_REQUEST_ERR - 0 HIERARCHY_REQUEST_ERR",
66               "WrongDocumentError - 4 WrongDocumentError",
67               "WRONG_DOCUMENT_ERR - 0 WRONG_DOCUMENT_ERR",
68               "InvalidCharacterError - 5 InvalidCharacterError",
69               "INVALID_CHARACTER_ERR - 0 INVALID_CHARACTER_ERR",
70               "NoModificationAllowedError - 7 NoModificationAllowedError",
71               "NO_MODIFICATION_ALLOWED_ERR - 0 NO_MODIFICATION_ALLOWED_ERR",
72               "NotFoundError - 8 NotFoundError",
73               "NOT_FOUND_ERR - 0 NOT_FOUND_ERR",
74               "NotSupportedError - 9 NotSupportedError",
75               "NOT_SUPPORTED_ERR - 0 NOT_SUPPORTED_ERR",
76               "InvalidStateError - 11 InvalidStateError",
77               "INVALID_STATE_ERR - 0 INVALID_STATE_ERR",
78               "InUseAttributeError - 10 InUseAttributeError",
79               "INUSE_ATTRIBUTE_ERR - 0 INUSE_ATTRIBUTE_ERR",
80               "SyntaxError - 12 SyntaxError",
81               "SYNTAX_ERR - 0 SYNTAX_ERR",
82               "InvalidModificationError - 13 InvalidModificationError",
83               "INVALID_MODIFICATION_ERR - 0 INVALID_MODIFICATION_ERR",
84               "NamespaceError - 14 NamespaceError",
85               "NAMESPACE_ERR - 0 NAMESPACE_ERR",
86               "InvalidAccessError - 15 InvalidAccessError",
87               "INVALID_ACCESS_ERR - 0 INVALID_ACCESS_ERR",
88               "TypeMismatchError - 17 TypeMismatchError",
89               "TYPE_MISMATCH_ERR - 0 TYPE_MISMATCH_ERR",
90               "SecurityError - 18 SecurityError",
91               "SECURITY_ERR - 0 SECURITY_ERR",
92               "NetworkError - 19 NetworkError",
93               "NETWORK_ERR - 0 NETWORK_ERR",
94               "AbortError - 20 AbortError",
95               "ABORT_ERR - 0 ABORT_ERR",
96               "URLMismatchError - 21 URLMismatchError",
97               "URL_MISMATCH_ERR - 0 URL_MISMATCH_ERR",
98               "QuotaExceededError - 22 QuotaExceededError",
99               "QUOTA_EXCEEDED_ERR - 0 QUOTA_EXCEEDED_ERR",
100              "TimeoutError - 23 TimeoutError",
101              "TIMEOUT_ERR - 0 TIMEOUT_ERR",
102              "InvalidNodeTypeError - 24 InvalidNodeTypeError",
103              "INVALID_NODE_TYPE_ERR - 0 INVALID_NODE_TYPE_ERR",
104              "DataCloneError - 25 DataCloneError",
105              "DATA_CLONE_ERR - 0 DATA_CLONE_ERR",
106              "EncodingError - 0 EncodingError",
107              "NotReadableError - 0 NotReadableError",
108              "UnknownError - 0 UnknownError",
109              "ConstraintError - 0 ConstraintError",
110              "DataError - 0 DataError",
111              "TransactionInactiveError - 0 TransactionInactiveError",
112              "ReadOnlyError - 0 ReadOnlyError",
113              "VersionError - 0 VersionError",
114              "OperationError - 0 OperationError",
115              "NotAllowedError - 0 NotAllowedError"})
116     public void name() throws Exception {
117         final String html = DOCTYPE_HTML
118             + "<html><head>\n"
119             + "<script>\n"
120             + LOG_TITLE_FUNCTION
121             // https://developer.mozilla.org/en-US/docs/Web/API/DOMException#error_names
122             + "  var commonErrorNames = ["
123                 + "'IndexSizeError', 'INDEX_SIZE_ERR',"
124                 + "'HierarchyRequestError', 'HIERARCHY_REQUEST_ERR',"
125                 + "'WrongDocumentError', 'WRONG_DOCUMENT_ERR',"
126                 + "'InvalidCharacterError', 'INVALID_CHARACTER_ERR',"
127                 + "'NoModificationAllowedError', 'NO_MODIFICATION_ALLOWED_ERR',"
128                 + "'NotFoundError', 'NOT_FOUND_ERR',"
129                 + "'NotSupportedError', 'NOT_SUPPORTED_ERR',"
130                 + "'InvalidStateError', 'INVALID_STATE_ERR',"
131                 + "'InUseAttributeError', 'INUSE_ATTRIBUTE_ERR',"
132                 + "'SyntaxError', 'SYNTAX_ERR',"
133                 + "'InvalidModificationError', 'INVALID_MODIFICATION_ERR',"
134                 + "'NamespaceError', 'NAMESPACE_ERR',"
135                 + "'InvalidAccessError', 'INVALID_ACCESS_ERR',"
136                 + "'TypeMismatchError', 'TYPE_MISMATCH_ERR',"
137                 + "'SecurityError', 'SECURITY_ERR',"
138                 + "'NetworkError', 'NETWORK_ERR',"
139                 + "'AbortError', 'ABORT_ERR',"
140                 + "'URLMismatchError', 'URL_MISMATCH_ERR',"
141                 + "'QuotaExceededError', 'QUOTA_EXCEEDED_ERR',"
142                 + "'TimeoutError', 'TIMEOUT_ERR',"
143                 + "'InvalidNodeTypeError', 'INVALID_NODE_TYPE_ERR',"
144                 + "'DataCloneError', 'DATA_CLONE_ERR',"
145                 + "'EncodingError',"
146                 + "'NotReadableError',"
147                 + "'UnknownError',"
148                 + "'ConstraintError',"
149                 + "'DataError',"
150                 + "'TransactionInactiveError',"
151                 + "'ReadOnlyError',"
152                 + "'VersionError',"
153                 + "'OperationError',"
154                 + "'NotAllowedError'"
155                 + "];\n"
156             + "  try {\n"
157             + "    for (var i = 0; i < commonErrorNames.length; i++) {\n"
158             + "      let ex = new DOMException('test', commonErrorNames[i]);"
159             + "      log(commonErrorNames[i] + ' - ' + ex.code + ' ' + ex.name);\n"
160             + "    }\n"
161             + "  } catch(e) { logEx(e);}\n"
162             + "</script></head>\n"
163             + "<body></body></html>";
164 
165         loadPageVerifyTitle2(html);
166     }
167 
168     /**
169      * @throws Exception if the test fails
170      */
171     @Test
172     @Alerts({"0", "urlMismatchERRoR"})
173     public void nameCaseSensitive() throws Exception {
174         final String html = DOCTYPE_HTML
175             + "<html><head>\n"
176             + "<script>\n"
177             + LOG_TITLE_FUNCTION
178             + "  try {\n"
179             + "    let ex = new DOMException('test', 'urlMismatchERRoR');"
180             + "    log(ex.code);\n"
181             + "    log(ex.name);\n"
182             + "  } catch(e) { logEx(e);}\n"
183             + "</script></head>\n"
184             + "<body></body></html>";
185 
186         loadPageVerifyTitle2(html);
187     }
188 
189     /**
190      * @throws Exception if the test fails
191      */
192     @Test
193     @Alerts({"0", "Error"})
194     public void nameNotProvided() throws Exception {
195         final String html = DOCTYPE_HTML
196             + "<html><head>\n"
197             + "<script>\n"
198             + LOG_TITLE_FUNCTION
199             + "  try {\n"
200             + "    let ex = new DOMException('test');"
201             + "    log(ex.code);\n"
202             + "    log(ex.name);\n"
203             + "  } catch(e) { logEx(e);}\n"
204             + "</script></head>\n"
205             + "<body></body></html>";
206 
207         loadPageVerifyTitle2(html);
208     }
209 
210     /**
211      * @throws Exception if the test fails
212      */
213     @Test
214     @Alerts({"0", "null"})
215     public void nameNull() throws Exception {
216         final String html = DOCTYPE_HTML
217             + "<html><head>\n"
218             + "<script>\n"
219             + LOG_TITLE_FUNCTION
220             + "  try {\n"
221             + "    let ex = new DOMException('test', null);"
222             + "    log(ex.code);\n"
223             + "    log(ex.name);\n"
224             + "  } catch(e) { logEx(e);}\n"
225             + "</script></head>\n"
226             + "<body></body></html>";
227 
228         loadPageVerifyTitle2(html);
229     }
230 
231     /**
232      * @throws Exception if the test fails
233      */
234     @Test
235     @Alerts({"0", "Error"})
236     public void nameUndefined() throws Exception {
237         final String html = DOCTYPE_HTML
238             + "<html><head>\n"
239             + "<script>\n"
240             + LOG_TITLE_FUNCTION
241             + "  try {\n"
242             + "    let ex = new DOMException('test', undefined);"
243             + "    log(ex.code);\n"
244             + "    log(ex.name);\n"
245             + "  } catch(e) { logEx(e);}\n"
246             + "</script></head>\n"
247             + "<body></body></html>";
248 
249         loadPageVerifyTitle2(html);
250     }
251 
252     /**
253      * @throws Exception if the test fails
254      */
255     @Test
256     @Alerts({"0", "unKnown"})
257     public void nameUnknown() throws Exception {
258         final String html = DOCTYPE_HTML
259             + "<html><head>\n"
260             + "<script>\n"
261             + LOG_TITLE_FUNCTION
262             + "  try {\n"
263             + "    let ex = new DOMException('test', 'unKnown');"
264             + "    log(ex.code);\n"
265             + "    log(ex.name);\n"
266             + "  } catch(e) { logEx(e);}\n"
267             + "</script></head>\n"
268             + "<body></body></html>";
269 
270         loadPageVerifyTitle2(html);
271     }
272 
273     /**
274      * @throws Exception if the test fails
275      */
276     @Test
277     @Alerts({"0", "7"})
278     public void nameNumber() throws Exception {
279         final String html = DOCTYPE_HTML
280             + "<html><head>\n"
281             + "<script>\n"
282             + LOG_TITLE_FUNCTION
283             + "  try {\n"
284             + "    let ex = new DOMException('test', 7);"
285             + "    log(ex.code);\n"
286             + "    log(ex.name);\n"
287             + "  } catch(e) { logEx(e);}\n"
288             + "</script></head>\n"
289             + "<body></body></html>";
290 
291         loadPageVerifyTitle2(html);
292     }
293 
294     /**
295      * @throws Exception if the test fails
296      */
297     @Test
298     @Alerts({"undefined", "undefined", "undefined", "undefined"})
299     public void properties() throws Exception {
300         final String html = DOCTYPE_HTML
301             + "<html><head>\n"
302             + "<script>\n"
303             + LOG_TITLE_FUNCTION
304             + "  try {\n"
305             + "    log(DOMException.code);\n"
306             + "    log(DOMException.filename);\n"
307             + "    log(DOMException.lineNumber);\n"
308             + "    log(DOMException.message);\n"
309             + "  } catch(e) { logEx(e);}\n"
310             + "</script></head>\n"
311             + "<body></body></html>";
312 
313         loadPageVerifyTitle2(html);
314     }
315 
316     /**
317      * Test exception throw by an illegal DOM appendChild.
318      * @throws Exception if the test fails
319      */
320     @Test
321     @Alerts(DEFAULT = {"3", "true", "undefined", "undefined", "HIERARCHY_REQUEST_ERR: 3", "1"},
322             FF = {"3", "true", "26", "§§URL§§", "HIERARCHY_REQUEST_ERR: 3", "1"},
323             FF_ESR = {"3", "true", "26", "§§URL§§", "HIERARCHY_REQUEST_ERR: 3", "1"})
324     /*
325      * Messages:
326      * CHROME: "A Node was inserted somewhere it doesn't belong."
327      * FF: "Node cannot be inserted at the specified point in the hierarchy"
328      */
329     public void appendChild_illegal_node() throws Exception {
330         final String html = DOCTYPE_HTML
331             + "<html><head>\n"
332             + "<script>\n"
333             + LOG_TITLE_FUNCTION
334             + "function test() {\n"
335             + "  var htmlNode = document.documentElement;\n"
336             + "  var body = document.body;\n"
337             + "  try {\n"
338             + "    body.appendChild(htmlNode);\n"
339             + "  } catch(e) {\n"
340             + "    log(e.code);\n"
341             + "    log(e.message != null);\n"
342             + "    log(e.lineNumber);\n"
343             + "    log(e.filename);\n"
344             + "    log('HIERARCHY_REQUEST_ERR: ' + e.HIERARCHY_REQUEST_ERR);\n"
345             + "  }\n"
346             + "  log(body.childNodes.length);\n"
347             + "}\n"
348             + "</script></head>\n"
349             + "<body onload='test()'><span>hi</span></body></html>";
350 
351         expandExpectedAlertsVariables(URL_FIRST);
352         loadPageVerifyTitle2(html);
353     }
354 }