|
25 | 25 | import android.widget.TextView; |
26 | 26 |
|
27 | 27 | import java.io.IOException; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.HashSet; |
28 | 30 |
|
29 | 31 | import okhttp3.Request; |
30 | 32 | import static org.fest.assertions.api.Assertions.assertThat; |
@@ -176,6 +178,20 @@ public class PeliasSearchViewTest { |
176 | 178 | assertThat(callback.error).isNull(); |
177 | 179 | } |
178 | 180 |
|
| 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 | + |
179 | 195 | @Test public void onItemClick_shouldSetQuery() throws Exception { |
180 | 196 | final AutoCompleteListView listView = new AutoCompleteListView(ACTIVITY); |
181 | 197 | listView.setAdapter(new TestAdapter()); |
@@ -348,6 +364,90 @@ private class TestCall implements Call<Result> { |
348 | 364 | } |
349 | 365 | } |
350 | 366 |
|
| 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 | + |
351 | 451 | private class TestCallback implements Callback<Result> { |
352 | 452 | private Result result; |
353 | 453 | private Throwable error; |
|
0 commit comments