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   * Unit tests for {@link Plugin}.
25   *
26   * @author Ronald Brill
27   */
28  @RunWith(BrowserRunner.class)
29  public class PluginTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if an error occurs
33       */
34      @Test
35      @Alerts({"5",
36          "PDF Viewer", "Portable Document Format", "internal-pdf-viewer", "undefined",
37          "Chrome PDF Viewer", "Portable Document Format", "internal-pdf-viewer", "undefined",
38          "Chromium PDF Viewer", "Portable Document Format", "internal-pdf-viewer", "undefined",
39          "Microsoft Edge PDF Viewer", "Portable Document Format", "internal-pdf-viewer", "undefined",
40          "WebKit built-in PDF", "Portable Document Format", "internal-pdf-viewer", "undefined"})
41      public void plugins() throws Exception {
42          final String html = DOCTYPE_HTML
43                  + "<html>\n"
44                  + "<head>\n"
45                  + "  <script>\n"
46                  + LOG_TITLE_FUNCTION
47                  + "    function doTest() {\n"
48                  + "      log(window.navigator.plugins.length);\n"
49                  + "      for (var i = 0; i < window.navigator.plugins.length; i++) {\n"
50                  + "        let pl = window.navigator.plugins[i];\n"
51                  + "        log(pl.name);\n"
52                  + "        log(pl.description);\n"
53                  + "        log(pl.filename);\n"
54                  + "        log(pl.version);\n"
55                  + "      }\n"
56                  + "    }\n"
57                  + "  </script>\n"
58                  + "</head>\n"
59                  + "<body onload='doTest()'>\n"
60                  + "</body>\n"
61                  + "</html>";
62  
63          loadPageVerifyTitle2(html);
64      }
65  
66      /**
67       * @throws Exception if the test fails
68       */
69      @Test
70      @Alerts("Chromium PDF Viewer")
71      public void index() throws Exception {
72          final String html = DOCTYPE_HTML
73              + "<html><head><script>\n"
74              + LOG_TITLE_FUNCTION
75              + "function test() {\n"
76              + "  log(navigator.plugins[2].name);\n"
77              + "}\n"
78              + "</script></head>\n"
79              + "<body onload='test()'></body></html>";
80  
81          loadPageVerifyTitle2(html);
82      }
83  
84      /**
85       * @throws Exception if the test fails
86       */
87      @Test
88      @Alerts("Microsoft Edge PDF Viewer")
89      public void item() throws Exception {
90          final String html = DOCTYPE_HTML
91              + "<html><head><script>\n"
92              + LOG_TITLE_FUNCTION
93              + "function test() {\n"
94              + "  log(navigator.plugins.item(3).name);\n"
95              + "}\n"
96              + "</script></head>\n"
97              + "<body onload='test()'></body></html>";
98  
99          loadPageVerifyTitle2(html);
100     }
101 
102     /**
103      * @throws Exception if the test fails
104      */
105     @Test
106     @Alerts("Chromium PDF Viewer")
107     public void namedItem() throws Exception {
108         final String html = DOCTYPE_HTML
109             + "<html><head><script>\n"
110             + LOG_TITLE_FUNCTION
111             + "function test() {\n"
112             + "  log(navigator.plugins.namedItem('Chromium PDF Viewer').name);\n"
113             + "}\n"
114             + "</script></head>\n"
115             + "<body onload='test()'></body></html>";
116 
117         loadPageVerifyTitle2(html);
118     }
119 
120     /**
121      * @throws Exception if the test fails
122      */
123     @Test
124     @Alerts({"true", "false"})
125     public void in() throws Exception {
126         final String html = DOCTYPE_HTML
127             + "<html><head><script>\n"
128             + LOG_TITLE_FUNCTION
129             + "function test() {\n"
130             + "  log('PDF Viewer' in navigator.plugins);\n"
131             + "  log('pdf' in navigator.plugins);\n"
132             + "}\n"
133             + "</script></head>\n"
134             + "<body onload='test()'></body></html>";
135 
136         loadPageVerifyTitle2(html);
137     }
138 
139     /**
140      * @throws Exception if the test fails
141      */
142     @Test
143     @Alerts({"2", "[object MimeType]", "application/pdf", "[object MimeType]", "text/pdf", "undefined"})
144     public void pluginIndex() throws Exception {
145         final String html = DOCTYPE_HTML
146             + "<html><head><script>\n"
147             + LOG_TITLE_FUNCTION
148             + "function test() {\n"
149             + "  let pl = navigator.plugins.item(4);\n"
150 
151             + "  log(pl.length);\n"
152             + "  log(pl[0]);\n"
153             + "  log(pl[0].type);\n"
154 
155             + "  log(pl[1]);\n"
156             + "  log(pl[1].type);\n"
157 
158             + "  log(pl[2]);\n"
159             + "}\n"
160             + "</script></head>\n"
161             + "<body onload='test()'></body></html>";
162 
163         loadPageVerifyTitle2(html);
164     }
165 
166     /**
167      * @throws Exception if the test fails
168      */
169     @Test
170     @Alerts({"2", "[object MimeType]", "application/pdf", "[object MimeType]", "text/pdf", "null"})
171     public void pluginItem() throws Exception {
172         final String html = DOCTYPE_HTML
173             + "<html><head><script>\n"
174             + LOG_TITLE_FUNCTION
175             + "function test() {\n"
176             + "  let pl = navigator.plugins.item(4);\n"
177 
178             + "  log(pl.length);\n"
179             + "  log(pl.item(0));\n"
180             + "  log(pl.item(0).type);\n"
181 
182             + "  log(pl.item(1));\n"
183             + "  log(pl.item(1).type);\n"
184 
185             + "  log(pl.item(2));\n"
186             + "}\n"
187             + "</script></head>\n"
188             + "<body onload='test()'></body></html>";
189 
190         loadPageVerifyTitle2(html);
191     }
192 
193     /**
194      * @throws Exception if the test fails
195      */
196     @Test
197     @Alerts({"[object MimeType]", "application/pdf", "[object MimeType]", "application/pdf", "null"})
198     public void pluginNamedItem() throws Exception {
199         final String html = DOCTYPE_HTML
200             + "<html><head><script>\n"
201             + LOG_TITLE_FUNCTION
202             + "function test() {\n"
203             + "  let pl = navigator.plugins.item(4);\n"
204 
205             + "  log(pl.namedItem('application/pdf'));\n"
206             + "  log(pl.namedItem('application/pdf').type);\n"
207 
208             + "  log(pl.namedItem('text/pdf'));\n"
209             + "  log(pl.namedItem('application/pdf').type);\n"
210 
211             + "  log(pl.namedItem('unknown'));\n"
212             + "}\n"
213             + "</script></head>\n"
214             + "<body onload='test()'></body></html>";
215 
216         loadPageVerifyTitle2(html);
217     }
218 
219     /**
220      * @throws Exception if the test fails
221      */
222     @Test
223     @Alerts({"5", "PDF Viewer", "Chrome PDF Viewer", "Chromium PDF Viewer",
224              "Microsoft Edge PDF Viewer", "WebKit built-in PDF"})
225     public void iterator() throws Exception {
226         final String html = DOCTYPE_HTML
227             + "<html><head><script>\n"
228             + LOG_TITLE_FUNCTION
229             + "function test() {\n"
230             + "  var plugs = navigator.plugins;\n"
231             + "  log(plugs.length);\n"
232 
233             + "  let iter = plugs[Symbol.iterator]();\n"
234             + "  for (const plug of iter) {\n"
235             + "    log(plug.name);\n"
236             + "  }"
237             + "}\n"
238             + "</script></head>\n"
239             + "<body onload='test()'></body></html>";
240 
241         loadPageVerifyTitle2(html);
242     }
243 
244     /**
245      * @throws Exception if the test fails
246      */
247     @Test
248     @Alerts({"2", "application/pdf", "text/pdf"})
249     public void pluginIterator() throws Exception {
250         final String html = DOCTYPE_HTML
251             + "<html><head><script>\n"
252             + LOG_TITLE_FUNCTION
253             + "function test() {\n"
254             + "  let pl = navigator.plugins.item(4);\n"
255             + "  log(pl.length);\n"
256 
257             + "  let iter = pl[Symbol.iterator]();\n"
258             + "  for (const plug of iter) {\n"
259             + "    log(plug.type);\n"
260             + "  }"
261             + "}\n"
262             + "</script></head>\n"
263             + "<body onload='test()'></body></html>";
264 
265         loadPageVerifyTitle2(html);
266     }
267 }