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.geo;
16  
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import org.htmlunit.WebClientOptions;
21  import org.htmlunit.WebDriverTestCase;
22  import org.htmlunit.junit.annotation.Alerts;
23  import org.junit.jupiter.api.Test;
24  import org.openqa.selenium.WebDriver;
25  import org.openqa.selenium.chrome.ChromeDriver;
26  import org.openqa.selenium.edge.EdgeDriver;
27  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
28  
29  /**
30   * Tests for {@link Geolocation}.
31   *
32   * @author Ahmed Ashour
33   * @author Frank Danek
34   * @author Ronald Brill
35   * @author cd alexndr
36   */
37  public class GeolocationTest extends WebDriverTestCase {
38  
39      /**
40       * @throws Exception if the test fails
41       */
42      @Test
43      @Alerts("TypeError")
44      public void ctor() throws Exception {
45          final String html = DOCTYPE_HTML
46              + "<html><head><script>\n"
47              + LOG_TITLE_FUNCTION
48              + "  function test() {\n"
49              + "    try {\n"
50              + "      new Geolocation();\n"
51              + "    } catch(e) { logEx(e) }\n"
52              + "  }\n"
53              + "</script></head><body onload='test()'>\n"
54              + "</body></html>";
55  
56          loadPageVerifyTitle2(html);
57      }
58  
59      /**
60       * @throws Exception if the test fails
61       */
62      @Test
63      @Alerts("[object Geolocation]")
64      public void navigatorGeolocation() throws Exception {
65          final String html = DOCTYPE_HTML
66              + "<html><head><script>\n"
67              + LOG_TITLE_FUNCTION
68              + "  function test() {\n"
69              + "    try {\n"
70              + "      log(navigator.geolocation);\n"
71              + "    } catch(e) { logEx(e) }\n"
72              + "  }\n"
73              + "</script></head><body onload='test()'>\n"
74              + "</body></html>";
75  
76          loadPageVerifyTitle2(html);
77      }
78  
79      /**
80       * @throws Exception if the test fails
81       */
82      @Test
83      @Alerts({"Latitude : 12.3456",
84               "Longitude: 7.654321",
85               "Accuracy: 0.1234"})
86      public void getCurrentPosition() throws Exception {
87          final String html = DOCTYPE_HTML
88              + "<html><head><script>\n"
89              + LOG_TITLE_FUNCTION
90              + "  function success(pos) {\n"
91              + "    const crd = pos.coords;\n"
92              + "    log(`Latitude : ${crd.latitude}`);\n"
93              + "    log(`Longitude: ${crd.longitude}`);\n"
94              + "    log(`Accuracy: ${crd.accuracy}`);\n"
95              + "  }\n"
96  
97              + "  function error(err) {\n"
98              + "    log(`ERROR(${err.code}): ${err.message}`);\n"
99              + "  }"
100 
101             + "  function test() {\n"
102             + "    try {\n"
103             + "      navigator.geolocation.getCurrentPosition(success, error);\n"
104             + "    } catch(e) { logEx(e) }\n"
105             + "  }\n"
106             + "</script></head>\n"
107             + "<body onload='test()'>\n"
108             + "</body></html>";
109 
110         final WebDriver driver = getWebDriver();
111         if (useRealBrowser()) {
112             final Map<String, Object> pos = new HashMap<>();
113             pos.put("latitude", 12.3456);
114             pos.put("longitude", 7.654321);
115             pos.put("accuracy", 0.1234);
116             if (driver instanceof ChromeDriver) {
117                 ((ChromeDriver) driver).executeCdpCommand("Emulation.setGeolocationOverride", pos);
118             }
119             else if (driver instanceof EdgeDriver) {
120                 ((EdgeDriver) driver).executeCdpCommand("Emulation.setGeolocationOverride", pos);
121             }
122         }
123         else {
124             final WebClientOptions.Geolocation geo =
125                     new WebClientOptions.Geolocation(12.3456, 7.654321, 0.1234, null, null, null, null);
126             ((HtmlUnitDriver) driver).getWebClient().getOptions().setGeolocationEnabled(true);
127             ((HtmlUnitDriver) driver).getWebClient().getOptions().setGeolocation(geo);
128         }
129 
130         loadPage2(html);
131         verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
132     }
133 }