Updated test suite for the android side

This commit is contained in:
Andrei Bechet 2015-06-26 10:50:07 +02:00
parent 3af8ac381e
commit cf13811057
4 changed files with 144 additions and 44 deletions

View file

@ -0,0 +1,40 @@
package com.esrlabs.geofence;
import android.location.Location;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import esrlabs.com.geofence.BuildConfig;
import static android.location.LocationManager.NETWORK_PROVIDER;
import static com.esrlabs.geofence.Utils.location;
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, emulateSdk = 17)
public class CircleGeofenceTest extends TestCase {
public static final double distanceSmallerThanRadiusInDeg = 0.0001;
public static final double distanceLargerThanRadiusInDeg = 0.001;
private static final float someRadiusInMeters = 100;
private static final Location someCenterForTheGeofence = location(NETWORK_PROVIDER, 48.118920, 11.601057);
public static final CircleGeofence someCircleGeofence = new CircleGeofence(someCenterForTheGeofence, someRadiusInMeters);
@Test
public void testContainsLocation() throws Exception {
assertTrue(someCircleGeofence.containsLocation(someLocationInside()));
assertFalse(someCircleGeofence.containsLocation(someLocationOutside()));
}
private Location someLocationInside() {
return location(NETWORK_PROVIDER, someCenterForTheGeofence.getLatitude() + distanceSmallerThanRadiusInDeg, someCenterForTheGeofence.getLongitude());
}
private Location someLocationOutside() {
return location(NETWORK_PROVIDER, someCenterForTheGeofence.getLatitude() + distanceLargerThanRadiusInDeg, someCenterForTheGeofence.getLongitude());
}
}

View file

@ -1,8 +1,5 @@
package com.esrlabs.geofence;
import static com.esrlabs.geofence.GeofenceApp.CAN_PROVIDER;
import static org.robolectric.Shadows.shadowOf;
import android.content.ComponentName;
import android.content.Context;
import android.location.Location;
@ -10,10 +7,9 @@ import android.location.LocationManager;
import android.os.IBinder;
import android.os.RemoteException;
import junit.framework.TestCase;
import com.esrlabs.headunitinterface.HeadUnit;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
@ -28,6 +24,11 @@ import java.util.concurrent.atomic.AtomicBoolean;
import esrlabs.com.geofence.BuildConfig;
import static android.location.LocationManager.NETWORK_PROVIDER;
import static com.esrlabs.geofence.CircleGeofenceTest.someCircleGeofence;
import static com.esrlabs.geofence.Utils.location;
import static org.robolectric.Shadows.shadowOf;
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, emulateSdk = 17)
@ -35,9 +36,10 @@ public class GeofenceAppTest extends TestCase {
private final LocationManager locationManager = (LocationManager)
RuntimeEnvironment.application.getSystemService(Context.LOCATION_SERVICE);
private final ShadowLocationManager shadowLocationManager = shadowOf(locationManager);
private final Location someLocation = location(CAN_PROVIDER, 12.0, 20.0);
private final Location someLocation = Utils.location(NETWORK_PROVIDER, 12.0, 20.0);
private final AtomicBoolean notificationVisibility = new AtomicBoolean(false);
private GeofenceApp geofenceApp;
@Before
@ -46,36 +48,6 @@ public class GeofenceAppTest extends TestCase {
setupGeofenceApp();
}
@After
public void tearDown() throws Exception {
geofenceApp.onDestroy();
}
@Test
public void testLatestLocation() throws Exception {
simulateNewLocation(someLocation);
assertTrue(someLocation.equals(geofenceApp.latestLocation()));
}
@Test
public void testThatPopupIsShownWhenTheCurrentLocationIsOutsideTheGeofence() throws Exception {
// Location locationInsideTheGeofence = location(CAN_PROVIDER, 10, 10);
// simulateNewLocation(locationManager, locationInsideTheGeofence);
// assertTrue(locationInsideTheGeofence.equals(mainService.latestLocation()));
// assertFalse(notificationVisibility.get());
Location locationOutsideTheGeofence = location(CAN_PROVIDER, 11, 11); // todo: put real vals
simulateNewLocation(locationOutsideTheGeofence);
assertTrue(locationOutsideTheGeofence.equals(geofenceApp.latestLocation()));
assertTrue(notificationVisibility.get());
}
private void setupGeofenceApp() {
GeofenceApp service = new GeofenceApp(locationManager);
service.onCreate();
geofenceApp = service;
}
private void initHeadUnitServiceMock() {
IBinder headUnitStub = newTestHeadUnitServerBinder();
shadowOf(RuntimeEnvironment.application).setComponentNameAndServiceForBindService(
@ -96,17 +68,47 @@ public class GeofenceAppTest extends TestCase {
};
}
private void setupGeofenceApp() {
GeofenceApp service = new GeofenceApp(locationManager, someCircleGeofence);
service.onCreate();
geofenceApp = service;
}
@After
public void tearDown() throws Exception {
geofenceApp.onDestroy();
}
@Test
public void shouldReceiveTheLatestLocation() throws Exception {
simulateNewLocation(someLocation);
assertTrue(someLocation.equals(geofenceApp.latestLocation()));
}
@Test
public void shouldShowPopupWhenTheCurrentLocationIsOutsideTheGeofence() throws Exception {
Location locationInside = location(NETWORK_PROVIDER, someCircleGeofence.center.getLatitude() + CircleGeofenceTest.distanceSmallerThanRadiusInDeg,
someCircleGeofence.center.getLongitude());
simulateNewLocation(locationInside);
assertTrue(locationInside.equals(geofenceApp.latestLocation()));
assertFalse(notificationVisibility.get());
Location locationOutside = location(NETWORK_PROVIDER, someCircleGeofence.center.getLatitude() + CircleGeofenceTest.distanceLargerThanRadiusInDeg,
someCircleGeofence.center.getLongitude());
simulateNewLocation(locationOutside);
assertTrue(locationOutside.equals(geofenceApp.latestLocation()));
assertTrue(notificationVisibility.get());
Location nextLocationInside = location(NETWORK_PROVIDER, someCircleGeofence.center.getLatitude() + CircleGeofenceTest.distanceSmallerThanRadiusInDeg,
someCircleGeofence.center.getLongitude());
simulateNewLocation(nextLocationInside);
assertTrue(nextLocationInside.equals(geofenceApp.latestLocation()));
assertFalse(notificationVisibility.get());
}
private void simulateNewLocation(Location someLocation) {
shadowLocationManager.simulateLocation(someLocation);
}
private Location location(String provider, double latitude, double longitude) {
Location location = new Location(provider);
location.setLatitude(latitude);
location.setLongitude(longitude);
location.setTime(System.currentTimeMillis());
return location;
}
}

View file

@ -0,0 +1,44 @@
package com.esrlabs.geofence;
import android.location.Location;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import esrlabs.com.geofence.BuildConfig;
import static android.location.LocationManager.NETWORK_PROVIDER;
import static com.esrlabs.geofence.CircleGeofenceTest.distanceLargerThanRadiusInDeg;
import static com.esrlabs.geofence.Utils.location;
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, emulateSdk = 17)
public class PolygonGeofenceTest extends TestCase {
// Test polygon defined physically like:
// 48.119033, 11.601664 48.119051, 11.601766
// 48.117726, 11.602404 48.117758, 11.602543
public static final Location testPolygonTopRightCorner = location(NETWORK_PROVIDER, 48.119033, 11.601664);
public static final Location testPolygonTopLeftCorner = location(NETWORK_PROVIDER, 48.119051, 11.601966);
public static final Location testPolygonBottomRightCorner = location(NETWORK_PROVIDER, 48.116726, 11.602404);
public static final Location testPolygonBottomLeftCorner = location(NETWORK_PROVIDER, 48.116758, 11.602743);
public static final PolygonGeofence testGeofence = new PolygonGeofence(testPolygonTopRightCorner, testPolygonTopLeftCorner,
testPolygonBottomRightCorner, testPolygonBottomLeftCorner);
@Test
public void testContainsLocation() throws Exception {
assertTrue(testGeofence.containsLocation(someLocationInside()));
assertFalse(testGeofence.containsLocation(someLocationOutside()));
}
private Location someLocationInside() {
return location(NETWORK_PROVIDER, testPolygonTopRightCorner.getLatitude() - 0.001, testPolygonTopRightCorner.getLongitude() - 0.001);
}
private Location someLocationOutside() {
return location(NETWORK_PROVIDER, testPolygonTopRightCorner.getLatitude() + distanceLargerThanRadiusInDeg, testPolygonTopRightCorner.getLongitude());
}
}

View file

@ -0,0 +1,14 @@
package com.esrlabs.geofence;
import android.location.Location;
public class Utils {
public static Location location(String provider, double latitude, double longitude) {
Location location = new Location(provider);
location.setLatitude(latitude);
location.setLongitude(longitude);
location.setTime(System.currentTimeMillis());
return location;
}
}