Skip to content
This repository was archived by the owner on Oct 7, 2025. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions spoon-client/src/main/java/com/squareup/spoon/SpoonRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,28 @@ public final class SpoonRule implements TestRule {
}

public File screenshot(Activity activity, String tag) {
File screenshotFile = getScreenshotFile(activity, tag);
Bitmap bitmap = Screenshot.capture(tag, activity);
writeBitmapToFile(bitmap, screenshotFile);
Log.d(TAG, "Captured screenshot '" + tag + "'.");
return screenshotFile;
}

public File screenshot(Bitmap bitmap, Context context, String tag) {
File screenshotFile = getScreenshotFile(context, tag);
writeBitmapToFile(bitmap, screenshotFile);
Log.d(TAG, "Saved screenshot '" + tag + "'.");
return screenshotFile;
}

private File getScreenshotFile(Context context, String tag) {
if (!TAG_VALIDATION.matcher(tag).matches()) {
throw new IllegalArgumentException("Tag must match " + TAG_VALIDATION.pattern() + ".");
}
File screenshotDirectory =
obtainDirectory(activity.getApplicationContext(), className, methodName, SPOON_SCREENSHOTS);
obtainDirectory(context.getApplicationContext(), className, methodName, SPOON_SCREENSHOTS);
String screenshotName = System.currentTimeMillis() + NAME_SEPARATOR + tag + EXTENSION;
File screenshotFile = new File(screenshotDirectory, screenshotName);
Bitmap bitmap = Screenshot.capture(tag, activity);
writeBitmapToFile(bitmap, screenshotFile);
Log.d(TAG, "Captured screenshot '" + tag + "'.");
return screenshotFile;
return new File(screenshotDirectory, screenshotName);
}

private static void writeBitmapToFile(Bitmap bitmap, File file) {
Expand Down