refactoring MyServiceTest

This commit is contained in:
Andrei Bechet 2015-06-15 19:15:52 +02:00
parent 69b86b345f
commit 0cf7a7fa52

View file

@ -22,6 +22,8 @@ import org.robolectric.shadows.ShadowLocationManager;
@Config(constants = BuildConfig.class, emulateSdk = 17) @Config(constants = BuildConfig.class, emulateSdk = 17)
public class MyServiceTest extends ServiceTestCase { public class MyServiceTest extends ServiceTestCase {
public final LocationManager locationManager = (LocationManager)
RuntimeEnvironment.application.getSystemService(Context.LOCATION_SERVICE);
Location someLocation = location(CAN_PROVIDER, 12.0, 20.0); Location someLocation = location(CAN_PROVIDER, 12.0, 20.0);
public MyServiceTest() { public MyServiceTest() {
@ -36,19 +38,26 @@ public class MyServiceTest extends ServiceTestCase {
@Test @Test
public void testLatestLocation() throws Exception { public void testLatestLocation() throws Exception {
LocationManager locationManager = (LocationManager) MyService mainService = setupMyService(locationManager);
RuntimeEnvironment.application.getSystemService(Context.LOCATION_SERVICE); simulateNewLocation(locationManager);
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())); assertTrue(areTheLocationsEqual(someLocation, mainService.latestLocation()));
} }
private void simulateNewLocation(LocationManager locationManager) {
ShadowLocationManager shadowLocationManager = Shadows.shadowOf(locationManager);
shadowLocationManager.simulateLocation(location(CAN_PROVIDER,
someLocation.getLatitude(), someLocation.getLongitude()));
}
private MyService setupMyService(LocationManager locationManager) {
MyService service = new MyService(locationManager);
service.onCreate();
return service;
}
private boolean areTheLocationsEqual(Location first, Location second) { private boolean areTheLocationsEqual(Location first, Location second) {
// timestamp is different
if ( (first.getAccuracy() == second.getAccuracy()) && if ( (first.getAccuracy() == second.getAccuracy()) &&
(first.getLatitude() == second.getLatitude()) && (first.getLatitude() == second.getLatitude()) &&
(first.getLongitude() == second.getLongitude()) && (first.getLongitude() == second.getLongitude()) &&