Skip to content

Commit 409fbfc

Browse files
Delta456navin772
andauthored
[java][BiDi] emulation: allow passing null to GeolocationOverride (#16594)
Co-authored-by: Navin Chandra <[email protected]>
1 parent 3114b93 commit 409fbfc

File tree

2 files changed

+60
-18
lines changed

2 files changed

+60
-18
lines changed

java/src/org/openqa/selenium/bidi/emulation/SetGeolocationOverrideParameters.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ public class SetGeolocationOverrideParameters extends AbstractOverrideParameters
2222
// Constructor for coordinates - must specify either contexts or userContexts later
2323
public SetGeolocationOverrideParameters(GeolocationCoordinates coordinates) {
2424
if (coordinates == null) {
25-
throw new IllegalArgumentException("GeolocationCoordinates cannot be null");
25+
map.put("coordinates", null);
26+
} else {
27+
map.put("coordinates", coordinates.toMap());
2628
}
27-
map.put("coordinates", coordinates.toMap());
2829
}
2930

3031
// Constructor for error - must specify either contexts or userContexts later

java/test/org/openqa/selenium/bidi/emulation/SetGeolocationOverrideTest.java

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.openqa.selenium.bidi.emulation;
1919

2020
import static java.lang.Math.abs;
21+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
2122
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
2223

2324
import java.util.List;
@@ -90,18 +91,15 @@ void canSetGeolocationOverrideWithCoordinatesInContext() {
9091
Object result = getBrowserGeolocation(driver, null, origin);
9192
Map<String, Object> r = ((Map<String, Object>) result);
9293

93-
assert !r.containsKey("error") : "Geolocation failed with error: " + r.get("error");
94+
assertThat(r.containsKey("error")).isFalse();
9495

9596
double latitude = ((Number) r.get("latitude")).doubleValue();
9697
double longitude = ((Number) r.get("longitude")).doubleValue();
9798
double accuracy = ((Number) r.get("accuracy")).doubleValue();
9899

99-
assert abs(latitude - coords.getLatitude()) < 0.0001
100-
: "Latitude mismatch: expected " + coords.getLatitude() + ", got " + latitude;
101-
assert abs(longitude - coords.getLongitude()) < 0.0001
102-
: "Longitude mismatch: expected " + coords.getLongitude() + ", got " + longitude;
103-
assert abs(accuracy - coords.getAccuracy()) < 0.0001
104-
: "Accuracy mismatch: expected " + coords.getAccuracy() + ", got " + accuracy;
100+
assertThat(abs(latitude - coords.getLatitude())).isLessThan(0.0001);
101+
assertThat(abs(longitude - coords.getLongitude())).isLessThan(0.0001);
102+
assertThat(abs(accuracy - coords.getAccuracy())).isLessThan(0.0001);
105103
}
106104

107105
@Test
@@ -134,15 +132,15 @@ void canSetGeolocationOverrideWithMultipleUserContexts() {
134132
Map<String, Object> r =
135133
(Map<String, Object>) getBrowserGeolocation(driver, userContext1, origin1);
136134

137-
assert !r.containsKey("error") : "Context1 geolocation failed with error: " + r.get("error");
135+
assertThat(r.containsKey("error")).isFalse();
138136

139137
double latitude1 = ((Number) r.get("latitude")).doubleValue();
140138
double longitude1 = ((Number) r.get("longitude")).doubleValue();
141139
double accuracy1 = ((Number) r.get("accuracy")).doubleValue();
142140

143-
assert abs(latitude1 - coords.getLatitude()) < 0.0001 : "Context1 latitude mismatch";
144-
assert abs(longitude1 - coords.getLongitude()) < 0.0001 : "Context1 longitude mismatch";
145-
assert abs(accuracy1 - coords.getAccuracy()) < 0.0001 : "Context1 accuracy mismatch";
141+
assertThat(abs(latitude1 - coords.getLatitude())).isLessThan(0.0001);
142+
assertThat(abs(longitude1 - coords.getLongitude())).isLessThan(0.0001);
143+
assertThat(abs(accuracy1 - coords.getAccuracy())).isLessThan(0.0001);
146144

147145
driver.switchTo().window(context2.getId());
148146
String url2 = appServer.whereIsSecure("blank.html");
@@ -154,15 +152,15 @@ void canSetGeolocationOverrideWithMultipleUserContexts() {
154152
Map<String, Object> r2 =
155153
(Map<String, Object>) getBrowserGeolocation(driver, userContext2, origin2);
156154

157-
assert !r2.containsKey("error") : "Context2 geolocation failed with error: " + r2.get("error");
155+
assertThat(r2.containsKey("error")).isFalse();
158156

159157
double latitude2 = ((Number) r2.get("latitude")).doubleValue();
160158
double longitude2 = ((Number) r2.get("longitude")).doubleValue();
161159
double accuracy2 = ((Number) r2.get("accuracy")).doubleValue();
162160

163-
assert abs(latitude2 - coords.getLatitude()) < 0.0001 : "Context2 latitude mismatch";
164-
assert abs(longitude2 - coords.getLongitude()) < 0.0001 : "Context2 longitude mismatch";
165-
assert abs(accuracy2 - coords.getAccuracy()) < 0.0001 : "Context2 accuracy mismatch";
161+
assertThat(abs(latitude2 - coords.getLatitude())).isLessThan(0.0001);
162+
assertThat(abs(longitude2 - coords.getLongitude())).isLessThan(0.0001);
163+
assertThat(abs(accuracy2 - coords.getAccuracy())).isLessThan(0.0001);
166164

167165
context1.close();
168166
context2.close();
@@ -171,6 +169,7 @@ void canSetGeolocationOverrideWithMultipleUserContexts() {
171169
}
172170

173171
@Test
172+
@NeedsFreshDriver
174173
@Ignore(FIREFOX)
175174
void canSetGeolocationOverrideWithError() {
176175

@@ -192,7 +191,49 @@ void canSetGeolocationOverrideWithError() {
192191
Object result = getBrowserGeolocation(driver, null, origin);
193192
Map<String, Object> r = ((Map<String, Object>) result);
194193

195-
assert r.containsKey("error") : "Expected geolocation to fail with error, but got: " + r;
194+
assertThat(r.containsKey("error")).isTrue();
195+
196+
context.close();
197+
}
198+
199+
@Test
200+
@NeedsFreshDriver
201+
void canResetGeolocationOverrideWithNullCoordinates() {
202+
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
203+
String contextId = context.getId();
204+
205+
String url = appServer.whereIsSecure("blank.html");
206+
context.navigate(url, ReadinessState.COMPLETE);
207+
driver.switchTo().window(context.getId());
208+
209+
String origin =
210+
(String) ((JavascriptExecutor) driver).executeScript("return window.location.origin;");
211+
212+
Emulation emul = new Emulation(driver);
213+
214+
GeolocationCoordinates coords = new GeolocationCoordinates(37.7749, -122.4194);
215+
emul.setGeolocationOverride(
216+
new SetGeolocationOverrideParameters(coords).contexts(List.of(contextId)));
217+
218+
Object firstResult = getBrowserGeolocation(driver, null, origin);
219+
Map<String, Object> r1 = ((Map<String, Object>) firstResult);
220+
221+
assertThat(r1.containsKey("error")).isFalse();
222+
double latitude1 = ((Number) r1.get("latitude")).doubleValue();
223+
double longitude1 = ((Number) r1.get("longitude")).doubleValue();
224+
225+
assertThat(abs(latitude1 - coords.getLatitude())).isLessThan(0.0001);
226+
assertThat(abs(longitude1 - coords.getLongitude())).isLessThan(0.0001);
227+
228+
emul.setGeolocationOverride(
229+
new SetGeolocationOverrideParameters((GeolocationCoordinates) null)
230+
.contexts(List.of(contextId)));
231+
232+
Object secondResult = getBrowserGeolocation(driver, null, origin);
233+
Map<String, Object> r2 = ((Map<String, Object>) secondResult);
234+
235+
// Error because there's no real geolocation available
236+
assertThat(r2.containsKey("error")).isTrue();
196237

197238
context.close();
198239
}

0 commit comments

Comments
 (0)