Skip to content

Commit

Permalink
Enh: move code from SnapshotReviewApp to ScreenCaptureSupport
Browse files Browse the repository at this point in the history
add ScreenCaptureSupport#
  - getTestResourcesDirectoryCandidatesDefault()
  - adjustTestResourcesDirectory(...)
  - getSnapshotReportDirectoryCandidatesDefault()
  - adjustSnapshotReportDirectory(...)

Also:
- documentation/fix typo
  • Loading branch information
abego committed Jun 19, 2022
1 parent acccdfb commit 7fedcfd
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ BufferedImage captureScreen(@Nullable Component component,
*
* @param imageA an {@link Image} to compare with the other
* @param imageB an {@link Image} to compare with the other
* @return the difference between {@code imageA} and {@code imageB} as an
* @return the difference between {@code imageA} and {@code imageB} as
* an {@link Image} with {@link Color#black} pixels marking the parts of that differ
* in both images or only exist in one of them
*/
Expand Down Expand Up @@ -278,8 +278,8 @@ BufferedImage waitUntilScreenshotMatchesImage(
/**
* Returns the path to the test resources directory
*
* @deprecated use {@link #getTestResourcesDirectory()} instead
* @return the path to the test resources directory
* @deprecated use {@link #getTestResourcesDirectory()} instead
*/
@Deprecated
default String getTestResourcesDirectoryPath() {
Expand All @@ -293,6 +293,39 @@ default String getTestResourcesDirectoryPath() {
*/
void setTestResourcesDirectory(File directory);

File[] getTestResourcesDirectoryCandidatesDefault();

/**
* Adjusts this object's testResourcesDirectory according to
* {@code directory} and {@code directoryCandidates}
* <p>
* The following procedure is used:
* <ul>
* <li>When {@code directory} is not {@code null} it becomes
* this object's testResourcesDirectory, otherwise</li>
* <li>when this object's testResourcesDirectory is a <b>not</b> directory
* the first file from {@code directoryCandidates} that is
* a directory becomes this object's testResourcesDirectory (if such
* file exists), otherwise
* </li>
* <li>when no file from {@code directoryCandidates} is a
* directory or this object's testResourcesDirectory is a directory
* this object's testResourcesDirectory is not changed.</li>
* </ul>
*/
void adjustTestResourcesDirectory(
@Nullable File directory, File[] directoryCandidates);

/**
* Adjusts this object's testResourcesDirectory according to
* {@code directory} and {@link #getTestResourcesDirectoryCandidatesDefault()}.
* <p>
* For details see {@link #adjustSnapshotReportDirectory(File, File[])}.
*/
default void adjustTestResourcesDirectory(@Nullable File directory) {
adjustTestResourcesDirectory(directory, getTestResourcesDirectoryCandidatesDefault());
}

/**
* Sets the path to the directory with test resources to {@code path}.
*
Expand Down Expand Up @@ -508,6 +541,39 @@ default Seq<SnapshotIssue> getSnapshotIssues() {

void setSnapshotReportDirectory(File directory);

File[] getSnapshotReportDirectoryCandidatesDefault();

/**
* Adjusts this object's snapshotReportDirectory according to
* {@code directory} and {@code directoryCandidates}
* <p>
* The following procedure is used:
* <ul>
* <li>When {@code directory} is not {@code null} it becomes
* this object's snapshotReportDirectory, otherwise</li>
* <li>when this object's snapshotReportDirectory is a <b>not</b> directory
* the first file from {@code directoryCandidates} that is
* a directory becomes this object's snapshotReportDirectory (if such
* file exists), otherwise
* </li>
* <li>when no file from {@code directoryCandidates} is a
* directory or this object's snapshotReportDirectory is a directory
* this object's snapshotReportDirectory is not changed.</li>
* </ul>
*/
void adjustSnapshotReportDirectory(
@Nullable File directory, File[] directoryCandidates);

/**
* Adjusts this object's snapshotReportDirectory according to
* {@code directory} and {@link #getSnapshotReportDirectoryCandidatesDefault()}
* <p>
* For details see {@link #adjustSnapshotReportDirectory(File, File[])}
*/
default void adjustSnapshotReportDirectory(@Nullable File directory) {
adjustSnapshotReportDirectory(directory, getSnapshotReportDirectoryCandidatesDefault());
}

/**
* Makes subsequent "waitUntilScreenshotMatches..." calls more tolerant by
* setting "ImageDifferenceIgnoredBorderSize",
Expand All @@ -530,63 +596,63 @@ default Seq<SnapshotIssue> getSnapshotIssues() {
*/
void resetScreenCaptureSupport();

/**
* The difference between two {@link Image}s ({@code imageA} and {@code imageB}).
*/
interface ImageDifference {

/**
* The difference between two {@link Image}s ({@code imageA} and {@code imageB}).
*/
interface ImageDifference {

/**
* Returns {@code true} when the images don't match, i.e. there are
* differences between the images, {@code false} otherwise.
*
* @return {@code true} when the images don't match, i.e. there are
* differences between the images, {@code false} otherwise
*/

boolean imagesAreDifferent();

/**
* Returns the first image of the comparison.
*
* @return the first image of the comparison
*/
BufferedImage getImageA();

/**
* Returns the second image of the comparison.
*
* @return the second image of the comparison
*/
BufferedImage getImageB();

/**
* Returns an image marking the differences between imageA and imageB
* with {@link Color#black} pixels on a (transparent) white canvas.
*
* <p>The images are compared pixel by pixel and all pixel that are not
* similar or that only exist in one image are marked {@link Color#black}.</p>
*
* @return an image marking the differences between imageA and imageB
* with {@link Color#black} pixels on a (transparent) white canvas
*/
BufferedImage getDifferenceMask();
}
* Returns {@code true} when the images don't match, i.e. there are
* differences between the images, {@code false} otherwise.
*
* @return {@code true} when the images don't match, i.e. there are
* differences between the images, {@code false} otherwise
*/

boolean imagesAreDifferent();

/**
* Returns the first image of the comparison.
*
* @return the first image of the comparison
*/
BufferedImage getImageA();

interface SnapshotIssue {
String getSnapshotName();
/**
* Returns the second image of the comparison.
*
* @return the second image of the comparison
*/
BufferedImage getImageB();

int getIndex();
/**
* Returns an image marking the differences between imageA and imageB
* with {@link Color#black} pixels on a (transparent) white canvas.
*
* <p>The images are compared pixel by pixel and all pixel that are not
* similar or that only exist in one image are marked {@link Color#black}.</p>
*
* @return an image marking the differences between imageA and imageB
* with {@link Color#black} pixels on a (transparent) white canvas
*/
BufferedImage getDifferenceMask();
}

String getLabel();
interface SnapshotIssue {
String getSnapshotName();

URL getActualImage();
int getIndex();

URL getExpectedImage();
String getLabel();

URL getDifferenceImage();
URL getActualImage();

URL getOverwriteURL();
URL getExpectedImage();

URL getAddAlternativeURL();
}
URL getDifferenceImage();

URL getOverwriteURL();

URL getAddAlternativeURL();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,16 @@ public void setTestResourcesDirectory(File directory) {
screenCaptureSupport.setTestResourcesDirectory(directory);
}

@Override
public File[] getTestResourcesDirectoryCandidatesDefault() {
return screenCaptureSupport.getTestResourcesDirectoryCandidatesDefault();
}

@Override
public void adjustTestResourcesDirectory(@Nullable File directory, File[] directoryCandidates) {
screenCaptureSupport.adjustTestResourcesDirectory(directory, directoryCandidates);
}

@Override
public String getSnapshotName(@Nullable String name) {
return screenCaptureSupport.getSnapshotName(name);
Expand Down Expand Up @@ -520,6 +530,16 @@ public void setSnapshotReportDirectory(File directory) {
screenCaptureSupport.setSnapshotReportDirectory(directory);
}

@Override
public File[] getSnapshotReportDirectoryCandidatesDefault() {
return screenCaptureSupport.getSnapshotReportDirectoryCandidatesDefault();
}

@Override
public void adjustSnapshotReportDirectory(@Nullable File directory, File[] directoryCandidates) {
screenCaptureSupport.adjustSnapshotReportDirectory(directory, directoryCandidates);
}

@Override
public void makeScreenshotMatchingTolerant() {
screenCaptureSupport.makeScreenshotMatchingTolerant();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,16 @@ public void setTestResourcesDirectory(File directory) {
throw new HeadlessGuiTestingException();
}

@Override
public File[] getTestResourcesDirectoryCandidatesDefault() {
throw new HeadlessGuiTestingException();
}

@Override
public void adjustTestResourcesDirectory(@Nullable File directory, File[] directoryCandidates) {
throw new HeadlessGuiTestingException();
}

@Override
public String getSnapshotName(@Nullable String name) {
throw new HeadlessGuiTestingException();
Expand All @@ -373,6 +383,16 @@ public void setSnapshotReportDirectory(File directory) {
throw new HeadlessGuiTestingException();
}

@Override
public File[] getSnapshotReportDirectoryCandidatesDefault() {
throw new HeadlessGuiTestingException();
}

@Override
public void adjustSnapshotReportDirectory(@Nullable File directory, File[] directoryCandidates) {
throw new HeadlessGuiTestingException();
}

@Override
public void makeScreenshotMatchingTolerant() {
throw new HeadlessGuiTestingException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.util.logging.Logger;

import static java.util.logging.Logger.getLogger;
import static org.abego.commons.io.FileUtil.findExistingDirectory;
import static org.abego.commons.lang.StringUtil.replaceRange;
import static org.abego.guitesting.swing.internal.GuiTestingUtil.checkIsPngFilename;
import static org.abego.guitesting.swing.internal.GuiTestingUtil.getNameDefiningCall;
Expand All @@ -66,6 +67,20 @@ public class ScreenCaptureSupportImpl implements ScreenCaptureSupport {
static final String SCREENSHOT_IMAGES_DIRECTORY_NAME_DEFAULT = "images"; //NON-NLS
static final String SNAP_SHOTS_DIRECTORY_NAME = "/snap-shots"; //NON-NLS
private static final String SNAPSHOT_NAME_DEFAULT = "snapshot"; //NON-NLS
@SuppressWarnings("DuplicateStringLiteralInspection")
private static final File[] testResourcesDirectoryCandidates = new File[]{
new File("src/test/resources"), //NON-NLS
new File("test_resources"), //NON-NLS
new File("test_src/resources"), //NON-NLS
new File("../src/test/resources"), //NON-NLS
new File("../test_resources"), //NON-NLS
new File("../test_src/resources"), //NON-NLS
};
@SuppressWarnings("DuplicateStringLiteralInspection")
private static final File[] snapshotReportDirectoryCandidates = new File[]{
new File("target/guitesting-reports"), //NON-NLS
new File("../target/guitesting-reports"), //NON-NLS
};
private static final Logger LOGGER = getLogger(ScreenCaptureSupportImpl.class.getName());
private static final Duration DELAY_BEFORE_NEW_SNAPSHOT_DEFAULT = Duration.ofSeconds(1);
@SuppressWarnings("DuplicateStringLiteralInspection")
Expand Down Expand Up @@ -264,6 +279,27 @@ public void setTestResourcesDirectory(File directory) {
testResourcesDirectory = directory;
}

@Override
public File[] getTestResourcesDirectoryCandidatesDefault() {
return testResourcesDirectoryCandidates;
}

@Override
public void adjustTestResourcesDirectory(
@Nullable File directory, File[] directoryCandidates) {
// When no testResourcesDirectory is explicitly specified
// and {@code gt}'s current one does not exist check if one of the
// testResourcesDirectoryOptions exists. If yes, use it.
if (directory == null && !getTestResourcesDirectory().isDirectory()) {
directory =
findExistingDirectory(directoryCandidates);
}

if (directory != null) {
setTestResourcesDirectory(directory);
}
}

private String resolveSnapshotName(
@Nullable String snapshotName, String calleeName) {
// absolute snapshot names are used "as is"
Expand Down Expand Up @@ -383,6 +419,25 @@ public void setSnapshotReportDirectory(File directory) {
snapshotReportDirectory = directory;
}

@Override
public File[] getSnapshotReportDirectoryCandidatesDefault() {
return snapshotReportDirectoryCandidates;
}

@Override
public void adjustSnapshotReportDirectory(
@Nullable File directory,
File[] directoryCandidates) {

if (directory == null && !getSnapshotReportDirectory().isDirectory()) {
directory =
findExistingDirectory(directoryCandidates);
}
if (directory != null) {
setSnapshotReportDirectory(directory);
}
}

@Override
public void resetScreenCaptureSupport() {
setImageDifferenceIgnoredBorderSize(0);
Expand Down
Loading

0 comments on commit 7fedcfd

Please sign in to comment.