1st test for the location listener

This commit is contained in:
Andrei Bechet 2015-06-15 18:38:22 +02:00
parent 6f473ff4c7
commit 69b86b345f
2 changed files with 74 additions and 11 deletions

View file

@ -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<String> 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;

View file

@ -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;
}
}