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.datamodel.AlignmentI;
26 import jalview.io.DataSourceType;
27 import jalview.io.FileLoader;
30 import java.awt.FontMetrics;
32 import org.testng.annotations.Test;
34 import junit.extensions.PA;
36 public class SeqCanvasTest
39 * Test the method that computes wrapped width in residues, height of wrapped
40 * widths in pixels, and the number of widths visible
42 @Test(groups = "Functional")
43 public void testCalculateWrappedGeometry_noAnnotations()
45 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
46 "examples/uniref50.fa", DataSourceType.FILE);
47 AlignViewport av = af.getViewport();
48 AlignmentI al = av.getAlignment();
49 assertEquals(al.getWidth(), 157);
50 assertEquals(al.getHeight(), 15);
51 av.getRanges().setStartEndSeq(0, 14);
53 SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
55 av.setWrapAlignment(true);
56 av.setFont(new Font("SansSerif", Font.PLAIN, 14), true);
57 int charHeight = av.getCharHeight();
58 int charWidth = av.getCharWidth();
59 assertEquals(charHeight, 17);
60 assertEquals(charWidth, 12);
63 * first with scales above, left, right
65 av.setShowAnnotation(false);
66 av.setScaleAboveWrapped(true);
67 av.setScaleLeftWrapped(true);
68 av.setScaleRightWrapped(true);
69 FontMetrics fm = testee.getFontMetrics(av.getFont());
70 int labelWidth = fm.stringWidth("000") + charWidth;
71 assertEquals(labelWidth, 39); // 3 x 9 + charWidth
74 * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
75 * take the whole multiple of character widths
77 int canvasWidth = 400;
78 int canvasHeight = 300;
79 int residueColumns = (canvasWidth - 2 * labelWidth) / charWidth;
80 int wrappedWidth = testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
81 assertEquals(wrappedWidth, residueColumns);
82 assertEquals(PA.getValue(testee, "labelWidthWest"), labelWidth);
83 assertEquals(PA.getValue(testee, "labelWidthEast"), labelWidth);
84 assertEquals(PA.getValue(testee, "wrappedSpaceAboveAlignment"),
86 int repeatingHeight = (int) PA.getValue(testee, "wrappedRepeatHeightPx");
87 assertEquals(repeatingHeight, charHeight * (2 + al.getHeight()));
88 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
91 * repeat height is 17 * (2 + 15) = 289
92 * make canvas height 2 * 289 + 3 * charHeight so just enough to
93 * draw 2 widths and the first sequence of a third
95 canvasHeight = charHeight * (17 * 2 + 3);
96 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
97 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
100 * reduce canvas height by 1 pixel - should not be enough height
104 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
105 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
108 * turn off scale above - can now fit in 2 and a bit widths
110 av.setScaleAboveWrapped(false);
111 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
112 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
115 * reduce height to enough for 2 widths and not quite a third
116 * i.e. two repeating heights + spacer + sequence - 1 pixel
118 canvasHeight = charHeight * (16 * 2 + 2) - 1;
119 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
120 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
123 * make canvas width enough for scales and 20 residues
125 canvasWidth = 2 * labelWidth + 20 * charWidth;
126 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
128 assertEquals(wrappedWidth, 20);
131 * reduce width by 1 pixel - rounds down to 19 residues
134 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
136 assertEquals(wrappedWidth, 19);
139 * turn off West scale - adds labelWidth (39) to available for residues
140 * which with the 11 remainder makes 50 which is 4 more charWidths rem 2
142 av.setScaleLeftWrapped(false);
143 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
145 assertEquals(wrappedWidth, 23);
148 * add 10 pixels to width to fit in another whole residue column
151 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
153 assertEquals(wrappedWidth, 23);
155 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
157 assertEquals(wrappedWidth, 24);
160 * turn off East scale to gain 39 more pixels (3 columns remainder 3)
162 av.setScaleRightWrapped(false);
163 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
165 assertEquals(wrappedWidth, 27);
168 * add 9 pixels to width to gain a residue column
171 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
173 assertEquals(wrappedWidth, 27);
175 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
177 assertEquals(wrappedWidth, 28);
180 * now West but not East scale - lose 39 pixels or 4 columns
182 av.setScaleLeftWrapped(true);
183 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
185 assertEquals(wrappedWidth, 24);
188 * adding 3 pixels to width regains one column
191 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
193 assertEquals(wrappedWidth, 24);
195 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
197 assertEquals(wrappedWidth, 25);
200 * turn off scales left and right, make width exactly 157 columns
202 av.setScaleLeftWrapped(false);
203 canvasWidth = al.getWidth() * charWidth;
204 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
205 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
209 * Test the method that computes wrapped width in residues, height of wrapped
210 * widths in pixels, and the number of widths visible
212 @Test(groups = "Functional")
213 public void testCalculateWrappedGeometry_withAnnotations()
215 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
216 "examples/uniref50.fa", DataSourceType.FILE);
217 AlignViewport av = af.getViewport();
218 AlignmentI al = av.getAlignment();
219 assertEquals(al.getWidth(), 157);
220 assertEquals(al.getHeight(), 15);
222 av.setWrapAlignment(true);
223 av.getRanges().setStartEndSeq(0, 14);
224 av.setFont(new Font("SansSerif", Font.PLAIN, 14), true);
225 int charHeight = av.getCharHeight();
226 int charWidth = av.getCharWidth();
227 assertEquals(charHeight, 17);
228 assertEquals(charWidth, 12);
230 SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
233 * first with scales above, left, right
235 av.setShowAnnotation(true);
236 av.setScaleAboveWrapped(true);
237 av.setScaleLeftWrapped(true);
238 av.setScaleRightWrapped(true);
239 FontMetrics fm = testee.getFontMetrics(av.getFont());
240 int labelWidth = fm.stringWidth("000") + charWidth;
241 assertEquals(labelWidth, 39); // 3 x 9 + charWidth
242 int annotationHeight = testee.getAnnotationHeight();
245 * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
246 * take the whole multiple of character widths
248 int canvasWidth = 400;
249 int canvasHeight = 300;
250 int residueColumns = (canvasWidth - 2 * labelWidth) / charWidth;
251 int wrappedWidth = testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
252 assertEquals(wrappedWidth, residueColumns);
253 assertEquals(PA.getValue(testee, "labelWidthWest"), labelWidth);
254 assertEquals(PA.getValue(testee, "labelWidthEast"), labelWidth);
255 assertEquals(PA.getValue(testee, "wrappedSpaceAboveAlignment"),
257 int repeatingHeight = (int) PA.getValue(testee, "wrappedRepeatHeightPx");
258 assertEquals(repeatingHeight, charHeight * (2 + al.getHeight())
260 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
263 * repeat height is 17 * (2 + 15) = 289 + annotationHeight = 507
264 * make canvas height 2 * 289 + 3 * charHeight so just enough to
265 * draw 2 widths and the first sequence of a third
267 canvasHeight = charHeight * (17 * 2 + 3) + 2 * annotationHeight;
268 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
269 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
272 * reduce canvas height by 1 pixel - should not be enough height
276 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
277 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
280 * turn off scale above - can now fit in 2 and a bit widths
282 av.setScaleAboveWrapped(false);
283 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
284 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
287 * reduce height to enough for 2 widths and not quite a third
288 * i.e. two repeating heights + spacer + sequence - 1 pixel
290 canvasHeight = charHeight * (16 * 2 + 2) + 2 * annotationHeight - 1;
291 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
292 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
295 * add 1 pixel to height - should now get 3 widths drawn
298 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
299 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
303 * Test simulates loading an unwrapped alignment, shrinking it vertically so
304 * not all sequences are visible, then changing to wrapped mode. The ranges
305 * endSeq should be unchanged, but the vertical repeat height should include
308 @Test(groups = "Functional")
309 public void testCalculateWrappedGeometry_fromScrolled()
311 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
312 "examples/uniref50.fa", DataSourceType.FILE);
313 AlignViewport av = af.getViewport();
314 AlignmentI al = av.getAlignment();
315 assertEquals(al.getWidth(), 157);
316 assertEquals(al.getHeight(), 15);
317 av.getRanges().setStartEndSeq(0, 3);
318 av.setShowAnnotation(false);
319 av.setScaleAboveWrapped(true);
321 SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
323 av.setWrapAlignment(true);
324 av.setFont(new Font("SansSerif", Font.PLAIN, 14), true);
325 int charHeight = av.getCharHeight();
326 int charWidth = av.getCharWidth();
327 assertEquals(charHeight, 17);
328 assertEquals(charWidth, 12);
330 int canvasWidth = 400;
331 int canvasHeight = 300;
332 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
334 assertEquals(av.getRanges().getEndSeq(), 3); // unchanged
335 int repeatingHeight = (int) PA.getValue(testee,
336 "wrappedRepeatHeightPx");
337 assertEquals(repeatingHeight, charHeight * (2 + al.getHeight()));