Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] - fix icon test to use main thread, simplify executing on m…
Browse files Browse the repository at this point in the history
…ain thread for tests
  • Loading branch information
tobrun committed May 25, 2018
1 parent 27876a8 commit d90289e
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mapbox.mapboxsdk.annotations;

import android.graphics.Bitmap;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ public void testTilePrefetch() {
}));
}

private class MapboxMapAction implements ViewAction {
public class MapboxMapAction implements ViewAction {

private InvokeViewAction invokeViewAction;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MapboxMapAction implements ViewAction {
private OnInvokeActionListener invokeViewAction;
private MapboxMap mapboxMap;

private MapboxMapAction(OnInvokeActionListener invokeViewAction, MapboxMap mapboxMap) {
public MapboxMapAction(OnInvokeActionListener invokeViewAction, MapboxMap mapboxMap) {
this.invokeViewAction = invokeViewAction;
this.mapboxMap = mapboxMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
import android.net.NetworkInfo;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.IdlingResourceTimeoutException;
import android.support.test.espresso.ViewInteraction;
import android.support.test.rule.ActivityTestRule;

import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction;
import com.mapbox.mapboxsdk.testapp.action.WaitAction;
import com.mapbox.mapboxsdk.testapp.utils.OnMapReadyIdlingResource;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;

import timber.log.Timber;

import static android.support.test.espresso.Espresso.onView;
Expand Down Expand Up @@ -83,6 +81,14 @@ static boolean isConnected(Context context) {
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

protected ViewInteraction onMapView() {
return onView(withId(R.id.mapView));
}

protected MapboxMapAction getMapboxMapAction(MapboxMapAction.OnInvokeActionListener onInvokeActionListener) {
return new MapboxMapAction(onInvokeActionListener, mapboxMap);
}

@After
public void afterTest() {
Timber.e("@After test: unregister idle resource");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@

import android.app.Activity;
import android.support.v4.content.res.ResourcesCompat;

import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.IconFactory;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.IconManagerResolver;
import com.mapbox.mapboxsdk.maps.MapboxMap;
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.IconUtils;

import org.junit.Before;
import org.junit.Test;

Expand All @@ -39,104 +36,114 @@ public void beforeTest() {
}

@Test
public void testEmpty() throws Exception {
public void testEmpty() {
assertTrue(iconMap.isEmpty());
}

@Test
public void testAddSameIconMarker() throws Exception {
Icon defaultMarker = IconFactory.getInstance(rule.getActivity()).defaultMarker();
getMapboxMap().addMarker(new MarkerOptions().position(new LatLng()));
getMapboxMap().addMarker(new MarkerOptions().position(new LatLng(1, 1)));
assertEquals(1, iconMap.size());
assertEquals(2, iconMap.get(defaultMarker), 0);
public void testAddSameIconMarker() {
validateTestSetup();
onMapView().perform(getMapboxMapAction((uiController, mapboxMap) -> {
Icon defaultMarker = IconFactory.getInstance(rule.getActivity()).defaultMarker();
mapboxMap.addMarker(new MarkerOptions().position(new LatLng()));
mapboxMap.addMarker(new MarkerOptions().position(new LatLng(1, 1)));
assertEquals(1, iconMap.size());
assertEquals(2, iconMap.get(defaultMarker), 0);
}));
}

@Test
public void testAddDifferentIconMarker() throws Exception {
Icon icon = IconFactory.getInstance(rule.getActivity()).fromResource(R.drawable.mapbox_logo_icon);
getMapboxMap().addMarker(new MarkerOptions().icon(icon).position(new LatLng()));
getMapboxMap().addMarker(new MarkerOptions().position(new LatLng(1, 1)));
assertEquals(iconMap.size(), 2);
assertTrue(iconMap.containsKey(icon));
assertTrue(iconMap.get(icon) == 1);
public void testAddDifferentIconMarker() {
validateTestSetup();
onMapView().perform(getMapboxMapAction((uiController, mapboxMap) -> {
Icon icon = IconFactory.getInstance(rule.getActivity()).fromResource(R.drawable.mapbox_logo_icon);
getMapboxMap().addMarker(new MarkerOptions().icon(icon).position(new LatLng()));
getMapboxMap().addMarker(new MarkerOptions().position(new LatLng(1, 1)));
assertEquals(iconMap.size(), 2);
assertTrue(iconMap.containsKey(icon));
assertTrue(iconMap.get(icon) == 1);
}));
}

@Test
public void testAddRemoveIconMarker() throws Exception {
MapboxMap mapboxMap = getMapboxMap();

Icon icon = IconFactory.getInstance(rule.getActivity()).fromResource(R.drawable.mapbox_logo_icon);
Marker marker = mapboxMap.addMarker(new MarkerOptions().icon(icon).position(new LatLng()));
mapboxMap.addMarker(new MarkerOptions().position(new LatLng(1, 1)));
assertEquals(iconMap.size(), 2);
assertTrue(iconMap.containsKey(icon));
assertTrue(iconMap.get(icon) == 1);

mapboxMap.removeMarker(marker);
assertEquals(iconMap.size(), 1);
assertFalse(iconMap.containsKey(icon));
public void testAddRemoveIconMarker() {
validateTestSetup();
onMapView().perform(getMapboxMapAction((uiController, mapboxMap) -> {
Icon icon = IconFactory.getInstance(rule.getActivity()).fromResource(R.drawable.mapbox_logo_icon);
Marker marker = mapboxMap.addMarker(new MarkerOptions().icon(icon).position(new LatLng()));
mapboxMap.addMarker(new MarkerOptions().position(new LatLng(1, 1)));
assertEquals(iconMap.size(), 2);
assertTrue(iconMap.containsKey(icon));
assertTrue(iconMap.get(icon) == 1);

mapboxMap.removeMarker(marker);
assertEquals(iconMap.size(), 1);
assertFalse(iconMap.containsKey(icon));
}));
}

@Test
public void testAddRemoveDefaultMarker() throws Exception {
MapboxMap mapboxMap = getMapboxMap();

Marker marker = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(1, 1)));
assertEquals(iconMap.size(), 1);

mapboxMap.removeMarker(marker);
assertEquals(iconMap.size(), 0);

mapboxMap.addMarker(new MarkerOptions().position(new LatLng()));
assertEquals(iconMap.size(), 1);
public void testAddRemoveDefaultMarker() {
validateTestSetup();
onMapView().perform(getMapboxMapAction((uiController, mapboxMap) -> {
Marker marker = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(1, 1)));
assertEquals(iconMap.size(), 1);

mapboxMap.removeMarker(marker);
assertEquals(iconMap.size(), 0);

mapboxMap.addMarker(new MarkerOptions().position(new LatLng()));
assertEquals(iconMap.size(), 1);
}));
}

@Test
public void testAddRemoveMany() throws Exception {
Activity activity = rule.getActivity();
MapboxMap mapboxMap = getMapboxMap();
IconFactory iconFactory = IconFactory.getInstance(activity);

// add 2 default icon markers
Marker defaultMarkerOne = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(1, 1)));
Marker defaultMarkerTwo = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(2, 1)));

// add 4 unique icon markers
mapboxMap.addMarker(new MarkerOptions()
.icon(iconFactory.fromResource(R.drawable.mapbox_logo_icon))
.position(new LatLng(3, 1))
);
mapboxMap.addMarker(new MarkerOptions()
.icon(iconFactory.fromResource(R.drawable.mapbox_compass_icon))
.position(new LatLng(4, 1))
);
mapboxMap.addMarker(new MarkerOptions()
.icon(IconUtils.drawableToIcon(activity, R.drawable.ic_stars,
ResourcesCompat.getColor(activity.getResources(),
R.color.blueAccent, activity.getTheme())))
.position(new LatLng(5, 1))
);
mapboxMap.addMarker(new MarkerOptions()
.icon(iconFactory.fromResource(R.drawable.ic_android))
.position(new LatLng(6, 1))
);

assertEquals("Amount of icons should match 5", 5, iconMap.size());
assertEquals("Refcounter of default marker should match 2", 2, iconMap.get(iconFactory.defaultMarker()), 0);

mapboxMap.removeMarker(defaultMarkerOne);

assertEquals("Amount of icons should match 5", 5, iconMap.size());
assertEquals("Refcounter of default marker should match 1", 1, iconMap.get(iconFactory.defaultMarker()), 0);

mapboxMap.removeMarker(defaultMarkerTwo);

assertEquals("Amount of icons should match 4", 4, iconMap.size());
assertNull("DefaultMarker shouldn't exist anymore", iconMap.get(iconFactory.defaultMarker()));

mapboxMap.clear();
assertEquals("Amount of icons should match 0", 0, iconMap.size());
public void testAddRemoveMany() {
validateTestSetup();
onMapView().perform(getMapboxMapAction((uiController, mapboxMap) -> {
Activity activity = rule.getActivity();
IconFactory iconFactory = IconFactory.getInstance(activity);

// add 2 default icon markers
Marker defaultMarkerOne = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(1, 1)));
Marker defaultMarkerTwo = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(2, 1)));

// add 4 unique icon markers
mapboxMap.addMarker(new MarkerOptions()
.icon(iconFactory.fromResource(R.drawable.mapbox_logo_icon))
.position(new LatLng(3, 1))
);
mapboxMap.addMarker(new MarkerOptions()
.icon(iconFactory.fromResource(R.drawable.mapbox_compass_icon))
.position(new LatLng(4, 1))
);
mapboxMap.addMarker(new MarkerOptions()
.icon(IconUtils.drawableToIcon(activity, R.drawable.ic_stars,
ResourcesCompat.getColor(activity.getResources(),
R.color.blueAccent, activity.getTheme())))
.position(new LatLng(5, 1))
);
mapboxMap.addMarker(new MarkerOptions()
.icon(iconFactory.fromResource(R.drawable.ic_android))
.position(new LatLng(6, 1))
);

assertEquals("Amount of icons should match 5", 5, iconMap.size());
assertEquals("Refcounter of default marker should match 2", 2, iconMap.get(iconFactory.defaultMarker()), 0);

mapboxMap.removeMarker(defaultMarkerOne);

assertEquals("Amount of icons should match 5", 5, iconMap.size());
assertEquals("Refcounter of default marker should match 1", 1, iconMap.get(iconFactory.defaultMarker()), 0);

mapboxMap.removeMarker(defaultMarkerTwo);

assertEquals("Amount of icons should match 4", 4, iconMap.size());
assertNull("DefaultMarker shouldn't exist anymore", iconMap.get(iconFactory.defaultMarker()));

mapboxMap.clear();
assertEquals("Amount of icons should match 0", 0, iconMap.size());
}));
}

@Override
Expand Down

0 comments on commit d90289e

Please sign in to comment.