refactoring of GeofenceTestApp

This commit is contained in:
Andrei Bechet 2015-06-18 16:33:47 +02:00
parent 94c298d6ba
commit f8214e79a8

View file

@ -17,6 +17,8 @@ import android.test.ServiceTestCase;
import com.esrlabs.headunitinterface.HeadUnit; import com.esrlabs.headunitinterface.HeadUnit;
import junit.framework.TestCase;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -35,71 +37,58 @@ import esrlabs.com.geofence.BuildConfig;
@RunWith(RobolectricGradleTestRunner.class) @RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, emulateSdk = 17) @Config(constants = BuildConfig.class, emulateSdk = 17)
public class GeofenceAppTest extends ServiceTestCase { public class GeofenceAppTest extends TestCase {
public final LocationManager locationManager = (LocationManager) public final LocationManager locationManager = (LocationManager)
RuntimeEnvironment.application.getSystemService(Context.LOCATION_SERVICE); RuntimeEnvironment.application.getSystemService(Context.LOCATION_SERVICE);
private GeofenceApp geofenceApp; private GeofenceApp geofenceApp;
final AtomicBoolean notificationVisibility = new AtomicBoolean(false);
private final Location someLocation = location(CAN_PROVIDER, 12.0, 20.0); private final Location someLocation = location(CAN_PROVIDER, 12.0, 20.0);
public GeofenceAppTest() {
super(GeofenceApp.class);
}
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
geofenceApp = setupMyService(locationManager); initHeadUnitServiceMock();
setupMyService();
} }
@After @After
public void tearDown() throws Exception { } public void tearDown() throws Exception {
geofenceApp.onDestroy();
}
@Test @Test
public void testLatestLocation() throws Exception { public void testLatestLocation() throws Exception {
simulateNewLocation(locationManager, someLocation); simulateNewLocation(someLocation);
assertTrue(someLocation.equals(geofenceApp.latestLocation())); assertTrue(someLocation.equals(geofenceApp.latestLocation()));
} }
private GeofenceApp setupMyService(LocationManager locationManager) {
GeofenceApp service = new GeofenceApp(locationManager);
service.onCreate();
return service;
}
private void simulateNewLocation(LocationManager locationManager, Location someLocation) {
ShadowLocationManager shadowLocationManager = shadowOf(locationManager);
shadowLocationManager.simulateLocation(someLocation);
}
@Test @Test
public void testThatPopupIsShownWhenTheCurrentLocationIsOutsideTheGeofence() throws Exception { public void testThatPopupIsShownWhenTheCurrentLocationIsOutsideTheGeofence() throws Exception {
final AtomicBoolean notificationVisibility = new AtomicBoolean(false);
initHeadUnitServiceMock(notificationVisibility);
GeofenceApp mainService = setupMyService(locationManager);
// Location locationInsideTheGeofence = location(CAN_PROVIDER, 10, 10); // Location locationInsideTheGeofence = location(CAN_PROVIDER, 10, 10);
// simulateNewLocation(locationManager, locationInsideTheGeofence); // simulateNewLocation(locationManager, locationInsideTheGeofence);
// assertTrue(locationInsideTheGeofence.equals(mainService.latestLocation())); // assertTrue(locationInsideTheGeofence.equals(mainService.latestLocation()));
// assertFalse(notificationVisibility.get()); // assertFalse(notificationVisibility.get());
Location locationOutsideTheGeofence = location(CAN_PROVIDER, 11, 11); Location locationOutsideTheGeofence = location(CAN_PROVIDER, 11, 11);
simulateNewLocation(locationManager, locationOutsideTheGeofence); simulateNewLocation(locationOutsideTheGeofence);
assertTrue(locationOutsideTheGeofence.equals(mainService.latestLocation())); assertTrue(locationOutsideTheGeofence.equals(geofenceApp.latestLocation()));
assertTrue(notificationVisibility.get()); assertTrue(notificationVisibility.get());
} }
private void initHeadUnitServiceMock(AtomicBoolean notificationVisibility) { private void setupMyService() {
IBinder headUnitStub = getHeadUnitServiceBinder(notificationVisibility); GeofenceApp service = new GeofenceApp(locationManager);
service.onCreate();
geofenceApp = service;
}
private void initHeadUnitServiceMock() {
IBinder headUnitStub = getHeadUnitServiceBinder();
shadowOf(RuntimeEnvironment.application).setComponentNameAndServiceForBindService( shadowOf(RuntimeEnvironment.application).setComponentNameAndServiceForBindService(
new ComponentName("com.esrlabs.headunitinterface", "HeadUnit"), headUnitStub); new ComponentName("com.esrlabs.headunitinterface", "HeadUnit"), headUnitStub);
} }
private IBinder getHeadUnitServiceBinder(final AtomicBoolean notificationVisibility) { private IBinder getHeadUnitServiceBinder() {
return new HeadUnit.Stub() { return new HeadUnit.Stub() {
@Override @Override
public void showNotification(String text) throws RemoteException { public void showNotification(String text) throws RemoteException {
@ -113,6 +102,11 @@ public class GeofenceAppTest extends ServiceTestCase {
}; };
} }
private void simulateNewLocation(Location someLocation) {
ShadowLocationManager shadowLocationManager = shadowOf(locationManager);
shadowLocationManager.simulateLocation(someLocation);
}
private Location location(String provider, double latitude, double longitude) { private Location location(String provider, double latitude, double longitude) {
Location location = new Location(provider); Location location = new Location(provider);