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