AutocompletePendingResult extends PendingResult<AutocompletePredictionBuffer> { @Override public void setResultCallback( @NonNull final ResultCallback<? super AutocompletePredictionBuffer> callback) { LatLng center = bounds.getCenter(); pelias.suggest(query, center.getLatitude(), center.getLongitude(), new Callback<Result>() { @Override public void onResponse(Call<Result> call, Response<Result> response) { Status status = new Status(Status.SUCCESS); final ArrayList<AutocompletePrediction> predictions = new ArrayList<>(); final List<Feature> features = response.body().getFeatures(); for (Feature feature : features) { AutocompletePrediction prediction = new AutocompletePrediction(feature.properties.gid, feature.properties.name); predictions.add(prediction); } AutocompletePredictionBuffer buffer = new AutocompletePredictionBuffer(status, predictions); callback.onResult(buffer); } @Override public void onFailure(Call<Result> call, Throwable t) { Status status = new Status(Status.INTERNAL_ERROR); AutocompletePredictionBuffer buffer = new AutocompletePredictionBuffer(status, null); callback.onResult(buffer); } }); } AutocompletePendingResult(Pelias pelias, String query, LatLngBounds bounds,
      AutocompleteFilter filter); @NonNull @Override AutocompletePredictionBuffer await(); @NonNull @Override AutocompletePredictionBuffer await(long time, @NonNull TimeUnit timeUnit); @Override void cancel(); @Override boolean isCanceled(); @Override void setResultCallback(
      @NonNull final ResultCallback<? super AutocompletePredictionBuffer> callback); @Override void setResultCallback(
      @NonNull ResultCallback<? super AutocompletePredictionBuffer> callback, long time,
      @NonNull TimeUnit timeUnit);  }
@Test public void setResultCallback_shouldInvokePeliasSuggest() { TestResultCallback callback = new TestResultCallback(); result.setResultCallback(callback); LatLng center = bounds.getCenter(); verify(pelias).suggest(eq(query), eq(center.getLatitude()), eq(center.getLongitude()), any(Callback.class)); }
@Test public void setResultCallback_shouldReturnSuccessStatus() { doAnswer(new Answer() { public Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); Callback callback = (Callback) args[3]; final Result result = new Result(); callback.onResponse(null, Response.success(result)); return null; } }).when(pelias).suggest(anyString(), anyDouble(), anyDouble(), any(Callback.class)); TestResultCallback callback = new TestResultCallback(); result.setResultCallback(callback); assertThat(callback.getBuffer().getStatus().getStatusCode()).isEqualTo(Status.SUCCESS); }
@Test public void setResultCallback_shouldReturnFailureStatus() { doAnswer(new Answer() { public Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); Callback callback = (Callback) args[3]; callback.onFailure(null, null); return null; } }).when(pelias).suggest(anyString(), anyDouble(), anyDouble(), any(Callback.class)); TestResultCallback callback = new TestResultCallback(); result.setResultCallback(callback); assertThat(callback.getBuffer().getStatus().getStatusCode()).isEqualTo(Status.INTERNAL_ERROR); }
@Test(expected = RuntimeException.class) public void setResultCallbackTimeout_shouldThrowException() { result.setResultCallback(null, 1, TimeUnit.SECONDS); }