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.setFont(new Font("SansSerif", Font.PLAIN, 14), true);
36 int charHeight = av.getCharHeight();
37 int charWidth = av.getCharWidth();
38 assertEquals(charHeight, 17);
39 assertEquals(charWidth, 12);
41 SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
44 * first with scales above, left, right
46 av.setShowAnnotation(false);
47 av.setScaleAboveWrapped(true);
48 av.setScaleLeftWrapped(true);
49 av.setScaleRightWrapped(true);
50 FontMetrics fm = SwingUtilities2.getFontMetrics(testee, av.getFont());
51 int labelWidth = fm.stringWidth("000") + charWidth;
52 assertEquals(labelWidth, 39); // 3 x 9 + charWidth
55 * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
56 * take the whole multiple of character widths
58 int canvasWidth = 400;
59 int canvasHeight = 300;
60 int residueColumns = (canvasWidth - 2 * labelWidth) / charWidth;
61 int wrappedWidth = testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
62 assertEquals(wrappedWidth, residueColumns);
63 assertEquals(PA.getValue(testee, "labelWidthWest"), labelWidth);
64 assertEquals(PA.getValue(testee, "labelWidthEast"), labelWidth);
65 assertEquals(PA.getValue(testee, "wrappedSpaceAboveAlignment"),
67 int repeatingHeight = (int) PA.getValue(testee, "wrappedRepeatHeightPx");
68 assertEquals(repeatingHeight, charHeight * (2 + al.getHeight()));
69 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
72 * repeat height is 17 * (2 + 15) = 289
73 * make canvas height 2 * 289 + 3 * charHeight so just enough to
74 * draw 2 widths and the first sequence of a third
76 canvasHeight = charHeight * (17 * 2 + 3);
77 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
78 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
81 * reduce canvas height by 1 pixel - should not be enough height
85 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
86 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
89 * turn off scale above - can now fit in 2 and a bit widths
91 av.setScaleAboveWrapped(false);
92 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
93 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
96 * reduce height to enough for 2 widths and not quite a third
97 * i.e. two repeating heights + spacer + sequence - 1 pixel
99 canvasHeight = charHeight * (16 * 2 + 2) - 1;
100 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
101 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
104 * make canvas width enough for scales and 20 residues
106 canvasWidth = 2 * labelWidth + 20 * charWidth;
107 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
109 assertEquals(wrappedWidth, 20);
112 * reduce width by 1 pixel - rounds down to 19 residues
115 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
117 assertEquals(wrappedWidth, 19);
120 * turn off West scale - adds labelWidth (39) to available for residues
121 * which with the 11 remainder makes 50 which is 4 more charWidths rem 2
123 av.setScaleLeftWrapped(false);
124 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
126 assertEquals(wrappedWidth, 23);
129 * add 10 pixels to width to fit in another whole residue column
132 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
134 assertEquals(wrappedWidth, 23);
136 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
138 assertEquals(wrappedWidth, 24);
141 * turn off East scale to gain 39 more pixels (3 columns remainder 3)
143 av.setScaleRightWrapped(false);
144 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
146 assertEquals(wrappedWidth, 27);
149 * add 9 pixels to width to gain a residue column
152 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
154 assertEquals(wrappedWidth, 27);
156 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
158 assertEquals(wrappedWidth, 28);
161 * now West but not East scale - lose 39 pixels or 4 columns
163 av.setScaleLeftWrapped(true);
164 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
166 assertEquals(wrappedWidth, 24);
169 * adding 3 pixels to width regains one column
172 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
174 assertEquals(wrappedWidth, 24);
176 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
178 assertEquals(wrappedWidth, 25);
181 * turn off scales left and right, make width exactly 157 columns
183 av.setScaleLeftWrapped(false);
184 canvasWidth = al.getWidth() * charWidth;
185 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
186 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
190 * Test the method that computes wrapped width in residues, height of wrapped
191 * widths in pixels, and the number of widths visible
193 @Test(groups = "Functional")
194 public void testCalculateWrappedGeometry_withAnnotations()
196 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
197 "examples/uniref50.fa", DataSourceType.FILE);
198 AlignViewport av = af.getViewport();
199 AlignmentI al = av.getAlignment();
200 assertEquals(al.getWidth(), 157);
201 assertEquals(al.getHeight(), 15);
203 av.setWrapAlignment(true);
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 = SwingUtilities2.getFontMetrics(testee, 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);