Skip to content

Commit

Permalink
[annotation-plugin] - add marker plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Sep 4, 2018
1 parent 4970332 commit 0ecff07
Show file tree
Hide file tree
Showing 19 changed files with 2,442 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ dependencies {
implementation project(':plugin-places')
implementation project(':plugin-offline')
implementation project(':plugin-localization')
implementation project(':plugin-annotation')
}

apply from: "${rootDir}/gradle/checkstyle.gradle"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.mapbox.mapboxsdk.plugins.annotation;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.IdlingRegistry;
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.plugins.utils.OnMapReadyIdlingResource;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestName;
import timber.log.Timber;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;

public abstract class BaseActivityTest {

@Rule
public ActivityTestRule<Activity> rule = new ActivityTestRule<>(getActivityClass());

@Rule
public TestName testName = new TestName();

protected MapboxMap mapboxMap;
protected OnMapReadyIdlingResource idlingResource;

@Before
public void beforeTest() {
try {
Timber.e("@Before %s: register idle resource", testName.getMethodName());
idlingResource = new OnMapReadyIdlingResource(rule.getActivity());
IdlingRegistry.getInstance().register(idlingResource);
checkViewIsDisplayed(android.R.id.content);
mapboxMap = idlingResource.getMapboxMap();
} catch (IdlingResourceTimeoutException idlingResourceTimeoutException) {
Timber.e("Idling resource timed out. Couldn't not validate if map is ready.");
throw new RuntimeException("Could not start test for " + getActivityClass().getSimpleName() + ".\n"
+ "The ViewHierarchy doesn't contain a view with resource id = android.R.id.content or \n"
+ "the Activity doesn't contain an instance variable with a name equal to mapboxMap.\n"
+ "You can resolve this issue by adding the requirements above or\n add "
+ getActivityClass().getSimpleName() + " to the platform/android/scripts/exclude-activity-gen.json to blacklist"
+ " the Activity from being generated.\n");
}
}

protected void validateTestSetup() {
Assert.assertTrue("Device is not connected to the Internet.", isConnected(rule.getActivity()));
checkViewIsDisplayed(android.R.id.content);
Assert.assertNotNull(mapboxMap);
}

protected MapboxMap getMapboxMap() {
return mapboxMap;
}

protected abstract Class getActivityClass();

protected void checkViewIsDisplayed(int id) {
onView(withId(id)).check(matches(isDisplayed()));
}

protected void waitAction() {
waitAction(500);
}

protected void waitAction(long waitTime) {
onView(withId(android.R.id.content)).perform(new WaitAction(waitTime));
}

static boolean isConnected(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

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

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

@After
public void afterTest() {
Timber.e("@After test: unregister idle resource");
IdlingRegistry.getInstance().unregister(idlingResource);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.mapbox.mapboxsdk.plugins.annotation;

import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.view.View;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import org.hamcrest.Matcher;

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;

public class MapboxMapAction implements ViewAction {

private OnInvokeActionListener invokeViewAction;
private MapboxMap mapboxMap;

public MapboxMapAction(OnInvokeActionListener invokeViewAction, MapboxMap mapboxMap) {
this.invokeViewAction = invokeViewAction;
this.mapboxMap = mapboxMap;
}

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

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

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

public static void invoke(MapboxMap mapboxMap, OnInvokeActionListener invokeViewAction) {
onView(withId(android.R.id.content)).perform(new MapboxMapAction(invokeViewAction, mapboxMap));
}

public interface OnInvokeActionListener {
void onInvokeAction(UiController uiController, MapboxMap mapboxMap);
}
}


Loading

0 comments on commit 0ecff07

Please sign in to comment.