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

Commit

Permalink
[android] - Render tests with PixelMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed May 22, 2018
1 parent 162ee6f commit 36607d1
Show file tree
Hide file tree
Showing 20 changed files with 513 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,17 @@ run-android-ui-test-$1-%: platform/android/gradle/configuration.gradle
android-ndk-stack-$1: platform/android/gradle/configuration.gradle
adb logcat | ndk-stack -sym platform/android/MapboxGLAndroidSDK/build/intermediates/cmake/debug/obj/$2/

# Run render tests with pixelmatch
.PHONY: run-android-render-test-$1
run-android-render-test-$1: $(BUILD_DEPS) platform/android/gradle/configuration.gradle
-adb uninstall com.mapbox.mapboxsdk.testapp 2> /dev/null
# run RenderTest.java to generate static map images
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=$2 :MapboxGLAndroidSDKTestApp:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class="com.mapbox.mapboxsdk.testapp.render.RenderTest"
# pull generated images from the device
adb pull "`adb shell 'printenv EXTERNAL_STORAGE' | tr -d '\r'`/mapbox" platform/android/build/render-test
# copy expected result and run pixelmatch
python platform/android/scripts/run-render-test.py

endef

# Explodes the arguments into individual variables
Expand Down Expand Up @@ -682,6 +693,11 @@ android-javadoc: platform/android/gradle/configuration.gradle
.PHONY: android-ndk-stack
android-ndk-stack: android-ndk-stack-arm-v7

# Update render tests
,PHONY: update-android-render-test
update-android-render-test:
python platform/android/scripts/update-render-expected.py

# Open Android Studio if machine is macos
ifeq ($(HOST_PLATFORM), macos)
.PHONY: aproj
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.mapbox.mapboxsdk.testapp.render;

import android.os.Build;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.IdlingPolicies;
import android.support.test.espresso.IdlingResourceTimeoutException;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.mapbox.mapboxsdk.testapp.activity.render.RenderTestActivity;
import com.mapbox.mapboxsdk.testapp.utils.SnapshotterIdlingResource;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.TimeUnit;

import timber.log.Timber;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.InstrumentationRegistry.getTargetContext;
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;

/**
* Instrumentation render tests
*/
@RunWith(AndroidJUnit4.class)
public class RenderTest {

private SnapshotterIdlingResource idlingResource;

@Rule
public ActivityTestRule<RenderTestActivity> rule = new ActivityTestRule<>(RenderTestActivity.class);

@Before
public void beforeTest() {
grantWriteRuntimePermission();
setupIdlingResource();
}

private void grantWriteRuntimePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getInstrumentation().getUiAutomation().executeShellCommand(
"pm grant " + getTargetContext().getPackageName()
+ " android.permission.WRITE_EXTERNAL_STORAGE");
getInstrumentation().getUiAutomation().executeShellCommand(
"pm grant " + getTargetContext().getPackageName()
+ " android.permission.READ_EXTERNAL_STORAGE");
}
}

private void setupIdlingResource() {
try {
Timber.e("@Before test: register idle resource");
IdlingPolicies.setIdlingResourceTimeout(2, TimeUnit.MINUTES);
Espresso.registerIdlingResources(idlingResource = new SnapshotterIdlingResource(rule.getActivity()));
} catch (IdlingResourceTimeoutException idlingResourceTimeoutException) {
throw new RuntimeException("Idling out!");
}
}

@Test
public void testRender() {
onView(withId(android.R.id.content)).check(matches(isDisplayed()));
}

@After
public void afterTest() {
Timber.e("@After test: unregister idle resource");
Espresso.unregisterIdlingResources(idlingResource);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.mapbox.mapboxsdk.testapp.utils;

import android.support.test.espresso.IdlingResource;

import com.mapbox.mapboxsdk.testapp.activity.render.RenderTestActivity;

public class SnapshotterIdlingResource implements IdlingResource, RenderTestActivity.OnSnapshotReadyListener {

private IdlingResource.ResourceCallback resourceCallback;
private boolean isSnapshotReady;

public SnapshotterIdlingResource(RenderTestActivity activity) {
activity.setOnSnapshotReadyListener(this);
}

@Override
public String getName() {
return "SnapshotterIdlingResource";
}

@Override
public boolean isIdleNow() {
return isSnapshotReady;
}

@Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
this.resourceCallback = resourceCallback;
}

@Override
public void onSnapshotReady() {
isSnapshotReady = true;
if (resourceCallback != null) {
resourceCallback.onTransitionToIdle();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,9 @@
<activity
android:name=".activity.style.FillExtrusionStyleTestActivity"
android:screenOrientation="portrait" />
<activity
android:name=".activity.render.RenderTestActivity"
android:screenOrientation="portrait"/>
<!-- Configuration Settings -->
<meta-data
android:name="com.mapbox.TestEventsServer"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
[
{
"name": "World",
"width": 512,
"height": 512,
"center": [
0,
0
],
"zoom": 0,
"tilt": 0,
"bearing": 0,
"styleUrl": "mapbox://styles/mapbox/streets-v10"
},
{
"name": "Brussels",
"width": 512,
"height": 512,
"center": [
50.846728,
4.352429
],
"zoom": 12,
"tilt": 0,
"bearing": 0,
"styleUrl": "mapbox://styles/mapbox/streets-v10"
},
{
"name": "San Francisco",
"width": 512,
"height": 512,
"center": [
37.772544,
-122.426622
],
"zoom": 9,
"tilt": 60,
"bearing": 0,
"styleUrl": "mapbox://styles/mapbox/streets-v10"
},
{
"name": "Washington DC",
"width": 512,
"height": 512,
"center": [
38.897717,
-77.036035
],
"zoom": 18,
"tilt": 20,
"bearing": 180,
"styleUrl": "mapbox://styles/mapbox/streets-v10"
},
{
"name": "Ayacucho",
"width": 512,
"height": 512,
"center": [
-13.164131,
-74.220973
],
"zoom": 12,
"tilt": 30,
"bearing": 270,
"styleUrl": "mapbox://styles/mapbox/streets-v10"
},
{
"name": "Bangalore",
"width": 512,
"height": 512,
"center": [
12.970020,
77.590421
],
"zoom": 14,
"tilt": 0,
"bearing": 0,
"styleUrl": "mapbox://styles/mapbox/streets-v10"
},
{
"name": "Berlin",
"width": 512,
"height": 512,
"center": [
52.519716,
13.402068
],
"zoom": 12,
"tilt": 0,
"bearing": 0,
"styleUrl": "mapbox://styles/mapbox/streets-v10"
},
{
"name": "Lisbon",
"width": 512,
"height": 512,
"center": [
38.722062,
-9.138746
],
"zoom": 15,
"tilt": 45,
"bearing": 90,
"styleUrl": "mapbox://styles/mapbox/outdoors-v10"
},
{
"name": "Helsinki",
"width": 512,
"height": 512,
"center": [
60.171283,
24.933533
],
"zoom": 12,
"tilt": 0,
"bearing": 0,
"styleUrl": "mapbox://styles/mapbox/streets-v10"
},
{
"name": "Madrid",
"width": 512,
"height": 512,
"center": [
40.417025,
-3.705287
],
"zoom": 16,
"tilt": 35,
"bearing": 0,
"styleUrl": "mapbox://styles/mapbox/light-v9"
},
{
"name": "London",
"width": 512,
"height": 512,
"center": [
51.508049,
-0.129756
],
"zoom": 12,
"tilt": 0,
"bearing": 0,
"styleUrl": "mapbox://styles/mapbox/dark-v9"
}
]
Loading

0 comments on commit 36607d1

Please sign in to comment.