3 import static org.testng.Assert.assertEquals;
5 import jalview.datamodel.AlignmentI;
6 import jalview.io.DataSourceType;
7 import jalview.io.FileLoader;
10 import java.awt.FontMetrics;
12 import junit.extensions.PA;
14 import org.testng.annotations.Test;
16 public class SeqCanvasTest
19 * Test the method that computes wrapped width in residues, height of wrapped
20 * widths in pixels, and the number of widths visible
22 @Test(groups = "Functional")
23 public void testCalculateWrappedGeometry_noAnnotations()
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);
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);
40 SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
43 * first with scales above, left, right
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
54 * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
55 * take the whole multiple of character widths
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"),
66 int repeatingHeight = (int) PA.getValue(testee, "wrappedRepeatHeightPx");
67 assertEquals(repeatingHeight, charHeight * (2 + al.getHeight()));
68 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
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
75 canvasHeight = charHeight * (17 * 2 + 3);
76 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
77 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
80 * reduce canvas height by 1 pixel - should not be enough height
84 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
85 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
88 * turn off scale above - can now fit in 2 and a bit widths
90 av.setScaleAboveWrapped(false);
91 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
92 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
95 * reduce height to enough for 2 widths and not quite a third
96 * i.e. two repeating heights + spacer + sequence - 1 pixel
98 canvasHeight = charHeight * (16 * 2 + 2) - 1;
99 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
100 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
103 * make canvas width enough for scales and 20 residues
105 canvasWidth = 2 * labelWidth + 20 * charWidth;
106 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
108 assertEquals(wrappedWidth, 20);
111 * reduce width by 1 pixel - rounds down to 19 residues
114 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
116 assertEquals(wrappedWidth, 19);
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
122 av.setScaleLeftWrapped(false);
123 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
125 assertEquals(wrappedWidth, 23);
128 * add 10 pixels to width to fit in another whole residue column
131 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
133 assertEquals(wrappedWidth, 23);
135 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
137 assertEquals(wrappedWidth, 24);
140 * turn off East scale to gain 39 more pixels (3 columns remainder 3)
142 av.setScaleRightWrapped(false);
143 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
145 assertEquals(wrappedWidth, 27);
148 * add 9 pixels to width to gain a residue column
151 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
153 assertEquals(wrappedWidth, 27);
155 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
157 assertEquals(wrappedWidth, 28);
160 * now West but not East scale - lose 39 pixels or 4 columns
162 av.setScaleLeftWrapped(true);
163 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
165 assertEquals(wrappedWidth, 24);
168 * adding 3 pixels to width regains one column
171 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
173 assertEquals(wrappedWidth, 24);
175 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
177 assertEquals(wrappedWidth, 25);
180 * turn off scales left and right, make width exactly 157 columns
182 av.setScaleLeftWrapped(false);
183 canvasWidth = al.getWidth() * charWidth;
184 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
185 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
189 * Test the method that computes wrapped width in residues, height of wrapped
190 * widths in pixels, and the number of widths visible
192 @Test(groups = "Functional")
193 public void testCalculateWrappedGeometry_withAnnotations()
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);
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);
210 SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
213 * first with scales above, left, right
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();
225 * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
226 * take the whole multiple of character widths
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"),
237 int repeatingHeight = (int) PA.getValue(testee, "wrappedRepeatHeightPx");
238 assertEquals(repeatingHeight, charHeight * (2 + al.getHeight())
240 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
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
247 canvasHeight = charHeight * (17 * 2 + 3) + 2 * annotationHeight;
248 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
249 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
252 * reduce canvas height by 1 pixel - should not be enough height
256 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
257 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
260 * turn off scale above - can now fit in 2 and a bit widths
262 av.setScaleAboveWrapped(false);
263 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
264 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
267 * reduce height to enough for 2 widths and not quite a third
268 * i.e. two repeating heights + spacer + sequence - 1 pixel
270 canvasHeight = charHeight * (16 * 2 + 2) + 2 * annotationHeight - 1;
271 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
272 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
275 * add 1 pixel to height - should now get 3 widths drawn
278 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
279 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);