From: gmungoc Date: Thu, 30 Mar 2017 10:59:58 +0000 (+0100) Subject: JAL-2346 more test coverage for AnnotationColourGradient X-Git-Tag: Release_2_10_3b1~336^2~2^2~1^2~2^2~2 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=96d47aa43e18fe1df80d602c5f8744da6ebd77bc;p=jalview.git JAL-2346 more test coverage for AnnotationColourGradient --- diff --git a/test/jalview/schemes/AnnotationColourGradientTest.java b/test/jalview/schemes/AnnotationColourGradientTest.java index 42e3eb4..8de90bf 100644 --- a/test/jalview/schemes/AnnotationColourGradientTest.java +++ b/test/jalview/schemes/AnnotationColourGradientTest.java @@ -2,11 +2,15 @@ package jalview.schemes; import static org.testng.Assert.assertEquals; -import java.awt.Color; - +import jalview.datamodel.Alignment; import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.AlignmentI; import jalview.datamodel.Annotation; import jalview.datamodel.GraphLine; +import jalview.datamodel.Sequence; +import jalview.datamodel.SequenceI; + +import java.awt.Color; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -15,31 +19,45 @@ public class AnnotationColourGradientTest { final static int WIDTH = 11; - final static int THRESHOLD = 5; + final static int THRESHOLD_FIVE = 5; private AlignmentAnnotation ann; + private SequenceI seq; + + private AlignmentI al; + Color minColour = new Color(50, 200, 150); Color maxColour = new Color(150, 100, 250); + /** + * Setup creates an annotation over 11 columns with values 0-10 and threshold + * 5 + */ @BeforeClass public void setUp() { Annotation[] anns = new Annotation[WIDTH]; /* - * set annotations with values 0-10 + * set annotations with values 0-10, graded colours */ for (int col = 0; col < WIDTH; col++) { - anns[col] = new Annotation("a", "a", 'a', col); + int hue = col * 20; + Color colour = new Color(hue, hue, hue); + anns[col] = new Annotation("a", "a", 'a', col, colour); } + seq = new Sequence("", ""); + al = new Alignment(new SequenceI[]{ seq}); + /* * AlignmentAnnotation constructor works out min-max range */ ann = new AlignmentAnnotation("", "", anns); - ann.setThreshold(new GraphLine(THRESHOLD, "", Color.RED)); + ann.setThreshold(new GraphLine(THRESHOLD_FIVE, "", Color.RED)); + seq.addAlignmentAnnotation(ann); } @Test(groups = "Functional") @@ -72,8 +90,7 @@ public class AnnotationColourGradientTest Color result = testee.shadeCalculation(ann, col); /* * colour is derived regardless of the threshold value - * (the renderer that will suppress colouring if - * above/below threshold) + * (the renderer will suppress colouring if above/below threshold) */ Color expected = new Color(50 + 10 * col, 200 - 10 * col, 150 + 10 * col); @@ -85,7 +102,7 @@ public class AnnotationColourGradientTest * (annotation value == column number in this test) */ testee.setThresholdIsMinMax(true); - for (int col = 0; col < THRESHOLD; col++) + for (int col = 0; col < THRESHOLD_FIVE; col++) { /* * colours below the threshold are computed as before @@ -95,13 +112,13 @@ public class AnnotationColourGradientTest Color result = testee.shadeCalculation(ann, col); assertEquals(result, expected, "for column " + col); } - for (int col = THRESHOLD; col < WIDTH; col++) + for (int col = THRESHOLD_FIVE; col < WIDTH; col++) { /* * colours for values >= threshold are graduated * range is 6-10 so steps of 100/5 = 20 */ - int factor = col - THRESHOLD; + int factor = col - THRESHOLD_FIVE; Color expected = new Color(50 + 20 * factor, 200 - 20 * factor, 150 + 20 * factor); Color result = testee.shadeCalculation(ann, col); @@ -115,12 +132,6 @@ public class AnnotationColourGradientTest @Test(groups = "Functional") public void testShadeCalculation_belowThreshold() { - /* - * change threshold to 5 so we have an easy calculation of - * the min-max range 0-5 - */ - int threshold = 5; - ann.setThreshold(new GraphLine(threshold, "", Color.RED)); AnnotationColourGradient testee = new AnnotationColourGradient(ann, minColour, maxColour, AnnotationColourGradient.BELOW_THRESHOLD); @@ -129,8 +140,7 @@ public class AnnotationColourGradientTest Color result = testee.shadeCalculation(ann, col); /* * colour is derived regardless of the threshold value - * (the renderer that will suppress colouring if - * above/below threshold) + * (the renderer will suppress colouring if above/below threshold) */ Color expected = new Color(50 + 10 * col, 200 - 10 * col, 150 + 10 * col); @@ -142,7 +152,7 @@ public class AnnotationColourGradientTest * (annotation value == column number in this test) */ testee.setThresholdIsMinMax(true); - for (int col = threshold + 1; col < WIDTH; col++) + for (int col = THRESHOLD_FIVE + 1; col < WIDTH; col++) { /* * colours above the threshold are computed as before @@ -153,7 +163,7 @@ public class AnnotationColourGradientTest assertEquals(result, expected, "for column " + col); } - for (int col = 0; col <= threshold; col++) + for (int col = 0; col <= THRESHOLD_FIVE; col++) { /* * colours for values <= threshold are graduated @@ -165,4 +175,126 @@ public class AnnotationColourGradientTest assertEquals(result, expected, "for column " + col); } } + + /** + * Test the 'colour above threshold' case + */ + @Test(groups = "Functional") + public void testFindColour_aboveThreshold() + { + AnnotationColourGradient testee = new AnnotationColourGradient(ann, + minColour, maxColour, AnnotationColourGradient.ABOVE_THRESHOLD); + testee = (AnnotationColourGradient) testee.getInstance(al, null); + + for (int col = 0; col < WIDTH; col++) + { + Color result = testee.findColour('a', col, seq); + /* + * expect white below threshold of 5 + */ + Color expected = col < 5 ? Color.white : new Color(50 + 10 * col, + 200 - 10 * col, + 150 + 10 * col); + assertEquals(result, expected, "for column " + col); + } + + /* + * now make 6-10 the span of the colour range + * (annotation value == column number in this test) + */ + testee.setThresholdIsMinMax(true); + for (int col = 0; col < WIDTH; col++) + { + /* + * colours for values >= threshold are graduated + * range is 6-10 so steps of 100/5 = 20 + */ + int factor = col - THRESHOLD_FIVE; + Color expected = col < 5 ? Color.white : new Color(50 + 20 * factor, + 200 - 20 * factor, + 150 + 20 * factor); + Color result = testee.findColour('a', col, seq); + assertEquals(result, expected, "for column " + col); + } + } + + /** + * Test the 'colour below threshold' case + */ + @Test(groups = "Functional") + public void testFindColour_belowThreshold() + { + AnnotationColourGradient testee = new AnnotationColourGradient(ann, + minColour, maxColour, AnnotationColourGradient.BELOW_THRESHOLD); + testee = (AnnotationColourGradient) testee.getInstance(al, null); + + for (int col = 0; col < WIDTH; col++) + { + Color result = testee.findColour('a', col, seq); + Color expected = col > 5 ? Color.white : new Color(50 + 10 * col, + 200 - 10 * col, 150 + 10 * col); + assertEquals(result, expected, "for column " + col); + } + + /* + * now make 0-5 the span of the colour range + * (annotation value == column number in this test) + */ + testee.setThresholdIsMinMax(true); + for (int col = 0; col < WIDTH; col++) + { + /* + * colours for values <= threshold are graduated + * range is 0-5 so steps of 100/5 = 20 + */ + Color expected = col > 5 ? Color.white : new Color(50 + 20 * col, + 200 - 20 * col, 150 + 20 * col); + Color result = testee.findColour('a', col, seq); + assertEquals(result, expected, "for column " + col); + } + } + + @Test(groups = "Functional") + public void testFindColour_noThreshold() + { + AnnotationColourGradient testee = new AnnotationColourGradient(ann, + minColour, maxColour, AnnotationColourGradient.NO_THRESHOLD); + testee = (AnnotationColourGradient) testee.getInstance(al, null); + + for (int col = 0; col < WIDTH; col++) + { + Color result = testee.findColour('a', col, seq); + /* + * column is n/10 of the way from minCol to maxCol + */ + Color expected = new Color(50 + 10 * col, 200 - 10 * col, + 150 + 10 * col); + assertEquals(result, expected, "for column " + col); + } + } + + @Test(groups = "Functional") + public void testFindColour_originalColours() + { + AnnotationColourGradient testee = new AnnotationColourGradient(ann, + minColour, maxColour, AnnotationColourGradient.NO_THRESHOLD); + testee = (AnnotationColourGradient) testee.getInstance(al, null); + + /* + * flag corresponding to 'use original colours' checkbox + * - just use the individual annotation colours + */ + testee.setPredefinedColours(true); + + /* + * the annotation colour is returned, except for column 0 where it is + * black - in this case the colour scheme colour overrides it + */ + for (int col = 0; col < WIDTH; col++) + { + int hue = col * 20; + Color c = col == 0 ? minColour : new Color(hue, hue, hue); + assertEquals(testee.findColour('a', col, seq), c, "for column " + col); + } + } }