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