JAL-2609 tests updated for revised label width
[jalview.git] / test / jalview / gui / SeqCanvasTest.java
1 package jalview.gui;
2
3 import static org.testng.Assert.assertEquals;
4
5 import jalview.datamodel.AlignmentI;
6 import jalview.io.DataSourceType;
7 import jalview.io.FileLoader;
8
9 import java.awt.Font;
10 import java.awt.FontMetrics;
11
12 import junit.extensions.PA;
13
14 import org.testng.annotations.Test;
15
16 import sun.swing.SwingUtilities2;
17
18 public class SeqCanvasTest
19 {
20   /**
21    * Test the method that computes wrapped width in residues, height of wrapped
22    * widths in pixels, and the number of widths visible
23    */
24   @Test(groups = "Functional")
25   public void testCalculateWrappedGeometry_noAnnotations()
26   {
27     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
28             "examples/uniref50.fa", DataSourceType.FILE);
29     AlignViewport av = af.getViewport();
30     AlignmentI al = av.getAlignment();
31     assertEquals(al.getWidth(), 157);
32     assertEquals(al.getHeight(), 15);
33
34     av.setWrapAlignment(true);
35     av.setFont(new Font("SansSerif", Font.PLAIN, 14), true);
36     int charHeight = av.getCharHeight();
37     int charWidth = av.getCharWidth();
38     assertEquals(charHeight, 17);
39     assertEquals(charWidth, 12);
40
41     SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
42
43     /*
44      * first with scales above, left, right
45      */
46     av.setShowAnnotation(false);
47     av.setScaleAboveWrapped(true);
48     av.setScaleLeftWrapped(true);
49     av.setScaleRightWrapped(true);
50     FontMetrics fm = SwingUtilities2.getFontMetrics(testee, av.getFont());
51     int labelWidth = fm.stringWidth("000") + charWidth;
52     assertEquals(labelWidth, 39); // 3 x 9 + charWidth
53
54     /*
55      * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
56      * take the whole multiple of character widths
57      */
58     int canvasWidth = 400;
59     int canvasHeight = 300;
60     int residueColumns = (canvasWidth - 2 * labelWidth) / charWidth;
61     int wrappedWidth = testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
62     assertEquals(wrappedWidth, residueColumns);
63     assertEquals(PA.getValue(testee, "labelWidthWest"), labelWidth);
64     assertEquals(PA.getValue(testee, "labelWidthEast"), labelWidth);
65     assertEquals(PA.getValue(testee, "wrappedSpaceAboveAlignment"),
66             2 * charHeight);
67     int repeatingHeight = (int) PA.getValue(testee, "wrappedRepeatHeightPx");
68     assertEquals(repeatingHeight, charHeight * (2 + al.getHeight()));
69     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
70
71     /*
72      * repeat height is 17 * (2 + 15) = 289
73      * make canvas height 2 * 289 + 3 * charHeight so just enough to
74      * draw 2 widths and the first sequence of a third
75      */
76     canvasHeight = charHeight * (17 * 2 + 3);
77     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
78     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
79
80     /*
81      * reduce canvas height by 1 pixel - should not be enough height
82      * to draw 3 widths
83      */
84     canvasHeight -= 1;
85     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
86     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
87
88     /*
89      * turn off scale above - can now fit in 2 and a bit widths
90      */
91     av.setScaleAboveWrapped(false);
92     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
93     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
94
95     /*
96      * reduce height to enough for 2 widths and not quite a third
97      * i.e. two repeating heights + spacer + sequence - 1 pixel
98      */
99     canvasHeight = charHeight * (16 * 2 + 2) - 1;
100     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
101     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
102
103     /*
104      * make canvas width enough for scales and 20 residues
105      */
106     canvasWidth = 2 * labelWidth + 20 * charWidth;
107     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
108             canvasHeight);
109     assertEquals(wrappedWidth, 20);
110
111     /*
112      * reduce width by 1 pixel - rounds down to 19 residues
113      */
114     canvasWidth -= 1;
115     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
116             canvasHeight);
117     assertEquals(wrappedWidth, 19);
118
119     /*
120      * turn off West scale - adds labelWidth (39) to available for residues
121      * which with the 11 remainder makes 50 which is 4 more charWidths rem 2
122      */
123     av.setScaleLeftWrapped(false);
124     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
125             canvasHeight);
126     assertEquals(wrappedWidth, 23);
127
128     /*
129      * add 10 pixels to width to fit in another whole residue column
130      */
131     canvasWidth += 9;
132     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
133             canvasHeight);
134     assertEquals(wrappedWidth, 23);
135     canvasWidth += 1;
136     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
137             canvasHeight);
138     assertEquals(wrappedWidth, 24);
139
140     /*
141      * turn off East scale to gain 39 more pixels (3 columns remainder 3)
142      */
143     av.setScaleRightWrapped(false);
144     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
145             canvasHeight);
146     assertEquals(wrappedWidth, 27);
147
148     /*
149      * add 9 pixels to width to gain a residue column
150      */
151     canvasWidth += 8;
152     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
153             canvasHeight);
154     assertEquals(wrappedWidth, 27);
155     canvasWidth += 1;
156     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
157             canvasHeight);
158     assertEquals(wrappedWidth, 28);
159
160     /*
161      * now West but not East scale - lose 39 pixels or 4 columns
162      */
163     av.setScaleLeftWrapped(true);
164     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
165             canvasHeight);
166     assertEquals(wrappedWidth, 24);
167
168     /*
169      * adding 3 pixels to width regains one column
170      */
171     canvasWidth += 2;
172     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
173             canvasHeight);
174     assertEquals(wrappedWidth, 24);
175     canvasWidth += 1;
176     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
177             canvasHeight);
178     assertEquals(wrappedWidth, 25);
179
180     /*
181      * turn off scales left and right, make width exactly 157 columns
182      */
183     av.setScaleLeftWrapped(false);
184     canvasWidth = al.getWidth() * charWidth;
185     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
186     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
187   }
188
189   /**
190    * Test the method that computes wrapped width in residues, height of wrapped
191    * widths in pixels, and the number of widths visible
192    */
193   @Test(groups = "Functional")
194   public void testCalculateWrappedGeometry_withAnnotations()
195   {
196     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
197             "examples/uniref50.fa", DataSourceType.FILE);
198     AlignViewport av = af.getViewport();
199     AlignmentI al = av.getAlignment();
200     assertEquals(al.getWidth(), 157);
201     assertEquals(al.getHeight(), 15);
202   
203     av.setWrapAlignment(true);
204     av.setFont(new Font("SansSerif", Font.PLAIN, 14), true);
205     int charHeight = av.getCharHeight();
206     int charWidth = av.getCharWidth();
207     assertEquals(charHeight, 17);
208     assertEquals(charWidth, 12);
209   
210     SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
211   
212     /*
213      * first with scales above, left, right
214      */
215     av.setShowAnnotation(true);
216     av.setScaleAboveWrapped(true);
217     av.setScaleLeftWrapped(true);
218     av.setScaleRightWrapped(true);
219     FontMetrics fm = SwingUtilities2.getFontMetrics(testee, av.getFont());
220     int labelWidth = fm.stringWidth("000") + charWidth;
221     assertEquals(labelWidth, 39); // 3 x 9 + charWidth
222     int annotationHeight = testee.getAnnotationHeight();
223
224     /*
225      * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
226      * take the whole multiple of character widths
227      */
228     int canvasWidth = 400;
229     int canvasHeight = 300;
230     int residueColumns = (canvasWidth - 2 * labelWidth) / charWidth;
231     int wrappedWidth = testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
232     assertEquals(wrappedWidth, residueColumns);
233     assertEquals(PA.getValue(testee, "labelWidthWest"), labelWidth);
234     assertEquals(PA.getValue(testee, "labelWidthEast"), labelWidth);
235     assertEquals(PA.getValue(testee, "wrappedSpaceAboveAlignment"),
236             2 * charHeight);
237     int repeatingHeight = (int) PA.getValue(testee, "wrappedRepeatHeightPx");
238     assertEquals(repeatingHeight, charHeight * (2 + al.getHeight())
239             + annotationHeight);
240     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
241   
242     /*
243      * repeat height is 17 * (2 + 15) = 289 + annotationHeight = 507
244      * make canvas height 2 * 289 + 3 * charHeight so just enough to
245      * draw 2 widths and the first sequence of a third
246      */
247     canvasHeight = charHeight * (17 * 2 + 3) + 2 * annotationHeight;
248     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
249     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
250   
251     /*
252      * reduce canvas height by 1 pixel - should not be enough height
253      * to draw 3 widths
254      */
255     canvasHeight -= 1;
256     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
257     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
258   
259     /*
260      * turn off scale above - can now fit in 2 and a bit widths
261      */
262     av.setScaleAboveWrapped(false);
263     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
264     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
265   
266     /*
267      * reduce height to enough for 2 widths and not quite a third
268      * i.e. two repeating heights + spacer + sequence - 1 pixel
269      */
270     canvasHeight = charHeight * (16 * 2 + 2) + 2 * annotationHeight - 1;
271     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
272     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
273
274     /*
275      * add 1 pixel to height - should now get 3 widths drawn
276      */
277     canvasHeight += 1;
278     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
279     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
280   }
281 }