1st test for the location listener
This commit is contained in:
parent
6f473ff4c7
commit
69b86b345f
2 changed files with 74 additions and 11 deletions
|
@ -1,24 +1,52 @@
|
||||||
package esrlabs.com.geofence;
|
package esrlabs.com.geofence;
|
||||||
|
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
import android.location.LocationListener;
|
import android.location.LocationListener;
|
||||||
|
import android.location.LocationManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class MyService extends Service implements LocationListener {
|
public class MyService extends Service implements LocationListener {
|
||||||
|
public static final String CAN_PROVIDER = "CanLocationProvider";
|
||||||
|
|
||||||
|
private LocationManager locationManager;
|
||||||
private Location latestLocation;
|
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
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
// TODO: Return the communication channel to the service.
|
|
||||||
throw new UnsupportedOperationException("Not yet implemented");
|
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
|
@Override
|
||||||
public void onLocationChanged(Location location) {
|
public void onLocationChanged(Location location) {
|
||||||
latestLocation = location;
|
latestLocation = location;
|
||||||
|
|
|
@ -1,33 +1,68 @@
|
||||||
package esrlabs.com.geofence;
|
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 android.test.ServiceTestCase;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
import static org.junit.Assert.*;
|
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 {
|
public class MyServiceTest extends ServiceTestCase {
|
||||||
|
|
||||||
|
Location someLocation = location(CAN_PROVIDER, 12.0, 20.0);
|
||||||
|
|
||||||
public MyServiceTest() {
|
public MyServiceTest() {
|
||||||
super(MyService.class);
|
super(MyService.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception { }
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() throws Exception {
|
public void tearDown() throws Exception { }
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLatestLocation() throws Exception {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue