From 2380b4de1359cd9db1e65405b10aa5360da75b80 Mon Sep 17 00:00:00 2001 From: Tobrun Date: Tue, 3 May 2016 08:19:31 +0200 Subject: [android] bring back ScreenshotActivityTest --- .../testapp/espresso/ScreenshotActivityTest.java | 124 +++++++++++++++++++++ .../testapp/espresso/utils/ScreenshotUtil.java | 20 ++-- 2 files changed, 137 insertions(+), 7 deletions(-) create mode 100644 platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/espresso/ScreenshotActivityTest.java (limited to 'platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com') diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/espresso/ScreenshotActivityTest.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/espresso/ScreenshotActivityTest.java new file mode 100644 index 0000000000..60649ad4ae --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/espresso/ScreenshotActivityTest.java @@ -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 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; + } + } +} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/espresso/utils/ScreenshotUtil.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/espresso/utils/ScreenshotUtil.java index bbb0c40ad2..fdb06e52d8 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/espresso/utils/ScreenshotUtil.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/espresso/utils/ScreenshotUtil.java @@ -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 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; } -- cgit v1.2.1