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

Commit

Permalink
Render test using the node render test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed May 31, 2018
1 parent 7dc19cd commit 1da8f0c
Show file tree
Hide file tree
Showing 23 changed files with 604 additions and 179 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,11 @@ android-ndk-stack-$1: platform/android/gradle/configuration.gradle
.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
# delete old test results
rm -rf platform/android/build/render-test/mapbox/
# copy test definitions to test app assets folder, clear old ones first
rm -rf platform/android/MapboxGLAndroidSDKTestApp/src/main/assets/integration
cp -r mapbox-gl-js/test/integration/ platform/android/MapboxGLAndroidSDKTestApp/src/main/assets
# 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
Expand Down
4 changes: 4 additions & 0 deletions platform/android/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/acti

# Generated list files from code generation
/scripts/generate-style-code.list

# Input files for running render tests
MapboxGLAndroidSDKTestApp/src/main/assets/integration/

Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,16 @@

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import com.mapbox.android.telemetry.TelemetryUtils;
import com.mapbox.mapboxsdk.BuildConfig;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.constants.MapboxConstants;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.NoRouteToHostException;
import java.net.ProtocolException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.concurrent.locks.ReentrantLock;

import javax.net.ssl.SSLException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Dispatcher;
Expand All @@ -30,8 +20,21 @@
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.BufferedSource;
import okio.Okio;
import timber.log.Timber;

import javax.net.ssl.SSLException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.net.NoRouteToHostException;
import java.net.ProtocolException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.util.concurrent.locks.ReentrantLock;

import static android.util.Log.DEBUG;
import static android.util.Log.ERROR;
import static android.util.Log.INFO;
Expand All @@ -57,43 +60,12 @@ class HTTPRequest implements Callback {
private HTTPRequest(long nativePtr, String resourceUrl, String etag, String modified) {
this.nativePtr = nativePtr;

try {
HttpUrl httpUrl = HttpUrl.parse(resourceUrl);
if (httpUrl == null) {
log(Log.ERROR, String.format("[HTTP] Unable to parse resourceUrl %s", resourceUrl));
}

final String host = httpUrl.host().toLowerCase(MapboxConstants.MAPBOX_LOCALE);
// Don't try a request to remote server if we aren't connected
if (!Mapbox.isConnected() && !host.equals("127.0.0.1") && !host.equals("localhost")) {
throw new NoRouteToHostException("No Internet connection available.");
}

if (host.equals("mapbox.com") || host.endsWith(".mapbox.com") || host.equals("mapbox.cn")
|| host.endsWith(".mapbox.cn")) {
if (httpUrl.querySize() == 0) {
resourceUrl = resourceUrl + "?";
} else {
resourceUrl = resourceUrl + "&";
}
resourceUrl = resourceUrl + "events=true";
}

Request.Builder builder = new Request.Builder()
.url(resourceUrl)
.tag(resourceUrl.toLowerCase(MapboxConstants.MAPBOX_LOCALE))
.addHeader("User-Agent", getUserAgent());
if (etag.length() > 0) {
builder = builder.addHeader("If-None-Match", etag);
} else if (modified.length() > 0) {
builder = builder.addHeader("If-Modified-Since", modified);
}
Request request = builder.build();
call = client.newCall(request);
call.enqueue(this);
} catch (Exception exception) {
handleFailure(call, exception);
if (resourceUrl.startsWith("local://")) {
// used by render test to serve files from assets
executeLocalRequest(resourceUrl);
return;
}
executeRequest(resourceUrl, etag, modified);
}

public void cancel() {
Expand Down Expand Up @@ -178,6 +150,57 @@ private static Dispatcher getDispatcher() {
return dispatcher;
}

private void executeRequest(String resourceUrl, String etag, String modified){
try {
HttpUrl httpUrl = HttpUrl.parse(resourceUrl);
if (httpUrl == null) {
log(Log.ERROR, String.format("[HTTP] Unable to parse resourceUrl %s", resourceUrl));
}

final String host = httpUrl.host().toLowerCase(MapboxConstants.MAPBOX_LOCALE);
// Don't try a request to remote server if we aren't connected
if (!Mapbox.isConnected() && !host.equals("127.0.0.1") && !host.equals("localhost")) {
throw new NoRouteToHostException("No Internet connection available.");
}

if (host.equals("mapbox.com") || host.endsWith(".mapbox.com") || host.equals("mapbox.cn")
|| host.endsWith(".mapbox.cn")) {
if (httpUrl.querySize() == 0) {
resourceUrl = resourceUrl + "?";
} else {
resourceUrl = resourceUrl + "&";
}
resourceUrl = resourceUrl + "events=true";
}

Request.Builder builder = new Request.Builder()
.url(resourceUrl)
.tag(resourceUrl.toLowerCase(MapboxConstants.MAPBOX_LOCALE))
.addHeader("User-Agent", getUserAgent());
if (etag.length() > 0) {
builder = builder.addHeader("If-None-Match", etag);
} else if (modified.length() > 0) {
builder = builder.addHeader("If-Modified-Since", modified);
}
Request request = builder.build();
call = client.newCall(request);
call.enqueue(this);
} catch (Exception exception) {
handleFailure(call, exception);
}
}

private void executeLocalRequest(String resourceUrl){
new LocalRequestTask(new LocalRequestTask.OnLocalRequestResponse() {
@Override
public void onResponse(byte[] bytes) {
if(bytes!=null) {
nativeOnResponse(200, null, null, null, null, null, null, bytes);
}
}
}).execute(resourceUrl);
}

private void handleFailure(Call call, Exception e) {
String errorMessage = e.getMessage() != null ? e.getMessage() : "Error processing the request";
int type = getFailureType(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.mapbox.mapboxsdk.http;

import android.content.res.AssetManager;
import android.os.AsyncTask;
import com.mapbox.mapboxsdk.Mapbox;
import timber.log.Timber;

import java.io.IOException;
import java.io.InputStream;

class LocalRequestTask extends AsyncTask<String, Void, byte[]> {

private OnLocalRequestResponse requestResponse;

LocalRequestTask(OnLocalRequestResponse requestResponse) {
this.requestResponse = requestResponse;
}

@Override
protected byte[] doInBackground(String... strings) {
try {
return loadFile(Mapbox.getApplicationContext().getAssets(),
"integration/" + strings[0]
.substring(8)
.replaceAll("%20", " ")
.replaceAll("%2c", ","));
} catch (IOException exception) {
Timber.e(exception);
}
return null;
}

@Override
protected void onPostExecute(byte[] bytes) {
super.onPostExecute(bytes);
if (bytes != null && requestResponse != null) {
requestResponse.onResponse(bytes);
}
}

private static byte[] loadFile(AssetManager assets, String path) throws IOException {
InputStream input = assets.open(path);
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
return buffer;
}

public interface OnLocalRequestResponse {
void onResponse(byte[] bytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class RenderTest {

@Before
public void beforeTest() {
IdlingPolicies.setMasterPolicyTimeout(30, TimeUnit.MINUTES);
grantWriteRuntimePermission();
setupIdlingResource();
}
Expand All @@ -58,7 +59,7 @@ private void grantWriteRuntimePermission() {
private void setupIdlingResource() {
try {
Timber.e("@Before test: register idle resource");
IdlingPolicies.setIdlingResourceTimeout(2, TimeUnit.MINUTES);
IdlingPolicies.setIdlingResourceTimeout(30, TimeUnit.MINUTES);
Espresso.registerIdlingResources(idlingResource = new SnapshotterIdlingResource(rule.getActivity()));
} catch (IdlingResourceTimeoutException idlingResourceTimeoutException) {
throw new RuntimeException("Idling out!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@
android:screenOrientation="portrait" />
<activity
android:name=".activity.render.RenderTestActivity"
android:label="@string/activity_heatmaplayer"
android:screenOrientation="portrait"/>
<!-- Configuration Settings -->
<meta-data
Expand Down
Loading

0 comments on commit 1da8f0c

Please sign in to comment.