From d8ffb76ff36915125543c80a0c04b9089cbbb370 Mon Sep 17 00:00:00 2001 From: Antonio Zugaldia Date: Tue, 15 Sep 2015 10:33:34 -0400 Subject: [PATCH] Take a screenshot after every test --- .../com/mapbox/mapboxgl/testapp/BaseTest.java | 27 ++++++ .../testapp/utils/ScreenshotUtil.java | 88 +++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 android/java/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxgl/testapp/utils/ScreenshotUtil.java diff --git a/android/java/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxgl/testapp/BaseTest.java b/android/java/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxgl/testapp/BaseTest.java index d9801cf1e..7ec3c3181 100644 --- a/android/java/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxgl/testapp/BaseTest.java +++ b/android/java/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxgl/testapp/BaseTest.java @@ -3,6 +3,8 @@ package com.mapbox.mapboxgl.testapp; import android.support.test.InstrumentationRegistry; import android.test.ActivityInstrumentationTestCase2; +import com.mapbox.mapboxgl.testapp.utils.ScreenshotUtil; + import org.junit.Before; /** @@ -17,6 +19,10 @@ public class BaseTest super(MainActivity.class); } + /* + * Get the activity before running every test + */ + @Before public void setUp() throws Exception { super.setUp(); @@ -24,4 +30,25 @@ public class BaseTest mActivity = getActivity(); } + /* + * Take a screenshot after every test + */ + + @Override + protected void tearDown() throws Exception { + try { + // Screenshots should be taken on the UI thread + runTestOnUiThread(new Runnable() { + @Override + public void run() { + ScreenshotUtil.take(getActivity()); + } + }); + } catch (Throwable throwable) { + throwable.printStackTrace(); + } + + super.tearDown(); + } + } diff --git a/android/java/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxgl/testapp/utils/ScreenshotUtil.java b/android/java/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxgl/testapp/utils/ScreenshotUtil.java new file mode 100644 index 000000000..0e35fad68 --- /dev/null +++ b/android/java/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxgl/testapp/utils/ScreenshotUtil.java @@ -0,0 +1,88 @@ +package com.mapbox.mapboxgl.testapp.utils; + +import android.app.Activity; +import android.graphics.Bitmap; +import android.os.Environment; +import android.util.Log; +import android.view.View; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.UUID; + +/** + * The built-in Fuzz Suite on AWS Device Farm takes screenshots after every test. However, + * this doesn't happen with Espresso unless we manually do it. This class fixes it. + */ +public class ScreenshotUtil { + + private static final String LOG_TAG = ScreenshotUtil.class.getName(); + + // Where to store the files. This path is required by AWS Device Farm: + // http://docs.aws.amazon.com/devicefarm/latest/developerguide/test-types-android-instrumentation.html#test-types-android-instrumentation-screenshots + private static final String SCREENSHOT_FOLDER = "test-screenshots"; + + // Image type and quality + private static final String DEFAULT_IMAGE_EXTENSION = ".png"; + private static final Bitmap.CompressFormat DEFAULT_IMAGE_FORMAT = Bitmap.CompressFormat.PNG; + private static final int DEFAULT_IMAGE_QUALITY = 100; + + public static void take(Activity activity) { + + // Check if storage is available + if (!isExternalStorageWritable()) { + Log.d(LOG_TAG, "External storage is not available."); + return; + } + + // 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. + View rootView = activity.getWindow().getDecorView().getRootView(); + rootView.setDrawingCacheEnabled(true); + Bitmap bitmap = Bitmap.createBitmap(rootView.getDrawingCache()); + rootView.setDrawingCacheEnabled(false); + + // Save the bitmap in external storage + String uniqueAbsolutePath = getUniqueAbsolutePath(); + File outputFile = new File(uniqueAbsolutePath); + OutputStream outputStream = null; + try { + outputStream = new FileOutputStream(outputFile); + bitmap.compress(DEFAULT_IMAGE_FORMAT, DEFAULT_IMAGE_QUALITY, outputStream); + outputStream.flush(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (outputStream != null) { + try { + outputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + /* Checks if external storage is available for read and write */ + public static boolean isExternalStorageWritable() { + String state = Environment.getExternalStorageState(); + if (Environment.MEDIA_MOUNTED.equals(state)) { + return true; + } + return false; + } + + private static String getUniqueAbsolutePath() { + String externalPath = Environment.getExternalStorageDirectory().toString(); + String filename = UUID.randomUUID().toString() + DEFAULT_IMAGE_EXTENSION; + String path = externalPath + File.separator + SCREENSHOT_FOLDER + File.separator + filename; + Log.d(LOG_TAG, "Screenshot path: " + path); + return path; + } + +}