FileDocCategorySizeDatePackage
LocationManagerProximityTest.javaAPI DocAndroid 1.5 API9762Wed May 06 22:42:02 BST 2009android.location

LocationManagerProximityTest

public 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
Constructors Summary
Methods Summary
private voidassertProximityType(boolean expectedEnterProximity)
Asserts that the received intent had the enter proximity property set as expected

param
expectedEnterProximity - true if enter proximity expected, false if exit expected

        boolean proximityTest = mIntentReceiver.getLastReceivedIntent().
                getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING, 
                !expectedEnterProximity);
        assertEquals("proximity alert not set to expected enter proximity value",
                expectedEnterProximity, proximityTest);
    
private voiddoTestEnterProximity(long expiration)
Helper variant for testing enter proximity scenario TODO: add additional parameters as more scenarios are added

param
expiration - expiry of proximity alert

        // update location to outside proximity range
        synchronousSendLocation(30, 30);
        registerProximityListener(0, 0, 1000, expiration);
        sendLocation(0, 0);
        waitForAlert();
        assertProximityType(true);
    
private voidregisterProximityListener(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 voidsendLocation(double latitude, double longitude)
Asynchronously update the mock location provider without notification

        sendLocation(latitude, longitude, null);
    
private voidsendLocation(double latitude, double longitude, java.lang.Object observer)
Asynchronously update the mock location provider with given latitude and longitude

param
latitude - update location
param
longitude - update location
param
observer - optionally, object to notify when update is sent.If null, no update will be sent

        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 voidsetUp()


    
         
        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 voidsynchronousSendLocation(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 voidtearDown()

        mLocationManager.removeTestProvider(PROVIDER_NAME);

        if (mPendingIntent != null) {
            mLocationManager.removeProximityAlert(mPendingIntent);
        }
        if (mIntentReceiver != null) {
            getContext().unregisterReceiver(mIntentReceiver);
        }
    
public voidtestEnterProximity()
Tests basic proximity alert when entering proximity

        doTestEnterProximity(10000);
    
public voidtestEnterProximity_noexpire()
Tests proximity alert when entering proximity, with no expiration

        doTestEnterProximity(-1);
    
public voidtestExitProximity()
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 voidwaitForAlert()
Blocks until proximity intent notification is received

throws
InterruptedException

        Log.d(LOG_TAG, "Waiting for proximity update");
        synchronized (mIntentReceiver) {
            mIntentReceiver.wait(PROXIMITY_WAIT_TIME);
        }

        assertNotNull("Did not receive proximity alert", 
                mIntentReceiver.getLastReceivedIntent());