[android] - derived source attribution (#8630)
This commit is contained in:
@@ -8,9 +8,14 @@ import android.support.test.espresso.UiController;
|
||||
import android.support.test.espresso.ViewAction;
|
||||
import android.support.test.espresso.intent.Intents;
|
||||
import android.support.test.rule.ActivityTestRule;
|
||||
import android.text.Html;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.TextUtils;
|
||||
import android.text.style.URLSpan;
|
||||
import android.view.View;
|
||||
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
import com.mapbox.mapboxsdk.style.sources.Source;
|
||||
import com.mapbox.mapboxsdk.testapp.R;
|
||||
import com.mapbox.mapboxsdk.testapp.activity.espresso.EspressoTestActivity;
|
||||
import com.mapbox.mapboxsdk.testapp.utils.OnMapReadyIdlingResource;
|
||||
@@ -22,6 +27,7 @@ import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import static android.support.test.espresso.Espresso.onData;
|
||||
import static android.support.test.espresso.Espresso.onView;
|
||||
import static android.support.test.espresso.action.ViewActions.click;
|
||||
import static android.support.test.espresso.assertion.ViewAssertions.matches;
|
||||
@@ -32,7 +38,8 @@ import static android.support.test.espresso.intent.matcher.IntentMatchers.hasDat
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.withId;
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.withText;
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
import static org.hamcrest.CoreMatchers.allOf;
|
||||
import static org.hamcrest.CoreMatchers.anything;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
|
||||
public class AttributionTest {
|
||||
@@ -42,23 +49,32 @@ public class AttributionTest {
|
||||
|
||||
private OnMapReadyIdlingResource idlingResource;
|
||||
|
||||
private String[] dialogTexts;
|
||||
private String[] dialogLinks;
|
||||
private MapboxMap mapboxMap;
|
||||
private URLSpan[] urlSpans;
|
||||
|
||||
@Before
|
||||
public void beforeTest() {
|
||||
idlingResource = new OnMapReadyIdlingResource(rule.getActivity());
|
||||
Espresso.registerIdlingResources(idlingResource);
|
||||
Intents.init();
|
||||
dialogTexts = rule.getActivity().getResources().getStringArray(R.array.mapbox_attribution_names);
|
||||
dialogLinks = rule.getActivity().getResources().getStringArray(R.array.mapbox_attribution_links);
|
||||
ViewUtils.checkViewIsDisplayed(R.id.mapView);
|
||||
mapboxMap = rule.getActivity().getMapboxMap();
|
||||
onView(withId(R.id.mapView)).perform(new MapboxMapAction(new InvokeViewAction() {
|
||||
@Override
|
||||
public void onViewAction(UiController uiController, View view) {
|
||||
for (Source source : mapboxMap.getSources()) {
|
||||
String attributionSource = source.getAttribution();
|
||||
if (!TextUtils.isEmpty(attributionSource)) {
|
||||
SpannableStringBuilder htmlBuilder = (SpannableStringBuilder) Html.fromHtml(attributionSource);
|
||||
urlSpans = htmlBuilder.getSpans(0, htmlBuilder.length(), URLSpan.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDisabled() {
|
||||
ViewUtils.checkViewIsDisplayed(R.id.mapView);
|
||||
MapboxMap mapboxMap = rule.getActivity().getMapboxMap();
|
||||
|
||||
// Default
|
||||
onView(withId(R.id.attributionView)).check(matches(isDisplayed()));
|
||||
|
||||
@@ -69,46 +85,61 @@ public class AttributionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapboxLink() {
|
||||
public void testMapboxStreetsMapboxAttributionLink() {
|
||||
ViewUtils.checkViewIsDisplayed(R.id.mapView);
|
||||
if (urlSpans == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// click on View to open dialog
|
||||
onView(withId(R.id.attributionView)).perform(click());
|
||||
onView(withText(R.string.mapbox_attributionsDialogTitle)).check(matches(isDisplayed()));
|
||||
|
||||
// click on link and validate browser opening
|
||||
Matcher<Intent> expectedIntent = allOf(hasAction(Intent.ACTION_VIEW), hasData(Uri.parse(dialogLinks[0])));
|
||||
// test for trigger url intent
|
||||
Matcher<Intent> expectedIntent = allOf(hasAction(Intent.ACTION_VIEW), hasData(Uri.parse(urlSpans[0].getURL())));
|
||||
intending(expectedIntent).respondWith(new Instrumentation.ActivityResult(0, null));
|
||||
onView(withText(dialogTexts[0])).perform(click());
|
||||
|
||||
// click item and test for url
|
||||
onData(anything()).inAdapterView(withId(R.id.select_dialog_listview)).atPosition(0).perform(click());
|
||||
intended(expectedIntent);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenStreetMapLink() {
|
||||
public void testMapboxStreetsOpenStreetMapAttributionLink() {
|
||||
ViewUtils.checkViewIsDisplayed(R.id.mapView);
|
||||
|
||||
if (urlSpans == null) {
|
||||
return;
|
||||
}
|
||||
// click on View to open dialog
|
||||
onView(withId(R.id.attributionView)).perform(click());
|
||||
onView(withText(R.string.mapbox_attributionsDialogTitle)).check(matches(isDisplayed()));
|
||||
|
||||
// click on link and validate browser opening
|
||||
Matcher<Intent> expectedIntent = allOf(hasAction(Intent.ACTION_VIEW), hasData(Uri.parse(dialogLinks[1])));
|
||||
// test for trigger url intent
|
||||
Matcher<Intent> expectedIntent = allOf(hasAction(Intent.ACTION_VIEW), hasData(Uri.parse(urlSpans[1].getURL())));
|
||||
intending(expectedIntent).respondWith(new Instrumentation.ActivityResult(0, null));
|
||||
onView(withText(dialogTexts[1])).perform(click());
|
||||
|
||||
// click item and test for url
|
||||
onData(anything()).inAdapterView(withId(R.id.select_dialog_listview)).atPosition(1).perform(click());
|
||||
intended(expectedIntent);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImproveMapLink() {
|
||||
ViewUtils.checkViewIsDisplayed(R.id.mapView);
|
||||
|
||||
if (urlSpans == null) {
|
||||
return;
|
||||
}
|
||||
// click on View to open dialog
|
||||
onView(withId(R.id.attributionView)).perform(click());
|
||||
onView(withText(R.string.mapbox_attributionsDialogTitle)).check(matches(isDisplayed()));
|
||||
|
||||
// click on Mapbox link and validate browser opening
|
||||
Matcher<Intent> expectedIntent = allOf(hasAction(Intent.ACTION_VIEW), hasData(Uri.parse(dialogLinks[3])));
|
||||
// test for trigger url intent
|
||||
Matcher<Intent> expectedIntent = hasAction(Intent.ACTION_VIEW);
|
||||
intending(expectedIntent).respondWith(new Instrumentation.ActivityResult(0, null));
|
||||
onView(withText(dialogTexts[3])).perform(click());
|
||||
|
||||
// click item and test for url
|
||||
onData(anything()).inAdapterView(withId(R.id.select_dialog_listview)).atPosition(2).perform(click());
|
||||
intended(expectedIntent);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -120,7 +151,7 @@ public class AttributionTest {
|
||||
onView(withText(R.string.mapbox_attributionsDialogTitle)).check(matches(isDisplayed()));
|
||||
|
||||
// click on item to open second dialog
|
||||
onView(withText(dialogTexts[3])).perform(click());
|
||||
onView(withText(R.string.mapbox_telemetrySettings)).perform(click());
|
||||
onView(withText(R.string.mapbox_attributionTelemetryTitle)).check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
@@ -153,4 +184,32 @@ public class AttributionTest {
|
||||
mapboxMap.getUiSettings().setAttributionEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user