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