2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import static org.testng.Assert.assertEquals;
25 import jalview.bin.Cache;
26 import jalview.bin.Jalview;
27 import jalview.datamodel.AlignmentI;
28 import jalview.io.DataSourceType;
29 import jalview.io.FileLoader;
30 import jalview.util.Platform;
33 import java.awt.FontMetrics;
35 import org.testng.Assert;
36 import org.testng.annotations.BeforeClass;
37 import org.testng.annotations.Test;
39 import junit.extensions.PA;
41 public class SeqCanvasTest
43 @BeforeClass(alwaysRun = true)
47 Jalview.setSynchronous(true);
51 * Test the method that computes wrapped width in residues, height of wrapped
52 * widths in pixels, and the number of widths visible
54 @Test(groups = "Functional")
55 public void testCalculateWrappedGeometry_noAnnotations()
57 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
58 "examples/uniref50.fa", DataSourceType.FILE);
59 AlignViewport av = af.getViewport();
60 AlignmentI al = av.getAlignment();
61 assertEquals(al.getWidth(), 157);
62 assertEquals(al.getHeight(), 15);
63 av.getRanges().setStartEndSeq(0, 14);
65 SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
67 av.setWrapAlignment(true);
68 av.setFont(new Font("SansSerif", Font.PLAIN, 14), true);
69 int charHeight = av.getCharHeight();
70 int charWidth = av.getCharWidth();
71 Assert.assertTrue(charHeight == 17 && charWidth == 12
72 || charHeight == 19 && charWidth == 11,
73 "char height/width " + charHeight + "/" + charWidth);
75 // assertEquals(charHeight, Platform.isMac() ? 17 : 19);
76 // assertEquals(charWidth, Platform.isMac() ? 12 : 11);
79 * first with scales above, left, right
81 av.setShowAnnotation(false);
82 av.setScaleAboveWrapped(true);
83 av.setScaleLeftWrapped(true);
84 av.setScaleRightWrapped(true);
85 FontMetrics fm = testee.getFontMetrics(av.getFont());
86 int labelWidth = fm.stringWidth("000") + charWidth;
87 // BH 2020.03.22 It is not really necessary to be this detailed. Different
89 // always have slightly different parameters. StringgWidths are not
90 // necessarily linear sums of the letters involved.
91 // for example, the calculation for JavaScript is a float that has to be
93 // ..............................mac................PC................linux?
95 labelWidth == 39 || labelWidth == 35 || labelWidth == 36);// 3 * 9 +
107 * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
108 * take the whole multiple of character widths
110 int canvasWidth = 400;
111 int canvasHeight = 300;
112 int residueColumns = (canvasWidth - 2 * labelWidth) / charWidth;
113 int wrappedWidth = testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
114 assertEquals(wrappedWidth, residueColumns);
115 assertEquals(PA.getValue(testee, "labelWidthWest"), labelWidth);
116 assertEquals(PA.getValue(testee, "labelWidthEast"), labelWidth);
117 assertEquals(PA.getValue(testee, "wrappedSpaceAboveAlignment"),
119 int repeatingHeight = (int) PA.getValue(testee, "wrappedRepeatHeightPx");
120 assertEquals(repeatingHeight, charHeight * (2 + al.getHeight()));
121 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
124 * repeat height is 17 * (2 + 15) = 289
125 * make canvas height 2 * 289 + 3 * charHeight so just enough to
126 * draw 2 widths and the first sequence of a third
128 canvasHeight = charHeight * (17 * 2 + 3);
129 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
130 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
133 * reduce canvas height by 1 pixel
134 * - should not be enough height to draw 3 widths
137 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
138 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
141 * turn off scale above - can now fit in 2 and a bit widths
143 av.setScaleAboveWrapped(false);
144 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
145 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
148 * reduce height to enough for 2 widths and not quite a third
149 * i.e. two repeating heights + spacer + sequence - 1 pixel
151 canvasHeight = charHeight * (16 * 2 + 2) - 1;
152 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
153 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
156 * make canvas width enough for scales and 20 residues
158 canvasWidth = 2 * labelWidth + 20 * charWidth;
159 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
161 assertEquals(wrappedWidth, 20);
164 * reduce width by 1 pixel - rounds down to 19 residues
167 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
169 assertEquals(wrappedWidth, 19);
172 * turn off West scale - adds labelWidth (39) to available for residues
173 * which with the 11 remainder makes 50 which is 4 more charWidths rem 2
175 av.setScaleLeftWrapped(false);
176 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
178 assertEquals(wrappedWidth, 23);
181 * add 10 pixels to width to fit in another whole residue column
184 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
186 assertEquals(wrappedWidth, 23);
188 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
190 assertEquals(wrappedWidth, 24);
193 * turn off East scale to gain 39 more pixels (3 columns remainder 3)
195 av.setScaleRightWrapped(false);
196 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
198 assertEquals(wrappedWidth, 27);
201 * add 9 pixels to width to gain a residue column
204 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
206 assertEquals(wrappedWidth, 27); // 8px not enough
208 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
210 assertEquals(wrappedWidth, 28); // 9px is enough
213 * now West but not East scale - lose 39 pixels or 4 columns
215 av.setScaleLeftWrapped(true);
216 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
218 assertEquals(wrappedWidth, 24);
221 * adding 3 pixels to width regains one column
224 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
226 assertEquals(wrappedWidth, Platform.isMac() ? 24 : 25); // 2px not enough
228 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
230 assertEquals(wrappedWidth, 25); // 3px is enough
233 * turn off scales left and right, make width exactly 157 columns
235 av.setScaleLeftWrapped(false);
236 canvasWidth = al.getWidth() * charWidth;
237 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
238 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
242 * Test the method that computes wrapped width in residues, height of wrapped
243 * widths in pixels, and the number of widths visible
245 @Test(groups = "Functional")
246 public void testCalculateWrappedGeometry_withAnnotations()
248 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
249 "examples/uniref50.fa", DataSourceType.FILE);
250 AlignViewport av = af.getViewport();
251 AlignmentI al = av.getAlignment();
252 assertEquals(al.getWidth(), 157);
253 assertEquals(al.getHeight(), 15);
255 av.setWrapAlignment(true);
256 av.getRanges().setStartEndSeq(0, 14);
257 av.setFont(new Font("SansSerif", Font.PLAIN, 14), true);
258 int charHeight = av.getCharHeight();
259 int charWidth = av.getCharWidth();
262 charHeight == 17 && charWidth == 12
263 || charHeight == 19 && charWidth == 11,
264 "char height/width " + charHeight + "/" + charWidth);
265 // assertEquals(charHeight, Platform.isMac() ? 17 : 19);
266 // assertEquals(charWidth, Platform.isMac() ? 12 : 11);
268 SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
271 * first with scales above, left, right
273 av.setShowAnnotation(true);
274 av.setScaleAboveWrapped(true);
275 av.setScaleLeftWrapped(true);
276 av.setScaleRightWrapped(true);
278 FontMetrics fm = testee.getFontMetrics(av.getFont());
279 int labelWidth = fm.stringWidth("000") + charWidth;
280 // BH 2020.03.22 It is not really necessary to be this detailed. Different
282 // always have slightly different parameters. StringgWidths are not
283 // necessarily linear sums of the letters involved.
284 // for example, the calculation for JavaScript is a float that has to be
286 // ..............................mac................PC................linux?
288 labelWidth == 39 || labelWidth == 35 || labelWidth == 36);// 3 * 9 +
298 // int labelWidth = fm.stringWidth("000") + charWidth;
299 // assertEquals(labelWidth,
300 // Platform.isMac() ? 3 * 9 + charWidth : 3 * 8 + charWidth);
302 int annotationHeight = testee.getAnnotationHeight();
305 * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
306 * take the whole multiple of character widths
308 int canvasWidth = 400;
309 int canvasHeight = 300;
310 int residueColumns = (canvasWidth - 2 * labelWidth) / charWidth;
311 int wrappedWidth = testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
312 assertEquals(wrappedWidth, residueColumns);
313 assertEquals(PA.getValue(testee, "labelWidthWest"), labelWidth);
314 assertEquals(PA.getValue(testee, "labelWidthEast"), labelWidth);
315 assertEquals(PA.getValue(testee, "wrappedSpaceAboveAlignment"),
317 int repeatingHeight = (int) PA.getValue(testee, "wrappedRepeatHeightPx");
318 assertEquals(repeatingHeight, charHeight * (2 + al.getHeight())
319 + SeqCanvas.SEQS_ANNOTATION_GAP + annotationHeight);
320 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
323 * repeat height is 17 * (2 + 15) = 289 + 3 + annotationHeight = 510
324 * make canvas height 2 of these plus 3 charHeights
325 * so just enough to draw 2 widths, gap + scale + the first sequence of a third
327 canvasHeight = charHeight * (17 * 2 + 3)
328 + 2 * (annotationHeight + SeqCanvas.SEQS_ANNOTATION_GAP);
329 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
330 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
333 * reduce canvas height by 1 pixel - should not be enough height
337 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
338 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
341 * turn off scale above - can now fit in 2 and a bit widths
343 av.setScaleAboveWrapped(false);
344 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
345 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
348 * reduce height to enough for 2 widths and not quite a third
349 * i.e. two repeating heights + spacer + sequence - 1 pixel
351 canvasHeight = charHeight * (16 * 2 + 2)
352 + 2 * (annotationHeight + SeqCanvas.SEQS_ANNOTATION_GAP) - 1;
353 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
354 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
357 * add 1 pixel to height - should now get 3 widths drawn
360 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
361 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
365 * Test simulates loading an unwrapped alignment, shrinking it vertically so
366 * not all sequences are visible, then changing to wrapped mode. The ranges
367 * endSeq should be unchanged, but the vertical repeat height should include
370 @Test(groups = "Functional")
371 public void testCalculateWrappedGeometry_fromScrolled()
373 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
374 "examples/uniref50.fa", DataSourceType.FILE);
375 AlignViewport av = af.getViewport();
376 AlignmentI al = av.getAlignment();
377 assertEquals(al.getWidth(), 157);
378 assertEquals(al.getHeight(), 15);
379 av.getRanges().setStartEndSeq(0, 3);
380 av.setShowAnnotation(false);
381 av.setScaleAboveWrapped(true);
383 SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
384 av.setWrapAlignment(true);
385 av.setFont(new Font("SansSerif", Font.PLAIN, 14), true);
386 int charHeight = av.getCharHeight();
387 int charWidth = av.getCharWidth();
388 // Windows h=19, w=11; Mac (and Linux?) 17,11
389 Assert.assertTrue(charHeight == 17 && charWidth == 12
390 || charHeight == 19 && charWidth == 11,
391 "char height/width " + charHeight + "/" + charWidth);
392 int canvasWidth = 400;
393 int canvasHeight = 300;
394 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
395 int repeatingHeight = (int) PA.getValue(testee,
396 "wrappedRepeatHeightPx");
397 assertEquals(av.getRanges().getEndSeq(), 3,
398 "endSeq should be 3; av.getRanges=" + av.getRanges()); // unchanged
399 assertEquals(repeatingHeight, charHeight * (2 + al.getHeight()));