MaxAccuracyWithinTimeoutLocationClient implements LocationClient.LocationClientListener, LocationListener { public void requestLocationUpdates(long timeoutSeconds) { new Handler(Looper.getMainLooper()).post(() -> { locationClient.setListener(this); locationClient.start(); }); highestAccuracyReading = null; timerHandler.removeCallbacksAndMessages(null); timerHandler.postDelayed(() -> { locationClient.stop(); locationClient.setListener(null); Timber.i("MaxAccuracyWithinTimeoutLocationClient: stopping location updates"); }, timeoutSeconds * 1000); } MaxAccuracyWithinTimeoutLocationClient(LocationClient locationClient, LocationListener listener); void requestLocationUpdates(long timeoutSeconds); @Override void onClientStart(); @Override void onClientStartFailure(); @Override void onClientStop(); @Override void onLocationChanged(Location location);  }
@Test public void requestingLocationUpdates_ShouldResultInUpdate() { maxAccuracyLocationClient.requestLocationUpdates(5); Location location = createLocation("GPS", 1, 1, 1, 100); fakeLocationClient.receiveFix(location); assertEquals(location, testLocationListener.getLastLocation()); }
@Test public void denyingLocationPermissions_ShouldResultInNoUpdates() { fakeLocationClient.setFailOnRequest(true); maxAccuracyLocationClient.requestLocationUpdates(5); Location location = createLocation("GPS", 1, 1, 1, 100); fakeLocationClient.receiveFix(location); assertNull(testLocationListener.getLastLocation()); }
@Test public void onlyMoreAccurateFixes_ShouldResultInUpdates() { maxAccuracyLocationClient.requestLocationUpdates(5); Location location = createLocation("GPS", 1, 1, 1, 100); fakeLocationClient.receiveFix(location); assertEquals(location, testLocationListener.getLastLocation()); Location moreAccurateLocation = createLocation("GPS", 1, 1, 1, 2); fakeLocationClient.receiveFix(moreAccurateLocation); assertEquals(moreAccurateLocation, testLocationListener.getLastLocation()); Location lessAccurateLocation = createLocation("GPS", 1, 1, 1, 10); fakeLocationClient.receiveFix(lessAccurateLocation); assertEquals(moreAccurateLocation, testLocationListener.getLastLocation()); }
@Test public void fixWithAccuracyAfterAFixWithout_ShouldResultInUpdate() { maxAccuracyLocationClient.requestLocationUpdates(5); Location locationWithoutAccuracy = createLocation("GPS", 1, 1, 1); fakeLocationClient.receiveFix(locationWithoutAccuracy); assertEquals(locationWithoutAccuracy, testLocationListener.getLastLocation()); Location locationWithAccuracy = createLocation("GPS", 5, 5, 5, 100); fakeLocationClient.receiveFix(locationWithAccuracy); assertEquals(locationWithAccuracy, testLocationListener.getLastLocation()); }
@Test public void fixWithoutAccuracyAfterAFixWith_ShouldNotResultInUpdate() { maxAccuracyLocationClient.requestLocationUpdates(5); Location locationWithAccuracy = createLocation("GPS", 5, 5, 5, 100); fakeLocationClient.receiveFix(locationWithAccuracy); assertEquals(locationWithAccuracy, testLocationListener.getLastLocation()); Location locationWithoutAccuracy = createLocation("GPS", 1, 1, 1); fakeLocationClient.receiveFix(locationWithoutAccuracy); assertEquals(locationWithAccuracy, testLocationListener.getLastLocation()); }
@Test public void requestingLocationUpdatesAgain_ShouldResetHighestAccuracy() { maxAccuracyLocationClient.requestLocationUpdates(5); Location location = createLocation("GPS", 1, 1, 1, 2); fakeLocationClient.receiveFix(location); assertEquals(location, testLocationListener.getLastLocation()); maxAccuracyLocationClient.requestLocationUpdates(5); Location lessAccurateLocation = createLocation("GPS", 1, 1, 1, 10); fakeLocationClient.receiveFix(lessAccurateLocation); assertEquals(lessAccurateLocation, testLocationListener.getLastLocation()); }
@Test public void timeoutSecondsPassing_ShouldStopUpdates() { int timeoutSeconds = 5; maxAccuracyLocationClient.requestLocationUpdates(timeoutSeconds); shadowOf(Looper.getMainLooper()).getScheduler().advanceBy(timeoutSeconds, TimeUnit.SECONDS); assertFalse(fakeLocationClient.isRunning()); Location location = createLocation("GPS", 1, 1, 1, 100); fakeLocationClient.receiveFix(location); assertNull(testLocationListener.getLastLocation()); }
@Test public void requestingLocationUpdatesAgain_ShouldResetTimeout() { int timeoutSeconds = 5; maxAccuracyLocationClient.requestLocationUpdates(timeoutSeconds); shadowOf(Looper.getMainLooper()).getScheduler().advanceBy(timeoutSeconds - 1, TimeUnit.SECONDS); assertTrue(fakeLocationClient.isRunning()); maxAccuracyLocationClient.requestLocationUpdates(timeoutSeconds); shadowOf(Looper.getMainLooper()).getScheduler().advanceBy(timeoutSeconds - 1, TimeUnit.SECONDS); assertTrue(fakeLocationClient.isRunning()); }