LocationManagerProximityTestpublic class LocationManagerProximityTest extends android.test.AndroidTestCase Tests for LocationManager.addProximityAlert
TODO: add tests for more scenarios
To run:
adb shell am instrument -e class android.location.LocationManagerProximityTest \
-w android.core/android.test.InstrumentationTestRunner
This test requires that the "Allow mock locations" setting be enabled |
Fields Summary |
---|
private static final int | UPDATE_LOCATION_WAIT_TIME | private static final int | PROXIMITY_WAIT_TIME | private android.location.LocationManager | mLocationManager | private android.app.PendingIntent | mPendingIntent | private TestIntentReceiver | mIntentReceiver | private static final String | LOG_TAG | private static final String | PROVIDER_NAME |
Methods Summary |
---|
private void | assertProximityType(boolean expectedEnterProximity)Asserts that the received intent had the enter proximity property set as
expected
boolean proximityTest = mIntentReceiver.getLastReceivedIntent().
getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING,
!expectedEnterProximity);
assertEquals("proximity alert not set to expected enter proximity value",
expectedEnterProximity, proximityTest);
| private void | doTestEnterProximity(long expiration)Helper variant for testing enter proximity scenario
TODO: add additional parameters as more scenarios are added
// update location to outside proximity range
synchronousSendLocation(30, 30);
registerProximityListener(0, 0, 1000, expiration);
sendLocation(0, 0);
waitForAlert();
assertProximityType(true);
| private void | registerProximityListener(double latitude, double longitude, float radius, long expiration)Registers the proximity intent receiver
String intentKey = "testProximity";
Intent proximityIntent = new Intent(intentKey);
mPendingIntent = PendingIntent.getBroadcast(getContext(), 0,
proximityIntent, PendingIntent.FLAG_CANCEL_CURRENT);
mIntentReceiver = new TestIntentReceiver(intentKey);
mLocationManager.addProximityAlert(latitude, longitude, radius,
expiration, mPendingIntent);
getContext().registerReceiver(mIntentReceiver,
mIntentReceiver.getFilter());
| private void | sendLocation(double latitude, double longitude)Asynchronously update the mock location provider without notification
sendLocation(latitude, longitude, null);
| private void | sendLocation(double latitude, double longitude, java.lang.Object observer)Asynchronously update the mock location provider with given latitude and
longitude
Thread locationUpdater = new Thread() {
@Override
public void run() {
Location loc = new Location(PROVIDER_NAME);
loc.setLatitude(latitude);
loc.setLongitude(longitude);
loc.setTime(java.lang.System.currentTimeMillis());
Log.d(LOG_TAG, "Sending update for " + PROVIDER_NAME);
mLocationManager.setTestProviderLocation(PROVIDER_NAME, loc);
if (observer != null) {
synchronized (observer) {
observer.notify();
}
}
}
};
locationUpdater.start();
| protected void | setUp()
super.setUp();
// test that mock locations are allowed so a more descriptive error message can be logged
if (Settings.Secure.getInt(getContext().getContentResolver(),
Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 0) {
fail("Mock locations are currently disabled in Settings - this test requires " +
"mock locations");
}
mLocationManager = (LocationManager) getContext().
getSystemService(Context.LOCATION_SERVICE);
if (mLocationManager.getProvider(PROVIDER_NAME) != null) {
mLocationManager.removeTestProvider(PROVIDER_NAME);
}
mLocationManager.addTestProvider(PROVIDER_NAME, true, //requiresNetwork,
false, // requiresSatellite,
true, // requiresCell,
false, // hasMonetaryCost,
false, // supportsAltitude,
false, // supportsSpeed, s
false, // upportsBearing,
Criteria.POWER_MEDIUM, // powerRequirement
Criteria.ACCURACY_FINE); // accuracy
| private void | synchronousSendLocation(double latitude, double longitude)Synchronous variant of sendLocation
sendLocation(latitude, longitude, this);
// wait for location to be set
synchronized (this) {
wait(UPDATE_LOCATION_WAIT_TIME);
}
| protected void | tearDown()
mLocationManager.removeTestProvider(PROVIDER_NAME);
if (mPendingIntent != null) {
mLocationManager.removeProximityAlert(mPendingIntent);
}
if (mIntentReceiver != null) {
getContext().unregisterReceiver(mIntentReceiver);
}
| public void | testEnterProximity()Tests basic proximity alert when entering proximity
doTestEnterProximity(10000);
| public void | testEnterProximity_noexpire()Tests proximity alert when entering proximity, with no expiration
doTestEnterProximity(-1);
| public void | testExitProximity()Tests basic proximity alert when exiting proximity
// first do enter proximity scenario
doTestEnterProximity(-1);
// now update to trigger exit proximity proximity
mIntentReceiver.clearReceivedIntents();
sendLocation(20, 20);
waitForAlert();
assertProximityType(false);
| private void | waitForAlert()Blocks until proximity intent notification is received
Log.d(LOG_TAG, "Waiting for proximity update");
synchronized (mIntentReceiver) {
mIntentReceiver.wait(PROXIMITY_WAIT_TIME);
}
assertNotNull("Did not receive proximity alert",
mIntentReceiver.getLastReceivedIntent());
|
|