Skip to content

Commit bc39a23

Browse files
authored
fix: Fix building of Android geo location parameters (#2146)
1 parent f0aad8a commit bc39a23

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ test {
204204
testLogging.showStandardStreams = true
205205
testLogging.exceptionFormat = 'full'
206206
filter {
207+
includeTestsMatching 'io.appium.java_client.android.geolocation.*'
207208
includeTestsMatching 'io.appium.java_client.drivers.options.*'
208209
includeTestsMatching 'io.appium.java_client.events.*'
209210
includeTestsMatching 'io.appium.java_client.internal.*'

src/main/java/io/appium/java_client/android/geolocation/AndroidGeoLocation.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,12 @@ public AndroidGeoLocation withSpeed(double speed) {
111111
*/
112112
public Map<String, ?> build() {
113113
var map = new HashMap<String, Object>();
114-
ofNullable(longitude).map(x -> map.put("longitude", x)).orElseThrow(() -> new IllegalArgumentException(
115-
"A valid 'longitude' must be provided"));
116-
ofNullable(latitude).map(x -> map.put("latitude", x)).orElseThrow(() -> new IllegalArgumentException(
117-
"A valid 'latitude' must be provided"));
114+
ofNullable(longitude).ifPresentOrElse(x -> map.put("longitude", x), () -> {
115+
throw new IllegalArgumentException("A valid 'longitude' must be provided");
116+
});
117+
ofNullable(latitude).ifPresentOrElse(x -> map.put("latitude", x), () -> {
118+
throw new IllegalArgumentException("A valid 'latitude' must be provided");
119+
});
118120
ofNullable(altitude).ifPresent(x -> map.put("altitude", x));
119121
ofNullable(satellites).ifPresent(x -> map.put("satellites", x));
120122
ofNullable(speed).ifPresent(x -> map.put("speed", x));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package io.appium.java_client.android.geolocation;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Map;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertThrows;
9+
10+
class AndroidGeoLocationTest {
11+
12+
@Test
13+
void shouldThrowExceptionWhenLatitudeIsNotSet() {
14+
var androidGeoLocation = new AndroidGeoLocation().withLongitude(24.105078);
15+
16+
var exception = assertThrows(IllegalArgumentException.class, androidGeoLocation::build);
17+
18+
assertEquals("A valid 'latitude' must be provided", exception.getMessage());
19+
}
20+
21+
@Test
22+
void shouldThrowExceptionWhenLongitudeIsNotSet() {
23+
var androidGeoLocation = new AndroidGeoLocation().withLatitude(56.946285);
24+
25+
var exception = assertThrows(IllegalArgumentException.class, androidGeoLocation::build);
26+
27+
assertEquals("A valid 'longitude' must be provided", exception.getMessage());
28+
}
29+
30+
@Test
31+
void shodBuildMinimalParameters() {
32+
var androidGeoLocation = new AndroidGeoLocation()
33+
.withLongitude(24.105078)
34+
.withLatitude(56.946285);
35+
36+
assertParameters(androidGeoLocation.build(), 24.105078, 56.946285, null, null, null);
37+
}
38+
39+
@Test
40+
void shodBuildFullParameters() {
41+
var androidGeoLocation = new AndroidGeoLocation()
42+
.withLongitude(24.105078)
43+
.withLatitude(56.946285)
44+
.withAltitude(7)
45+
.withSpeed(1.5)
46+
.withSatellites(12);
47+
48+
assertParameters(androidGeoLocation.build(), 24.105078, 56.946285, 7.0, 1.5, 12);
49+
}
50+
51+
private static void assertParameters(Map<String, ?> actualParams, double longitude, double latitude,
52+
Double altitude, Double speed, Integer satellites) {
53+
assertEquals(longitude, actualParams.get("longitude"));
54+
assertEquals(latitude, actualParams.get("latitude"));
55+
assertEquals(altitude, actualParams.get("altitude"));
56+
assertEquals(speed, actualParams.get("speed"));
57+
assertEquals(satellites, actualParams.get("satellites"));
58+
}
59+
}

0 commit comments

Comments
 (0)