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.worker;
16  
17  import java.net.URL;
18  
19  import org.htmlunit.WebDriverTestCase;
20  import org.htmlunit.junit.BrowserRunner;
21  import org.htmlunit.junit.annotation.Alerts;
22  import org.htmlunit.util.MimeType;
23  import org.junit.Test;
24  import org.junit.runner.RunWith;
25  
26  /**
27   * Unit tests for {@link WorkerNavigator}.
28   *
29   * @author Ronald Brill
30   */
31  @RunWith(BrowserRunner.class)
32  public class WorkerNavigatorTest extends WebDriverTestCase {
33  
34      /**
35       * @throws Exception if the test fails
36       */
37      @Test
38      @Alerts("[object\\sWorkerNavigator]")
39      public void navigator() throws Exception {
40          final String workerJs = "postMessage('' + navigator);\n";
41  
42          expandExpectedAlertsVariables(URL_FIRST);
43          testJs(workerJs);
44      }
45  
46      /**
47       * @throws Exception if the test fails
48       */
49      @Test
50      @Alerts("object")
51      public void typeOf() throws Exception {
52          final String workerJs = "postMessage(typeof navigator);\n";
53          testJs(workerJs);
54      }
55  
56      /**
57       * @throws Exception if the test fails
58       */
59      @Test
60      @Alerts("Mozilla")
61      public void appCodeName() throws Exception {
62          final String workerJs = "postMessage(navigator.appCodeName);\n";
63  
64          expandExpectedAlertsVariables(URL_FIRST);
65          testJs(workerJs);
66      }
67  
68      /**
69       * @throws Exception if the test fails
70       */
71      @Test
72      @Alerts("Netscape")
73      public void appName() throws Exception {
74          final String workerJs = "postMessage(navigator.appName);\n";
75  
76          expandExpectedAlertsVariables(URL_FIRST);
77          testJs(workerJs);
78      }
79  
80      /**
81       * @throws Exception if the test fails
82       */
83      @Test
84      @Alerts(CHROME = "5.0\\s(Windows\\sNT\\s10.0;\\sWin64;\\sx64)\\sAppleWebKit/537.36\\s"
85                          + "(KHTML,\\slike\\sGecko)\\sChrome/136.0.0.0\\sSafari/537.36",
86              EDGE = "5.0\\s(Windows\\sNT\\s10.0;\\sWin64;\\sx64)\\sAppleWebKit/537.36\\s"
87                          + "(KHTML,\\slike\\sGecko)\\sChrome/136.0.0.0\\sSafari/537.36\\sEdg/136.0.0.0",
88              FF = "5.0\\s(Windows)",
89              FF_ESR = "5.0\\s(Windows)")
90      public void appVersion() throws Exception {
91          final String workerJs = "postMessage(navigator.appVersion);\n";
92  
93          expandExpectedAlertsVariables(URL_FIRST);
94          testJs(workerJs);
95      }
96  
97      /**
98       * @throws Exception if the test fails
99       */
100     @Test
101     @Alerts(DEFAULT = {},
102             FF = "undefined",
103             FF_ESR = "undefined")
104     public void connection() throws Exception {
105         final String workerJs = "postMessage(navigator.connection);\n";
106 
107         expandExpectedAlertsVariables(URL_FIRST);
108         testJs(workerJs);
109     }
110 
111     /**
112      * @throws Exception if the test fails
113      */
114     @Test
115     @Alerts("en-US")
116     public void language() throws Exception {
117         final String workerJs = "postMessage(navigator.language);\n";
118 
119         expandExpectedAlertsVariables(URL_FIRST);
120         testJs(workerJs);
121     }
122 
123     /**
124      * @throws Exception if the test fails
125      */
126     @Test
127     @Alerts("en-US,en")
128     public void languages() throws Exception {
129         final String workerJs = "postMessage(navigator.languages);\n";
130 
131         expandExpectedAlertsVariables(URL_FIRST);
132         testJs(workerJs);
133     }
134 
135     /**
136      * @throws Exception if the test fails
137      */
138     @Test
139     @Alerts("Win32")
140     public void platform() throws Exception {
141         final String workerJs = "postMessage(navigator.platform);\n";
142 
143         expandExpectedAlertsVariables(URL_FIRST);
144         testJs(workerJs);
145     }
146 
147     /**
148      * @throws Exception if the test fails
149      */
150     @Test
151     @Alerts("Gecko")
152     public void product() throws Exception {
153         final String workerJs = "postMessage(navigator.product);\n";
154 
155         expandExpectedAlertsVariables(URL_FIRST);
156         testJs(workerJs);
157     }
158 
159     /**
160      * @throws Exception if the test fails
161      */
162     @Test
163     @Alerts(CHROME = "Mozilla/5.0\\s(Windows\\sNT\\s10.0;\\sWin64;\\sx64)\\sAppleWebKit/537.36\\s"
164                         + "(KHTML,\\slike\\sGecko)\\sChrome/136.0.0.0\\sSafari/537.36",
165             EDGE = "Mozilla/5.0\\s(Windows\\sNT\\s10.0;\\sWin64;\\sx64)\\sAppleWebKit/537.36\\s"
166                         + "(KHTML,\\slike\\sGecko)\\sChrome/136.0.0.0\\sSafari/537.36\\sEdg/136.0.0.0",
167             FF = "Mozilla/5.0\\s(Windows\\sNT\\s10.0;\\sWin64;\\sx64;\\srv:138.0)\\sGecko/20100101\\sFirefox/138.0",
168             FF_ESR = "Mozilla/5.0\\s(Windows\\sNT\\s10.0;\\sWin64;\\sx64;\\srv:128.0)\\sGecko/20100101\\sFirefox/128.0")
169     public void userAgent() throws Exception {
170         final String workerJs = "postMessage(navigator.userAgent);\n";
171 
172         expandExpectedAlertsVariables(URL_FIRST);
173         testJs(workerJs);
174     }
175 
176     /**
177      * @throws Exception if the test fails
178      */
179     @Test
180     @Alerts("exception")
181     public void ctor() throws Exception {
182         final String workerJs =
183                 "try {\n"
184                 + "  var l = new WorkerNavigator();\n"
185                 + "  postMessage(l);\n"
186                 + "} catch(e) {\n"
187                 + "  postMessage('exception');\n"
188                 + "}";
189         testJs(workerJs);
190     }
191 
192     private void testJs(final String workerJs) throws Exception {
193         final String html = DOCTYPE_HTML
194             + "<html><body>\n"
195             + "<script async>\n"
196             + LOG_TITLE_FUNCTION_NORMALIZE
197             + "try {\n"
198             + "  var myWorker = new Worker('worker.js#somehash');\n"
199             + "  myWorker.onmessage = function(e) {\n"
200             + "    log(e.data);\n"
201             + "  };\n"
202             + "} catch(e) { logEx(e); }\n"
203             + "</script></body></html>\n";
204 
205         getMockWebConnection().setResponse(new URL(URL_FIRST, "worker.js"),
206                 workerJs, MimeType.TEXT_JAVASCRIPT);
207 
208         loadPage2(html);
209         verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
210     }
211 }