}
}
@Override
public void check(PageValidation pageValidation, String objectName, SpecImage spec) throws ValidationErrorException {
PageElement pageElement = pageValidation.findPageElement(objectName);
checkAvailability(pageElement, objectName);
final BufferedImage pageImage = pageValidation.getPage().getScreenshotImage();
// TODO fix. should only take screenshot once per the same page if there are multiple image comparison checks
int tolerance = spec.getTolerance() != null ? Math.abs(spec.getTolerance()) : 25;
ComparisonOptions options = new ComparisonOptions();
options.setStretchToFit(spec.isStretch());
options.setOriginalFilters(spec.getOriginalFilters());
options.setSampleFilters(spec.getSampleFilters());
options.setMapFilters(spec.getMapFilters());
options.setTolerance(tolerance);
Rect elementArea = pageElement.getArea();
ImageCheck minCheck = new ImageCheck(spec.getImagePaths().get(0), elementArea.getHeight() * elementArea.getWidth() * 2, null, null);
Iterator<String> it = spec.getImagePaths().iterator();
if (!it.hasNext()) {
throw new ValidationErrorException("There are now images defined to compare with")
.withErrorArea(new ErrorArea(pageElement.getArea(), objectName));
}
try {
while (minCheck.difference > 0 && it.hasNext()) {
String imagePath = it.next();
ImageCheck imageCheck = checkImages(spec, pageImage, options, elementArea, imagePath);
if (imageCheck.difference <= minCheck.difference) {
minCheck = imageCheck;
}
}
}
catch (ValidationErrorException ex) {
ex.withErrorArea(new ErrorArea(pageElement.getArea(), objectName));
throw ex;
}
catch (Exception ex) {
throw new ValidationErrorException(ex).withErrorArea(new ErrorArea(pageElement.getArea(), objectName));
}
if (minCheck.difference > 0) {
throw new ValidationErrorException(minCheck.errorMessage)
.withErrorArea(new ErrorArea(pageElement.getArea(), objectName))
.withImageComparison(new ImageComparison(spec.getSelectedArea(), minCheck.imagePath, minCheck.result.getComparisonMap()));
}
}