Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:38:20 GMT
  2. Package jalview.gui

File SeqCanvasTest.java

 

Code metrics

2
162
5
1
396
238
7
0.04
32.4
5
1.4

Classes

Class Line # Actions
SeqCanvasTest 43 162 7
0.869822587%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.gui;
22   
23    import static org.testng.Assert.assertEquals;
24    import static org.testng.Assert.assertFalse;
25    import static org.testng.Assert.assertNotNull;
26    import static org.testng.Assert.assertNull;
27    import static org.testng.Assert.assertTrue;
28   
29    import java.awt.Font;
30    import java.awt.FontMetrics;
31   
32    import org.testng.annotations.BeforeMethod;
33    import org.testng.annotations.Test;
34   
35    import jalview.bin.Cache;
36    import jalview.datamodel.AlignmentI;
37    import jalview.datamodel.SearchResults;
38    import jalview.datamodel.SearchResultsI;
39    import jalview.io.DataSourceType;
40    import jalview.io.FileLoader;
41    import junit.extensions.PA;
42   
 
43    public class SeqCanvasTest
44    {
45    private AlignFrame af;
46   
47    /**
48    * Test the method that computes wrapped width in residues, height of wrapped
49    * widths in pixels, and the number of widths visible
50    */
 
51  1 toggle @Test(groups = "Functional")
52    public void testCalculateWrappedGeometry_noAnnotations()
53    {
54  1 AlignViewport av = af.getViewport();
55  1 AlignmentI al = av.getAlignment();
56  1 assertEquals(al.getWidth(), 157);
57  1 assertEquals(al.getHeight(), 15);
58  1 av.getRanges().setStartEndSeq(0, 14);
59   
60  1 SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
61   
62  1 av.setWrapAlignment(true);
63  1 av.setFont(new Font("SansSerif", Font.PLAIN, 14), true);
64  1 int charHeight = av.getCharHeight();
65  1 int charWidth = av.getCharWidth();
66  1 assertEquals(charHeight, 17);
67  1 assertEquals(charWidth, 12);
68   
69    /*
70    * first with scales above, left, right
71    */
72  1 av.setShowAnnotation(false);
73  1 av.setScaleAboveWrapped(true);
74  1 av.setScaleLeftWrapped(true);
75  1 av.setScaleRightWrapped(true);
76  1 FontMetrics fm = testee.getFontMetrics(av.getFont());
77  1 int labelWidth = fm.stringWidth("000") + charWidth;
78    // some leeway for different OS rendering of text
79  1 assertTrue(labelWidth >= 36 && labelWidth <= 39);
80   
81    /*
82    * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
83    * take the whole multiple of character widths
84    */
85  1 int canvasWidth = 400;
86  1 int canvasHeight = 300;
87  1 int residueColumns = (canvasWidth - 2 * labelWidth) / charWidth;
88  1 int wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
89    canvasHeight);
90  1 assertEquals(wrappedWidth, residueColumns);
91  1 assertEquals(PA.getValue(testee, "labelWidthWest"), labelWidth);
92  1 assertEquals(PA.getValue(testee, "labelWidthEast"), labelWidth);
93  1 assertEquals(PA.getValue(testee, "wrappedSpaceAboveAlignment"),
94    2 * charHeight);
95  1 int repeatingHeight = (int) PA.getValue(testee,
96    "wrappedRepeatHeightPx");
97  1 assertEquals(repeatingHeight, charHeight * (2 + al.getHeight()));
98  1 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
99   
100    /*
101    * repeat height is 17 * (2 + 15) = 289
102    * make canvas height 2 * 289 + 3 * charHeight so just enough to
103    * draw 2 widths and the first sequence of a third
104    */
105  1 canvasHeight = charHeight * (17 * 2 + 3);
106  1 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
107  1 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
108   
109    /*
110    * reduce canvas height by 1 pixel
111    * - should not be enough height to draw 3 widths
112    */
113  1 canvasHeight -= 1;
114  1 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
115  1 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
116   
117    /*
118    * turn off scale above - can now fit in 2 and a bit widths
119    */
120  1 av.setScaleAboveWrapped(false);
121  1 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
122  1 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
123   
124    /*
125    * reduce height to enough for 2 widths and not quite a third
126    * i.e. two repeating heights + spacer + sequence - 1 pixel
127    */
128  1 canvasHeight = charHeight * (16 * 2 + 2) - 1;
129  1 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
130  1 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
131   
132    /*
133    * make canvas width enough for scales and 20 residues
134    */
135  1 canvasWidth = 2 * labelWidth + 20 * charWidth;
136  1 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
137    canvasHeight);
138  1 assertEquals(wrappedWidth, 20);
139   
140    /*
141    * reduce width by 1 pixel - rounds down to 19 residues
142    */
143  1 canvasWidth -= 1;
144  1 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
145    canvasHeight);
146  1 assertEquals(wrappedWidth, 19);
147   
148    /*
149    * turn off West scale - adds labelWidth (39) to available for residues
150    * which with the 11 remainder makes 50 which is 4 more charWidths rem 2
151    */
152  1 av.setScaleLeftWrapped(false);
153  1 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
154    canvasHeight);
155    // some leeway for different OS rendering of text
156  1 assertTrue(wrappedWidth >= 22 && wrappedWidth <= 23);
157  1 int difference = wrappedWidth - 23;
158   
159    /*
160    * add 10 pixels to width to fit in another whole residue column
161    */
162  1 canvasWidth += 9;
163  1 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
164    canvasHeight);
165  1 assertEquals(wrappedWidth, 23);
166  1 canvasWidth += 1;
167  1 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
168    canvasHeight);
169  1 assertEquals(wrappedWidth, 24 + difference);
170   
171    /*
172    * turn off East scale to gain 39 more pixels (3 columns remainder 3)
173    */
174  1 av.setScaleRightWrapped(false);
175  1 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
176    canvasHeight);
177  1 assertEquals(wrappedWidth, 27 + difference);
178   
179    /*
180    * add 9 pixels to width to gain a residue column
181    */
182  1 canvasWidth += 8;
183  1 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
184    canvasHeight);
185  1 assertEquals(wrappedWidth, 27); // 8px not enough
186  1 canvasWidth += 1;
187  1 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
188    canvasHeight);
189  1 assertEquals(wrappedWidth, 28 + difference); // 9px is enough
190   
191    /*
192    * now West but not East scale - lose 39 pixels or 4 columns
193    */
194  1 av.setScaleLeftWrapped(true);
195  1 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
196    canvasHeight);
197  1 assertEquals(wrappedWidth, 24);
198   
199    /*
200    * adding 3 pixels to width regains one column
201    */
202  1 canvasWidth += 2;
203  1 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
204    canvasHeight);
205  1 assertEquals(wrappedWidth, 24); // 2px not enough
206  1 canvasWidth += 1;
207  1 wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
208    canvasHeight);
209  1 assertEquals(wrappedWidth, 25 + difference); // 3px is enough
210   
211    /*
212    * turn off scales left and right, make width exactly 157 columns
213    */
214  1 av.setScaleLeftWrapped(false);
215  1 canvasWidth = al.getWidth() * charWidth;
216  1 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
217  1 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
218    }
219   
220    /**
221    * Test the method that computes wrapped width in residues, height of wrapped
222    * widths in pixels, and the number of widths visible
223    */
 
224  1 toggle @Test(groups = "Functional")
225    public void testCalculateWrappedGeometry_withAnnotations()
226    {
227  1 AlignViewport av = af.getViewport();
228  1 AlignmentI al = av.getAlignment();
229  1 assertEquals(al.getWidth(), 157);
230  1 assertEquals(al.getHeight(), 15);
231   
232  1 av.setWrapAlignment(true);
233  1 av.getRanges().setStartEndSeq(0, 14);
234  1 av.setFont(new Font("SansSerif", Font.PLAIN, 14), true);
235  1 int charHeight = av.getCharHeight();
236  1 int charWidth = av.getCharWidth();
237  1 assertEquals(charHeight, 17);
238  1 assertEquals(charWidth, 12);
239   
240  1 SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
241   
242    /*
243    * first with scales above, left, right
244    */
245  1 av.setShowAnnotation(true);
246  1 av.setScaleAboveWrapped(true);
247  1 av.setScaleLeftWrapped(true);
248  1 av.setScaleRightWrapped(true);
249  1 FontMetrics fm = testee.getFontMetrics(av.getFont());
250  1 int labelWidth = fm.stringWidth("000") + charWidth;
251    // some leeway for different OS rendering of text
252  1 assertTrue(labelWidth >= 36 && labelWidth <= 39);
253  1 int annotationHeight = testee.getAnnotationHeight();
254   
255    /*
256    * width 400 pixels leaves (400 - 2*labelWidth) for residue columns
257    * take the whole multiple of character widths
258    */
259  1 int canvasWidth = 400;
260  1 int canvasHeight = 300;
261  1 int residueColumns = (canvasWidth - 2 * labelWidth) / charWidth;
262  1 int wrappedWidth = testee.calculateWrappedGeometry(canvasWidth,
263    canvasHeight);
264  1 assertEquals(wrappedWidth, residueColumns);
265  1 assertEquals(PA.getValue(testee, "labelWidthWest"), labelWidth);
266  1 assertEquals(PA.getValue(testee, "labelWidthEast"), labelWidth);
267  1 assertEquals(PA.getValue(testee, "wrappedSpaceAboveAlignment"),
268    2 * charHeight);
269  1 int repeatingHeight = (int) PA.getValue(testee,
270    "wrappedRepeatHeightPx");
271  1 assertEquals(repeatingHeight, charHeight * (2 + al.getHeight())
272    + SeqCanvas.SEQS_ANNOTATION_GAP + annotationHeight);
273  1 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 1);
274   
275    /*
276    * repeat height is 17 * (2 + 15) = 289 + 3 + annotationHeight = 510
277    * make canvas height 2 of these plus 3 charHeights
278    * so just enough to draw 2 widths, gap + scale + the first sequence of a third
279    */
280  1 canvasHeight = charHeight * (17 * 2 + 3)
281    + 2 * (annotationHeight + SeqCanvas.SEQS_ANNOTATION_GAP);
282  1 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
283  1 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
284   
285    /*
286    * reduce canvas height by 1 pixel - should not be enough height
287    * to draw 3 widths
288    */
289  1 canvasHeight -= 1;
290  1 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
291  1 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
292   
293    /*
294    * turn off scale above - can now fit in 2 and a bit widths
295    */
296  1 av.setScaleAboveWrapped(false);
297  1 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
298  1 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
299   
300    /*
301    * reduce height to enough for 2 widths and not quite a third
302    * i.e. two repeating heights + spacer + sequence - 1 pixel
303    */
304  1 canvasHeight = charHeight * (16 * 2 + 2)
305    + 2 * (annotationHeight + SeqCanvas.SEQS_ANNOTATION_GAP) - 1;
306  1 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
307  1 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 2);
308   
309    /*
310    * add 1 pixel to height - should now get 3 widths drawn
311    */
312  1 canvasHeight += 1;
313  1 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
314  1 assertEquals(PA.getValue(testee, "wrappedVisibleWidths"), 3);
315    }
316   
317    /**
318    * Test simulates loading an unwrapped alignment, shrinking it vertically so
319    * not all sequences are visible, then changing to wrapped mode. The ranges
320    * endSeq should be unchanged, but the vertical repeat height should include
321    * all sequences.
322    */
 
323  0 toggle @Test(groups = "Functional_Failing")
324    public void testCalculateWrappedGeometry_fromScrolled()
325    {
326  0 AlignViewport av = af.getViewport();
327  0 AlignmentI al = av.getAlignment();
328  0 assertEquals(al.getWidth(), 157);
329  0 assertEquals(al.getHeight(), 15);
330  0 av.getRanges().setStartEndSeq(0, 3);
331  0 av.setFont(new Font("SansSerif", Font.PLAIN, 14), true);
332  0 av.setWrapAlignment(true);
333  0 av.setShowAnnotation(false);
334  0 av.setScaleAboveWrapped(true);
335   
336  0 SeqCanvas testee = af.alignPanel.getSeqPanel().seqCanvas;
337   
338  0 int charHeight = av.getCharHeight();
339  0 int charWidth = av.getCharWidth();
340  0 assertEquals(charHeight, 17);
341  0 assertEquals(charWidth, 12);
342   
343  0 int canvasWidth = 400;
344  0 int canvasHeight = 300;
345  0 testee.calculateWrappedGeometry(canvasWidth, canvasHeight);
346   
347  0 assertEquals(av.getRanges().getEndSeq(), 3); // unchanged
348  0 int repeatingHeight = (int) PA.getValue(testee,
349    "wrappedRepeatHeightPx");
350  0 assertEquals(repeatingHeight, charHeight * (2 + al.getHeight()));
351    }
352   
 
353  3 toggle @BeforeMethod(alwaysRun = true)
354    public void setUp()
355    {
356  3 Cache.loadProperties("test/jalview/io/testProps.jvprops");
357  3 Cache.applicationProperties.setProperty("SHOW_IDENTITY",
358    Boolean.TRUE.toString());
359  3 af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
360    DataSourceType.FILE);
361   
362    /*
363    * wait for Consensus thread to complete
364    */
365  3 do
366    {
367  3 try
368    {
369  3 Thread.sleep(50);
370    } catch (InterruptedException x)
371    {
372    }
373  3 } while (af.getViewport().getCalcManager().isWorking());
374    }
375   
 
376  1 toggle @Test(groups = "Functional")
377    public void testClear_HighlightAndSelection()
378    {
379  1 AlignViewport av = af.getViewport();
380  1 SearchResultsI highlight = new SearchResults();
381  1 highlight.addResult(
382    av.getAlignment().getSequenceAt(1).getDatasetSequence(), 50,
383    80);
384  1 af.alignPanel.highlightSearchResults(highlight);
385  1 af.avc.markHighlightedColumns(false, false, false);
386  1 assertNotNull(av.getSearchResults(),
387    "No highlight was created on alignment");
388  1 assertFalse(av.getColumnSelection().isEmpty(),
389    "No selection was created from highlight");
390  1 af.deselectAllSequenceMenuItem_actionPerformed(null);
391  1 assertTrue(av.getColumnSelection().isEmpty(),
392    "No Selection should be present after deselecting all.");
393  1 assertNull(av.getSearchResults(),
394    "No higlighted search results should be present after deselecting all.");
395    }
396    }