Skip to content

fix: Fix building of Android geo location parameters #2146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ test {
testLogging.showStandardStreams = true
testLogging.exceptionFormat = 'full'
filter {
includeTestsMatching 'io.appium.java_client.android.geolocation.*'
includeTestsMatching 'io.appium.java_client.drivers.options.*'
includeTestsMatching 'io.appium.java_client.events.*'
includeTestsMatching 'io.appium.java_client.internal.*'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,12 @@ public AndroidGeoLocation withSpeed(double speed) {
*/
public Map<String, ?> build() {
var map = new HashMap<String, Object>();
ofNullable(longitude).map(x -> map.put("longitude", x)).orElseThrow(() -> new IllegalArgumentException(
"A valid 'longitude' must be provided"));
ofNullable(latitude).map(x -> map.put("latitude", x)).orElseThrow(() -> new IllegalArgumentException(
"A valid 'latitude' must be provided"));
ofNullable(longitude).ifPresentOrElse(x -> map.put("longitude", x), () -> {
throw new IllegalArgumentException("A valid 'longitude' must be provided");
});
ofNullable(latitude).ifPresentOrElse(x -> map.put("latitude", x), () -> {
throw new IllegalArgumentException("A valid 'latitude' must be provided");
});
ofNullable(altitude).ifPresent(x -> map.put("altitude", x));
ofNullable(satellites).ifPresent(x -> map.put("satellites", x));
ofNullable(speed).ifPresent(x -> map.put("speed", x));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.appium.java_client.android.geolocation;

import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class AndroidGeoLocationTest {

@Test
void shouldThrowExceptionWhenLatitudeIsNotSet() {
var androidGeoLocation = new AndroidGeoLocation().withLongitude(24.105078);

var exception = assertThrows(IllegalArgumentException.class, androidGeoLocation::build);

assertEquals("A valid 'latitude' must be provided", exception.getMessage());
}

@Test
void shouldThrowExceptionWhenLongitudeIsNotSet() {
var androidGeoLocation = new AndroidGeoLocation().withLatitude(56.946285);

var exception = assertThrows(IllegalArgumentException.class, androidGeoLocation::build);

assertEquals("A valid 'longitude' must be provided", exception.getMessage());
}

@Test
void shodBuildMinimalParameters() {
var androidGeoLocation = new AndroidGeoLocation()
.withLongitude(24.105078)
.withLatitude(56.946285);

assertParameters(androidGeoLocation.build(), 24.105078, 56.946285, null, null, null);
}

@Test
void shodBuildFullParameters() {
var androidGeoLocation = new AndroidGeoLocation()
.withLongitude(24.105078)
.withLatitude(56.946285)
.withAltitude(7)
.withSpeed(1.5)
.withSatellites(12);

assertParameters(androidGeoLocation.build(), 24.105078, 56.946285, 7.0, 1.5, 12);
}

private static void assertParameters(Map<String, ?> actualParams, double longitude, double latitude,
Double altitude, Double speed, Integer satellites) {
assertEquals(longitude, actualParams.get("longitude"));
assertEquals(latitude, actualParams.get("latitude"));
assertEquals(altitude, actualParams.get("altitude"));
assertEquals(speed, actualParams.get("speed"));
assertEquals(satellites, actualParams.get("satellites"));
}
}
Loading