@@ -0,0 +1,44 @@
|
||||
package com.mapbox.mapboxsdk.testapp;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
import com.mapbox.mapboxsdk.testapp.utils.ScreenshotUtil;
|
||||
|
||||
import static android.support.test.espresso.Espresso.onView;
|
||||
import static android.support.test.espresso.assertion.ViewAssertions.matches;
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.withId;
|
||||
|
||||
/**
|
||||
* Base Espresso class for all tests, helps working with ActivityInstrumentationTestCase2
|
||||
*/
|
||||
public class BaseTest {
|
||||
|
||||
protected final static String HOME_BUTTON_STRING = "Navigate up";
|
||||
|
||||
/*
|
||||
* Shortcuts for common UI tests
|
||||
*/
|
||||
|
||||
protected void checkViewIsDisplayed(int id) {
|
||||
onView(withId(id))
|
||||
.check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
/*
|
||||
* Screenshots logic
|
||||
*/
|
||||
|
||||
protected void takeNamedScreenshot(final Activity activity, final String name) {
|
||||
|
||||
// Screenshots need to be taken on the UI thread
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ScreenshotUtil.take(activity, name);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.mapbox.mapboxsdk.testapp;
|
||||
|
||||
import android.support.test.rule.ActivityTestRule;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import android.test.suitebuilder.annotation.LargeTest;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
/**
|
||||
* Tests on BulkMarkerActivity
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@LargeTest
|
||||
public class BulkMarkerActivityTest extends BaseTest {
|
||||
|
||||
@Rule
|
||||
public ActivityTestRule<BulkMarkerActivity> mActivityRule = new ActivityTestRule<>(
|
||||
BulkMarkerActivity.class);
|
||||
|
||||
private BulkMarkerActivity mActivity = null;
|
||||
|
||||
@Before
|
||||
public void setActivity() {
|
||||
mActivity = mActivityRule.getActivity();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSanity() {
|
||||
checkViewIsDisplayed(R.id.mapView);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.mapbox.mapboxsdk.testapp;
|
||||
|
||||
import android.support.test.rule.ActivityTestRule;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import android.test.suitebuilder.annotation.LargeTest;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
/**
|
||||
* Tests on InfoWindowActivity
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@LargeTest
|
||||
public class InfoWindowActivityTest extends BaseTest {
|
||||
|
||||
@Rule
|
||||
public ActivityTestRule<InfoWindowActivity> mActivityRule = new ActivityTestRule<>(
|
||||
InfoWindowActivity.class);
|
||||
|
||||
private InfoWindowActivity mActivity = null;
|
||||
|
||||
@Before
|
||||
public void setActivity() {
|
||||
mActivity = mActivityRule.getActivity();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSanity() {
|
||||
checkViewIsDisplayed(R.id.mapView);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.mapbox.mapboxsdk.testapp;
|
||||
|
||||
import android.support.test.rule.ActivityTestRule;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import android.test.suitebuilder.annotation.LargeTest;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
/**
|
||||
* Tests on InfoWindowAdapterActivity.
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@LargeTest
|
||||
public class InfoWindowAdapterActivityTest extends BaseTest {
|
||||
|
||||
@Rule
|
||||
public ActivityTestRule<InfoWindowAdapterActivity> mActivityRule = new ActivityTestRule<>(
|
||||
InfoWindowAdapterActivity.class);
|
||||
|
||||
private InfoWindowAdapterActivity mActivity = null;
|
||||
|
||||
@Before
|
||||
public void setActivity() {
|
||||
mActivity = mActivityRule.getActivity();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSanity() {
|
||||
checkViewIsDisplayed(R.id.mapView);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.mapbox.mapboxsdk.testapp;
|
||||
|
||||
import android.support.test.rule.ActivityTestRule;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import android.test.suitebuilder.annotation.LargeTest;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static android.support.test.espresso.Espresso.onView;
|
||||
import static android.support.test.espresso.action.ViewActions.click;
|
||||
import static android.support.test.espresso.action.ViewActions.doubleClick;
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.withId;
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.withText;
|
||||
|
||||
/**
|
||||
* Tests on MainActivity with screenshots
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@LargeTest
|
||||
public class MainActivityScreenTest extends BaseTest {
|
||||
|
||||
private final static String HOME_BUTTON_STRING = "Navigate up";
|
||||
|
||||
@Rule
|
||||
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
|
||||
MainActivity.class);
|
||||
|
||||
private MainActivity mActivity = null;
|
||||
|
||||
@Before
|
||||
public void setActivity() {
|
||||
mActivity = mActivityRule.getActivity();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSanity() {
|
||||
checkViewIsDisplayed(R.id.mainMapView);
|
||||
}
|
||||
|
||||
/*
|
||||
* Take a screenshot of Mapbox Streets to monitor #1649
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void testMapboxStreetsBlackAndWhite() {
|
||||
// Click home and switch to Mapbox streets
|
||||
onView(withContentDescription(HOME_BUTTON_STRING))
|
||||
.perform(click());
|
||||
onView(withText(R.string.styleMapboxStreets))
|
||||
.perform(click());
|
||||
|
||||
// Zoom in
|
||||
onView(withId(R.id.mainMapView))
|
||||
.perform(doubleClick());
|
||||
onView(withId(R.id.mainMapView))
|
||||
.perform(doubleClick());
|
||||
|
||||
// Standard screenshot
|
||||
takeNamedScreenshot(mActivity, "testMapboxStreetsBlackAndWhite");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,289 @@
|
||||
package com.mapbox.mapboxsdk.testapp;
|
||||
|
||||
import android.support.test.rule.ActivityTestRule;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import android.test.suitebuilder.annotation.LargeTest;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static android.support.test.espresso.Espresso.onView;
|
||||
import static android.support.test.espresso.action.ViewActions.click;
|
||||
import static android.support.test.espresso.action.ViewActions.doubleClick;
|
||||
import static android.support.test.espresso.action.ViewActions.longClick;
|
||||
import static android.support.test.espresso.action.ViewActions.swipeDown;
|
||||
import static android.support.test.espresso.action.ViewActions.swipeLeft;
|
||||
import static android.support.test.espresso.action.ViewActions.swipeRight;
|
||||
import static android.support.test.espresso.action.ViewActions.swipeUp;
|
||||
import static android.support.test.espresso.assertion.ViewAssertions.matches;
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.withId;
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.withText;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
/**
|
||||
* Tests on MainActivity
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@LargeTest
|
||||
public class MainActivityTest extends BaseTest {
|
||||
|
||||
@Rule
|
||||
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
|
||||
MainActivity.class);
|
||||
|
||||
private MainActivity mActivity = null;
|
||||
|
||||
@Before
|
||||
public void setActivity() {
|
||||
mActivity = mActivityRule.getActivity();
|
||||
}
|
||||
|
||||
/*
|
||||
* Note that we need to keep the `test` prefix if we want to be able to run these
|
||||
* tests on AWS Device Farm, annotations are not enough:
|
||||
* https://github.com/awslabs/aws-device-farm-sample-app-for-android/issues/5#issuecomment-138258444
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void testSanity() {
|
||||
checkViewIsDisplayed(R.id.mainMapView);
|
||||
}
|
||||
|
||||
/*
|
||||
* Check UI ids are visible
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void testDrawerLayoutIsDisplayed() {
|
||||
onView(withId(R.id.drawer_layout))
|
||||
.check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToolbarIsDisplayed() {
|
||||
onView(withId(R.id.toolbar))
|
||||
.check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContentFrameIsDisplayed() {
|
||||
onView(withId(R.id.content_frame))
|
||||
.check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testViewFpsIsDisplayed() {
|
||||
// By default, R.id.view_fps is not displayed
|
||||
onView(withId(R.id.view_fps))
|
||||
.check(matches(not(isDisplayed())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocationFABIsDisplayed() {
|
||||
onView(withId(R.id.locationFAB))
|
||||
.check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNavViewIsNotDisplayed() {
|
||||
// By default, nav_view not displayed
|
||||
onView(withId(R.id.nav_view))
|
||||
.check(matches(not(isDisplayed())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNavViewIsDisplayed() {
|
||||
// However, it's displayed when we click the home icon
|
||||
onView(withContentDescription(HOME_BUTTON_STRING))
|
||||
.perform(click());
|
||||
onView(withId(R.id.nav_view))
|
||||
.check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
/*
|
||||
* Some more tests on the map view (clicks, gestures)
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void testClickMap() {
|
||||
onView(withId(R.id.mainMapView))
|
||||
.perform(click())
|
||||
.check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleClickMap() {
|
||||
onView(withId(R.id.mainMapView))
|
||||
.perform(doubleClick())
|
||||
.check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongClickMap() {
|
||||
onView(withId(R.id.mainMapView))
|
||||
.perform(longClick())
|
||||
.check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSwipeLeftMap() {
|
||||
onView(withId(R.id.mainMapView))
|
||||
.perform(swipeLeft())
|
||||
.check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSwipeRightMap() {
|
||||
onView(withId(R.id.mainMapView))
|
||||
.perform(swipeRight())
|
||||
.check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSwipeDownMap() {
|
||||
onView(withId(R.id.mainMapView))
|
||||
.perform(swipeDown())
|
||||
.check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSwipeUpMap() {
|
||||
onView(withId(R.id.mainMapView))
|
||||
.perform(swipeUp())
|
||||
.check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
/*
|
||||
* Test the main drawer options
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void testNavViewActionDebug() {
|
||||
onView(withContentDescription(HOME_BUTTON_STRING))
|
||||
.perform(click());
|
||||
onView(withText(R.string.action_debug))
|
||||
.perform(click());
|
||||
|
||||
// Clicking the item closes the drawer
|
||||
onView(withId(R.id.nav_view))
|
||||
.check(matches(not(isDisplayed())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNavViewActionPointAnnotations() {
|
||||
onView(withContentDescription(HOME_BUTTON_STRING))
|
||||
.perform(click());
|
||||
onView(withText(R.string.action_point_annotations))
|
||||
.perform(click());
|
||||
|
||||
// Clicking the item closes the drawer
|
||||
onView(withId(R.id.nav_view))
|
||||
.check(matches(not(isDisplayed())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNavViewActionCompass() {
|
||||
onView(withContentDescription(HOME_BUTTON_STRING))
|
||||
.perform(click());
|
||||
onView(withText(R.string.action_compass))
|
||||
.perform(click());
|
||||
|
||||
// Clicking the item closes the drawer
|
||||
onView(withId(R.id.nav_view))
|
||||
.check(matches(not(isDisplayed())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNavViewStyleMapboxStreets() {
|
||||
onView(withContentDescription(HOME_BUTTON_STRING))
|
||||
.perform(click());
|
||||
onView(withText(R.string.styleMapboxStreets))
|
||||
.perform(click());
|
||||
|
||||
// Clicking the item closes the drawer
|
||||
onView(withId(R.id.nav_view))
|
||||
.check(matches(not(isDisplayed())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNavViewStyleEmerald() {
|
||||
onView(withContentDescription(HOME_BUTTON_STRING))
|
||||
.perform(click());
|
||||
onView(withText(R.string.styleEmerald))
|
||||
.perform(click());
|
||||
|
||||
// Clicking the item closes the drawer
|
||||
onView(withId(R.id.nav_view))
|
||||
.check(matches(not(isDisplayed())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNavViewStyleLight() {
|
||||
onView(withContentDescription(HOME_BUTTON_STRING))
|
||||
.perform(click());
|
||||
onView(withText(R.string.styleLight))
|
||||
.perform(click());
|
||||
|
||||
// Clicking the item closes the drawer
|
||||
onView(withId(R.id.nav_view))
|
||||
.check(matches(not(isDisplayed())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNavViewStyleDark() {
|
||||
onView(withContentDescription(HOME_BUTTON_STRING))
|
||||
.perform(click());
|
||||
onView(withText(R.string.styleDark))
|
||||
.perform(click());
|
||||
|
||||
// Clicking the item closes the drawer
|
||||
onView(withId(R.id.nav_view))
|
||||
.check(matches(not(isDisplayed())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNavViewStyleSatellite() {
|
||||
onView(withContentDescription(HOME_BUTTON_STRING))
|
||||
.perform(click());
|
||||
onView(withText(R.string.styleSatellite))
|
||||
.perform(click());
|
||||
|
||||
// Clicking the item closes the drawer
|
||||
onView(withId(R.id.nav_view))
|
||||
.check(matches(not(isDisplayed())));
|
||||
}
|
||||
|
||||
/*
|
||||
* We can also check the map inner elements are visible
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void testMapIconContentDescription() {
|
||||
// Mapbox logo
|
||||
onView(withContentDescription(mActivity.getResources()
|
||||
.getString(R.string.mapboxIconContentDescription)))
|
||||
.check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void testMapCompassContentDescription() {
|
||||
// //FIXME this is currently broken hence compass view is only being showed when rotating the map
|
||||
// // Map compass
|
||||
//// onView(withContentDescription(mActivity.getResources()
|
||||
//// .getString(R.string.compassContentDescription)))
|
||||
//// .check(matches(isDisplayed()));
|
||||
// }
|
||||
|
||||
@Test
|
||||
public void testMapAttributionsIconContentDescription() {
|
||||
// Attribution icon
|
||||
onView(withContentDescription(mActivity.getResources()
|
||||
.getString(R.string.attributionsIconContentDescription)))
|
||||
.check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.mapbox.mapboxsdk.testapp;
|
||||
|
||||
import android.support.test.rule.ActivityTestRule;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import android.test.suitebuilder.annotation.LargeTest;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
/**
|
||||
* Tests on MapFragmentActivity
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@LargeTest
|
||||
public class MapFragmentActivityTest extends BaseTest {
|
||||
|
||||
@Rule
|
||||
public ActivityTestRule<MapFragmentActivity> mActivityRule = new ActivityTestRule<>(
|
||||
MapFragmentActivity.class);
|
||||
|
||||
private MapFragmentActivity mActivity = null;
|
||||
|
||||
@Before
|
||||
public void setActivity() {
|
||||
mActivity = mActivityRule.getActivity();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSanity() {
|
||||
checkViewIsDisplayed(R.id.fragment_container);
|
||||
}
|
||||
|
||||
// FIXME need a way to check if fragment was correctly added to Activity
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.mapbox.mapboxsdk.testapp;
|
||||
|
||||
import android.support.test.rule.ActivityTestRule;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import android.test.suitebuilder.annotation.LargeTest;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
/**
|
||||
* Tests on PressForMarkerActivity
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@LargeTest
|
||||
public class PressForMarkerActivityTest extends BaseTest {
|
||||
|
||||
@Rule
|
||||
public ActivityTestRule<PressForMarkerActivity> mActivityRule = new ActivityTestRule<>(
|
||||
PressForMarkerActivity.class);
|
||||
|
||||
private PressForMarkerActivity mActivity = null;
|
||||
|
||||
@Before
|
||||
public void setActivity() {
|
||||
mActivity = mActivityRule.getActivity();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSanity() {
|
||||
// we are adding mapview on the fly.. just validating if we have a toolbar
|
||||
checkViewIsDisplayed(R.id.secondToolBar);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.mapbox.mapboxsdk.testapp;
|
||||
|
||||
import android.support.test.rule.ActivityTestRule;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import android.test.suitebuilder.annotation.LargeTest;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
/**
|
||||
* Tests on VisibleCoordinateBoundsActivity
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@LargeTest
|
||||
public class VisibleCoordinateBoundsActivityTest extends BaseTest {
|
||||
|
||||
@Rule
|
||||
public ActivityTestRule<VisibleCoordinateBoundsActivity> mActivityRule = new ActivityTestRule<>(
|
||||
VisibleCoordinateBoundsActivity.class);
|
||||
|
||||
private VisibleCoordinateBoundsActivity mActivity = null;
|
||||
|
||||
@Before
|
||||
public void setActivity() {
|
||||
mActivity = mActivityRule.getActivity();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSanity() {
|
||||
checkViewIsDisplayed(R.id.mapView);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package com.mapbox.mapboxsdk.testapp.utils;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
import android.view.TextureView;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
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, String testName) {
|
||||
|
||||
// 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);
|
||||
|
||||
// Add the SurfaceView bit (see getAllTextureViews() below)
|
||||
List<TextureView> tilingViews = getAllTextureViews(rootView);
|
||||
if (tilingViews.size() > 0) {
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
for (TextureView TextureView : tilingViews) {
|
||||
Bitmap b = TextureView.getBitmap(TextureView.getWidth(), TextureView.getHeight());
|
||||
int[] location = new int[2];
|
||||
TextureView.getLocationInWindow(location);
|
||||
int[] location2 = new int[2];
|
||||
TextureView.getLocationOnScreen(location2);
|
||||
canvas.drawBitmap(b, location[0], location[1], null);
|
||||
}
|
||||
}
|
||||
|
||||
// Save the bitmap in external storage
|
||||
String uniqueAbsolutePath = getUniqueAbsolutePath(testName);
|
||||
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 (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (outputStream != null) {
|
||||
try {
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* The classic way of taking a screenshot (above) doesn't work with TextureView, this fixes it:
|
||||
* http://stackoverflow.com/questions/19704060/screen-capture-textureview-is-black-using-drawingcache
|
||||
*/
|
||||
|
||||
public static List<TextureView> getAllTextureViews(View view)
|
||||
{
|
||||
List<TextureView> tilingViews = new ArrayList<TextureView>();
|
||||
if (view instanceof TextureView) {
|
||||
tilingViews.add((TextureView)view);
|
||||
} else if(view instanceof ViewGroup) {
|
||||
ViewGroup viewGroup = (ViewGroup)view;
|
||||
for (int i = 0; i < viewGroup.getChildCount(); i++) {
|
||||
tilingViews.addAll(getAllTextureViews(viewGroup.getChildAt(i)));
|
||||
}
|
||||
}
|
||||
|
||||
return tilingViews;
|
||||
}
|
||||
|
||||
/*
|
||||
* Utils
|
||||
*/
|
||||
|
||||
public static boolean isExternalStorageWritable() {
|
||||
// Checks if external storage is available for read and write
|
||||
String state = Environment.getExternalStorageState();
|
||||
return Environment.MEDIA_MOUNTED.equals(state);
|
||||
}
|
||||
|
||||
private static String getUniqueAbsolutePath(String testName) {
|
||||
// A screenshot after every test vs. manual tests
|
||||
String filename = UUID.randomUUID().toString() + DEFAULT_IMAGE_EXTENSION;
|
||||
if (testName != null && !testName.isEmpty()) {
|
||||
filename = testName + DEFAULT_IMAGE_EXTENSION;
|
||||
}
|
||||
|
||||
String externalPath = Environment.getExternalStorageDirectory().toString();
|
||||
String path = externalPath + File.separator + SCREENSHOT_FOLDER + File.separator + filename;
|
||||
Log.d(LOG_TAG, "Screenshot path: " + path);
|
||||
return path;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user