summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/MapboxMapTest.java
blob: b4bc118129ac68c93e79f58e3c02bd8e64416d53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
package com.mapbox.mapboxsdk.maps;

import android.graphics.Color;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.view.View;

import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.annotations.Polygon;
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
import com.mapbox.mapboxsdk.annotations.Polyline;
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.exceptions.InvalidMarkerPositionException;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest;
import com.mapbox.mapboxsdk.testapp.activity.espresso.EspressoTestActivity;
import com.mapbox.mapboxsdk.testapp.utils.TestConstants;
import com.mapbox.mapboxsdk.testapp.utils.ViewUtils;

import org.hamcrest.Matcher;
import org.junit.Ignore;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import timber.log.Timber;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static com.mapbox.mapboxsdk.testapp.utils.TestConstants.LAT_LNG_DELTA;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * This test is responsible for testing the public API.
 * <p>
 * Methods executed on MapboxMap are called from a ViewAction to ensure correct synchronisation
 * with the application UI-thread.
 * </p>
 */
public class MapboxMapTest extends BaseActivityTest {

  @Override
  protected Class getActivityClass() {
    return EspressoTestActivity.class;
  }

  @Test
  public void testSanity() {
    validateTestSetup();
    assertNotNull("mapboxMap should not be null", mapboxMap);
  }

  //
  // Style wide transition options
  //

  @Test
  public void testTransitionDuration() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      long transitionDuration = 600;
      mapboxMap.setTransitionDuration(transitionDuration);
      assertEquals(
        "TransitionDuration should match", transitionDuration, mapboxMap.getTransitionDuration(), 0
      );
    }));
  }

  @Test
  public void testTransitionDelay() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      long transitionDelay = 50;
      mapboxMap.setTransitionDelay(transitionDelay);
      assertEquals(
        "TransitionDelay should match", transitionDelay, mapboxMap.getTransitionDelay(), 0
      );
    }));
  }

  //
  // Camera tests
  //
  @Test
  public void testCameraPositionOnFinish() {
    ViewUtils.checkViewIsDisplayed(R.id.mapView);
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {

      final LatLng latLng = new LatLng(30.0, 30.0);
      mapboxMap.moveCamera(CameraUpdateFactory.newLatLng(latLng), new MapboxMap.CancelableCallback() {
        @Override
        public void onCancel() {
        }

        @Override
        public void onFinish() {
          LatLng cameraPositionLatLng = mapboxMap.getCameraPosition().target;
          Timber.d(cameraPositionLatLng.toString());
          assertEquals(cameraPositionLatLng.getLatitude(), latLng.getLatitude(), LAT_LNG_DELTA);
          assertEquals(cameraPositionLatLng.getLongitude(), latLng.getLongitude(), LAT_LNG_DELTA);
        }
      });
    }));
  }

  @Test
  public void testCameraForLatLngBounds() {
    ViewUtils.checkViewIsDisplayed(R.id.mapView);
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      // set
      mapboxMap.setLatLngBoundsForCameraTarget(
        new LatLngBounds.Builder().include(new LatLng()).include(new LatLng(1, 1)).build());
      // reset
      mapboxMap.setLatLngBoundsForCameraTarget(null);
    }));
  }

  //
  // MinZoomLevel
  //

  @Test
  public void testMinZoom() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      mapboxMap.setMinZoomPreference(10);
      assertEquals("MinZoom should match", 10, mapboxMap.getMinZoomLevel(), 10);
    }));
  }

  @Test
  public void testMaxZoom() {
    validateTestSetup();
    final double zoom = 10;
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      mapboxMap.setMaxZoomPreference(zoom);
      assertEquals("MaxZoom should match", zoom, mapboxMap.getMaxZoomLevel(), 10);
    }));
  }

  @Test
  @Ignore
  public void testInitialZoomLevels() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      assertEquals("MaxZoom should match", MapboxConstants.MAXIMUM_ZOOM, mapboxMap.getMaxZoomLevel(),
        TestConstants.ZOOM_DELTA);
      assertEquals("MinZoom should match", MapboxConstants.MINIMUM_ZOOM, mapboxMap.getMinZoomLevel(),
        TestConstants.ZOOM_DELTA);
    }));
  }

  //
  // TrackingSettings
  //

  @Test
  public void testTrackingSettings() {
    validateTestSetup();
    assertNotNull("TrackingSettings should not be null", mapboxMap.getTrackingSettings());
  }

  //
  // InfoWindow
  //

  @Test
  public void testConcurrentInfoWindowEnabled() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      mapboxMap.setAllowConcurrentMultipleOpenInfoWindows(true);
      assertTrue("ConcurrentWindows should be true", mapboxMap.isAllowConcurrentMultipleOpenInfoWindows());
    }));
  }

  @Test
  public void testConcurrentInfoWindowDisabled() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      mapboxMap.setAllowConcurrentMultipleOpenInfoWindows(false);
      assertFalse("ConcurrentWindows should be false", mapboxMap.isAllowConcurrentMultipleOpenInfoWindows());
    }));
  }

  @Test
  public void testInfoWindowAdapter() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      MapboxMap.InfoWindowAdapter infoWindowAdapter = marker -> null;
      mapboxMap.setInfoWindowAdapter(infoWindowAdapter);
      assertEquals("InfoWindowAdpter should be the same", infoWindowAdapter, mapboxMap.getInfoWindowAdapter());
    }));
  }

  //
  // Location
  //

  @Test
  @Ignore /* disabled due to enabling permissions during test #7177 */
  public void testMyLocationEnabled() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      mapboxMap.setMyLocationEnabled(true);
      assertTrue("MyLocationEnabled should be true", mapboxMap.isMyLocationEnabled());
    }));
  }

  @Test
  @Ignore /* can't create handler inside thread that not called Looper.prepare() */
  public void testMyLocationDisabled() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      mapboxMap.setMyLocationEnabled(false);
      assertFalse("MyLocationEnabled should be false", mapboxMap.isMyLocationEnabled());
    }));
  }

  //
  // setters/getters interfaces
  //

  @Test
  public void testFpsListener() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      MapboxMap.OnFpsChangedListener fpsChangedListener = fps -> {

      };
      mapboxMap.setOnFpsChangedListener(fpsChangedListener);
      assertEquals("FpsListener should match", fpsChangedListener, mapboxMap.getOnFpsChangedListener());
    }));
  }

  @Test
  public void testInfoWindowClickListener() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      MapboxMap.OnInfoWindowClickListener clickListener = marker -> false;
      mapboxMap.setOnInfoWindowClickListener(clickListener);
      assertEquals(
        "InfoWidowClickListener should match", clickListener, mapboxMap.getOnInfoWindowClickListener()
      );
    }));
  }

  @Test
  public void testInfoWindowCloseListener() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      MapboxMap.OnInfoWindowCloseListener listener = marker -> {

      };
      mapboxMap.setOnInfoWindowCloseListener(listener);
      assertEquals("InfoWindowCloseListener should match", listener, mapboxMap.getOnInfoWindowCloseListener());
    }));
  }

  @Test
  public void testInfoWindowLongClickListener() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      MapboxMap.OnInfoWindowLongClickListener listener = marker -> {

      };
      mapboxMap.setOnInfoWindowLongClickListener(listener);
      assertEquals("InfoWindowLongClickListener should match", listener,
        mapboxMap.getOnInfoWindowLongClickListener());
    }));
  }

  //
  // Annotations
  //

  @Test
  public void testAddMarker() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      MarkerOptions markerOptions = new MarkerOptions().position(new LatLng());
      Marker marker = mapboxMap.addMarker(markerOptions);
      assertTrue("Marker should be contained", mapboxMap.getMarkers().contains(marker));
    }));
  }

  @Test(expected = InvalidMarkerPositionException.class)
  public void testAddMarkerInvalidPosition() {
    new MarkerOptions().getMarker();
  }

  @Test
  public void testAddMarkers() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      List<BaseMarkerOptions> markerList = new ArrayList<>();
      MarkerOptions markerOptions1 = new MarkerOptions().position(new LatLng()).title("a");
      MarkerOptions markerOptions2 = new MarkerOptions().position(new LatLng()).title("b");
      markerList.add(markerOptions1);
      markerList.add(markerOptions2);
      List<Marker> markers = mapboxMap.addMarkers(markerList);
      assertEquals("Markers size should be 2", 2, mapboxMap.getMarkers().size());
      assertTrue(mapboxMap.getMarkers().contains(markers.get(0)));
      assertTrue(mapboxMap.getMarkers().contains(markers.get(1)));
    }));
  }

  @Test
  public void testAddMarkersEmpty() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      List<BaseMarkerOptions> markerList = new ArrayList<>();
      mapboxMap.addMarkers(markerList);
      assertEquals("Markers size should be 0", 0, mapboxMap.getMarkers().size());
    }));
  }

  @Test
  public void testAddMarkersSingleMarker() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      List<BaseMarkerOptions> markerList = new ArrayList<>();
      MarkerOptions markerOptions = new MarkerOptions().title("a").position(new LatLng());
      markerList.add(markerOptions);
      List<Marker> markers = mapboxMap.addMarkers(markerList);
      assertEquals("Markers size should be 1", 1, mapboxMap.getMarkers().size());
      assertTrue(mapboxMap.getMarkers().contains(markers.get(0)));
    }));
  }

  @Test
  public void testAddPolygon() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      PolygonOptions polygonOptions = new PolygonOptions().add(new LatLng());
      Polygon polygon = mapboxMap.addPolygon(polygonOptions);
      assertTrue("Polygon should be contained", mapboxMap.getPolygons().contains(polygon));
    }));
  }

  @Test
  public void testAddEmptyPolygon() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      PolygonOptions polygonOptions = new PolygonOptions();
      Polygon polygon = mapboxMap.addPolygon(polygonOptions);
      assertTrue("Polygon should be ignored", !mapboxMap.getPolygons().contains(polygon));
    }));
  }

  @Test
  public void testAddPolygons() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      List<PolygonOptions> polygonList = new ArrayList<>();
      PolygonOptions polygonOptions1 = new PolygonOptions().fillColor(Color.BLACK).add(new LatLng());
      PolygonOptions polygonOptions2 = new PolygonOptions().fillColor(Color.WHITE).add(new LatLng());
      PolygonOptions polygonOptions3 = new PolygonOptions();
      polygonList.add(polygonOptions1);
      polygonList.add(polygonOptions2);
      polygonList.add(polygonOptions3);
      mapboxMap.addPolygons(polygonList);
      assertEquals("Polygons size should be 2", 2, mapboxMap.getPolygons().size());
      assertTrue(mapboxMap.getPolygons().contains(polygonOptions1.getPolygon()));
      assertTrue(mapboxMap.getPolygons().contains(polygonOptions2.getPolygon()));
      assertTrue("Polygon should be ignored", !mapboxMap.getPolygons().contains(polygonOptions3.getPolygon()));
    }));
  }

  @Test
  public void addPolygonsEmpty() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      mapboxMap.addPolygons(new ArrayList<PolygonOptions>());
      assertEquals("Polygons size should be 0", 0, mapboxMap.getPolygons().size());
    }));
  }

  @Test
  public void addPolygonsSingle() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      List<PolygonOptions> polygonList = new ArrayList<>();
      PolygonOptions polygonOptions = new PolygonOptions().fillColor(Color.BLACK).add(new LatLng());
      polygonList.add(polygonOptions);
      mapboxMap.addPolygons(polygonList);
      assertEquals("Polygons size should be 1", 1, mapboxMap.getPolygons().size());
      assertTrue(mapboxMap.getPolygons().contains(polygonOptions.getPolygon()));
    }));
  }

  @Test
  public void testAddPolyline() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      PolylineOptions polylineOptions = new PolylineOptions().add(new LatLng());
      Polyline polyline = mapboxMap.addPolyline(polylineOptions);
      assertTrue("Polyline should be contained", mapboxMap.getPolylines().contains(polyline));
    }));
  }

  @Test
  public void testAddEmptyPolyline() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      PolylineOptions polylineOptions = new PolylineOptions();
      Polyline polyline = mapboxMap.addPolyline(polylineOptions);
      assertTrue("Polyline should be ignored", !mapboxMap.getPolylines().contains(polyline));
    }));
  }

  @Test
  public void testAddPolylines() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      List<PolylineOptions> polylineList = new ArrayList<>();
      PolylineOptions polygonOptions1 = new PolylineOptions().color(Color.BLACK).add(new LatLng());
      PolylineOptions polygonOptions2 = new PolylineOptions().color(Color.WHITE).add(new LatLng());
      PolylineOptions polygonOptions3 = new PolylineOptions();
      polylineList.add(polygonOptions1);
      polylineList.add(polygonOptions2);
      polylineList.add(polygonOptions3);
      mapboxMap.addPolylines(polylineList);
      assertEquals("Polygons size should be 2", 2, mapboxMap.getPolylines().size());
      assertTrue(mapboxMap.getPolylines().contains(polygonOptions1.getPolyline()));
      assertTrue(mapboxMap.getPolylines().contains(polygonOptions2.getPolyline()));
      assertTrue(
        "Polyline should be ignored", !mapboxMap.getPolylines().contains(polygonOptions3.getPolyline())
      );
    }));
  }

  @Test
  public void testAddPolylinesEmpty() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      mapboxMap.addPolylines(new ArrayList<PolylineOptions>());
      assertEquals("Polygons size should be 0", 0, mapboxMap.getPolylines().size());
    }));
  }

  @Test
  public void testAddPolylinesSingle() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      List<PolylineOptions> polylineList = new ArrayList<>();
      PolylineOptions polygonOptions = new PolylineOptions().color(Color.BLACK).add(new LatLng());
      polylineList.add(polygonOptions);
      mapboxMap.addPolylines(polylineList);
      assertEquals("Polygons size should be 1", 1, mapboxMap.getPolylines().size());
      assertTrue(mapboxMap.getPolylines().contains(polygonOptions.getPolyline()));
    }));
  }

  @Test
  public void testRemoveMarker() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      MarkerOptions markerOptions = new MarkerOptions().position(new LatLng());
      Marker marker = mapboxMap.addMarker(markerOptions);
      mapboxMap.removeMarker(marker);
      assertTrue("Markers should be empty", mapboxMap.getMarkers().isEmpty());
    }));
  }

  @Test
  public void testRemovePolygon() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      PolygonOptions polygonOptions = new PolygonOptions();
      Polygon polygon = mapboxMap.addPolygon(polygonOptions);
      mapboxMap.removePolygon(polygon);
      assertTrue("Polygons should be empty", mapboxMap.getPolylines().isEmpty());
    }));
  }

  @Test
  public void testRemovePolyline() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      PolylineOptions polylineOptions = new PolylineOptions();
      Polyline polyline = mapboxMap.addPolyline(polylineOptions);
      mapboxMap.removePolyline(polyline);
      assertTrue("Polylines should be empty", mapboxMap.getPolylines().isEmpty());
    }));
  }

  @Test
  public void testRemoveAnnotation() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      MarkerOptions markerOptions = new MarkerOptions().position(new LatLng());
      Marker marker = mapboxMap.addMarker(markerOptions);
      mapboxMap.removeAnnotation(marker);
      assertTrue("Annotations should be empty", mapboxMap.getAnnotations().isEmpty());
    }));
  }

  @Test
  public void testRemoveAnnotationById() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      MarkerOptions markerOptions = new MarkerOptions().position(new LatLng());
      mapboxMap.addMarker(markerOptions);
      // id will always be 0 in unit tests
      mapboxMap.removeAnnotation(0);
      assertTrue("Annotations should be empty", mapboxMap.getAnnotations().isEmpty());
    }));
  }

  @Test
  public void testRemoveAnnotations() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      List<BaseMarkerOptions> markerList = new ArrayList<>();
      MarkerOptions markerOptions1 = new MarkerOptions().title("a").position(new LatLng());
      MarkerOptions markerOptions2 = new MarkerOptions().title("b").position(new LatLng());
      markerList.add(markerOptions1);
      markerList.add(markerOptions2);
      mapboxMap.addMarkers(markerList);
      mapboxMap.removeAnnotations();
      assertTrue("Annotations should be empty", mapboxMap.getAnnotations().isEmpty());
    }));
  }

  @Test
  public void testClear() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      List<BaseMarkerOptions> markerList = new ArrayList<>();
      MarkerOptions markerOptions1 = new MarkerOptions().title("a").position(new LatLng());
      MarkerOptions markerOptions2 = new MarkerOptions().title("b").position(new LatLng());
      markerList.add(markerOptions1);
      markerList.add(markerOptions2);
      mapboxMap.addMarkers(markerList);
      mapboxMap.clear();
      assertTrue("Annotations should be empty", mapboxMap.getAnnotations().isEmpty());
    }));
  }

  @Test
  public void testRemoveAnnotationsByList() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      List<BaseMarkerOptions> markerList = new ArrayList<>();
      MarkerOptions markerOptions1 = new MarkerOptions().title("a").position(new LatLng());
      MarkerOptions markerOptions2 = new MarkerOptions().title("b").position(new LatLng());
      markerList.add(markerOptions1);
      markerList.add(markerOptions2);
      List<Marker> markers = mapboxMap.addMarkers(markerList);
      Marker marker = mapboxMap.addMarker(new MarkerOptions().position(new LatLng()).title("c"));
      mapboxMap.removeAnnotations(markers);
      assertTrue("Annotations should not be empty", mapboxMap.getAnnotations().size() == 1);
      assertTrue("Marker should be contained", mapboxMap.getAnnotations().contains(marker));
    }));
  }

  @Test
  public void testGetAnnotationById() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      MarkerOptions markerOptions = new MarkerOptions().position(new LatLng());
      Marker initialMarker = mapboxMap.addMarker(markerOptions);
      Marker retrievedMarker = (Marker) mapboxMap.getAnnotation(0);
      assertEquals("Markers should match", initialMarker, retrievedMarker);
    }));
  }

  @Test
  public void testGetAnnotations() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(
      new MapboxMapAction((uiController, view) ->
        assertNotNull("Annotations should be non null", mapboxMap.getAnnotations()))
    );
  }

  @Test
  public void testGetMarkers() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(
      new MapboxMapAction((uiController, view) ->
        assertNotNull("Markers should be non null", mapboxMap.getMarkers()))
    );
  }

  @Test
  public void testGetPolygons() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) ->
      assertNotNull("Polygons should be non null", mapboxMap.getPolygons()))
    );
  }

  @Test
  public void testGetPolylines() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) ->
      assertNotNull("Polylines should be non null", mapboxMap.getPolylines()))
    );
  }

  @Test
  public void testGetSelectedMarkers() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) ->
      assertNotNull("Selected markers should be non null", mapboxMap.getSelectedMarkers()))
    );
  }

  @Test
  public void testSelectMarker() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      MarkerOptions markerOptions = new MarkerOptions().position(new LatLng());
      Marker marker = mapboxMap.addMarker(markerOptions);
      mapboxMap.selectMarker(marker);
      assertTrue("Marker should be contained", mapboxMap.getSelectedMarkers().contains(marker));
    }));
  }

  @Test
  public void testDeselectMarker() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      MarkerOptions markerOptions = new MarkerOptions().position(new LatLng());
      Marker marker = mapboxMap.addMarker(markerOptions);
      mapboxMap.selectMarker(marker);
      mapboxMap.deselectMarker(marker);
      assertTrue("Selected markers should be empty", mapboxMap.getSelectedMarkers().isEmpty());
    }));
  }

  @Test
  public void testDeselectMarkers() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      MarkerOptions markerOptions = new MarkerOptions().position(new LatLng());
      Marker marker1 = mapboxMap.addMarker(markerOptions);
      Marker marker2 = mapboxMap.addMarker(markerOptions);
      mapboxMap.selectMarker(marker1);
      mapboxMap.selectMarker(marker2);
      mapboxMap.deselectMarkers();
      assertTrue("Selected markers should be empty", mapboxMap.getSelectedMarkers().isEmpty());
    }));
  }

  // Tile pre-fetching

  @Test
  public void testTilePrefetch() {
    validateTestSetup();
    onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
      mapboxMap.setPrefetchesTiles(true);
      assertTrue(mapboxMap.getPrefetchesTiles());
      mapboxMap.setPrefetchesTiles(false);
      assertFalse(mapboxMap.getPrefetchesTiles());
    }));
  }

  private class MapboxMapAction implements ViewAction {

    private InvokeViewAction invokeViewAction;

    MapboxMapAction(InvokeViewAction invokeViewAction) {
      this.invokeViewAction = invokeViewAction;
    }

    @Override
    public Matcher<View> getConstraints() {
      return isDisplayed();
    }

    @Override
    public String getDescription() {
      return getClass().getSimpleName();
    }

    @Override
    public void perform(UiController uiController, View view) {
      invokeViewAction.onViewAction(uiController, view);
    }
  }

  interface InvokeViewAction {
    void onViewAction(UiController uiController, View view);
  }
}