@Test public void requestLocation_Success() throws SecurityException { final Location expectedLocation = buildFakeLocation(provider); // set provider enabled setIsProviderEnabled(provider, true); // answer Mockito.doAnswer( new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { final Object[] args = invocation.getArguments(); final LocationListener l = (LocationListener) args[1]; l.onLocationChanged(expectedLocation); return null; } }) .when(locationManager) .requestSingleUpdate( Mockito.eq(provider), Mockito.any(LocationListener.class), Mockito.any(Looper.class)); final RxLocationManager rxLocationManager = getDefaullRxLocationManager(); final TestSubscriber<Location> subscriber = new TestSubscriber<>(); rxLocationManager.requestLocation(provider).subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertCompleted(); subscriber.assertValue(expectedLocation); }
/** Test that request location throw Exception if provider disabled */ @Test public void requestLocation_ProviderDisabled() { // set provider disabled setIsProviderEnabled(provider, false); final RxLocationManager rxLocationManager = getDefaullRxLocationManager(); final TestSubscriber<Location> subscriber = new TestSubscriber<>(); rxLocationManager.requestLocation(provider).subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertError(ProviderDisabledException.class); }
/** * Test that all fine * * @throws SecurityException */ @Test public void getLastLocation_Success() throws SecurityException { final Location expectedLocation = buildFakeLocation(provider); Mockito.when(locationManager.getLastKnownLocation(provider)).thenReturn(expectedLocation); final RxLocationManager rxLocationManager = getDefaullRxLocationManager(); final TestSubscriber<Location> subscriber = new TestSubscriber<>(); rxLocationManager.getLastLocation(provider).subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertCompleted(); subscriber.assertValue(expectedLocation); }
/** Test that request location throw TimeOutException */ @Test public void requestLocation_TimeOutError() { // set provider enabled setIsProviderEnabled(provider, true); final RxLocationManager rxLocationManager = getDefaullRxLocationManager(); final TestSubscriber<Location> subscriber = new TestSubscriber<>(); rxLocationManager .requestLocation(provider, new LocationTime(5, TimeUnit.SECONDS)) .subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertError(TimeoutException.class); }
/** * Test that getLastLocation throw ElderLocationException if howOldCanBe is provided * * @throws SecurityException */ @Test public void getLastLocation_Old() throws SecurityException { final Location expectedLocation = buildFakeLocation(provider); expectedLocation.setTime(System.currentTimeMillis() - TimeUnit.HOURS.toMillis(1)); Mockito.when(locationManager.getLastKnownLocation(provider)).thenReturn(expectedLocation); final RxLocationManager rxLocationManager = getDefaullRxLocationManager(); final TestSubscriber<Location> subscriber = new TestSubscriber<>(); rxLocationManager .getLastLocation(provider, new LocationTime(30, TimeUnit.MINUTES)) .subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertError(ElderLocationException.class); }