e38732204c12cefc9e3ade5c133d3afe5b9f56f5
[jalview.git] / test / jalview / gui / SeqCanvasTest.java
1 package jalview.gui;
2
3 import static org.testng.Assert.assertEquals;
4
5 import jalview.datamodel.AlignmentI;
6 import jalview.io.DataSourceType;
7 import jalview.io.FileLoader;
8
9 import java.awt.Font;
10 import java.awt.FontMetrics;
11
12 import junit.extensions.PA;
13
14 import org.testng.annotations.Test;
15
16 import sun.swing.SwingUtilities2;
17
18 public class SeqCanvasTest
19 {
20   /**
21    * Test the method that computes wrapped width in residues, height of wrapped
22    * widths in pixels, and the number of widths visible
23    */
24   @Test(groups = "Functional")
25   public void testCalculateWrappedGeometry_noAnnotations()
26   {
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);
33
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);
40
41     SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
42
43     /*
44      * first with scales above, left, right
45      */
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
53
54     /*
55      * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
56      * take the whole multiple of character widths
57      */
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"),
66             2 * charHeight);
67     int repeatingHeight = (int) PA.getValue(testee, "wrappedRepeatHeightPx");
68     assertEquals(repeatingHeight, charHeight * (2 + al.getHeight()));
69     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
70
71     /*
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
75      */
76     canvasHeight = charHeight * (17 * 2 + 3);
77     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
78     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
79
80     /*
81      * reduce canvas height by 1 pixel - should not be enough height
82      * to draw 3 widths
83      */
84     canvasHeight -= 1;
85     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
86     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
87
88     /*
89      * turn off scale above - can now fit in 2 and a bit widths
90      */
91     av.setScaleAboveWrapped(false);
92     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
93     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
94
95     /*
96      * reduce height to enough for 2 widths and not quite a third
97      * i.e. two repeating heights + spacer + sequence - 1 pixel
98      */
99     canvasHeight = charHeight * (16 * 2 + 2) - 1;
100     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
101     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
102
103     /*
104      * make canvas width enough for scales and 20 residues
105      */
106     canvasWidth = 2 * labelWidth + 20 * charWidth;
107     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
108             canvasHeight);
109     assertEquals(wrappedWidth, 20);
110
111     /*
112      * reduce width by 1 pixel - rounds down to 19 residues
113      */
114     canvasWidth -= 1;
115     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
116             canvasHeight);
117     assertEquals(wrappedWidth, 19);
118
119     /*
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
122      */
123     av.setScaleLeftWrapped(false);
124     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
125             canvasHeight);
126     assertEquals(wrappedWidth, 23);
127
128     /*
129      * add 4 pixels to width to fit in another whole residue column
130      */
131     canvasWidth += 3;
132     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
133             canvasHeight);
134     assertEquals(wrappedWidth, 23);
135     canvasWidth += 1;
136     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
137             canvasHeight);
138     assertEquals(wrappedWidth, 24);
139
140     /*
141      * turn off East scale to gain 45 more pixels (3 columns remainder 9)
142      */
143     av.setScaleRightWrapped(false);
144     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
145             canvasHeight);
146     assertEquals(wrappedWidth, 27);
147
148     /*
149      * add 3 pixels to width to gain a residue column
150      */
151     canvasWidth += 3;
152     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
153             canvasHeight);
154     assertEquals(wrappedWidth, 28);
155
156     /*
157      * now West but not East scale - lose 45 pixels or 4 columns
158      */
159     av.setScaleLeftWrapped(true);
160     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
161             canvasHeight);
162     assertEquals(wrappedWidth, 24);
163
164     /*
165      * adding 9 pixels to width regains one column
166      */
167     canvasWidth += 8;
168     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
169             canvasHeight);
170     assertEquals(wrappedWidth, 24);
171     canvasWidth += 1;
172     wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
173             canvasHeight);
174     assertEquals(wrappedWidth, 25);
175
176     /*
177      * turn off scales left and right, make width exactly 157 columns
178      */
179     av.setScaleLeftWrapped(false);
180     canvasWidth = al.getWidth() * charWidth;
181     testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
182     assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
183   }
184 }