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   * Test for {@link External}.
23   *
24   * @author Peter Faller
25   * @author Ahmed Ashour
26   * @author Ronald Brill
27   */
28  public class ExternalTest extends WebDriverTestCase {
29  
30      /**
31       * @throws Exception if the test fails
32       */
33      @Test
34      @Alerts({"external defined", "no AutoCompleteSaveForm"})
35      public void autoCompleteSaveForm() throws Exception {
36          final String html = DOCTYPE_HTML
37              + "<html><head>\n"
38              + "<script>\n"
39              + LOG_TITLE_FUNCTION
40              + "function fnSaveForm() {\n"
41              + "  if (window.external) {\n"
42              + "    log('external defined');\n"
43              + "    if ('AutoCompleteSaveForm' in window.external) {\n"
44              + "      log('AutoCompleteSaveForm defined');\n"
45              + "      window.external.AutoCompleteSaveForm(oForm);\n"
46              + "      oForm.AutoCompleteTest.value = '';\n"
47              + "      oForm.AutoCompleteIgnore.value = '';\n"
48              + "    } else {\n"
49              + "      log('no AutoCompleteSaveForm');\n"
50              + "    }\n"
51              + "  } else {\n"
52              + "    log('no external');\n"
53              + "  }\n"
54              + "}\n"
55              + "</script>\n"
56              + "</head>\n"
57              + "<body onload='fnSaveForm()'>\n"
58              + "<form name='oForm'>\n"
59              + "This text is saved:\n"
60              + "<input type='text' name='AutoCompleteTest' value='abcdef'>\n"
61              + "This text is not saved:"
62              + "<input type='text' name='AutoCompleteIgnore' autocomplete='off' value='ghijklm'>\n"
63              + "</form>\n"
64              + "</body></html>";
65          loadPageVerifyTitle2(html);
66      }
67  
68      /**
69       * @throws Exception if the test fails
70       */
71      @Test
72      @Alerts("AddSearchProvider defined")
73      public void addSearchProvider() throws Exception {
74          final String html = DOCTYPE_HTML
75              + "<html><head>\n"
76              + "<script>\n"
77              + LOG_TITLE_FUNCTION
78              + "function test() {\n"
79              + "  if (window.external) {\n"
80              + "    if ('AddSearchProvider' in window.external) {\n"
81              + "      log('AddSearchProvider defined');\n"
82              + "    } else {\n"
83              + "      log('no AddSearchProvider');\n"
84              + "    }\n"
85              + "  } else {\n"
86              + "    log('no external');\n"
87              + "  }\n"
88              + "}\n"
89              + "</script>\n"
90              + "</head>\n"
91              + "<body onload='test()'>\n"
92              + "</body></html>";
93          loadPageVerifyTitle2(html);
94      }
95  
96      /**
97       * @throws Exception if the test fails
98       */
99      @Test
100     @Alerts({"IsSearchProviderInstalled defined", "IsSearchProviderInstalled: undefined"})
101     public void isSearchProviderInstalled() throws Exception {
102         final String html = DOCTYPE_HTML
103             + "<html><head>\n"
104             + "<script>\n"
105             + LOG_TITLE_FUNCTION
106             + "function test() {\n"
107             + "  if (window.external) {\n"
108             + "    if ('IsSearchProviderInstalled' in window.external) {\n"
109             + "      log('IsSearchProviderInstalled defined');\n"
110             + "      try {\n"
111             + "        var res = window.external.IsSearchProviderInstalled('http://htmlunit.sourceforge.net');\n"
112             + "        log('IsSearchProviderInstalled: ' + res);\n"
113             + "      } catch(e) { logEx(e); }\n"
114             + "    } else {\n"
115             + "      log('no IsSearchProviderInstalled');\n"
116             + "    }\n"
117             + "  } else {\n"
118             + "    log('no external');\n"
119             + "  }\n"
120             + "}\n"
121             + "</script>\n"
122             + "</head>\n"
123             + "<body onload='test()'>\n"
124             + "</body></html>";
125         loadPageVerifyTitle2(html);
126     }
127 
128     /**
129      * @throws Exception if the test fails
130      */
131     @Test
132     @Alerts(DEFAULT = {"true", "[object External]", "[object External]", "undefined", "[object External]",
133                        "true", "function External() { [native code] }", "function External() { [native code] }",
134                        "[object External]", "function () { [native code] }"},
135             FF = {"true", "[object External]", "[object External]", "undefined", "[object External]",
136                   "false", "undefined", "ReferenceError", "ReferenceError", "ReferenceError"},
137             FF_ESR = {"true", "[object External]", "[object External]", "undefined", "[object External]",
138                       "false", "undefined", "ReferenceError", "ReferenceError", "ReferenceError"})
139     public void windowScope() throws Exception {
140         final String html = DOCTYPE_HTML
141             + "<html></body>\n"
142             + "<script>\n"
143             + LOG_TITLE_FUNCTION
144             + "  log('external' in window);\n"
145             + "  log(window.external);\n"
146             + "  try { log(external); } catch(e) { logEx(e); };\n"
147             + "  try { log(external.prototype); } catch(e) { logEx(e); };\n"
148             + "  try { log(external.__proto__); } catch(e) { logEx(e); };\n"
149 
150             + "  log('External' in window);\n"
151             + "  log(window.External);\n"
152             + "  try { log(External); } catch(e) { logEx(e); };\n"
153             + "  try { log(External.prototype); } catch(e) { logEx(e); };\n"
154             + "  try { log(External.__proto__); } catch(e) { logEx(e); };\n"
155             + "</script>\n"
156             + "</body></html>";
157 
158         loadPageVerifyTitle2(html);
159     }
160 }