Skip to content

Commit

Permalink
Add simpleComparison and load tests (#220)
Browse files Browse the repository at this point in the history
Co-authored-by: Roman Beekeeper <roman.beskrovnyy@gmail.com>
  • Loading branch information
Hannnnnnn404 and romankh3 authored Jun 17, 2024
1 parent d22bfe6 commit 97af3fc
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,25 @@ public ImageComparisonResult compareImages() {
.setRectangles(rectangles);
}

/**
* Compare two images and exclude drawing rectangles with the differences.
*
* @return the result comparisonState
*/
public ImageComparisonResult simpleComparison(){
// check that the images have the same size
if (isImageSizesNotEqual(expected, actual)) {
BufferedImage actualResized = ImageComparisonUtil.resize(actual, expected.getWidth(), expected.getHeight());
return ImageComparisonResult.defaultSizeMisMatchResult(expected, actual, getDifferencePercent(actualResized, expected));
}

if (isFirstDifferences()){
return ImageComparisonResult.defaultMisMatchResult(expected, actual, 0);
}else {
return ImageComparisonResult.defaultMatchResult(expected, actual);
}
}

/**
* Check images for equals their widths and heights.
*
Expand Down Expand Up @@ -301,6 +320,24 @@ private List<Rectangle> populateRectangles() {
return mergeRectangles(mergeRectangles(rectangles));
}

/**
* say if there exists two pixels equal or not.
*
* @return true, if first find two different pixels.
*/
private boolean isFirstDifferences() {
for (int y = 0; y < expected.getHeight(); y++) {
for (int x = 0; x < expected.getWidth(); x++) {
if (!excludedAreas.contains(new Point(x, y))) {
if (isDifferentPixels(expected.getRGB(x, y), actual.getRGB(x, y))) {
return true;
}
}
}
}
return false;
}

/**
* Say if provided {@param countOfDifferentPixels} is allowed for {@link ImageComparisonState#MATCH} state.
*
Expand Down
119 changes: 119 additions & 0 deletions src/test/java/com/github/romankh3/image/comparison/LoadTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.github.romankh3.image.comparison;

import com.github.romankh3.image.comparison.model.ImageComparisonResult;
import com.github.romankh3.image.comparison.model.ImageComparisonState;
import java.awt.image.BufferedImage;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@DisplayName("Load tests for {@link simpleComparison} method")
public class LoadTest {
@Test
@DisplayName("should result MisMatch for two images")
public void simpleComparisonTest(){
//load images to be compared:
BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected.png");
BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual.png");

//Create ImageComparison object and compare the images.
ImageComparisonResult imageComparisonResult = new ImageComparison(expectedImage, actualImage).simpleComparison();

//Check the result
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult.getImageComparisonState());
}


@Test
@DisplayName("compare load differences between {@link simpleComparison} method and {@link compareImage} method")
public void loadTest1(){
//load images to be compared:
BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected.png");
BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual.png");

long time1 = System.currentTimeMillis();
//Create ImageComparison object and compare the images.
ImageComparisonResult imageComparisonResult1 = new ImageComparison(expectedImage, actualImage).compareImages();
long etime1 = System.currentTimeMillis();
//Time for compareImage method
long compareImageTime = etime1 - time1;

//Check the result
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult1.getImageComparisonState());

long time2 = System.currentTimeMillis();
//Create ImageComparison2 object and compare the images.
ImageComparisonResult imageComparisonResult2 = new ImageComparison(expectedImage, actualImage).simpleComparison();
long etime2 = System.currentTimeMillis();
//Time for simpleComparison method
long simpleComparisonTime = etime2 - time2;

//Check the result
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult2.getImageComparisonState());
//check the load differences between simpleComparison and compareImage pattern
assertTrue(simpleComparisonTime < compareImageTime);
}

@Test
@DisplayName("compare load differences between {@link simpleComparison} method and {@link compareImage} method")
public void loadTest2(){
//load images to be compared:
BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected#201.png");
BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual#201.png");

long time1 = System.currentTimeMillis();
//Create ImageComparison object and compare the images.
ImageComparisonResult imageComparisonResult1 = new ImageComparison(expectedImage, actualImage).compareImages();
long etime1 = System.currentTimeMillis();
//Time for compareImage method
long compareImageTime = etime1 - time1;

//Check the result
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult1.getImageComparisonState());

long time2 = System.currentTimeMillis();
//Create ImageComparison2 object and compare the images.
ImageComparisonResult imageComparisonResult2 = new ImageComparison(expectedImage, actualImage).simpleComparison();
long etime2 = System.currentTimeMillis();
//Time for simpleComparison method
long simpleComparisonTime = etime2 - time2;

//Check the result
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult2.getImageComparisonState());
//check the load differences between simpleComparison and compareImage pattern
assertTrue(simpleComparisonTime < compareImageTime);
}

@Test
@DisplayName("compare load differences between {@link simpleComparison} method and {@link compareImage} method")
public void loadTest3(){
//load images to be compared:
BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected#98.png");
BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual#98.png");

long time1 = System.currentTimeMillis();
//Create ImageComparison object and compare the images.
ImageComparisonResult imageComparisonResult1 = new ImageComparison(expectedImage, actualImage).compareImages();
long etime1 = System.currentTimeMillis();
//Time for compareImage method
long compareImageTime = etime1 - time1;

//Check the result
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult1.getImageComparisonState());

long time2 = System.currentTimeMillis();
//Create ImageComparison2 object and compare the images.
ImageComparisonResult imageComparisonResult2 = new ImageComparison(expectedImage, actualImage).simpleComparison();
long etime2 = System.currentTimeMillis();
//Time for simpleComparison method
long simpleComparisonTime = etime2 - time2;

//Check the result
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult2.getImageComparisonState());
//check the load differences between simpleComparison and compareImage pattern
assertTrue(simpleComparisonTime < compareImageTime);
}

}

0 comments on commit 97af3fc

Please sign in to comment.