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;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link ActiveXObject}.
23   *
24   * @author Marc Guillemot
25   * @author Ahmed Ashour
26   * @author Frank Danek
27   * @author Ronald Brill
28   */
29  public class ActiveXObject2Test extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if the test fails
33       */
34      @Test
35      @Alerts({"undefined", "undefined", "NaN", "false", "No", "No", "No", "No"})
36      public void browserDetection() throws Exception {
37          final String html = DOCTYPE_HTML
38              + "<html>\n"
39              + "<head>\n"
40              + "<script>\n"
41              + LOG_TITLE_FUNCTION_NORMALIZE
42              + "  function test() {\n"
43              + "    log(typeof window.ActiveXObject);\n"
44              + "    log(String(window.ActiveXObject));\n"
45              + "    log(Number(window.ActiveXObject));\n"
46              + "    log(Boolean(window.ActiveXObject));\n"
47              + "    log(window.ActiveXObject ? 'Yes' : 'No');\n"
48              + "    if (window.ActiveXObject) { log('Yes') } else { log('No') }\n"
49              + "    log(('ActiveXObject' in window) ? 'Yes' : 'No');\n"
50              + "    if ('ActiveXObject' in window) { log('Yes') } else { log('No') }\n"
51              + "  }\n"
52              + "</script>\n"
53              + "</head>\n"
54              + "<body onload='test()'>\n"
55              + "</body></html>";
56          loadPageVerifyTitle2(html);
57      }
58  
59      /**
60       * @throws Exception if the test fails
61       */
62      @Test
63      @Alerts("ReferenceError")
64      public void xmlDocument() throws Exception {
65          final String html = DOCTYPE_HTML
66              + "<html>\n"
67              + "<head>\n"
68              + "<script>\n"
69              + LOG_TITLE_FUNCTION
70              + "  function test() {\n"
71              + "    try {\n"
72              + "      var doc = new ActiveXObject('Microsoft.XMLDOM');\n"
73              + "      log(typeof doc);\n"
74              + "    } catch(e) { logEx(e); }\n"
75              + "  }\n"
76              + "</script></head>\n"
77              + "<body onload='test()'>\n"
78              + "</body></html>";
79  
80          loadPageVerifyTitle2(html);
81      }
82  
83      /**
84       * @throws Exception if the test fails
85       */
86      @Test
87      @Alerts("ActiveXObject undefined")
88      public void activex() throws Exception {
89          final String html = DOCTYPE_HTML
90              + "<html>\n"
91              + "<head>\n"
92              + "<script>\n"
93              + LOG_TITLE_FUNCTION
94              + "  function test() {\n"
95              + "    try {\n"
96              + "      if ('ActiveXObject' in window) {\n"
97              + "        new ActiveXObject('InternetExplorer.Application');\n"
98              + "      } else {\n"
99              + "        log('ActiveXObject undefined');\n"
100             + "      }\n"
101             + "    } catch(e) {logEx(e);}\n"
102             + "  }\n"
103             + "</script></head><body onload='test()'>\n"
104             + "</body></html>";
105 
106         loadPageVerifyTitle2(html);
107     }
108 }