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