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 WorkerLocation}.
28   *
29   * @author Ronald Brill
30   */
31  @RunWith(BrowserRunner.class)
32  public class WorkerLocationTest extends WebDriverTestCase {
33  
34      /**
35       * @throws Exception if the test fails
36       */
37      @Test
38      @Alerts("§§URL§§worker.js#somehash")
39      public void location() throws Exception {
40          final String workerJs = "postMessage('' + location);\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 location);\n";
53          testJs(workerJs);
54      }
55  
56      /**
57       * @throws Exception if the test fails
58       */
59      @Test
60      @Alerts("§§URL§§worker.js#somehash")
61      public void href() throws Exception {
62          final String workerJs = "postMessage(location.href);\n";
63  
64          expandExpectedAlertsVariables(URL_FIRST);
65          testJs(workerJs);
66      }
67  
68      /**
69       * @throws Exception if the test fails
70       */
71      @Test
72      @Alerts("http:")
73      public void protocol() throws Exception {
74          final String workerJs = "postMessage(location.protocol);\n";
75          testJs(workerJs);
76      }
77  
78      /**
79       * @throws Exception if the test fails
80       */
81      @Test
82      @Alerts("§§URL§§")
83      public void host() throws Exception {
84          final String workerJs = "postMessage(location.host);\n";
85  
86          expandExpectedAlertsVariables("localhost:" + PORT);
87          testJs(workerJs);
88      }
89  
90      /**
91       * @throws Exception if the test fails
92       */
93      @Test
94      @Alerts("localhost")
95      public void hostname() throws Exception {
96          final String workerJs = "postMessage(location.hostname);\n";
97          testJs(workerJs);
98      }
99  
100     /**
101      * @throws Exception if the test fails
102      */
103     @Test
104     @Alerts("§§URL§§")
105     public void origin() throws Exception {
106         final String workerJs = "postMessage(location.origin);\n";
107 
108         expandExpectedAlertsVariables("http://localhost:" + PORT);
109         testJs(workerJs);
110     }
111 
112     /**
113      * @throws Exception if the test fails
114      */
115     @Test
116     @Alerts("§§URL§§")
117     public void port() throws Exception {
118         final String workerJs = "postMessage(location.port);\n";
119 
120         expandExpectedAlertsVariables("" + PORT);
121         testJs(workerJs);
122     }
123 
124     /**
125      * @throws Exception if the test fails
126      */
127     @Test
128     @Alerts("/worker.js")
129     public void pathname() throws Exception {
130         final String workerJs = "postMessage(location.pathname);\n";
131         testJs(workerJs);
132     }
133 
134     /**
135      * @throws Exception if the test fails
136      */
137     @Test
138     @Alerts("")
139     public void search() throws Exception {
140         final String workerJs = "postMessage(location.search);\n";
141         testJs(workerJs);
142     }
143 
144     /**
145      * @throws Exception if the test fails
146      */
147     @Test
148     @Alerts("#somehash")
149     public void hash() throws Exception {
150         final String workerJs = "postMessage(location.hash);\n";
151         testJs(workerJs);
152     }
153 
154     /**
155      * @throws Exception if the test fails
156      */
157     @Test
158     @Alerts("exception")
159     public void ctor() throws Exception {
160         final String workerJs =
161                 "try {\n"
162                 + "  var l = new WorkerLocation();\n"
163                 + "  postMessage(l);\n"
164                 + "} catch(e) {\n"
165                 + "  postMessage('exception');\n"
166                 + "}";
167         testJs(workerJs);
168     }
169 
170     private void testJs(final String workerJs) throws Exception {
171         final String html = DOCTYPE_HTML
172             + "<html><body>\n"
173             + "<script async>\n"
174             + LOG_TITLE_FUNCTION_NORMALIZE
175             + "try {\n"
176             + "  var myWorker = new Worker('worker.js#somehash');\n"
177             + "  myWorker.onmessage = function(e) {\n"
178             + "    log(e.data);\n"
179             + "  };\n"
180             + "} catch(e) { logEx(e); }\n"
181             + "</script></body></html>\n";
182 
183         getMockWebConnection().setResponse(new URL(URL_FIRST, "worker.js"),
184                 workerJs, MimeType.TEXT_JAVASCRIPT);
185 
186         loadPage2(html);
187         verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
188     }
189 }