Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import android.os.SystemClock;
import android.platform.test.annotations.AppModeFull;
import android.provider.Settings;
import android.util.Log;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
Expand All @@ -69,38 +68,33 @@
@RunWith(AndroidJUnit4.class)
public class LocationManagerCoarseTest {

private static final String TAG = "LocationManagerCoarseTest";

private static final long TIMEOUT_MS = 5000;
private static final long RANDOM_SEED = 0;

private static final float MIN_COARSE_FUDGE_DISTANCE_M = 2000f;
private static final float MIN_COARSE_LOCATION_OFFSET_SCALE_METERS = 2000f;

private static final String TEST_PROVIDER = "test_provider";

private Random mRandom;
private Context mContext;
private LocationManager mManager;

private float mMaxCoarseFudgeDistanceM;
private float mCoarseLocationOffsetScaleMeters;

@Before
public void setUp() throws Exception {
LocationUtils.registerMockLocationProvider(InstrumentationRegistry.getInstrumentation(),
true);

long seed = System.currentTimeMillis();
Log.i(TAG, "location random seed: " + seed);

mRandom = new Random(seed);
mRandom = new Random(RANDOM_SEED);
mContext = ApplicationProvider.getApplicationContext();
mManager = mContext.getSystemService(LocationManager.class);

float coarseLocationAccuracyM = Settings.Secure.getFloat(
mContext.getContentResolver(),
LOCATION_COARSE_ACCURACY_M,
MIN_COARSE_FUDGE_DISTANCE_M);
mMaxCoarseFudgeDistanceM = (float) Math.sqrt(
2 * coarseLocationAccuracyM * coarseLocationAccuracyM);
mCoarseLocationOffsetScaleMeters =
Settings.Secure.getFloat(
mContext.getContentResolver(),
LOCATION_COARSE_ACCURACY_M,
MIN_COARSE_LOCATION_OFFSET_SCALE_METERS);

assertNotNull(mManager);

Expand Down Expand Up @@ -135,20 +129,20 @@ public void tearDown() throws Exception {
}

@Test
public void testMinCoarseLocationDistance() {
public void testMinCoarseLocationOffsetScale() {
assertThat(Settings.Secure.getFloat(
mContext.getContentResolver(),
LOCATION_COARSE_ACCURACY_M,
MIN_COARSE_FUDGE_DISTANCE_M)).isAtLeast(MIN_COARSE_FUDGE_DISTANCE_M);
MIN_COARSE_LOCATION_OFFSET_SCALE_METERS))
.isAtLeast(MIN_COARSE_LOCATION_OFFSET_SCALE_METERS);
}

@Test
public void testGetLastKnownLocation() {
Location loc = createLocation(TEST_PROVIDER, mRandom);

mManager.setTestProviderLocation(TEST_PROVIDER, loc);
assertThat(mManager.getLastKnownLocation(TEST_PROVIDER)).isNearby(loc,
mMaxCoarseFudgeDistanceM);
assertCoarseLocationIsNearby(mManager.getLastKnownLocation(TEST_PROVIDER), loc);
}

@Test
Expand All @@ -158,6 +152,7 @@ public void testGetLastKnownLocation_FastInterval() {

mManager.setTestProviderLocation(TEST_PROVIDER, loc1);
Location coarseLocation = mManager.getLastKnownLocation(TEST_PROVIDER);
assertNotNull(coarseLocation);
mManager.setTestProviderLocation(TEST_PROVIDER, loc2);
assertThat(mManager.getLastKnownLocation(TEST_PROVIDER)).isEqualTo(coarseLocation);
}
Expand All @@ -179,7 +174,7 @@ public void testRequestLocationUpdates() throws Exception {
Runnable::run,
capture);
mManager.setTestProviderLocation(TEST_PROVIDER, loc);
assertThat(capture.getNextLocation(TIMEOUT_MS)).isNearby(loc, mMaxCoarseFudgeDistanceM);
assertCoarseLocationIsNearby(capture.getNextLocation(TIMEOUT_MS), loc);
mManager.setTestProviderLocation(TEST_PROVIDER, createLocation(TEST_PROVIDER, mRandom));
assertThat(capture.getNextLocation(TIMEOUT_MS)).isNull();
}
Expand All @@ -203,7 +198,7 @@ public void testRequestLocationUpdates_Passive() throws Exception {
Runnable::run,
capture);
mManager.setTestProviderLocation(TEST_PROVIDER, loc);
assertThat(capture.getNextLocation(TIMEOUT_MS)).isNearby(loc, mMaxCoarseFudgeDistanceM);
assertCoarseLocationIsNearby(capture.getNextLocation(TIMEOUT_MS), loc);
mManager.setTestProviderLocation(TEST_PROVIDER, createLocation(TEST_PROVIDER, mRandom));
assertThat(capture.getNextLocation(TIMEOUT_MS)).isNull();
}
Expand All @@ -221,10 +216,19 @@ public void testRequestLocationUpdates_PendingIntent() throws Exception {
try (LocationPendingIntentCapture capture = new LocationPendingIntentCapture(mContext)) {
mManager.requestLocationUpdates(TEST_PROVIDER, 0, 0, capture.getPendingIntent());
mManager.setTestProviderLocation(TEST_PROVIDER, loc);
assertThat(capture.getNextLocation(TIMEOUT_MS)).isNearby(loc, mMaxCoarseFudgeDistanceM);
assertCoarseLocationIsNearby(capture.getNextLocation(TIMEOUT_MS), loc);
}
}

private void assertCoarseLocationIsNearby(Location coarseLocation, Location fineLocation) {
assertNotNull(coarseLocation);
float cellEdgeMeters = coarseLocation.getAccuracy();
float approximateCellDiagonalMeters = (float) Math.hypot(cellEdgeMeters, cellEdgeMeters);
float distanceAllowanceMeters =
approximateCellDiagonalMeters + mCoarseLocationOffsetScaleMeters;
assertThat(coarseLocation).isNearby(fineLocation, distanceAllowanceMeters);
}

@Test
public void testGetProviders() {
List<String> providers = mManager.getProviders(false);
Expand Down
Loading