Skip to content

Commit 155e875

Browse files
committed
use java pattern for setgeolocationoverrideparameters
1 parent ef53996 commit 155e875

File tree

2 files changed

+39
-44
lines changed

2 files changed

+39
-44
lines changed

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

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//
99
// http://www.apache.org/licenses/LICENSE-2.0
1010
//
11-
// Unless required by applicable law or agreed to in writing,
11+
// Unless required by applicable law or agreed in writing,
1212
// software distributed under the License is distributed on an
1313
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1414
// KIND, either express or implied. See the License for the
@@ -22,56 +22,51 @@
2222
import java.util.Map;
2323

2424
public class SetGeolocationOverrideParameters {
25-
private final GeolocationCoordinates coordinates;
26-
private final GeolocationPositionError error;
27-
private final List<String> contexts;
28-
private final List<String> userContexts;
25+
private final Map<String, Object> map = new HashMap<>();
2926

30-
public SetGeolocationOverrideParameters(
31-
GeolocationCoordinates coordinates,
32-
GeolocationPositionError error,
33-
List<String> contexts,
34-
List<String> userContexts) {
35-
36-
this.coordinates = coordinates;
37-
this.error = error;
38-
this.contexts = contexts;
39-
this.userContexts = userContexts;
40-
41-
if (this.coordinates != null && this.error != null) {
42-
throw new IllegalArgumentException("Cannot specify both coordinates and error");
43-
}
44-
if (this.contexts != null && this.userContexts != null) {
45-
throw new IllegalArgumentException("Cannot specify both contexts and userContexts");
27+
// Constructor for coordinates - must specify either contexts or userContexts later
28+
public SetGeolocationOverrideParameters(GeolocationCoordinates coordinates) {
29+
if (coordinates == null) {
30+
throw new IllegalArgumentException("GeolocationCoordinates cannot be null");
4631
}
32+
map.put("coordinates", coordinates.toMap());
33+
}
4734

48-
if (this.contexts == null && this.userContexts == null) {
49-
throw new IllegalArgumentException("Must specify either contexts or userContexts");
35+
// Constructor for error - must specify either contexts or userContexts later
36+
public SetGeolocationOverrideParameters(GeolocationPositionError error) {
37+
if (error == null) {
38+
throw new IllegalArgumentException("GeolocationPositionError cannot be null");
5039
}
40+
map.put("error", error.toMap());
5141
}
5242

53-
public SetGeolocationOverrideParameters(
54-
GeolocationCoordinates coordinates, GeolocationPositionError error) {
55-
this(coordinates, error, null, null);
43+
public SetGeolocationOverrideParameters contexts(List<String> contexts) {
44+
if (contexts == null || contexts.isEmpty()) {
45+
throw new IllegalArgumentException("Contexts cannot be null or empty");
46+
}
47+
if (map.containsKey("userContexts")) {
48+
throw new IllegalArgumentException("Cannot specify both contexts and userContexts");
49+
}
50+
map.put("contexts", contexts);
51+
return this;
5652
}
5753

58-
public Map<String, Object> toMap() {
59-
Map<String, Object> param = new HashMap<>();
60-
61-
if (this.coordinates != null) {
62-
param.put("coordinates", this.coordinates.toMap());
54+
public SetGeolocationOverrideParameters userContexts(List<String> userContexts) {
55+
if (userContexts == null || userContexts.isEmpty()) {
56+
throw new IllegalArgumentException("User contexts cannot be null or empty");
6357
}
64-
65-
if (this.error != null) {
66-
param.put("error", this.error.toMap());
58+
if (map.containsKey("contexts")) {
59+
throw new IllegalArgumentException("Cannot specify both contexts and userContexts");
6760
}
61+
map.put("userContexts", userContexts);
62+
return this;
63+
}
6864

69-
if (this.contexts != null) {
70-
param.put("contexts", this.contexts);
71-
} else {
72-
param.put("userContexts", this.userContexts);
65+
public Map<String, Object> toMap() {
66+
// Validate that either contexts or userContexts is set
67+
if (!map.containsKey("contexts") && !map.containsKey("userContexts")) {
68+
throw new IllegalStateException("Must specify either contexts or userContexts");
7369
}
74-
75-
return Map.copyOf(param);
70+
return Map.copyOf(map);
7671
}
7772
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void canSetGeolocationOverrideWithCoordinatesInContext() {
8585
Emulation emul = new Emulation(driver);
8686
GeolocationCoordinates coords = new GeolocationCoordinates(37.7749, -122.4194);
8787
emul.setGeolocationOverride(
88-
new SetGeolocationOverrideParameters(coords, null, List.of(contextId), null));
88+
new SetGeolocationOverrideParameters(coords).contexts(List.of(contextId)));
8989

9090
Object result = getBrowserGeolocation(driver, null, origin);
9191
Map<String, Object> r = ((Map<String, Object>) result);
@@ -121,8 +121,8 @@ void canSetGeolocationOverrideWithMultipleUserContexts() {
121121

122122
Emulation emulation = new Emulation(driver);
123123
emulation.setGeolocationOverride(
124-
new SetGeolocationOverrideParameters(
125-
coords, null, null, List.of(userContext1, userContext2)));
124+
new SetGeolocationOverrideParameters(coords)
125+
.userContexts(List.of(userContext1, userContext2)));
126126

127127
driver.switchTo().window(context1.getId());
128128
String url1 = appServer.whereIsSecure("blank.html");
@@ -187,7 +187,7 @@ void canSetGeolocationOverrideWithError() {
187187
GeolocationPositionError error = new GeolocationPositionError();
188188
Emulation emul = new Emulation(driver);
189189
emul.setGeolocationOverride(
190-
new SetGeolocationOverrideParameters(null, error, List.of(contextId), null));
190+
new SetGeolocationOverrideParameters(error).contexts(List.of(contextId)));
191191

192192
Object result = getBrowserGeolocation(driver, null, origin);
193193
Map<String, Object> r = ((Map<String, Object>) result);

0 commit comments

Comments
 (0)