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 MimeType}.
23   *
24   * @author Marc Guillemot
25   * @author Ronald Brill
26   */
27  public class MimeTypeTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception if the test fails
31       */
32      @Test
33      @Alerts({"2", "pdf", "Portable Document Format", "application/pdf",
34               "pdf", "Portable Document Format", "text/pdf"})
35      public void mimeType() throws Exception {
36          final String html = DOCTYPE_HTML
37              + "<html><head><script>\n"
38              + LOG_TITLE_FUNCTION
39              + "function test() {\n"
40              + "  var mimeTypes = navigator.mimeTypes;\n"
41              + "  log(mimeTypes.length);\n"
42  
43              + "  log(mimeTypes.item(0).suffixes);\n"
44              + "  log(mimeTypes.item(0).description);\n"
45              + "  log(mimeTypes.item(0).type);\n"
46  
47              + "  log(mimeTypes.item(1).suffixes);\n"
48              + "  log(mimeTypes.item(1).description);\n"
49              + "  log(mimeTypes.item(1).type);\n"
50              + "}\n"
51              + "</script></head>\n"
52              + "<body onload='test()'></body></html>";
53  
54          loadPageVerifyTitle2(html);
55      }
56  
57      /**
58       * @throws Exception if the test fails
59       */
60      @Test
61      @Alerts({"true", "false"})
62      public void in() throws Exception {
63          final String html = DOCTYPE_HTML
64              + "<html><head><script>\n"
65              + LOG_TITLE_FUNCTION
66              + "function test() {\n"
67              + "  log('application/pdf' in navigator.mimeTypes);\n"
68              + "  log('pdf' in navigator.mimeTypes);\n"
69              + "}\n"
70              + "</script></head>\n"
71              + "<body onload='test()'></body></html>";
72  
73          loadPageVerifyTitle2(html);
74      }
75  
76      /**
77       * Tests default configuration of Pdf plugin.
78       * @throws Exception if the test fails
79       */
80      @Test
81      @Alerts({"[object MimeType]", "pdf", "PDF Viewer", "true", "true"})
82      public void pdfMimeType() throws Exception {
83          final String html = DOCTYPE_HTML
84              + "<html><head><script>\n"
85              + LOG_TITLE_FUNCTION
86              + "function test() {\n"
87              + "  var mimeTypePdf = navigator.mimeTypes['application/pdf'];\n"
88              + "  log(mimeTypePdf);\n"
89              + "  if (mimeTypePdf) {\n"
90              + "    log(mimeTypePdf.suffixes);\n"
91              + "    var pluginPdf = mimeTypePdf.enabledPlugin;\n"
92              + "    log(pluginPdf.name);\n"
93              + "    log(pluginPdf == navigator.plugins[pluginPdf.name]);\n"
94              + "    log(pluginPdf == navigator.plugins.namedItem(pluginPdf.name));\n"
95              + "  }\n"
96              + "}\n"
97              + "</script></head>\n"
98              + "<body onload='test()'></body></html>";
99  
100         loadPageVerifyTitle2(html);
101     }
102 
103     /**
104      * Tests default configuration of Pdf plugin.
105      * @throws Exception if the test fails
106      */
107     @Test
108     @Alerts({"[object MimeType]", "pdf", "PDF Viewer", "true", "true"})
109     public void textPdfMimeType() throws Exception {
110         final String html = DOCTYPE_HTML
111             + "<html><head><script>\n"
112             + LOG_TITLE_FUNCTION
113             + "function test() {\n"
114             + "  var mimeTypePdf = navigator.mimeTypes['text/pdf'];\n"
115             + "  log(mimeTypePdf);\n"
116             + "  if (mimeTypePdf) {\n"
117             + "    log(mimeTypePdf.suffixes);\n"
118             + "    var pluginPdf = mimeTypePdf.enabledPlugin;\n"
119             + "    log(pluginPdf.name);\n"
120             + "    log(pluginPdf == navigator.plugins[pluginPdf.name]);\n"
121             + "    log(pluginPdf == navigator.plugins.namedItem(pluginPdf.name));\n"
122             + "  }\n"
123             + "}\n"
124             + "</script></head>\n"
125             + "<body onload='test()'></body></html>";
126 
127         loadPageVerifyTitle2(html);
128     }
129 
130     /**
131      * Tests default configuration of Flash plugin for Firefox.
132      * @throws Exception if the test fails
133      */
134     @Test
135     @Alerts("undefined")
136     public void flashMimeType() throws Exception {
137         final String html = DOCTYPE_HTML
138             + "<html><head><script>\n"
139             + LOG_TITLE_FUNCTION
140             + "function test() {\n"
141             + "  var mimeTypeFlash = navigator.mimeTypes['application/x-shockwave-flash'];\n"
142             + "  log(mimeTypeFlash);\n"
143             + "}\n"
144             + "</script></head>\n"
145             + "<body onload='test()'></body></html>";
146 
147         loadPageVerifyTitle2(html);
148     }
149 
150     /**
151      * @throws Exception if the test fails
152      */
153     @Test
154     @Alerts({"2", "application/pdf", "text/pdf"})
155     public void iterator() throws Exception {
156         final String html = DOCTYPE_HTML
157             + "<html><head><script>\n"
158             + LOG_TITLE_FUNCTION
159             + "function test() {\n"
160             + "  var mimeTypes = navigator.mimeTypes;\n"
161             + "  log(mimeTypes.length);\n"
162 
163             + "  let iter = mimeTypes[Symbol.iterator]();\n"
164             + "  for (const mime of iter) {\n"
165             + "    log(mime.type);\n"
166             + "  }"
167             + "}\n"
168             + "</script></head>\n"
169             + "<body onload='test()'></body></html>";
170 
171         loadPageVerifyTitle2(html);
172     }
173 }