From 69b86b345f755fe063ebbed816594f501c3442c8 Mon Sep 17 00:00:00 2001 From: Andrei Bechet Date: Mon, 15 Jun 2015 18:38:22 +0200 Subject: [PATCH] 1st test for the location listener --- .../java/esrlabs/com/geofence/MyService.java | 32 ++++++++++- .../esrlabs.com.geofence/MyServiceTest.java | 53 +++++++++++++++---- 2 files changed, 74 insertions(+), 11 deletions(-) diff --git a/2015/android/Geofence/app/src/main/java/esrlabs/com/geofence/MyService.java b/2015/android/Geofence/app/src/main/java/esrlabs/com/geofence/MyService.java index 00d5074..566f842 100644 --- a/2015/android/Geofence/app/src/main/java/esrlabs/com/geofence/MyService.java +++ b/2015/android/Geofence/app/src/main/java/esrlabs/com/geofence/MyService.java @@ -1,24 +1,52 @@ package esrlabs.com.geofence; import android.app.Service; +import android.content.Context; import android.content.Intent; import android.location.Location; import android.location.LocationListener; +import android.location.LocationManager; import android.os.Bundle; import android.os.IBinder; +import java.util.List; + public class MyService extends Service implements LocationListener { + public static final String CAN_PROVIDER = "CanLocationProvider"; + + private LocationManager locationManager; private Location latestLocation; - public MyService() { + public MyService(LocationManager locationManager) { + this.locationManager = locationManager; + } + + @Override + public void onCreate() { + super.onCreate(); + + if (locationManager == null) { + locationManager = (LocationManager) + this.getSystemService(Context.LOCATION_SERVICE); + } + + initLocationListener(); } @Override public IBinder onBind(Intent intent) { - // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } + private void initLocationListener() { + List allProviders = locationManager.getAllProviders(); + for (String provider : allProviders) { + locationManager.requestLocationUpdates(provider, 0, 0, this); + } + + locationManager.requestLocationUpdates(CAN_PROVIDER, 0, 0, this); + } + @Override public void onLocationChanged(Location location) { latestLocation = location; diff --git a/2015/android/Geofence/app/src/test/java/esrlabs.com.geofence/MyServiceTest.java b/2015/android/Geofence/app/src/test/java/esrlabs.com.geofence/MyServiceTest.java index 7f4079a..df94a08 100644 --- a/2015/android/Geofence/app/src/test/java/esrlabs.com.geofence/MyServiceTest.java +++ b/2015/android/Geofence/app/src/test/java/esrlabs.com.geofence/MyServiceTest.java @@ -1,33 +1,68 @@ package esrlabs.com.geofence; +import static esrlabs.com.geofence.MyService.CAN_PROVIDER; + +import android.content.Context; +import android.location.Location; +import android.location.LocationManager; import android.test.ServiceTestCase; import org.junit.After; import org.junit.Before; import org.junit.Test; - -import static org.junit.Assert.*; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.Shadows; +import org.robolectric.annotation.Config; +import org.robolectric.shadows.ShadowLocationManager; +@RunWith(RobolectricGradleTestRunner.class) +@Config(constants = BuildConfig.class, emulateSdk = 17) public class MyServiceTest extends ServiceTestCase { + Location someLocation = location(CAN_PROVIDER, 12.0, 20.0); public MyServiceTest() { super(MyService.class); } @Before - public void setUp() throws Exception { - - } + public void setUp() throws Exception { } @After - public void tearDown() throws Exception { - - } + public void tearDown() throws Exception { } @Test public void testLatestLocation() throws Exception { - assert(false); + LocationManager locationManager = (LocationManager) + RuntimeEnvironment.application.getSystemService(Context.LOCATION_SERVICE); + ShadowLocationManager shadowLocationManager = Shadows.shadowOf(locationManager); + + MyService mainService = new MyService(locationManager); + mainService.onCreate(); + shadowLocationManager.simulateLocation(location(CAN_PROVIDER, + someLocation.getLatitude(), someLocation.getLongitude())); + + assertTrue(areTheLocationsEqual(someLocation, mainService.latestLocation())); + } + + private boolean areTheLocationsEqual(Location first, Location second) { + if ( (first.getAccuracy() == second.getAccuracy()) && + (first.getLatitude() == second.getLatitude()) && + (first.getLongitude() == second.getLongitude()) && + (first.getAltitude() == second.getAltitude()) ) { + return true; + } + return false; + } + + 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; } } \ No newline at end of file