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