Skip to content

Commit

Permalink
Merge pull request #81 from catehstn/master
Browse files Browse the repository at this point in the history
bit of refactoring, improving test coverage
  • Loading branch information
MichaelDiBernardo committed Aug 25, 2014
2 parents 2e6db38 + 29aec42 commit c37f523
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
Expand Up @@ -121,9 +121,6 @@ public void keyPressed() {
case 'p':
redrawImage = true;
break;
case 'w':
imageState.image().save(imageState.filepath() + "-new.png");
break;
case ' ':
imageState.resetImage(this, IMAGE_MAX);
redrawImage = true;
Expand Down
Expand Up @@ -12,9 +12,9 @@ enum ColorMode {
HIDE_DOMINANT_HUE
}

ColorHelper colorHelper;
IFAImage image;
String filepath;
private final ColorHelper colorHelper;
private IFAImage image;
private String filepath;

public static final int INITIAL_HUE_TOLERANCE = 5;

Expand Down Expand Up @@ -122,6 +122,9 @@ public void processKeyPress(char key, int inc, int rgbColorRange, int hueIncreme
colorModeState = ColorMode.SHOW_DOMINANT_HUE;
}
break;
case 'w':
image().save(filepath() + "-new.png");
break;
}
}

Expand Down
Expand Up @@ -32,6 +32,15 @@ public class ImageStateTest {
imageState = new ImageState(colorHelper);
}

private void assertState(ColorMode colorMode, int redFilter,
int greenFilter, int blueFilter, int hueTolerance) {
assertEquals(colorMode, imageState.getColorMode());
assertEquals(redFilter, imageState.redFilter());
assertEquals(greenFilter, imageState.greenFilter());
assertEquals(blueFilter, imageState.blueFilter());
assertEquals(hueTolerance, imageState.hueTolerance());
}

@Test public void testUpdateImageDominantHueHidden() {
imageState.setFilepath("filepath");
imageState.set(image, ColorMode.HIDE_DOMINANT_HUE, 5, 10, 15, 10);
Expand Down Expand Up @@ -71,39 +80,55 @@ public class ImageStateTest {

@Test public void testKeyPress() {
imageState.processKeyPress('r', 5, 100, 2, 200);
assertState(ColorMode.COLOR_FILTER, 5, 0, 0);
assertState(ColorMode.COLOR_FILTER, 5, 0, 0, 5);

imageState.processKeyPress('e', 5, 100, 2, 200);
assertState(ColorMode.COLOR_FILTER, 0, 0, 0);
assertState(ColorMode.COLOR_FILTER, 0, 0, 0, 5);

imageState.processKeyPress('g', 5, 100, 2, 200);
assertState(ColorMode.COLOR_FILTER, 0, 5, 0);
assertState(ColorMode.COLOR_FILTER, 0, 5, 0, 5);

imageState.processKeyPress('f', 5, 100, 2, 200);
assertState(ColorMode.COLOR_FILTER, 0, 0, 0);
assertState(ColorMode.COLOR_FILTER, 0, 0, 0, 5);

imageState.processKeyPress('b', 5, 100, 2, 200);
assertState(ColorMode.COLOR_FILTER, 0, 0, 5);
assertState(ColorMode.COLOR_FILTER, 0, 0, 5, 5);

imageState.processKeyPress('v', 5, 100, 2, 200);
assertState(ColorMode.COLOR_FILTER, 0, 0, 0);
assertState(ColorMode.COLOR_FILTER, 0, 0, 0, 5);

imageState.processKeyPress('h', 5, 100, 2, 200);
assertState(ColorMode.HIDE_DOMINANT_HUE, 0, 0, 0);
assertState(ColorMode.HIDE_DOMINANT_HUE, 0, 0, 0, 5);

imageState.processKeyPress('i', 5, 100, 2, 200);
assertState(ColorMode.HIDE_DOMINANT_HUE, 0, 0, 0, 7);

imageState.processKeyPress('u', 5, 100, 2, 200);
assertState(ColorMode.HIDE_DOMINANT_HUE, 0, 0, 0, 5);

imageState.processKeyPress('h', 5, 100, 2, 200);
assertState(ColorMode.COLOR_FILTER, 0, 0, 0, 5);

imageState.processKeyPress('s', 5, 100, 2, 200);
assertState(ColorMode.SHOW_DOMINANT_HUE, 0, 0, 0);
assertState(ColorMode.SHOW_DOMINANT_HUE, 0, 0, 0, 5);

imageState.processKeyPress('s', 5, 100, 2, 200);
assertState(ColorMode.COLOR_FILTER, 0, 0, 0, 5);

// Random key should do nothing.
imageState.processKeyPress('z', 5, 100, 2, 200);
assertState(ColorMode.COLOR_FILTER, 0, 0, 0, 5);
}

private void assertState(ColorMode colorMode, int redFilter,
int greenFilter, int blueFilter) {
assertEquals(colorMode, imageState.getColorMode());
assertEquals(redFilter, imageState.redFilter());
assertEquals(greenFilter, imageState.greenFilter());
assertEquals(blueFilter, imageState.blueFilter());
@Test public void testSave() {
imageState.set(image, ColorMode.SHOW_DOMINANT_HUE, 5, 10, 15, 10);
imageState.setFilepath("filepath");
imageState.processKeyPress('w', 5, 100, 2, 200);

verify(image).save("filepath-new.png");
}

@Test public void testSetupImage() {
@Test public void testSetupImageLandscape() {
imageState.set(image, ColorMode.SHOW_DOMINANT_HUE, 5, 10, 15, 10);
when(image.getWidth()).thenReturn(20);
when(image.getHeight()).thenReturn(8);
Expand All @@ -112,9 +137,18 @@ private void assertState(ColorMode colorMode, int redFilter,
verify(image).resize(10, 4);
}

@Test public void testSetupImagePortrait() {
imageState.set(image, ColorMode.SHOW_DOMINANT_HUE, 5, 10, 15, 10);
when(image.getWidth()).thenReturn(8);
when(image.getHeight()).thenReturn(20);
imageState.setUpImage(applet, 10);
verify(image).update(applet, null);
verify(image).resize(4, 10);
}

@Test public void testResetImage() {
imageState.set(image, ColorMode.SHOW_DOMINANT_HUE, 5, 10, 15, 10);
imageState.resetImage(applet, 10);
assertState(ColorMode.COLOR_FILTER, 0, 0, 0);
assertState(ColorMode.COLOR_FILTER, 0, 0, 0, 5);
}
}

0 comments on commit c37f523

Please sign in to comment.