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("00000"); // width of 3 digits and 2 spaces
52 assertEquals(labelWidth, 45); // note this is not 5 * 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 (45) to available for residues
121 * which with the 11 remainder makes 56 which is 4 more charWidths rem 8
123 av.setScaleLeftWrapped(false);
124 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
126 assertEquals(wrappedWidth, 23);
129 * add 4 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 45 more pixels (3 columns remainder 9)
143 av.setScaleRightWrapped(false);
144 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
146 assertEquals(wrappedWidth, 27);
149 * add 3 pixels to width to gain a residue column
152 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
154 assertEquals(wrappedWidth, 28);
157 * now West but not East scale - lose 45 pixels or 4 columns
159 av.setScaleLeftWrapped(true);
160 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
162 assertEquals(wrappedWidth, 24);
165 * adding 9 pixels to width regains one column
168 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
170 assertEquals(wrappedWidth, 24);
172 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
174 assertEquals(wrappedWidth, 25);
177 * turn off scales left and right, make width exactly 157 columns
179 av.setScaleLeftWrapped(false);
180 canvasWidth = al.getWidth() * charWidth;
181 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
182 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
186 * Test the method that computes wrapped width in residues, height of wrapped
187 * widths in pixels, and the number of widths visible
189 @Test(groups = "Functional")
190 public void testCalculateWrappedGeometry_withAnnotations()
192 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
193 "examples/uniref50.fa", DataSourceType.FILE);
194 AlignViewport av = af.getViewport();
195 AlignmentI al = av.getAlignment();
196 assertEquals(al.getWidth(), 157);
197 assertEquals(al.getHeight(), 15);
199 av.setWrapAlignment(true);
200 av.setFont(new Font("SansSerif", Font.PLAIN, 14), true);
201 int charHeight = av.getCharHeight();
202 int charWidth = av.getCharWidth();
203 assertEquals(charHeight, 17);
204 assertEquals(charWidth, 12);
206 SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
209 * first with scales above, left, right
211 av.setShowAnnotation(true);
212 av.setScaleAboveWrapped(true);
213 av.setScaleLeftWrapped(true);
214 av.setScaleRightWrapped(true);
215 FontMetrics fm = SwingUtilities2.getFontMetrics(testee, av.getFont());
216 int labelWidth = fm.stringWidth("00000"); // width of 3 digits and 2 spaces
217 assertEquals(labelWidth, 45); // note this is not 5 * charWidth
218 int annotationHeight = testee.getAnnotationHeight();
221 * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
222 * take the whole multiple of character widths
224 int canvasWidth = 400;
225 int canvasHeight = 300;
226 int residueColumns = (canvasWidth - 2 * labelWidth) / charWidth;
227 int wrappedWidth = testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
228 assertEquals(wrappedWidth, residueColumns);
229 assertEquals(PA.getValue(testee, "labelWidthWest"), labelWidth);
230 assertEquals(PA.getValue(testee, "labelWidthEast"), labelWidth);
231 assertEquals(PA.getValue(testee, "wrappedSpaceAboveAlignment"),
233 int repeatingHeight = (int) PA.getValue(testee, "wrappedRepeatHeightPx");
234 assertEquals(repeatingHeight, charHeight * (2 + al.getHeight())
236 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
239 * repeat height is 17 * (2 + 15) = 289 + annotationHeight = 507
240 * make canvas height 2 * 289 + 3 * charHeight so just enough to
241 * draw 2 widths and the first sequence of a third
243 canvasHeight = charHeight * (17 * 2 + 3) + 2 * annotationHeight;
244 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
245 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
248 * reduce canvas height by 1 pixel - should not be enough height
252 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
253 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
256 * turn off scale above - can now fit in 2 and a bit widths
258 av.setScaleAboveWrapped(false);
259 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
260 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
263 * reduce height to enough for 2 widths and not quite a third
264 * i.e. two repeating heights + spacer + sequence - 1 pixel
266 canvasHeight = charHeight * (16 * 2 + 2) + 2 * annotationHeight - 1;
267 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
268 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
271 * add 1 pixel to height - should now get 3 widths drawn
274 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
275 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);