From b2a49993b9436012f7206e3af8ea03a49d1644ad Mon Sep 17 00:00:00 2001 From: Brett Chabot Date: Mon, 29 Apr 2024 18:08:53 +0000 Subject: [PATCH] Update Screenshot sample ot latest versions and API. --- ui/espresso/ScreenshotSample/app/build.gradle | 2 +- .../screenshotsample/ScreenshotJavaTest.java | 34 ++++++--- .../screenshotsample/ScreenshotTest.kt | 73 ++++++++++--------- 3 files changed, 63 insertions(+), 46 deletions(-) diff --git a/ui/espresso/ScreenshotSample/app/build.gradle b/ui/espresso/ScreenshotSample/app/build.gradle index 6df9049e4..5c0f24cfb 100644 --- a/ui/espresso/ScreenshotSample/app/build.gradle +++ b/ui/espresso/ScreenshotSample/app/build.gradle @@ -9,7 +9,7 @@ android { defaultConfig { applicationId "com.example.android.testing.espresso.screenshotsample" - minSdk 18 + minSdk 19 targetSdkVersion 33 versionCode 1 versionName "1.0" diff --git a/ui/espresso/ScreenshotSample/app/src/androidTest/java/com/example/android/testing/espresso/screenshotsample/ScreenshotJavaTest.java b/ui/espresso/ScreenshotSample/app/src/androidTest/java/com/example/android/testing/espresso/screenshotsample/ScreenshotJavaTest.java index 9f779240c..0e154276d 100644 --- a/ui/espresso/ScreenshotSample/app/src/androidTest/java/com/example/android/testing/espresso/screenshotsample/ScreenshotJavaTest.java +++ b/ui/espresso/ScreenshotSample/app/src/androidTest/java/com/example/android/testing/espresso/screenshotsample/ScreenshotJavaTest.java @@ -3,15 +3,13 @@ import static androidx.test.core.app.DeviceCapture.takeScreenshot; import static androidx.test.core.graphics.BitmapStorage.writeToTestStorage; import static androidx.test.espresso.Espresso.onView; +import static androidx.test.espresso.action.ViewActions.captureToBitmap; import static androidx.test.espresso.matcher.ViewMatchers.isRoot; import static androidx.test.espresso.matcher.ViewMatchers.withText; -import static androidx.test.espresso.screenshot.ViewInteractionCapture.captureToBitmap; -import android.view.View; +import android.graphics.Bitmap; -import androidx.concurrent.futures.ResolvableFuture; -import androidx.test.core.app.ActivityScenario; -import androidx.test.core.view.ViewCapture; +import androidx.test.espresso.action.ViewActions; import androidx.test.ext.junit.rules.ActivityScenarioRule; import androidx.test.ext.junit.runners.AndroidJUnit4; @@ -21,8 +19,6 @@ import org.junit.runner.RunWith; import java.io.IOException; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; /** * Equivalent of {@link ScreenshotTest} for java. @@ -42,7 +38,16 @@ public class ScreenshotJavaTest { */ @Test public void saveActivityBitmap() throws IOException { - writeToTestStorage(captureToBitmap(onView(isRoot())), getClass().getSimpleName() + "_" + nameRule.getMethodName()); + onView(isRoot()).perform(captureToBitmap(new ViewActions.BitmapReceiver() { + @Override + public void onBitmapCaptured(Bitmap bitmap) { + try { + writeToTestStorage(bitmap, ScreenshotJavaTest.class.getSimpleName() + "_" + nameRule.getMethodName()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + })); } /** @@ -50,7 +55,18 @@ public void saveActivityBitmap() throws IOException { */ @Test public void saveViewBitmap() throws IOException { - writeToTestStorage(captureToBitmap(onView(withText("Hello World!"))), getClass().getSimpleName() + "_" + nameRule.getMethodName()); + onView(withText("Hello World!")).perform(captureToBitmap(new ViewActions.BitmapReceiver() { + @Override + public void onBitmapCaptured(Bitmap bitmap) { + try { + writeToTestStorage(bitmap, ScreenshotJavaTest.class.getSimpleName() + "_" + nameRule.getMethodName()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + })); + + } /** diff --git a/ui/espresso/ScreenshotSample/app/src/androidTest/java/com/example/android/testing/espresso/screenshotsample/ScreenshotTest.kt b/ui/espresso/ScreenshotSample/app/src/androidTest/java/com/example/android/testing/espresso/screenshotsample/ScreenshotTest.kt index 9af0d2151..f71c7f062 100644 --- a/ui/espresso/ScreenshotSample/app/src/androidTest/java/com/example/android/testing/espresso/screenshotsample/ScreenshotTest.kt +++ b/ui/espresso/ScreenshotSample/app/src/androidTest/java/com/example/android/testing/espresso/screenshotsample/ScreenshotTest.kt @@ -1,11 +1,14 @@ package com.example.android.testing.espresso.screenshotsample +import android.graphics.Bitmap import androidx.test.core.app.takeScreenshot import androidx.test.core.graphics.writeToTestStorage import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.action.ViewActions +import androidx.test.espresso.action.ViewActions.captureToBitmap import androidx.test.espresso.matcher.ViewMatchers.isRoot import androidx.test.espresso.matcher.ViewMatchers.withText -import androidx.test.espresso.screenshot.captureToBitmap + import androidx.test.ext.junit.rules.activityScenarioRule import androidx.test.ext.junit.runners.AndroidJUnit4 import org.junit.Rule @@ -23,43 +26,41 @@ import java.io.IOException @RunWith(AndroidJUnit4::class) class ScreenshotTest { - // a handy JUnit rule that stores the method name, so it can be used to generate unique - // screenshot files per test method - @get:Rule - var nameRule = TestName() + // a handy JUnit rule that stores the method name, so it can be used to generate unique + // screenshot files per test method + @get:Rule + var nameRule = TestName() - @get:Rule - val activityScenarioRule = activityScenarioRule() + @get:Rule + val activityScenarioRule = activityScenarioRule() - /** - * Captures and saves an image of the entire [MainActivity] contents. - */ - @Test - @Throws(IOException::class) - fun saveActivityBitmap() { - onView(isRoot()) - .captureToBitmap() - .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") - } + /** + * Captures and saves an image of the entire [MainActivity] contents. + */ + @Test + @Throws(IOException::class) + fun saveActivityBitmap() { + onView(isRoot()) + .perform(captureToBitmap({ bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") })) + } - /** - * Captures and saves an image of the 'Hello world' view. - */ - @Test - @Throws(IOException::class) - fun saveViewBitmap() { - onView(withText("Hello World!")) - .captureToBitmap() - .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") - } + /** + * Captures and saves an image of the 'Hello world' view. + */ + @Test + @Throws(IOException::class) + fun saveViewBitmap() { + onView(withText("Hello World!")) + .perform(captureToBitmap({ bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") })) + } - /** - * Captures and saves an image of the entire device screen to storage. - */ - @Test - @Throws(IOException::class) - fun saveDeviceScreenBitmap() { - takeScreenshot() - .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") - } + /** + * Captures and saves an image of the entire device screen to storage. + */ + @Test + @Throws(IOException::class) + fun saveDeviceScreenBitmap() { + takeScreenshot() + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") + } }