diff options
author | Tobrun <tobrun@mapbox.com> | 2016-05-03 08:19:31 +0200 |
---|---|---|
committer | Brad Leege <bleege@gmail.com> | 2016-05-03 16:31:16 -0500 |
commit | 2380b4de1359cd9db1e65405b10aa5360da75b80 (patch) | |
tree | 98a062483ce5e6a41f0db69de71a23afdcc04651 /platform/android | |
parent | c82b02c508bb4eb1d6ee25320cf83995a807e603 (diff) | |
download | qtlocation-mapboxgl-2380b4de1359cd9db1e65405b10aa5360da75b80.tar.gz |
[android] bring back ScreenshotActivityTest
Diffstat (limited to 'platform/android')
4 files changed, 151 insertions, 19 deletions
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<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; + } + } +} 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<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; } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml index 1aa6cfeb0b..bf70808f7a 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml @@ -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" diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java index 4b1305daed..6f0f0f3c78 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java @@ -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()); } } |