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