RideRequestButtonController { public void loadRideInformation(@NonNull RideParameters rideParameters) { if (rideParameters.getPickupLatitude() != null) { checkNotNull(rideParameters.getPickupLongitude(), "Pickup point latitude is set in " + "RideParameters but not the longitude."); } if (rideParameters.getPickupLongitude() != null) { checkNotNull(rideParameters.getPickupLatitude(), "Pickup point longitude is set in " + "RideParameters but not the latitude."); } if (rideParameters.getDropoffLatitude() != null) { checkNotNull(rideParameters.getDropoffLongitude(), "Dropoff point latitude is set in " + "RideParameters but not the longitude."); } if (rideParameters.getDropoffLongitude() != null) { checkNotNull(rideParameters.getDropoffLatitude(), "Dropoff point longitude is set in" + " RideParameters but not the latitude."); } cancelAllPending(); if (rideParameters.getPickupLatitude() != null) { if (rideParameters.getDropoffLatitude() != null) { TimePriceDelegate pendingDelegate = new TimePriceDelegate(rideRequestButtonView, rideRequestButtonCallback); loadPriceEstimate( rideParameters.getPickupLatitude().floatValue(), rideParameters.getPickupLongitude().floatValue(), rideParameters.getDropoffLatitude().floatValue(), rideParameters.getDropoffLongitude().floatValue(), rideParameters.getProductId(), pendingDelegate); this.pendingDelegate = pendingDelegate; } else { pendingDelegate = new TimeDelegate(rideRequestButtonView, rideRequestButtonCallback); } loadTimeEstimate( pendingDelegate, rideParameters.getPickupLatitude().floatValue(), rideParameters.getPickupLongitude().floatValue(), rideParameters.getProductId()); } else { rideRequestButtonView.showDefaultView(); } } @VisibleForTesting  RideRequestButtonController(
            @NonNull RideRequestButtonView rideRequestButtonView,
            @NonNull RidesService ridesService,
            @Nullable RideRequestButtonCallback callback);  RideRequestButtonController(
            @NonNull RideRequestButtonView rideRequestButtonView,
            @NonNull Session session,
            @Nullable RideRequestButtonCallback callback); void loadRideInformation(@NonNull RideParameters rideParameters); void destroy();  }
@Test public void testLoadInformationWithProductId_whenEstimatesSuccessful() throws Exception { stubPriceApiSuccessful(); stubTimeApiWithProductIdSuccessful(); controller.loadRideInformation(rideParameters); countDownLatch.await(3, TimeUnit.SECONDS); verify(callback).onRideInformationLoaded(); verify(callback, never()).onError(Mockito.any(ApiError.class)); verify(callback, never()).onError(Mockito.any(Throwable.class)); ArgumentCaptor<PriceEstimate> priceCaptor = ArgumentCaptor.forClass(PriceEstimate.class); ArgumentCaptor<TimeEstimate> timeCaptor = ArgumentCaptor.forClass(TimeEstimate.class); verify(view).showEstimate(timeCaptor.capture(), priceCaptor.capture()); assertThat(priceCaptor.getValue().getEstimate()).isEqualTo("$9-12"); assertThat(timeCaptor.getValue().getEstimate()).isEqualTo(120); }
@Test public void testLoadInformationNoProductId_whenEstimatesSuccessful() throws Exception { stubPriceApiSuccessful(); stubTimeApiSuccessful(); rideParameters = new RideParameters.Builder() .setDropoffLocation(valueOf(DROP_OFF_LATITUDE), valueOf(DROP_OFF_LONGITUDE), DROP_OFF_NICKNAME, DROP_OFF_ADDRESS) .setPickupLocation(valueOf(PICKUP_LATITUDE), valueOf(PICKUP_LONGITUDE), PICKUP_NICKNAME, PICKUP_ADDRESS) .build(); controller.loadRideInformation(rideParameters); countDownLatch.await(3, TimeUnit.SECONDS); verify(callback).onRideInformationLoaded(); verify(callback, never()).onError(Mockito.any(ApiError.class)); verify(callback, never()).onError(Mockito.any(Throwable.class)); ArgumentCaptor<PriceEstimate> priceCaptor = ArgumentCaptor.forClass(PriceEstimate.class); ArgumentCaptor<TimeEstimate> timeCaptor = ArgumentCaptor.forClass(TimeEstimate.class); verify(view).showEstimate(timeCaptor.capture(), priceCaptor.capture()); assertThat(priceCaptor.getValue().getEstimate()).isEqualTo("$5.75"); assertThat(timeCaptor.getValue().getEstimate()).isEqualTo(100); }
@Test public void testLoadInformationWithProductId_whenTimeOnly() throws Exception { stubPriceApi(aResponse().withStatus(500)); stubTimeApiWithProductIdSuccessful(); rideParameters = new RideParameters.Builder() .setProductId(PRODUCT_ID) .setPickupLocation(valueOf(PICKUP_LATITUDE), valueOf(PICKUP_LONGITUDE), PICKUP_NICKNAME, PICKUP_ADDRESS) .build(); controller.loadRideInformation(rideParameters); countDownLatch.countDown(); countDownLatch.await(3, TimeUnit.SECONDS); verify(callback).onRideInformationLoaded(); verify(callback, never()).onError(Mockito.any(ApiError.class)); verify(callback, never()).onError(Mockito.any(Throwable.class)); ArgumentCaptor<TimeEstimate> timeCaptor = ArgumentCaptor.forClass(TimeEstimate.class); verify(view).showEstimate(timeCaptor.capture()); assertThat(timeCaptor.getValue().getEstimate()).isEqualTo(120); }
@Test public void testLoadInformationNoProductId_whenTimeOnly() throws Exception { stubPriceApi(aResponse().withStatus(500)); stubTimeApiSuccessful(); rideParameters = new RideParameters.Builder() .setProductId(PRODUCT_ID) .setPickupLocation(valueOf(PICKUP_LATITUDE), valueOf(PICKUP_LONGITUDE), PICKUP_NICKNAME, PICKUP_ADDRESS) .build(); controller.loadRideInformation(rideParameters); countDownLatch.countDown(); countDownLatch.await(3, TimeUnit.SECONDS); verify(callback).onRideInformationLoaded(); verify(callback, never()).onError(Mockito.any(ApiError.class)); verify(callback, never()).onError(Mockito.any(Throwable.class)); ArgumentCaptor<TimeEstimate> timeCaptor = ArgumentCaptor.forClass(TimeEstimate.class); verify(view).showEstimate(timeCaptor.capture()); assertThat(timeCaptor.getValue().getEstimate()).isEqualTo(120); }
@Test public void testLoadInformation_whenPriceReturnsApiError() throws Exception { stubPriceApi(aResponse().withStatus(500)); stubTimeApiWithProductIdSuccessful(); controller.loadRideInformation(rideParameters); countDownLatch.await(3, TimeUnit.SECONDS); verify(callback, never()).onRideInformationLoaded(); ArgumentCaptor<ApiError> errorCaptor = ArgumentCaptor.forClass(ApiError.class); verify(callback).onError(errorCaptor.capture()); assertThat(errorCaptor.getValue().getClientErrors().get(0).getStatus()).isEqualTo(500); verify(callback, never()).onError(any(Throwable.class)); verify(view, never()).showEstimate(any(TimeEstimate.class)); verify(view, never()).showEstimate(any(TimeEstimate.class), any(PriceEstimate.class)); verify(view).showDefaultView(); }
@Test public void testLoadInformation_whenTimeReturnsApiError() throws Exception { stubPriceApiSuccessful(); stubTimeApi(aResponse().withStatus(500)); controller.loadRideInformation(rideParameters); countDownLatch.await(3, TimeUnit.SECONDS); verify(callback, never()).onRideInformationLoaded(); ArgumentCaptor<ApiError> errorCaptor = ArgumentCaptor.forClass(ApiError.class); verify(callback).onError(errorCaptor.capture()); assertThat(errorCaptor.getValue().getClientErrors().get(0).getStatus()).isEqualTo(500); verify(callback, never()).onError(any(Throwable.class)); verify(view, never()).showEstimate(any(TimeEstimate.class)); verify(view, never()).showEstimate(any(TimeEstimate.class), any(PriceEstimate.class)); verify(view).showDefaultView(); }
@Test public void testLoadInformation_whenNoMatchingProductTimeEstimates() throws Exception { stubPriceApiSuccessful(); stubTimeApi(aResponse().withBodyFile("times_estimate_no_uberx.json")); controller.loadRideInformation(rideParameters); countDownLatch.await(3, TimeUnit.SECONDS); verify(callback, never()).onRideInformationLoaded(); ArgumentCaptor<ApiError> errorCaptor = ArgumentCaptor.forClass(ApiError.class); verify(callback).onError(errorCaptor.capture()); assertThat(errorCaptor.getValue().getClientErrors().get(0).getStatus()).isEqualTo(404); verify(callback, never()).onError(any(Throwable.class)); verify(view, never()).showEstimate(any(TimeEstimate.class)); verify(view, never()).showEstimate(any(TimeEstimate.class), any(PriceEstimate.class)); verify(view).showDefaultView(); }
@Test public void testLoadInformation_whenNoMatchingProductPriceEstimates() throws Exception { stubTimeApiSuccessful(); stubPriceApi(aResponse().withBodyFile("prices_estimate_no_uberx.json")); controller.loadRideInformation(rideParameters); countDownLatch.await(3, TimeUnit.SECONDS); verify(callback, never()).onRideInformationLoaded(); ArgumentCaptor<ApiError> errorCaptor = ArgumentCaptor.forClass(ApiError.class); verify(callback).onError(errorCaptor.capture()); assertThat(errorCaptor.getValue().getClientErrors().get(0).getStatus()).isEqualTo(404); verify(callback, never()).onError(any(Throwable.class)); verify(view, never()).showEstimate(any(TimeEstimate.class)); verify(view, never()).showEstimate(any(TimeEstimate.class), any(PriceEstimate.class)); verify(view).showDefaultView(); }
@Test public void testLoadInformation_whenPriceEstimatesEmpty() throws Exception { stubTimeApiSuccessful(); stubPriceApi(aResponse().withBody("{}")); controller.loadRideInformation(rideParameters); countDownLatch.await(3, TimeUnit.SECONDS); verify(callback, never()).onRideInformationLoaded(); ArgumentCaptor<ApiError> errorCaptor = ArgumentCaptor.forClass(ApiError.class); verify(callback).onError(errorCaptor.capture()); assertThat(errorCaptor.getValue().getClientErrors().get(0).getStatus()).isEqualTo(404); verify(callback, never()).onError(any(Throwable.class)); verify(view, never()).showEstimate(any(TimeEstimate.class)); verify(view, never()).showEstimate(any(TimeEstimate.class), any(PriceEstimate.class)); verify(view).showDefaultView(); }
@Test public void testLoadInformation_whenTimeEstimatesEmpty() throws Exception { stubTimeApi(aResponse().withBody("{}")); stubPriceApiSuccessful(); controller.loadRideInformation(rideParameters); countDownLatch.await(3, TimeUnit.SECONDS); verify(callback, never()).onRideInformationLoaded(); ArgumentCaptor<ApiError> errorCaptor = ArgumentCaptor.forClass(ApiError.class); verify(callback).onError(errorCaptor.capture()); assertThat(errorCaptor.getValue().getClientErrors().get(0).getStatus()).isEqualTo(404); verify(callback, never()).onError(any(Throwable.class)); verify(view, never()).showEstimate(any(TimeEstimate.class)); verify(view, never()).showEstimate(any(TimeEstimate.class), any(PriceEstimate.class)); verify(view).showDefaultView(); }
@Test public void testLoadInformation_whenPriceFails() throws Exception { stubTimeApi(aResponse().withFault(Fault.RANDOM_DATA_THEN_CLOSE)); stubPriceApiSuccessful(); controller.loadRideInformation(rideParameters); countDownLatch.await(3, TimeUnit.SECONDS); verify(callback, never()).onRideInformationLoaded(); ArgumentCaptor<Throwable> errorCaptor = ArgumentCaptor.forClass(Throwable.class); verify(callback).onError(errorCaptor.capture()); assertThat(errorCaptor.getValue()).isNotNull(); verify(view, never()).showEstimate(any(TimeEstimate.class)); verify(view, never()).showEstimate(any(TimeEstimate.class), any(PriceEstimate.class)); verify(view).showDefaultView(); }
@Test public void testLoadInformation_whenTimeFails() throws Exception { stubTimeApiSuccessful(); stubPriceApi(aResponse().withFault(Fault.RANDOM_DATA_THEN_CLOSE)); controller.loadRideInformation(rideParameters); countDownLatch.await(3, TimeUnit.SECONDS); verify(callback, never()).onRideInformationLoaded(); ArgumentCaptor<Throwable> errorCaptor = ArgumentCaptor.forClass(Throwable.class); verify(callback).onError(errorCaptor.capture()); assertThat(errorCaptor.getValue()).isNotNull(); verify(view, never()).showEstimate(any(TimeEstimate.class)); verify(view, never()).showEstimate(any(TimeEstimate.class), any(PriceEstimate.class)); verify(view).showDefaultView(); }
@Test public void testLoadInformation_wheNoPickup() { rideParameters = new RideParameters.Builder() .setProductId(PRODUCT_ID) .setDropoffLocation(valueOf(DROP_OFF_LATITUDE), valueOf(DROP_OFF_LONGITUDE), DROP_OFF_NICKNAME, DROP_OFF_ADDRESS) .build(); controller.loadRideInformation(rideParameters); verify(view).showDefaultView(); verify(view, never()).showEstimate(any(TimeEstimate.class)); verify(view, never()).showEstimate(any(TimeEstimate.class), any(PriceEstimate.class)); }
@Test public void testLoadInformation_wheNoPickupLongitude() { rideParameters = new RideParameters.Builder() .setProductId(PRODUCT_ID) .setPickupLocation(valueOf(PICKUP_LATITUDE), null, PICKUP_NICKNAME, PICKUP_ADDRESS) .setDropoffLocation(valueOf(DROP_OFF_LATITUDE), valueOf(DROP_OFF_LONGITUDE), DROP_OFF_NICKNAME, DROP_OFF_ADDRESS) .build(); expectedException.expect(NullPointerException.class); expectedException.expectMessage("Pickup point latitude is set in " + "RideParameters but not the longitude."); controller.loadRideInformation(rideParameters); }
@Test public void testLoadInformation_wheNoPickupLatitude() { rideParameters = new RideParameters.Builder() .setProductId(PRODUCT_ID) .setPickupLocation(null, valueOf(PICKUP_LONGITUDE), PICKUP_NICKNAME, PICKUP_ADDRESS) .setDropoffLocation(null, valueOf(DROP_OFF_LONGITUDE), DROP_OFF_NICKNAME, DROP_OFF_ADDRESS) .build(); expectedException.expect(NullPointerException.class); expectedException.expectMessage("Pickup point longitude is set in " + "RideParameters but not the latitude."); controller.loadRideInformation(rideParameters); }
@Test public void testLoadInformation_wheNoDropOffLatitude() { rideParameters = new RideParameters.Builder() .setProductId(PRODUCT_ID) .setPickupLocation(valueOf(PICKUP_LATITUDE), valueOf(PICKUP_LONGITUDE), PICKUP_NICKNAME, PICKUP_ADDRESS) .setDropoffLocation(null, valueOf(DROP_OFF_LONGITUDE), DROP_OFF_NICKNAME, DROP_OFF_ADDRESS) .build(); expectedException.expect(NullPointerException.class); expectedException.expectMessage("Dropoff point longitude is set in RideParameters but not the latitude."); controller.loadRideInformation(rideParameters); }
@Test public void testLoadInformation_wheNoDropOffLongitude() { rideParameters = new RideParameters.Builder() .setProductId(PRODUCT_ID) .setPickupLocation(valueOf(PICKUP_LATITUDE), valueOf(PICKUP_LONGITUDE), PICKUP_NICKNAME, PICKUP_ADDRESS) .setDropoffLocation(valueOf(DROP_OFF_LATITUDE), null, DROP_OFF_NICKNAME, DROP_OFF_ADDRESS) .build(); expectedException.expect(NullPointerException.class); expectedException.expectMessage("Dropoff point latitude is set in RideParameters but not the longitude."); controller.loadRideInformation(rideParameters); }