Skip to content

Commit bc93f41

Browse files
sarahcodes100ecgreb
authored andcommitted
Handle null values in callback (#61)
* Gradle version 2.2.3 * Handle null values in suggest callback
1 parent fc3816a commit bc93f41

2 files changed

Lines changed: 132 additions & 22 deletions

File tree

lib/src/main/java/com/mapzen/pelias/widget/PeliasSearchView.java

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,33 @@ public void run() {
8585
private boolean cacheSearchResults = true;
8686
private boolean autoKeyboardShow = true;
8787

88+
private Callback<Result> suggestCallback = new Callback<Result>() {
89+
@Override public void onResponse(Call<Result> call, Response<Result> response) {
90+
final ArrayList<AutoCompleteItem> items = new ArrayList<>();
91+
if (response != null && response.body() != null) {
92+
final List<Feature> features = response.body().getFeatures();
93+
if (features != null) {
94+
for (Feature feature : features) {
95+
items.add(new AutoCompleteItem(SimpleFeature.fromFeature(feature)));
96+
}
97+
}
98+
}
99+
100+
if (autoCompleteListView == null) {
101+
return;
102+
}
103+
104+
final AutoCompleteAdapter adapter = (AutoCompleteAdapter) autoCompleteListView.getAdapter();
105+
adapter.clear();
106+
adapter.addAll(items);
107+
adapter.notifyDataSetChanged();
108+
}
109+
110+
@Override public void onFailure(Call<Result> call, Throwable t) {
111+
Log.e(TAG, "Unable to fetch autocomplete results", t);
112+
}
113+
};
114+
88115
/**
89116
* Constructs a new search view given a context.
90117
*/
@@ -235,28 +262,7 @@ private void fetchAutoCompleteSuggestions(String text) {
235262
return;
236263
}
237264

238-
pelias.suggest(text, new Callback<Result>() {
239-
@Override public void onResponse(Call<Result> call, Response<Result> response) {
240-
final ArrayList<AutoCompleteItem> items = new ArrayList<>();
241-
final List<Feature> features = response.body().getFeatures();
242-
for (Feature feature : features) {
243-
items.add(new AutoCompleteItem(SimpleFeature.fromFeature(feature)));
244-
}
245-
246-
if (autoCompleteListView == null) {
247-
return;
248-
}
249-
250-
final AutoCompleteAdapter adapter = (AutoCompleteAdapter) autoCompleteListView.getAdapter();
251-
adapter.clear();
252-
adapter.addAll(items);
253-
adapter.notifyDataSetChanged();
254-
}
255-
256-
@Override public void onFailure(Call<Result> call, Throwable t) {
257-
Log.e(TAG, "Unable to fetch autocomplete results", t);
258-
}
259-
});
265+
pelias.suggest(text, suggestCallback);
260266
}
261267

262268
/**
@@ -511,4 +517,8 @@ private void updateSavedSearch() {
511517
}
512518
}, 150);
513519
}
520+
521+
Callback<Result> getSuggestCallback() {
522+
return suggestCallback;
523+
}
514524
}

lib/src/test/java/com/mapzen/pelias/widget/PeliasSearchViewTest.java

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import android.widget.TextView;
2626

2727
import java.io.IOException;
28+
import java.util.Collection;
29+
import java.util.HashSet;
2830

2931
import okhttp3.Request;
3032
import static org.fest.assertions.api.Assertions.assertThat;
@@ -176,6 +178,20 @@ public class PeliasSearchViewTest {
176178
assertThat(callback.error).isNull();
177179
}
178180

181+
@Test public void onQueryTextSubmit_shouldHandleNullBody() throws Exception {
182+
final AutoCompleteListView listView = new AutoCompleteListView(ACTIVITY);
183+
final TestEmptyAdapter adapter = new TestEmptyAdapter();
184+
listView.setAdapter(adapter);
185+
peliasSearchView.setAutoCompleteListView(listView);
186+
final Pelias pelias = new TestEmptyPelias();
187+
final Callback<Result> callback = peliasSearchView.getSuggestCallback();
188+
pelias.setLocationProvider(new PeliasTest.TestLocationProvider());
189+
peliasSearchView.setPelias(pelias);
190+
peliasSearchView.setCallback(callback);
191+
peliasSearchView.setQuery("query", true);
192+
assertThat(adapter.getCount()).isEqualTo(0);
193+
}
194+
179195
@Test public void onItemClick_shouldSetQuery() throws Exception {
180196
final AutoCompleteListView listView = new AutoCompleteListView(ACTIVITY);
181197
listView.setAdapter(new TestAdapter());
@@ -348,6 +364,90 @@ private class TestCall implements Call<Result> {
348364
}
349365
}
350366

367+
private class TestEmptyPelias extends Pelias {
368+
protected TestEmptyPelias() {
369+
super(new TestEmptyPeliasService());
370+
}
371+
}
372+
373+
private class TestEmptyPeliasService implements PeliasService {
374+
@Override public Call<Result> getSuggest(@Query("text") String query,
375+
@Query("focus.point.lat") double lat, @Query("focus.point.lon") double lon) {
376+
return new TestEmptyCall();
377+
}
378+
379+
@Override public Call<Result> getSearch(@Query("text") String query,
380+
@Query("focus.viewport.min_lon") double minLon,
381+
@Query("focus.viewport.min_lat") double minLat,
382+
@Query("focus.viewport.max_lon") double maxLon,
383+
@Query("focus.viewport.max_lat") double maxLat) {
384+
return new TestEmptyCall();
385+
}
386+
387+
@Override public Call<Result> getSearch(@Query("text") String query,
388+
@Query("focus.point.lat") double lat, @Query("focus.point.lon") double lon) {
389+
return new TestEmptyCall();
390+
}
391+
392+
@Override public Call<Result> getReverse(@Query("point.lat") double lat,
393+
@Query("point.lon") double lon) {
394+
return new TestEmptyCall();
395+
}
396+
397+
@Override public Call<Result> getPlace(@Query("ids") String ids) {
398+
return new TestEmptyCall();
399+
}
400+
}
401+
402+
private class TestEmptyCall implements Call<Result> {
403+
@Override public Response<Result> execute() throws IOException {
404+
return Response.success(null);
405+
}
406+
407+
@Override public void enqueue(Callback<Result> callback) {
408+
callback.onResponse(null, null);
409+
}
410+
411+
@Override public boolean isExecuted() {
412+
return false;
413+
}
414+
415+
@Override public void cancel() {
416+
}
417+
418+
@Override public boolean isCanceled() {
419+
return false;
420+
}
421+
422+
@Override public Call<Result> clone() {
423+
return null;
424+
}
425+
426+
@Override public Request request() {
427+
return null;
428+
}
429+
}
430+
431+
private class TestEmptyAdapter extends AutoCompleteAdapter {
432+
Collection<? extends AutoCompleteItem> items = new HashSet<>();
433+
434+
public TestEmptyAdapter() {
435+
super(ACTIVITY, android.R.layout.simple_list_item_1);
436+
}
437+
438+
@Override public void addAll(Collection<? extends AutoCompleteItem> collection) {
439+
this.items = collection;
440+
}
441+
442+
@Override public AutoCompleteItem getItem(int position) {
443+
return new AutoCompleteItem("query");
444+
}
445+
446+
@Override public int getCount() {
447+
return items.size();
448+
}
449+
}
450+
351451
private class TestCallback implements Callback<Result> {
352452
private Result result;
353453
private Throwable error;

0 commit comments

Comments
 (0)