JAL-2754 reinstate PA import from bad conflict merge
[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 public class SeqCanvasTest
17 {
18   /**
19    * Test the method that computes wrapped width in residues, height of wrapped
20    * widths in pixels, and the number of widths visible
21    */
22   @Test(groups = "Functional")
23   public void testCalculateWrappedGeometry_noAnnotations()
24   {
25     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
26             "examples/uniref50.fa", DataSourceType.FILE);
27     AlignViewport av = af.getViewport();
28     AlignmentI al = av.getAlignment();
29     assertEquals(al.getWidth(), 157);
30     assertEquals(al.getHeight(), 15);
31
32     av.setWrapAlignment(true);
33     av.getRanges().setStartEndSeq(0, 14);
34     av.setFont(new Font("SansSerif", Font.PLAIN, 14), true);
35     int charHeight = av.getCharHeight();
36     int charWidth = av.getCharWidth();
37     assertEquals(charHeight, 17);
38     assertEquals(charWidth, 12);
39
40     SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
41
42     /*
43      * first with scales above, left, right
44      */
45     av.setShowAnnotation(false);
46     av.setScaleAboveWrapped(true);
47     av.setScaleLeftWrapped(true);
48     av.setScaleRightWrapped(true);
49     FontMetrics fm = testee.getFontMetrics(av.getFont());
50     int labelWidth = fm.stringWidth("000") + charWidth;
51     assertEquals(labelWidth, 39); // 3 x 9 + charWidth
52
53     /*
54      * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
55      * take the whole multiple of character widths
56      */
57     int canvasWidth = 400;
58     int canvasHeight = 300;
59     int residueColumns = (canvasWidth - 2 * labelWidth) / charWidth;
60     int wrappedWidth = testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
61     assertEquals(wrappedWidth, residueColumns);
62     assertEquals(PA.getValue(testee, "labelWidthWest"), labelWidth);
63     assertEquals(PA.getValue(testee, "labelWidthEast"), labelWidth);
64     assertEquals(PA.getValue(testee, "wrappedSpaceAboveAlignment"),
65             2 * charHeight);
66     int repeatingHeight = (int) PA.getValue(testee, "wrappedRepeatHeightPx");
67     assertEquals(repeatingHeight, charHeight * (2 + al.getHeight()));
68     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
69
70     /*
71      * repeat height is 17 * (2 + 15) = 289
72      * make canvas height 2 * 289 + 3 * charHeight so just enough to
73      * draw 2 widths and the first sequence of a third
74      */
75     canvasHeight = charHeight * (17 * 2 + 3);
76     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
77     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
78
79     /*
80      * reduce canvas height by 1 pixel - should not be enough height
81      * to draw 3 widths
82      */
83     canvasHeight -= 1;
84     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
85     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
86
87     /*
88      * turn off scale above - can now fit in 2 and a bit widths
89      */
90     av.setScaleAboveWrapped(false);
91     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
92     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
93
94     /*
95      * reduce height to enough for 2 widths and not quite a third
96      * i.e. two repeating heights + spacer + sequence - 1 pixel
97      */
98     canvasHeight = charHeight * (16 * 2 + 2) - 1;
99     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
100     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
101
102     /*
103      * make canvas width enough for scales and 20 residues
104      */
105     canvasWidth = 2 * labelWidth + 20 * charWidth;
106     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
107             canvasHeight);
108     assertEquals(wrappedWidth, 20);
109
110     /*
111      * reduce width by 1 pixel - rounds down to 19 residues
112      */
113     canvasWidth -= 1;
114     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
115             canvasHeight);
116     assertEquals(wrappedWidth, 19);
117
118     /*
119      * turn off West scale - adds labelWidth (39) to available for residues
120      * which with the 11 remainder makes 50 which is 4 more charWidths rem 2
121      */
122     av.setScaleLeftWrapped(false);
123     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
124             canvasHeight);
125     assertEquals(wrappedWidth, 23);
126
127     /*
128      * add 10 pixels to width to fit in another whole residue column
129      */
130     canvasWidth += 9;
131     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
132             canvasHeight);
133     assertEquals(wrappedWidth, 23);
134     canvasWidth += 1;
135     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
136             canvasHeight);
137     assertEquals(wrappedWidth, 24);
138
139     /*
140      * turn off East scale to gain 39 more pixels (3 columns remainder 3)
141      */
142     av.setScaleRightWrapped(false);
143     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
144             canvasHeight);
145     assertEquals(wrappedWidth, 27);
146
147     /*
148      * add 9 pixels to width to gain a residue column
149      */
150     canvasWidth += 8;
151     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
152             canvasHeight);
153     assertEquals(wrappedWidth, 27);
154     canvasWidth += 1;
155     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
156             canvasHeight);
157     assertEquals(wrappedWidth, 28);
158
159     /*
160      * now West but not East scale - lose 39 pixels or 4 columns
161      */
162     av.setScaleLeftWrapped(true);
163     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
164             canvasHeight);
165     assertEquals(wrappedWidth, 24);
166
167     /*
168      * adding 3 pixels to width regains one column
169      */
170     canvasWidth += 2;
171     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
172             canvasHeight);
173     assertEquals(wrappedWidth, 24);
174     canvasWidth += 1;
175     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
176             canvasHeight);
177     assertEquals(wrappedWidth, 25);
178
179     /*
180      * turn off scales left and right, make width exactly 157 columns
181      */
182     av.setScaleLeftWrapped(false);
183     canvasWidth = al.getWidth() * charWidth;
184     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
185     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
186   }
187
188   /**
189    * Test the method that computes wrapped width in residues, height of wrapped
190    * widths in pixels, and the number of widths visible
191    */
192   @Test(groups = "Functional")
193   public void testCalculateWrappedGeometry_withAnnotations()
194   {
195     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
196             "examples/uniref50.fa", DataSourceType.FILE);
197     AlignViewport av = af.getViewport();
198     AlignmentI al = av.getAlignment();
199     assertEquals(al.getWidth(), 157);
200     assertEquals(al.getHeight(), 15);
201   
202     av.setWrapAlignment(true);
203     av.getRanges().setStartEndSeq(0, 14);
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 = testee.getFontMetrics(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 }