Skip to content

Commit a59d128

Browse files
authored
Merge pull request #511 from android/screenshot
Update Screenshot sample ot latest versions and API.
2 parents 18a85b3 + b2a4999 commit a59d128

File tree

3 files changed

+63
-46
lines changed

3 files changed

+63
-46
lines changed

ui/espresso/ScreenshotSample/app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ android {
99

1010
defaultConfig {
1111
applicationId "com.example.android.testing.espresso.screenshotsample"
12-
minSdk 18
12+
minSdk 19
1313
targetSdkVersion 33
1414
versionCode 1
1515
versionName "1.0"

ui/espresso/ScreenshotSample/app/src/androidTest/java/com/example/android/testing/espresso/screenshotsample/ScreenshotJavaTest.java

+25-9
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
import static androidx.test.core.app.DeviceCapture.takeScreenshot;
44
import static androidx.test.core.graphics.BitmapStorage.writeToTestStorage;
55
import static androidx.test.espresso.Espresso.onView;
6+
import static androidx.test.espresso.action.ViewActions.captureToBitmap;
67
import static androidx.test.espresso.matcher.ViewMatchers.isRoot;
78
import static androidx.test.espresso.matcher.ViewMatchers.withText;
8-
import static androidx.test.espresso.screenshot.ViewInteractionCapture.captureToBitmap;
99

10-
import android.view.View;
10+
import android.graphics.Bitmap;
1111

12-
import androidx.concurrent.futures.ResolvableFuture;
13-
import androidx.test.core.app.ActivityScenario;
14-
import androidx.test.core.view.ViewCapture;
12+
import androidx.test.espresso.action.ViewActions;
1513
import androidx.test.ext.junit.rules.ActivityScenarioRule;
1614
import androidx.test.ext.junit.runners.AndroidJUnit4;
1715

@@ -21,8 +19,6 @@
2119
import org.junit.runner.RunWith;
2220

2321
import java.io.IOException;
24-
import java.util.concurrent.ExecutionException;
25-
import java.util.concurrent.Future;
2622

2723
/**
2824
* Equivalent of {@link ScreenshotTest} for java.
@@ -42,15 +38,35 @@ public class ScreenshotJavaTest {
4238
*/
4339
@Test
4440
public void saveActivityBitmap() throws IOException {
45-
writeToTestStorage(captureToBitmap(onView(isRoot())), getClass().getSimpleName() + "_" + nameRule.getMethodName());
41+
onView(isRoot()).perform(captureToBitmap(new ViewActions.BitmapReceiver() {
42+
@Override
43+
public void onBitmapCaptured(Bitmap bitmap) {
44+
try {
45+
writeToTestStorage(bitmap, ScreenshotJavaTest.class.getSimpleName() + "_" + nameRule.getMethodName());
46+
} catch (IOException e) {
47+
throw new RuntimeException(e);
48+
}
49+
}
50+
}));
4651
}
4752

4853
/**
4954
* Captures and saves an image of the 'Hello world' view.
5055
*/
5156
@Test
5257
public void saveViewBitmap() throws IOException {
53-
writeToTestStorage(captureToBitmap(onView(withText("Hello World!"))), getClass().getSimpleName() + "_" + nameRule.getMethodName());
58+
onView(withText("Hello World!")).perform(captureToBitmap(new ViewActions.BitmapReceiver() {
59+
@Override
60+
public void onBitmapCaptured(Bitmap bitmap) {
61+
try {
62+
writeToTestStorage(bitmap, ScreenshotJavaTest.class.getSimpleName() + "_" + nameRule.getMethodName());
63+
} catch (IOException e) {
64+
throw new RuntimeException(e);
65+
}
66+
}
67+
}));
68+
69+
5470
}
5571

5672
/**
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.example.android.testing.espresso.screenshotsample
22

3+
import android.graphics.Bitmap
34
import androidx.test.core.app.takeScreenshot
45
import androidx.test.core.graphics.writeToTestStorage
56
import androidx.test.espresso.Espresso.onView
7+
import androidx.test.espresso.action.ViewActions
8+
import androidx.test.espresso.action.ViewActions.captureToBitmap
69
import androidx.test.espresso.matcher.ViewMatchers.isRoot
710
import androidx.test.espresso.matcher.ViewMatchers.withText
8-
import androidx.test.espresso.screenshot.captureToBitmap
11+
912
import androidx.test.ext.junit.rules.activityScenarioRule
1013
import androidx.test.ext.junit.runners.AndroidJUnit4
1114
import org.junit.Rule
@@ -23,43 +26,41 @@ import java.io.IOException
2326
@RunWith(AndroidJUnit4::class)
2427
class ScreenshotTest {
2528

26-
// a handy JUnit rule that stores the method name, so it can be used to generate unique
27-
// screenshot files per test method
28-
@get:Rule
29-
var nameRule = TestName()
29+
// a handy JUnit rule that stores the method name, so it can be used to generate unique
30+
// screenshot files per test method
31+
@get:Rule
32+
var nameRule = TestName()
3033

31-
@get:Rule
32-
val activityScenarioRule = activityScenarioRule<MainActivity>()
34+
@get:Rule
35+
val activityScenarioRule = activityScenarioRule<MainActivity>()
3336

34-
/**
35-
* Captures and saves an image of the entire [MainActivity] contents.
36-
*/
37-
@Test
38-
@Throws(IOException::class)
39-
fun saveActivityBitmap() {
40-
onView(isRoot())
41-
.captureToBitmap()
42-
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
43-
}
37+
/**
38+
* Captures and saves an image of the entire [MainActivity] contents.
39+
*/
40+
@Test
41+
@Throws(IOException::class)
42+
fun saveActivityBitmap() {
43+
onView(isRoot())
44+
.perform(captureToBitmap({ bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") }))
45+
}
4446

45-
/**
46-
* Captures and saves an image of the 'Hello world' view.
47-
*/
48-
@Test
49-
@Throws(IOException::class)
50-
fun saveViewBitmap() {
51-
onView(withText("Hello World!"))
52-
.captureToBitmap()
53-
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
54-
}
47+
/**
48+
* Captures and saves an image of the 'Hello world' view.
49+
*/
50+
@Test
51+
@Throws(IOException::class)
52+
fun saveViewBitmap() {
53+
onView(withText("Hello World!"))
54+
.perform(captureToBitmap({ bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") }))
55+
}
5556

56-
/**
57-
* Captures and saves an image of the entire device screen to storage.
58-
*/
59-
@Test
60-
@Throws(IOException::class)
61-
fun saveDeviceScreenBitmap() {
62-
takeScreenshot()
63-
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
64-
}
57+
/**
58+
* Captures and saves an image of the entire device screen to storage.
59+
*/
60+
@Test
61+
@Throws(IOException::class)
62+
fun saveDeviceScreenBitmap() {
63+
takeScreenshot()
64+
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
65+
}
6566
}

0 commit comments

Comments
 (0)