93 lines
2.9 KiB
Java
93 lines
2.9 KiB
Java
package com.iamthefij.otbeta;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import com.iamthefij.otbeta.api.Location;
|
|
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
import org.junit.Test;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
/**
|
|
* Simple unit test for location sorting
|
|
*/
|
|
|
|
public class LocationSortUnitTest {
|
|
private final double lat = 0.0;
|
|
private final double lng = 0.0;
|
|
private int locationId = 0;
|
|
|
|
private Location getLocation(String name, Double lat, Double lng) {
|
|
try {
|
|
JSONObject locationJSON = new JSONObject()
|
|
.put("id", locationId++)
|
|
.put("name", name);
|
|
if (lat != null || lng != null) {
|
|
JSONObject address = new JSONObject();
|
|
if (!lat.isNaN()) {
|
|
address.put("lat", lat);
|
|
}
|
|
if (!lng.isNaN()) {
|
|
address.put("lng", lng);
|
|
}
|
|
locationJSON.put("address", address);
|
|
}
|
|
return new Location(locationJSON);
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
private Location getLocationWithNaNAddress(String name) {
|
|
return getLocation(name, Double.NaN, Double.NaN);
|
|
}
|
|
|
|
private Location getLocationWithNoAddress(String name) {
|
|
return getLocation(name, null, null);
|
|
}
|
|
|
|
private Location getLocationWithNearAddress(String name) {
|
|
return getLocation(name, lat, lng);
|
|
}
|
|
|
|
private Location getLocationWithFarAddress(String name) {
|
|
return getLocation(name, lat + 1.0, lng + 1.0);
|
|
}
|
|
|
|
private LoginActivity.LocationDistanceComparator getComparator() {
|
|
return new LoginActivity.LocationDistanceComparator(lat, lng);
|
|
}
|
|
|
|
@Test
|
|
public void testNaNAddress() throws Exception {
|
|
Location nan = getLocationWithNaNAddress("nan");
|
|
assertTrue("Is not NaN", nan.getAddress().getLatitude().isNaN());
|
|
|
|
assertFalse(nan.getAddress().getLatitude() == 0);
|
|
assertFalse(nan.getAddress().getLatitude() > 0);
|
|
assertFalse(nan.getAddress().getLatitude() < 0);
|
|
}
|
|
@Test
|
|
public void testLocationSort() throws Exception {
|
|
List<Location> locations = new ArrayList<>();
|
|
locations.add(getLocationWithNoAddress("no-adr"));
|
|
locations.add(getLocationWithNaNAddress("nan-adr"));
|
|
locations.add(getLocationWithNearAddress("near-adr"));
|
|
locations.add(getLocationWithFarAddress("far-adr"));
|
|
|
|
Collections.sort(locations, getComparator());
|
|
|
|
assertSame("near-adr", locations.get(0).getName());
|
|
assertSame("far-adr", locations.get(1).getName());
|
|
assertSame("nan-adr", locations.get(2).getName());
|
|
assertSame("no-adr", locations.get(3).getName());
|
|
}
|
|
}
|