Unit tests added, minor refactoring
[jalview.git] / test / jalview / schemes / AnnotationColourGradientTest.java
1 package jalview.schemes;
2
3 import static org.testng.Assert.assertEquals;
4
5 import java.awt.Color;
6
7 import jalview.datamodel.AlignmentAnnotation;
8 import jalview.datamodel.Annotation;
9 import jalview.datamodel.GraphLine;
10
11 import org.testng.annotations.BeforeClass;
12 import org.testng.annotations.Test;
13
14 public class AnnotationColourGradientTest
15 {
16   final static int WIDTH = 11;
17
18   final static int THRESHOLD = 5;
19
20   private AlignmentAnnotation ann;
21
22   Color minColour = new Color(50, 200, 150);
23
24   Color maxColour = new Color(150, 100, 250);
25
26   @BeforeClass
27   public void setUp()
28   {
29     Annotation[] anns = new Annotation[WIDTH];
30     /*
31      * set annotations with values 0-10
32      */
33     for (int col = 0; col < WIDTH; col++)
34     {
35       anns[col] = new Annotation("a", "a", 'a', col);
36     }
37
38     /*
39      * AlignmentAnnotation constructor works out min-max range
40      */
41     ann = new AlignmentAnnotation("", "", anns);
42     ann.setThreshold(new GraphLine(THRESHOLD, "", Color.RED));
43   }
44
45   @Test(groups = "Functional")
46   public void testShadeCalculation_noThreshold()
47   {
48     AnnotationColourGradient testee = new AnnotationColourGradient(ann,
49             minColour, maxColour, AnnotationColourGradient.NO_THRESHOLD);
50     for (int col = 0; col < WIDTH; col++)
51     {
52       Color result = testee.shadeCalculation(ann, col);
53       /*
54        * column <n> is n/10 of the way from minCol to maxCol
55        */
56       Color expected = new Color(50 + 10 * col, 200 - 10 * col,
57               150 + 10 * col);
58       assertEquals(result, expected, "for column " + col);
59     }
60   }
61
62   /**
63    * Test the 'colour above threshold' case
64    */
65   @Test(groups = "Functional")
66   public void testShadeCalculation_aboveThreshold()
67   {
68     AnnotationColourGradient testee = new AnnotationColourGradient(ann,
69             minColour, maxColour, AnnotationColourGradient.ABOVE_THRESHOLD);
70     for (int col = 0; col < WIDTH; col++)
71     {
72       Color result = testee.shadeCalculation(ann, col);
73       /*
74        * colour is derived regardless of the threshold value 
75        * (the renderer that will suppress colouring if
76        * above/below threshold)
77        */
78       Color expected = new Color(50 + 10 * col, 200 - 10 * col,
79               150 + 10 * col);
80       assertEquals(result, expected, "for column " + col);
81     }
82
83     /*
84      * now make 6-10 the span of the colour range
85      * (annotation value == column number in this test)
86      */
87     testee.setThresholdIsMinMax(true);
88     for (int col = 0; col < THRESHOLD; col++)
89     {
90       /*
91        * colours below the threshold are computed as before
92        */
93       Color expected = new Color(50 + 10 * col, 200 - 10 * col,
94               150 + 10 * col);
95       Color result = testee.shadeCalculation(ann, col);
96       assertEquals(result, expected, "for column " + col);
97     }
98     for (int col = THRESHOLD; col < WIDTH; col++)
99     {
100       /*
101        * colours for values >= threshold are graduated
102        * range is 6-10 so steps of 100/5 = 20
103        */
104       int factor = col - THRESHOLD;
105       Color expected = new Color(50 + 20 * factor, 200 - 20 * factor,
106               150 + 20 * factor);
107       Color result = testee.shadeCalculation(ann, col);
108       assertEquals(result, expected, "for column " + col);
109     }
110   }
111
112   /**
113    * Test the 'colour below threshold' case
114    */
115   @Test(groups = "Functional")
116   public void testShadeCalculation_belowThreshold()
117   {
118     /*
119      * change threshold to 5 so we have an easy calculation of
120      * the min-max range 0-5
121      */
122     int threshold = 5;
123     ann.setThreshold(new GraphLine(threshold, "", Color.RED));
124     AnnotationColourGradient testee = new AnnotationColourGradient(ann,
125             minColour, maxColour, AnnotationColourGradient.BELOW_THRESHOLD);
126
127     for (int col = 0; col < WIDTH; col++)
128     {
129       Color result = testee.shadeCalculation(ann, col);
130       /*
131        * colour is derived regardless of the threshold value 
132        * (the renderer that will suppress colouring if
133        * above/below threshold)
134        */
135       Color expected = new Color(50 + 10 * col, 200 - 10 * col,
136               150 + 10 * col);
137       assertEquals(result, expected, "for column " + col);
138     }
139
140     /*
141      * now make 0-5 the span of the colour range
142      * (annotation value == column number in this test)
143      */
144     testee.setThresholdIsMinMax(true);
145     for (int col = threshold + 1; col < WIDTH; col++)
146     {
147       /*
148        * colours above the threshold are computed as before
149        */
150       Color expected = new Color(50 + 10 * col, 200 - 10 * col,
151               150 + 10 * col);
152       Color result = testee.shadeCalculation(ann, col);
153       assertEquals(result, expected, "for column " + col);
154     }
155
156     for (int col = 0; col <= threshold; col++)
157     {
158       /*
159        * colours for values <= threshold are graduated
160        * range is 0-5 so steps of 100/5 = 20
161        */
162       Color expected = new Color(50 + 20 * col, 200 - 20 * col,
163               150 + 20 * col);
164       Color result = testee.shadeCalculation(ann, col);
165       assertEquals(result, expected, "for column " + col);
166     }
167   }
168 }