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.html;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.htmlunit.util.MimeType;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  
24  /**
25   * Unit tests for {@link HTMLObjectElement}.
26   *
27   * @author Daniel Gredler
28   * @author Ahmed Ashour
29   * @author Ronald Brill
30   * @author Frank Danek
31   */
32  @RunWith(BrowserRunner.class)
33  public class HTMLObjectElement2Test extends WebDriverTestCase {
34  
35      /**
36       * @throws Exception if the test fails
37       */
38      @Test
39      @Alerts({"[object HTMLFormElement]", "null"})
40      public void form() throws Exception {
41          final String html = DOCTYPE_HTML
42              + "<html>\n"
43              + "<body>\n"
44              + "  <form>\n"
45              + "    <object id='o1'></object>\n"
46              + "</form>\n"
47              + "<object id='o2'></object>\n"
48              + "<script>\n"
49              + LOG_TITLE_FUNCTION
50              + "  log(document.getElementById('o1').form);\n"
51              + "  log(document.getElementById('o2').form);\n"
52              + "</script>\n"
53              + "</body></html>";
54  
55          loadPageVerifyTitle2(html);
56      }
57  
58      /**
59       * There was an error that this code throws an illegal state ex.
60       * @throws Exception if the test fails
61       */
62      @Test
63      @Alerts("[object XMLDocument]")
64      public void responseXML_htmlObject() throws Exception {
65          final String html = DOCTYPE_HTML
66              + "<html><head><script>\n"
67              + LOG_TITLE_FUNCTION
68              + "  function test() {\n"
69              + "    var xhr = new XMLHttpRequest();\n"
70              + "    xhr.open('GET', 'foo.xml', false);\n"
71              + "    xhr.send('');\n"
72              + "    try {\n"
73              + "      log(xhr.responseXML);\n"
74              + "    } catch(e) { logEx(e); }\n"
75              + "  }\n"
76              + "</script></head><body onload='test()'>\n"
77              + "</body></html>";
78  
79          final String xml = "<html xmlns='http://www.w3.org/1999/xhtml'>\n"
80                      + "<object classid='CLSID:test'/>\n"
81                      + "</html>";
82  
83          getMockWebConnection().setDefaultResponse(xml, MimeType.TEXT_XML);
84          loadPage2(html);
85          verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
86      }
87  }