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