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 org.testng.annotations.Test;
14 public class SeqCanvasTest
17 * Test the method that computes wrapped width in residues, height of wrapped
18 * widths in pixels, and the number of widths visible
20 @Test(groups = "Functional")
21 public void testCalculateWrappedGeometry_noAnnotations()
23 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
24 "examples/uniref50.fa", DataSourceType.FILE);
25 AlignViewport av = af.getViewport();
26 AlignmentI al = av.getAlignment();
27 assertEquals(al.getWidth(), 157);
28 assertEquals(al.getHeight(), 15);
30 av.setWrapAlignment(true);
31 av.getRanges().setStartEndSeq(0, 14);
32 av.setFont(new Font("SansSerif", Font.PLAIN, 14), true);
33 int charHeight = av.getCharHeight();
34 int charWidth = av.getCharWidth();
35 assertEquals(charHeight, 17);
36 assertEquals(charWidth, 12);
38 SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
41 * first with scales above, left, right
43 av.setShowAnnotation(false);
44 av.setScaleAboveWrapped(true);
45 av.setScaleLeftWrapped(true);
46 av.setScaleRightWrapped(true);
47 FontMetrics fm = testee.getFontMetrics(av.getFont());
48 int labelWidth = fm.stringWidth("000") + charWidth;
49 assertEquals(labelWidth, 39); // 3 x 9 + charWidth
52 * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
53 * take the whole multiple of character widths
55 int canvasWidth = 400;
56 int canvasHeight = 300;
57 int residueColumns = (canvasWidth - 2 * labelWidth) / charWidth;
58 int wrappedWidth = testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
59 assertEquals(wrappedWidth, residueColumns);
60 assertEquals(PA.getValue(testee, "labelWidthWest"), labelWidth);
61 assertEquals(PA.getValue(testee, "labelWidthEast"), labelWidth);
62 assertEquals(PA.getValue(testee, "wrappedSpaceAboveAlignment"),
64 int repeatingHeight = (int) PA.getValue(testee, "wrappedRepeatHeightPx");
65 assertEquals(repeatingHeight, charHeight * (2 + al.getHeight()));
66 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
69 * repeat height is 17 * (2 + 15) = 289
70 * make canvas height 2 * 289 + 3 * charHeight so just enough to
71 * draw 2 widths and the first sequence of a third
73 canvasHeight = charHeight * (17 * 2 + 3);
74 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
75 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
78 * reduce canvas height by 1 pixel - should not be enough height
82 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
83 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
86 * turn off scale above - can now fit in 2 and a bit widths
88 av.setScaleAboveWrapped(false);
89 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
90 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
93 * reduce height to enough for 2 widths and not quite a third
94 * i.e. two repeating heights + spacer + sequence - 1 pixel
96 canvasHeight = charHeight * (16 * 2 + 2) - 1;
97 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
98 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
101 * make canvas width enough for scales and 20 residues
103 canvasWidth = 2 * labelWidth + 20 * charWidth;
104 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
106 assertEquals(wrappedWidth, 20);
109 * reduce width by 1 pixel - rounds down to 19 residues
112 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
114 assertEquals(wrappedWidth, 19);
117 * turn off West scale - adds labelWidth (39) to available for residues
118 * which with the 11 remainder makes 50 which is 4 more charWidths rem 2
120 av.setScaleLeftWrapped(false);
121 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
123 assertEquals(wrappedWidth, 23);
126 * add 10 pixels to width to fit in another whole residue column
129 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
131 assertEquals(wrappedWidth, 23);
133 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
135 assertEquals(wrappedWidth, 24);
138 * turn off East scale to gain 39 more pixels (3 columns remainder 3)
140 av.setScaleRightWrapped(false);
141 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
143 assertEquals(wrappedWidth, 27);
146 * add 9 pixels to width to gain a residue column
149 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
151 assertEquals(wrappedWidth, 27);
153 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
155 assertEquals(wrappedWidth, 28);
158 * now West but not East scale - lose 39 pixels or 4 columns
160 av.setScaleLeftWrapped(true);
161 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
163 assertEquals(wrappedWidth, 24);
166 * adding 3 pixels to width regains one column
169 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
171 assertEquals(wrappedWidth, 24);
173 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
175 assertEquals(wrappedWidth, 25);
178 * turn off scales left and right, make width exactly 157 columns
180 av.setScaleLeftWrapped(false);
181 canvasWidth = al.getWidth() * charWidth;
182 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
183 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
187 * Test the method that computes wrapped width in residues, height of wrapped
188 * widths in pixels, and the number of widths visible
190 @Test(groups = "Functional")
191 public void testCalculateWrappedGeometry_withAnnotations()
193 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
194 "examples/uniref50.fa", DataSourceType.FILE);
195 AlignViewport av = af.getViewport();
196 AlignmentI al = av.getAlignment();
197 assertEquals(al.getWidth(), 157);
198 assertEquals(al.getHeight(), 15);
200 av.setWrapAlignment(true);
201 av.getRanges().setStartEndSeq(0, 14);
202 av.setFont(new Font("SansSerif", Font.PLAIN, 14), true);
203 int charHeight = av.getCharHeight();
204 int charWidth = av.getCharWidth();
205 assertEquals(charHeight, 17);
206 assertEquals(charWidth, 12);
208 SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
211 * first with scales above, left, right
213 av.setShowAnnotation(true);
214 av.setScaleAboveWrapped(true);
215 av.setScaleLeftWrapped(true);
216 av.setScaleRightWrapped(true);
217 FontMetrics fm = testee.getFontMetrics(av.getFont());
218 int labelWidth = fm.stringWidth("000") + charWidth;
219 assertEquals(labelWidth, 39); // 3 x 9 + charWidth
220 int annotationHeight = testee.getAnnotationHeight();
223 * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
224 * take the whole multiple of character widths
226 int canvasWidth = 400;
227 int canvasHeight = 300;
228 int residueColumns = (canvasWidth - 2 * labelWidth) / charWidth;
229 int wrappedWidth = testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
230 assertEquals(wrappedWidth, residueColumns);
231 assertEquals(PA.getValue(testee, "labelWidthWest"), labelWidth);
232 assertEquals(PA.getValue(testee, "labelWidthEast"), labelWidth);
233 assertEquals(PA.getValue(testee, "wrappedSpaceAboveAlignment"),
235 int repeatingHeight = (int) PA.getValue(testee, "wrappedRepeatHeightPx");
236 assertEquals(repeatingHeight, charHeight * (2 + al.getHeight())
238 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
241 * repeat height is 17 * (2 + 15) = 289 + annotationHeight = 507
242 * make canvas height 2 * 289 + 3 * charHeight so just enough to
243 * draw 2 widths and the first sequence of a third
245 canvasHeight = charHeight * (17 * 2 + 3) + 2 * annotationHeight;
246 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
247 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
250 * reduce canvas height by 1 pixel - should not be enough height
254 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
255 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
258 * turn off scale above - can now fit in 2 and a bit widths
260 av.setScaleAboveWrapped(false);
261 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
262 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
265 * reduce height to enough for 2 widths and not quite a third
266 * i.e. two repeating heights + spacer + sequence - 1 pixel
268 canvasHeight = charHeight * (16 * 2 + 2) + 2 * annotationHeight - 1;
269 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
270 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
273 * add 1 pixel to height - should now get 3 widths drawn
276 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
277 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);