diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2008-12-17 18:05:43 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2008-12-17 18:05:43 -0800 |
commit | f013e1afd1e68af5e3b868c26a653bbfb39538f8 (patch) | |
tree | 7ad6c8fd9c7b55f4b4017171dec1cb760bbd26bf /tests/CoreTests/android/location | |
parent | e70cfafe580c6f2994c4827cd8a534aabf3eb05c (diff) | |
download | frameworks_base-f013e1afd1e68af5e3b868c26a653bbfb39538f8.zip frameworks_base-f013e1afd1e68af5e3b868c26a653bbfb39538f8.tar.gz frameworks_base-f013e1afd1e68af5e3b868c26a653bbfb39538f8.tar.bz2 |
Code drop from //branches/cupcake/...@124589
Diffstat (limited to 'tests/CoreTests/android/location')
-rw-r--r-- | tests/CoreTests/android/location/LocationManagerProximityTest.java | 294 | ||||
-rw-r--r-- | tests/CoreTests/android/location/LocationTest.java | 230 |
2 files changed, 524 insertions, 0 deletions
diff --git a/tests/CoreTests/android/location/LocationManagerProximityTest.java b/tests/CoreTests/android/location/LocationManagerProximityTest.java new file mode 100644 index 0000000..f2bff4d --- /dev/null +++ b/tests/CoreTests/android/location/LocationManagerProximityTest.java @@ -0,0 +1,294 @@ +/* + * Copyright (C) 2008 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package android.location; + +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.location.Criteria; +import android.location.Location; +import android.location.LocationManager; +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.MediumTest; +import android.util.Log; + +/** + * Tests for LocationManager.addProximityAlert + * + * TODO: add tests for more scenarios + * + * To run: + * adb shell am instrument -e class com.google.android.mapstests.api.LocationProximityTest \ + * -w com.google.android.mapstests/.MapInstrumentationTestRunner + * + */ +@MediumTest +public class LocationManagerProximityTest extends AndroidTestCase { + + private static final int UPDATE_LOCATION_WAIT_TIME = 1000; + private static final int PROXIMITY_WAIT_TIME = 2000; + + private LocationManager mLocationManager; + private PendingIntent mPendingIntent; + private TestIntentReceiver mIntentReceiver; + private String mOriginalAllowedProviders; + + private static final String LOG_TAG = "LocationProximityTest"; + + // use network provider as mock location provider, because: + // - proximity alert is hardcoded to listen to only network or gps + // - 'network' provider is not installed in emulator, so can mock it + // using test provider APIs + private static final String PROVIDER_NAME = LocationManager.NETWORK_PROVIDER; + + @Override + protected void setUp() throws Exception { + super.setUp(); + mOriginalAllowedProviders = + android.provider.Settings.Secure.getString( + getContext().getContentResolver(), + android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED); + + // ensure 'only' the mock provider is enabled + // need to do this so the proximity listener does not ignore the mock + // updates in favor of gps updates + android.provider.Settings.Secure.putString( + getContext().getContentResolver(), + android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED, + PROVIDER_NAME); + + 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 + } + + @Override + protected void tearDown() throws Exception { + mLocationManager.removeTestProvider(PROVIDER_NAME); + + if (mPendingIntent != null) { + mLocationManager.removeProximityAlert(mPendingIntent); + } + if (mIntentReceiver != null) { + getContext().unregisterReceiver(mIntentReceiver); + } + + if (mOriginalAllowedProviders != null) { + // restore original settings + android.provider.Settings.Secure.putString( + getContext().getContentResolver(), + android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED, + mOriginalAllowedProviders); + mLocationManager.updateProviders(); + } + } + + /** + * Tests basic proximity alert when entering proximity + */ + public void testEnterProximity() throws Exception { + doTestEnterProximity(10000); + } + + /** + * Tests proximity alert when entering proximity, with no expiration + */ + public void testEnterProximity_noexpire() throws Exception { + doTestEnterProximity(-1); + } + + /** + * Helper variant for testing enter proximity scenario + * TODO: add additional parameters as more scenarios are added + * + * @param expiration - expiry of proximity alert + */ + private void doTestEnterProximity(long expiration) throws Exception { + // update location to outside proximity range + synchronousSendLocation(30, 30); + registerProximityListener(0, 0, 1000, expiration); + sendLocation(0, 0); + waitForAlert(); + assertProximityType(true); + } + + /** + * Tests basic proximity alert when exiting proximity + */ + public void testExitProximity() throws Exception { + // first do enter proximity scenario + doTestEnterProximity(-1); + + // now update to trigger exit proximity proximity + mIntentReceiver.clearReceivedIntents(); + sendLocation(20, 20); + waitForAlert(); + assertProximityType(false); + } + + /** + * Registers the proximity intent receiver + */ + private void registerProximityListener(double latitude, double longitude, + float radius, long expiration) { + 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()); + + } + + /** + * Blocks until proximity intent notification is received + * @throws InterruptedException + */ + private void waitForAlert() 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()); + } + + /** + * Asserts that the received intent had the enter proximity property set as + * expected + * @param expectedEnterProximity - true if enter proximity expected, false if + * exit expected + */ + private void assertProximityType(boolean expectedEnterProximity) + throws Exception { + boolean proximityTest = mIntentReceiver.getLastReceivedIntent(). + getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING, + !expectedEnterProximity); + assertEquals("proximity alert not set to expected enter proximity value", + expectedEnterProximity, proximityTest); + } + + /** + * Synchronous variant of sendLocation + */ + private void synchronousSendLocation(final double latitude, + final double longitude) + throws InterruptedException { + sendLocation(latitude, longitude, this); + // wait for location to be set + synchronized (this) { + wait(UPDATE_LOCATION_WAIT_TIME); + } + } + + /** + * Asynchronously update the mock location provider without notification + */ + private void sendLocation(final double latitude, final double longitude) { + sendLocation(latitude, longitude, null); + } + + /** + * 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 + */ + private void sendLocation(final double latitude, final double longitude, + final Object observer) { + 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(); + + } + + /** + * Helper class that receives a proximity intent and notifies the main class + * when received + */ + private static class TestIntentReceiver extends BroadcastReceiver { + + private String mExpectedAction; + private Intent mLastReceivedIntent; + + public TestIntentReceiver(String expectedAction) { + mExpectedAction = expectedAction; + mLastReceivedIntent = null; + } + + public IntentFilter getFilter() { + IntentFilter filter = new IntentFilter(mExpectedAction); + return filter; + } + + @Override + public void onReceive(Context context, Intent intent) { + if (intent != null && mExpectedAction.equals(intent.getAction())) { + Log.d(LOG_TAG, "Intent Received: " + intent.toString()); + mLastReceivedIntent = intent; + synchronized (this) { + notify(); + } + } + } + + public Intent getLastReceivedIntent() { + return mLastReceivedIntent; + } + + public void clearReceivedIntents() { + mLastReceivedIntent = null; + } + } +} diff --git a/tests/CoreTests/android/location/LocationTest.java b/tests/CoreTests/android/location/LocationTest.java new file mode 100644 index 0000000..847ac7a --- /dev/null +++ b/tests/CoreTests/android/location/LocationTest.java @@ -0,0 +1,230 @@ +/* + * Copyright (C) 2007 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.location; + +import android.test.suitebuilder.annotation.SmallTest; + +import junit.framework.TestCase; + +/** + * Unit tests for android.location.Location + */ +@SmallTest +public class LocationTest extends TestCase { + + // ***** Tests for Location.convert + public void testConvert_DegreesToDouble(){ + String testDegreesCoord = "-80.075"; + String message; + double result; + + result = Location.convert(testDegreesCoord); + message = "degreesToDoubleTest: Double should be -80.075, actual value is " + + String.valueOf(result); + assertEquals(message, -80.075, result); + } + + public void testConvert_MinutesToDouble(){ + String testMinutesCoord = "-80:05.10000"; + String message; + double result; + + result = Location.convert(testMinutesCoord); + message = "minutesToDoubleTest: Double should be -80.085, actual value is " + + String.valueOf(result); + assertEquals(message, -80.085, result); + } + + public void testConvert_SecondsToDouble(){ + String testSecondsCoord = "-80:04:03.00000"; + String message; + double result; + + result = Location.convert(testSecondsCoord); + message = "secondsToDoubleTest: Double should be -80.0675, actual value is " + + String.valueOf(result); + assertEquals(message, -80.0675, result); + } + + public void testConvert_SecondsToDouble2(){ + String testSecondsCoord = "-80:4:3"; + String message; + double result; + + result = Location.convert(testSecondsCoord); + message = "secondsToDouble2Test: Double should be -80.0675, actual value is " + + String.valueOf(result); + assertEquals(message, -80.0675, result); + } + + // Testing the Convert(Double, Int) + public void testConvert_CoordinateToDegrees(){ + String message; + String result; + + result = Location.convert(-80.075, Location.FORMAT_DEGREES); + message = "coordinateToDegreesTest: Should return a string -80.075, but returned " + result; + assertEquals(message, "-80.075", result); + } + + public void testConvert_CoordinateToDegrees2(){ + String message; + String result; + result = Location.convert(-80.0, Location.FORMAT_DEGREES); + message = "coordinateToDegrees2Test: Should return a string -80, but returned " + result; + assertEquals(message, "-80", result); + } + + public void testConvert_CoordinateToMinutes(){ + String message; + String result; + double input = -80.085; + result = Location.convert(input, Location.FORMAT_MINUTES); + message = "coordinateToMinuteTest: Should return a string -80:5.1, but returned " + + result; + assertEquals(message, "-80:5.1", result); + } + + public void testConvert_CoordinateToMinutes2(){ + String message; + String result; + double input = -80; + result = Location.convert(input, Location.FORMAT_MINUTES); + message = "coordinateToMinute2Test: Should return a string -80:0, but returned " + + result; + assertEquals(message, "-80:0", result); + } + + public void testConvert_CoordinateToSeconds(){ + String message; + String result; + + result = Location.convert(-80.075, Location.FORMAT_SECONDS); + message = "coordinateToSecondsTest: Should return a string -80:4:30, but returned " + + result; + assertEquals(message, "-80:4:30", result); + } + // **** end tests for Location.convert + + + public void testBearingTo(){ + String message; + float bearing; + Location zeroLocation = new Location(""); + zeroLocation.setLatitude(0); + zeroLocation.setLongitude(0); + + Location testLocation = new Location(""); + testLocation.setLatitude(1000000); + testLocation.setLongitude(0); + + bearing = zeroLocation.bearingTo(zeroLocation); + message = "bearingToTest: Bearing should be 0, actual value is " + String.valueOf(bearing); + assertEquals(message, 0, bearing, 0); + + bearing = zeroLocation.bearingTo(testLocation); + message = "bearingToTest: Bearing should be 180, actual value is " + + String.valueOf(bearing); + assertEquals(message, 180, bearing, 0); + + testLocation.setLatitude(0); + testLocation.setLongitude(1000000); + bearing = zeroLocation.bearingTo(testLocation); + message = "bearingToTest: Bearing should be -90, actual value is " + + String.valueOf(bearing); + assertEquals(message, -90, bearing, 0); + + //TODO: Test a Random Middle Value + } + + public void testDistanceTo() { + String message; + boolean result = true; + float distance; + Location zeroLocation = new Location(""); + zeroLocation.setLatitude(0); + zeroLocation.setLongitude(0); + + Location testLocation = new Location(""); + testLocation.setLatitude(1000000); + testLocation.setLongitude(0); + + distance = zeroLocation.distanceTo(zeroLocation); + message = "distanceToTest: Distance should be 0, actual value is " + + String.valueOf(distance); + assertEquals(message, distance, 0, 0); + + distance = zeroLocation.distanceTo(testLocation); + message = "distanceToTest: Distance should be 8885140, actual value is " + + String.valueOf(distance); + assertEquals(message, distance, 8885140.0, 1); + } + + public void testAltitude() { + String message; + Location loc = new Location(""); + + loc.setAltitude(1); + message = "altitudeTest: set/getAltitude to 1 didn't work."; + assertEquals(message, loc.getAltitude(), 1, 0); + message = "altitudeTest: hasAltitude (a) didn't work."; + assertTrue(message, loc.hasAltitude()); + + loc.removeAltitude(); + message = "altitudeTest: hasAltitude (b) didn't work."; + assertFalse(message, loc.hasAltitude()); + message = "altitudeTest: getAltitude didn't return 0 when there was no altitude."; + assertEquals(message, loc.getAltitude(), 0, 0); + } + + public void testSpeed() { + String message; + Location loc = new Location(""); + + loc.setSpeed(1); + message = "speedTest: set/getSpeed to 1 didn't work."; + assertEquals(message, loc.getSpeed(), 1, 0); + message = "speedTest: hasSpeed (a) didn't work."; + assertTrue(message, loc.hasSpeed()); + + loc.removeSpeed(); + message = "speedTest: hasSpeed (b) didn't work."; + assertFalse(message, loc.hasSpeed()); + message = "speedTest: getSpeed didn't return 0 when there was no speed."; + assertEquals(message, loc.getSpeed(), 0, 0); + } + + public void testBearing() { + String message; + Location loc = new Location(""); + + loc.setBearing(1); + message = "bearingTest: set/getBearing to 1 didn't work."; + assertEquals(message, loc.getBearing(), 1, 0); + message = "bearingTest: hasBearing (a) didn't work."; + assertTrue(message, loc.hasBearing()); + + loc.removeBearing(); + message = "bearingTest: hasBearing (b) didn't work."; + assertFalse(message, loc.hasBearing()); + message = "bearingTest: getBearing didn't return 0 when there was no bearing."; + assertEquals(message, loc.getBearing(), 0, 0); + } + +} + + |