[android] bring back ScreenshotActivityTest
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
package com.mapbox.mapboxsdk.testapp.espresso;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.support.test.espresso.Espresso;
|
||||
import android.support.test.espresso.IdlingPolicies;
|
||||
import android.support.test.espresso.IdlingResource;
|
||||
import android.support.test.rule.ActivityTestRule;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import android.test.suitebuilder.annotation.LargeTest;
|
||||
import android.text.format.DateUtils;
|
||||
|
||||
import com.mapbox.mapboxsdk.testapp.R;
|
||||
import com.mapbox.mapboxsdk.testapp.activity.camera.LatLngBoundsActivity;
|
||||
import com.mapbox.mapboxsdk.testapp.espresso.utils.ScreenshotUtil;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static android.support.test.espresso.Espresso.onView;
|
||||
import static android.support.test.espresso.action.ViewActions.click;
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.withId;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@LargeTest
|
||||
public class
|
||||
ScreenshotActivityTest {
|
||||
|
||||
@Rule
|
||||
public ActivityTestRule<LatLngBoundsActivity> mActivityRule = new ActivityTestRule<>(LatLngBoundsActivity.class);
|
||||
|
||||
private Activity mActivity = null;
|
||||
|
||||
@Before
|
||||
public void setActivity() {
|
||||
mActivity = mActivityRule.getActivity();
|
||||
|
||||
// Make sure Espresso does not time out
|
||||
IdlingPolicies.setMasterPolicyTimeout(60, TimeUnit.SECONDS);
|
||||
IdlingPolicies.setIdlingResourceTimeout(26, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScreenshot() {
|
||||
waitFor(DateUtils.SECOND_IN_MILLIS * 5, mActivity);
|
||||
}
|
||||
|
||||
private static void waitFor(long waitingTime, Activity a) {
|
||||
// Start
|
||||
onView(withId(R.id.mapView)).perform(click());
|
||||
|
||||
// Make sure Espresso does not time out
|
||||
IdlingPolicies.setMasterPolicyTimeout(waitingTime * 2, TimeUnit.MILLISECONDS);
|
||||
IdlingPolicies.setIdlingResourceTimeout(waitingTime * 2, TimeUnit.MILLISECONDS);
|
||||
|
||||
// Now we wait
|
||||
IdlingResource idlingResource = new ElapsedTimeIdlingResource(waitingTime);
|
||||
Espresso.registerIdlingResources(idlingResource);
|
||||
|
||||
// Stop and verify
|
||||
onView(withId(R.id.mapView)).perform(click());
|
||||
|
||||
ScreenshotUtil.take(a, "testing");
|
||||
|
||||
// Clean up
|
||||
Espresso.unregisterIdlingResources(idlingResource);
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void takeScreenshot() {
|
||||
// // Start
|
||||
// checkViewIsDisplayed(R.id.mapView);
|
||||
//
|
||||
//
|
||||
// // Now we wait
|
||||
// IdlingResource idlingResource = new ElapsedTimeIdlingResource(4);
|
||||
// Espresso.registerIdlingResources(idlingResource);
|
||||
//
|
||||
// // We take a screenshot
|
||||
// ScreenshotUtil.take(mActivity, "test");
|
||||
//
|
||||
// // Clean up
|
||||
// Espresso.unregisterIdlingResources(idlingResource);
|
||||
// }
|
||||
|
||||
// @Test
|
||||
// public void testSanity() {
|
||||
// checkViewIsDisplayed(R.id.mapView);
|
||||
// }
|
||||
|
||||
public static class ElapsedTimeIdlingResource implements IdlingResource {
|
||||
private final long startTime;
|
||||
private final long waitingTime;
|
||||
private ResourceCallback resourceCallback;
|
||||
|
||||
public ElapsedTimeIdlingResource(long waitingTime) {
|
||||
this.startTime = System.currentTimeMillis();
|
||||
this.waitingTime = waitingTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return ElapsedTimeIdlingResource.class.getName() + ":" + waitingTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIdleNow() {
|
||||
long elapsed = System.currentTimeMillis() - startTime;
|
||||
boolean idle = (elapsed >= waitingTime);
|
||||
if (idle) {
|
||||
resourceCallback.onTransitionToIdle();
|
||||
}
|
||||
return idle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
|
||||
this.resourceCallback = resourceCallback;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,15 +43,17 @@ public class ScreenshotUtil {
|
||||
}
|
||||
|
||||
// Get a bitmap from the activity root view. When the drawing cache is enabled,
|
||||
// the next call to getDrawingCache() will draw the view in a bitmap.
|
||||
// // the next call to getDrawingCache() will draw the view in a bitmap.
|
||||
View rootView = activity.getWindow().getDecorView().getRootView();
|
||||
rootView.setDrawingCacheEnabled(true);
|
||||
Bitmap bitmap = Bitmap.createBitmap(rootView.getDrawingCache());
|
||||
rootView.setDrawingCacheEnabled(false);
|
||||
// rootView.setDrawingCacheEnabled(true);r
|
||||
// rootView.setDrawingCacheEnabled(false);
|
||||
|
||||
Bitmap bitmap = null;
|
||||
|
||||
// Add the SurfaceView bit (see getAllTextureViews() below)
|
||||
List<TextureView> tilingViews = getAllTextureViews(rootView);
|
||||
if (tilingViews.size() > 0) {
|
||||
bitmap = Bitmap.createBitmap(tilingViews.get(0).getHeight(),tilingViews.get(0).getWidth(), Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
for (TextureView TextureView : tilingViews) {
|
||||
Bitmap b = TextureView.getBitmap(TextureView.getWidth(), TextureView.getHeight());
|
||||
@@ -59,7 +61,7 @@ public class ScreenshotUtil {
|
||||
TextureView.getLocationInWindow(location);
|
||||
int[] location2 = new int[2];
|
||||
TextureView.getLocationOnScreen(location2);
|
||||
canvas.drawBitmap(b, location[0], location[1], null);
|
||||
canvas.drawBitmap(b, 0,0, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,7 +73,8 @@ public class ScreenshotUtil {
|
||||
outputStream = new FileOutputStream(outputFile);
|
||||
bitmap.compress(DEFAULT_IMAGE_FORMAT, DEFAULT_IMAGE_QUALITY, outputStream);
|
||||
outputStream.flush();
|
||||
} catch (IOException e) {
|
||||
} catch (Exception e) {
|
||||
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (outputStream != null) {
|
||||
@@ -122,7 +125,10 @@ public class ScreenshotUtil {
|
||||
}
|
||||
|
||||
String externalPath = Environment.getExternalStorageDirectory().toString();
|
||||
String path = externalPath + File.separator + SCREENSHOT_FOLDER + File.separator + filename;
|
||||
String path = externalPath + File.separator + SCREENSHOT_FOLDER;
|
||||
File dir = new File(path);
|
||||
dir.mkdirs();
|
||||
path += File.separator + filename;
|
||||
Log.d(LOG_TAG, "Screenshot path: " + path);
|
||||
return path;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
||||
|
||||
<application
|
||||
android:name=".MapboxApplication"
|
||||
@@ -84,6 +85,7 @@
|
||||
<activity
|
||||
android:name=".activity.camera.LatLngBoundsActivity"
|
||||
android:description="@string/description_visible_bounds"
|
||||
android:screenOrientation="portrait"
|
||||
android:label="@string/activity_visible_coordinate_bounds">
|
||||
<meta-data
|
||||
android:name="@string/category"
|
||||
|
||||
@@ -11,17 +11,17 @@ public class MapboxApplication extends Application {
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
LeakCanary.install(this);
|
||||
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
|
||||
.detectDiskReads()
|
||||
.detectDiskWrites()
|
||||
.detectNetwork() // or .detectAll() for all detectable problems
|
||||
.penaltyLog()
|
||||
.build());
|
||||
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
|
||||
.detectLeakedSqlLiteObjects()
|
||||
.detectLeakedClosableObjects()
|
||||
.penaltyLog()
|
||||
.penaltyDeath()
|
||||
.build());
|
||||
// StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
|
||||
// .detectDiskReads()
|
||||
// .detectDiskWrites()
|
||||
// .detectNetwork() // or .detectAll() for all detectable problems
|
||||
// .penaltyLog()
|
||||
// .build());
|
||||
// StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
|
||||
// .detectLeakedSqlLiteObjects()
|
||||
// .detectLeakedClosableObjects()
|
||||
// .penaltyLog()
|
||||
// .penaltyDeath()
|
||||
// .build());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user