Skip to content

Commit ef53996

Browse files
committed
use PartialCookie approach
1 parent 70c56a5 commit ef53996

File tree

2 files changed

+56
-102
lines changed

2 files changed

+56
-102
lines changed

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

Lines changed: 54 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -21,142 +21,98 @@
2121
import java.util.Map;
2222

2323
public class GeolocationCoordinates {
24-
private final double latitude;
25-
private final double longitude;
26-
private final double accuracy;
27-
private final Double altitude;
28-
private final Double altitudeAccuracy;
29-
private final Double heading;
30-
private final Double speed;
31-
32-
// Constructor with default accuracy = 1.0
33-
public GeolocationCoordinates(
34-
double latitude,
35-
double longitude,
36-
Double altitude,
37-
Double altitudeAccuracy,
38-
Double heading,
39-
Double speed) {
40-
this(latitude, longitude, 1.0, altitude, altitudeAccuracy, heading, speed);
41-
}
42-
43-
// Constructor with no altitude, altitudeAccuracy, heading, or speed
44-
public GeolocationCoordinates(double latitude, double longitude, double accuracy) {
45-
this(latitude, longitude, accuracy, null, null, null, null);
46-
}
47-
48-
// Constructor with no altitudeAccuracy, heading, or speed
49-
public GeolocationCoordinates(
50-
double latitude, double longitude, double accuracy, Double altitude) {
51-
this(latitude, longitude, accuracy, altitude, null, null, null);
52-
}
53-
54-
// Constructor no heading or speed
55-
public GeolocationCoordinates(
56-
double latitude,
57-
double longitude,
58-
double accuracy,
59-
Double altitude,
60-
Double altitudeAccuracy) {
61-
this(latitude, longitude, accuracy, altitude, altitudeAccuracy, null, null);
62-
}
63-
64-
// Constructor with no speed
65-
public GeolocationCoordinates(
66-
double latitude,
67-
double longitude,
68-
double accuracy,
69-
Double altitude,
70-
Double altitudeAccuracy,
71-
Double heading) {
72-
this(latitude, longitude, accuracy, altitude, altitudeAccuracy, heading, null);
73-
}
74-
75-
public GeolocationCoordinates(
76-
double latitude,
77-
double longitude,
78-
double accuracy,
79-
Double altitude,
80-
Double altitudeAccuracy,
81-
Double heading,
82-
Double speed) {
24+
private final Map<String, Object> map = new HashMap<>();
8325

26+
public GeolocationCoordinates(double latitude, double longitude) {
8427
if (latitude < -90.0 || latitude > 90.0) {
8528
throw new IllegalArgumentException("Latitude must be between -90.0 and 90.0");
8629
}
8730
if (longitude < -180.0 || longitude > 180.0) {
8831
throw new IllegalArgumentException("Longitude must be between -180.0 and 180.0");
8932
}
33+
34+
map.put("latitude", latitude);
35+
map.put("longitude", longitude);
36+
map.put("accuracy", 1.0); // Default accuracy
37+
}
38+
39+
public GeolocationCoordinates accuracy(double accuracy) {
9040
if (accuracy < 0.0) {
9141
throw new IllegalArgumentException("Accuracy must be >= 0.0");
9242
}
93-
if (altitudeAccuracy != null && altitude == null) {
94-
throw new IllegalArgumentException("altitudeAccuracy cannot be set without altitude");
95-
}
96-
if (altitudeAccuracy != null && altitudeAccuracy < 0.0) {
97-
throw new IllegalArgumentException("Altitude accuracy must be >= 0.0");
43+
map.put("accuracy", accuracy);
44+
return this;
45+
}
46+
47+
public GeolocationCoordinates altitude(Double altitude) {
48+
if (altitude != null) {
49+
map.put("altitude", altitude);
9850
}
99-
if (heading != null && (heading < 0.0 || heading >= 360.0)) {
100-
throw new IllegalArgumentException("Heading must be between 0.0 and 360.0");
51+
return this;
52+
}
53+
54+
public GeolocationCoordinates altitudeAccuracy(Double altitudeAccuracy) {
55+
if (altitudeAccuracy != null) {
56+
if (!map.containsKey("altitude")) {
57+
throw new IllegalArgumentException("altitudeAccuracy cannot be set without altitude");
58+
}
59+
if (altitudeAccuracy < 0.0) {
60+
throw new IllegalArgumentException("Altitude accuracy must be >= 0.0");
61+
}
62+
map.put("altitudeAccuracy", altitudeAccuracy);
10163
}
102-
if (speed != null && speed < 0.0) {
103-
throw new IllegalArgumentException("Speed must be >= 0.0");
64+
return this;
65+
}
66+
67+
public GeolocationCoordinates heading(Double heading) {
68+
if (heading != null) {
69+
if (heading < 0.0 || heading >= 360.0) {
70+
throw new IllegalArgumentException("Heading must be between 0.0 and 360.0");
71+
}
72+
map.put("heading", heading);
10473
}
74+
return this;
75+
}
10576

106-
this.latitude = latitude;
107-
this.longitude = longitude;
108-
this.accuracy = accuracy;
109-
this.altitude = altitude;
110-
this.altitudeAccuracy = altitudeAccuracy;
111-
this.heading = heading;
112-
this.speed = speed;
77+
public GeolocationCoordinates speed(Double speed) {
78+
if (speed != null) {
79+
if (speed < 0.0) {
80+
throw new IllegalArgumentException("Speed must be >= 0.0");
81+
}
82+
map.put("speed", speed);
83+
}
84+
return this;
11385
}
11486

11587
public double getLatitude() {
116-
return latitude;
88+
return (Double) map.get("latitude");
11789
}
11890

11991
public double getLongitude() {
120-
return longitude;
92+
return (Double) map.get("longitude");
12193
}
12294

12395
public double getAccuracy() {
124-
return accuracy;
96+
return (Double) map.get("accuracy");
12597
}
12698

12799
public Double getAltitude() {
128-
return altitude;
100+
return (Double) map.get("altitude");
129101
}
130102

131103
public Double getAltitudeAccuracy() {
132-
return altitudeAccuracy;
104+
return (Double) map.get("altitudeAccuracy");
133105
}
134106

135107
public Double getHeading() {
136-
return heading;
108+
return (Double) map.get("heading");
137109
}
138110

139111
public Double getSpeed() {
140-
return speed;
112+
return (Double) map.get("speed");
141113
}
142114

143115
public Map<String, Object> toMap() {
144-
Map<String, Object> map = new HashMap<>();
145-
map.put("latitude", getLatitude());
146-
map.put("longitude", getLongitude());
147-
map.put("accuracy", getAccuracy());
148-
if (getAltitude() != null) {
149-
map.put("altitude", getAltitude());
150-
}
151-
if (getAltitudeAccuracy() != null) {
152-
map.put("altitudeAccuracy", getAltitudeAccuracy());
153-
}
154-
if (getHeading() != null) {
155-
map.put("heading", getHeading());
156-
}
157-
if (getSpeed() != null) {
158-
map.put("speed", getSpeed());
159-
}
160116
return Map.copyOf(map);
161117
}
162118
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ void canSetGeolocationOverrideWithCoordinatesInContext() {
8383
(String) ((JavascriptExecutor) driver).executeScript("return window.location.origin;");
8484

8585
Emulation emul = new Emulation(driver);
86-
GeolocationCoordinates coords =
87-
new GeolocationCoordinates(37.7749, -122.4194, 10.0, null, null, null);
86+
GeolocationCoordinates coords = new GeolocationCoordinates(37.7749, -122.4194);
8887
emul.setGeolocationOverride(
8988
new SetGeolocationOverrideParameters(coords, null, List.of(contextId), null));
9089

@@ -118,8 +117,7 @@ void canSetGeolocationOverrideWithMultipleUserContexts() {
118117
new BrowsingContext(
119118
driver, new CreateContextParameters(WindowType.TAB).userContext(userContext2));
120119

121-
GeolocationCoordinates coords =
122-
new GeolocationCoordinates(45.5, -122.4194, 10.0, null, null, null);
120+
GeolocationCoordinates coords = new GeolocationCoordinates(45.5, -122.4194);
123121

124122
Emulation emulation = new Emulation(driver);
125123
emulation.setGeolocationOverride(

0 commit comments

Comments
 (0)