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 import sun.swing.SwingUtilities2;
18 public class SeqCanvasTest
21 * Test the method that computes wrapped width in residues, height of wrapped
22 * widths in pixels, and the number of widths visible
24 @Test(groups = "Functional")
25 public void testCalculateWrappedGeometry_noAnnotations()
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);
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);
42 SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
45 * first with scales above, left, right
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
56 * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
57 * take the whole multiple of character widths
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"),
68 int repeatingHeight = (int) PA.getValue(testee, "wrappedRepeatHeightPx");
69 assertEquals(repeatingHeight, charHeight * (2 + al.getHeight()));
70 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
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
77 canvasHeight = charHeight * (17 * 2 + 3);
78 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
79 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
82 * reduce canvas height by 1 pixel - should not be enough height
86 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
87 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
90 * turn off scale above - can now fit in 2 and a bit widths
92 av.setScaleAboveWrapped(false);
93 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
94 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
97 * reduce height to enough for 2 widths and not quite a third
98 * i.e. two repeating heights + spacer + sequence - 1 pixel
100 canvasHeight = charHeight * (16 * 2 + 2) - 1;
101 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
102 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
105 * make canvas width enough for scales and 20 residues
107 canvasWidth = 2 * labelWidth + 20 * charWidth;
108 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
110 assertEquals(wrappedWidth, 20);
113 * reduce width by 1 pixel - rounds down to 19 residues
116 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
118 assertEquals(wrappedWidth, 19);
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
124 av.setScaleLeftWrapped(false);
125 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
127 assertEquals(wrappedWidth, 23);
130 * add 10 pixels to width to fit in another whole residue column
133 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
135 assertEquals(wrappedWidth, 23);
137 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
139 assertEquals(wrappedWidth, 24);
142 * turn off East scale to gain 39 more pixels (3 columns remainder 3)
144 av.setScaleRightWrapped(false);
145 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
147 assertEquals(wrappedWidth, 27);
150 * add 9 pixels to width to gain a residue column
153 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
155 assertEquals(wrappedWidth, 27);
157 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
159 assertEquals(wrappedWidth, 28);
162 * now West but not East scale - lose 39 pixels or 4 columns
164 av.setScaleLeftWrapped(true);
165 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
167 assertEquals(wrappedWidth, 24);
170 * adding 3 pixels to width regains one column
173 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
175 assertEquals(wrappedWidth, 24);
177 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
179 assertEquals(wrappedWidth, 25);
182 * turn off scales left and right, make width exactly 157 columns
184 av.setScaleLeftWrapped(false);
185 canvasWidth = al.getWidth() * charWidth;
186 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
187 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
191 * Test the method that computes wrapped width in residues, height of wrapped
192 * widths in pixels, and the number of widths visible
194 @Test(groups = "Functional")
195 public void testCalculateWrappedGeometry_withAnnotations()
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);
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);
212 SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
215 * first with scales above, left, right
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();
227 * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
228 * take the whole multiple of character widths
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"),
239 int repeatingHeight = (int) PA.getValue(testee, "wrappedRepeatHeightPx");
240 assertEquals(repeatingHeight, charHeight * (2 + al.getHeight())
242 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
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
249 canvasHeight = charHeight * (17 * 2 + 3) + 2 * annotationHeight;
250 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
251 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
254 * reduce canvas height by 1 pixel - should not be enough height
258 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
259 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
262 * turn off scale above - can now fit in 2 and a bit widths
264 av.setScaleAboveWrapped(false);
265 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
266 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
269 * reduce height to enough for 2 widths and not quite a third
270 * i.e. two repeating heights + spacer + sequence - 1 pixel
272 canvasHeight = charHeight * (16 * 2 + 2) + 2 * annotationHeight - 1;
273 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
274 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
277 * add 1 pixel to height - should now get 3 widths drawn
280 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
281 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);