Merge pull request #511 from android/screenshot · android/testing-samples@a59d128 · GitHub
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

3 files changed

Lines changed: 63 additions & 46 deletions

File tree

‎ui/espresso/ScreenshotSample/app/build.gradle‎

Lines changed: 1 addition & 1 deletion

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

Lines changed: 25 additions & 9 deletions
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
/**
Lines changed: 37 additions & 36 deletions

0 commit comments

Comments
 (0)