Clover icon

Coverage Report

  1. Project Clover database Fri Nov 15 2024 13:56:46 GMT
  2. Package jalview.gui

File AnnotationLabelsTest.java

 

Code metrics

0
101
4
1
263
151
4
0.04
25.25
4
1

Classes

Class Line # Actions
AnnotationLabelsTest 37 101 4
1.0100%
 

Contributing tests

This file is covered by 9 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.assertNull;
25   
26    import java.awt.Font;
27    import java.awt.FontMetrics;
28   
29    import jalview.datamodel.Alignment;
30    import jalview.datamodel.AlignmentAnnotation;
31    import jalview.datamodel.Sequence;
32   
33    import org.mockito.Mockito;
34    import org.testng.annotations.DataProvider;
35    import org.testng.annotations.Test;
36   
 
37    public class AnnotationLabelsTest
38    {
 
39  1 toggle @Test(groups = "Functional")
40    public void testGetTooltip()
41    {
42  1 assertNull(AnnotationLabels.getTooltip(null));
43   
44    /*
45    * simple description only
46    */
47  1 AlignmentAnnotation ann = new AlignmentAnnotation("thelabel", "thedesc",
48    null);
49  1 String expected = "<html>thedesc</html>";
50  1 assertEquals(AnnotationLabels.getTooltip(ann), expected);
51   
52    /*
53    * description needing html encoding
54    * (no idea why '<' is encoded but '>' is not)
55    */
56  1 ann.description = "TCoffee scores < 56 and > 28";
57  1 expected = "<html>TCoffee scores &lt; 56 and > 28</html>";
58  1 assertEquals(AnnotationLabels.getTooltip(ann), expected);
59   
60    /*
61    * description already html formatted
62    */
63  1 ann.description = "<html>hello world</html>";
64  1 assertEquals(AnnotationLabels.getTooltip(ann), ann.description);
65   
66    /*
67    * simple description and score
68    */
69  1 ann.description = "hello world";
70  1 ann.setScore(2.34d);
71  1 expected = "<html>hello world<br/> Score: 2.34</html>";
72  1 assertEquals(AnnotationLabels.getTooltip(ann), expected);
73   
74    /*
75    * html description and score
76    */
77  1 ann.description = "<html>hello world</html>";
78  1 ann.setScore(2.34d);
79  1 expected = "<html>hello world<br/> Score: 2.34</html>";
80  1 assertEquals(AnnotationLabels.getTooltip(ann), expected);
81   
82    /*
83    * score, no description
84    */
85  1 ann.description = " ";
86  1 assertEquals(AnnotationLabels.getTooltip(ann),
87    "<html> Score: 2.34</html>");
88  1 ann.description = null;
89  1 assertEquals(AnnotationLabels.getTooltip(ann),
90    "<html> Score: 2.34</html>");
91   
92    /*
93    * sequenceref, simple description
94    */
95  1 ann.description = "Count < 12";
96  1 ann.sequenceRef = new Sequence("Seq1", "MLRIQST");
97  1 ann.hasScore = false;
98  1 ann.score = Double.NaN;
99  1 expected = "<html>Seq1 : Count &lt; 12</html>";
100  1 assertEquals(AnnotationLabels.getTooltip(ann), expected);
101   
102    /*
103    * sequenceref, html description, score
104    */
105  1 ann.description = "<html>Score < 4.8</html>";
106  1 ann.sequenceRef = new Sequence("Seq1", "MLRIQST");
107  1 ann.setScore(-2.1D);
108  1 expected = "<html>Seq1 : Score < 4.8<br/> Score: -2.1</html>";
109  1 assertEquals(AnnotationLabels.getTooltip(ann), expected);
110   
111    /*
112    * no score, null description
113    */
114  1 ann.description = null;
115  1 ann.hasScore = false;
116  1 ann.score = Double.NaN;
117  1 assertNull(AnnotationLabels.getTooltip(ann));
118   
119    /*
120    * no score, empty description, sequenceRef
121    */
122  1 ann.description = "";
123  1 assertEquals(AnnotationLabels.getTooltip(ann), "<html>Seq1 :</html>");
124   
125    /*
126    * no score, empty description, no sequenceRef
127    */
128  1 ann.sequenceRef = null;
129  1 assertNull(AnnotationLabels.getTooltip(ann));
130    }
131   
 
132  1 toggle @Test(groups = "Functional")
133    public void testGetStatusMessage()
134    {
135  1 assertNull(AnnotationLabels.getStatusMessage(null, null));
136   
137    /*
138    * simple label
139    */
140  1 AlignmentAnnotation aa = new AlignmentAnnotation("IUPredWS Short",
141    "Protein disorder", null);
142  1 assertEquals(AnnotationLabels.getStatusMessage(aa, null),
143    "IUPredWS Short");
144   
145    /*
146    * with sequence ref
147    */
148  1 aa.setSequenceRef(new Sequence("FER_CAPAA", "MIGRKQL"));
149  1 assertEquals(AnnotationLabels.getStatusMessage(aa, null),
150    "FER_CAPAA : IUPredWS Short");
151   
152    /*
153    * with graph group (degenerate, one annotation only)
154    */
155  1 aa.graphGroup = 1;
156  1 AlignmentAnnotation aa2 = new AlignmentAnnotation("IUPredWS Long",
157    "Protein disorder", null);
158  1 assertEquals(
159    AnnotationLabels.getStatusMessage(aa, new AlignmentAnnotation[]
160    { aa, aa2 }), "FER_CAPAA : IUPredWS Short");
161   
162    /*
163    * graph group with two members; note labels are appended in
164    * reverse order (matching rendering order on screen)
165    */
166  1 aa2.graphGroup = 1;
167  1 assertEquals(
168    AnnotationLabels.getStatusMessage(aa, new AlignmentAnnotation[]
169    { aa, aa2 }), "FER_CAPAA : IUPredWS Long, IUPredWS Short");
170   
171    /*
172    * graph group with no sequence ref
173    */
174  1 aa.sequenceRef = null;
175  1 assertEquals(
176    AnnotationLabels.getStatusMessage(aa, new AlignmentAnnotation[]
177    { aa, aa2 }), "IUPredWS Long, IUPredWS Short");
178    }
179   
180   
 
181  7 toggle @Test(groups = "Functional", dataProvider = "TestDrawLabelsData")
182    public void testDrawLabels(AlignmentAnnotation[] annotations, int expectedWidth) {
183   
184  7 FontMetrics mockFontMetrics = Mockito.mock(FontMetrics.class);
185  7 Mockito.when(mockFontMetrics.getHeight()).thenReturn(10);
186  7 Mockito.when(mockFontMetrics.getDescent()).thenReturn(2);
187  7 Mockito.when(mockFontMetrics.stringWidth(Mockito.anyString())).thenAnswer(invocation -> {
188  21 String str = invocation.getArgument(0);
189  21 return str.length() * 7;
190    });
191   
192  7 Font mockFont = new Font("Arial", Font.PLAIN, 12);
193  7 Mockito.when(mockFontMetrics.getFont()).thenReturn(mockFont);
194   
195  7 boolean actuallyDraw = false;
196  7 boolean clip = false;
197  7 int width = 200;
198  7 boolean forGUI = true;
199  7 boolean includeHidden = true;
200   
201  7 AlignViewport av = Mockito.mock(AlignViewport.class);
202  7 Alignment alignment = Mockito.mock(Alignment.class);
203  7 Mockito.when(alignment.getAlignmentAnnotation()).thenReturn(annotations);
204  7 Mockito.when(av.getAlignment()).thenReturn(alignment);
205  7 Mockito.when(av.getFont()).thenReturn(new Font("Arial", Font.PLAIN, 12)); // Mock font
206   
207  7 AnnotationLabels annotationLabels = new AnnotationLabels(av);
208   
209  7 int resultWidth = annotationLabels.drawLabels(null, clip, width, actuallyDraw, forGUI, mockFontMetrics, includeHidden);
210   
211  7 assertEquals(resultWidth, expectedWidth);
212  7 Mockito.reset(mockFontMetrics, av, alignment);
213    }
214   
 
215  1 toggle @DataProvider(name = "TestDrawLabelsData")
216    public static Object[][] provideSecondaryStructureAnnotation() {
217   
218   
219  1 Sequence s1 = new Sequence("Seq1", "MLRIQST");
220  1 Sequence s2 = new Sequence("Sequence2", "MLRIQST");
221   
222  1 String label1 = "Label1";
223  1 String label2 = "Label_2";
224  1 String label3 = "Label_three";
225  1 String description1 = "Desc1";
226  1 String description2 = "<html><h1>Desc_2</h1></html>";
227  1 String description3 = "Description_three";
228   
229   
230  1 AlignmentAnnotation a1 = new AlignmentAnnotation(label1, description1, null);
231  1 AlignmentAnnotation a2 = new AlignmentAnnotation(label2, description2, null);
232  1 AlignmentAnnotation a3 = new AlignmentAnnotation(label3, description3, null);
233  1 AlignmentAnnotation a4 = new AlignmentAnnotation(label3, description3, null);
234  1 AlignmentAnnotation a5 = new AlignmentAnnotation(label1, description2, null);
235  1 AlignmentAnnotation a6 = new AlignmentAnnotation(label1, description2, null);
236   
237  1 a1.sequenceRef = s1;
238  1 a2.sequenceRef = s1;
239  1 a3.sequenceRef = s1;
240  1 a4.sequenceRef = s2;
241  1 a5.sequenceRef = s1;
242  1 a6.sequenceRef = s2;
243   
244  1 a1.visible = true;
245  1 a2.visible = true;
246  1 a3.visible = true;
247  1 a4.visible = true;
248  1 a5.visible = true;
249  1 a6.visible = true;
250   
251  1 return new Object[][]{
252   
253    {new AlignmentAnnotation[]{a2, a2, a1}, 7 * ((s1.getName()+" ").length() + label2.length()) + 3},
254    {new AlignmentAnnotation[]{a1, a2, a3}, 7 * (label3.length()) + 3}, //Different labels and descriptions, same sequenceRef
255    {new AlignmentAnnotation[]{a1, a2, a4}, 7 * ((s2.getName()+" ").length() + label3.length()) + 3}, //Different labels, descriptions, sequenceRef
256    {new AlignmentAnnotation[]{a1, a1, a1}, 7 * ((s1.getName()+" ").length() + label1.length()) + 3}, //Same labels, descriptions, sequenceRef
257    {new AlignmentAnnotation[]{a1, a1, a2}, 7 * ((s1.getName()+" ").length() + label1.length()) + 3},
258    {new AlignmentAnnotation[]{a1, a5, a2}, 7 * ((s1.getName()+" ").length() + "Desc_2".length()) + 3}, //same labels and sequence, different description
259    {new AlignmentAnnotation[]{a1, a6, a2}, 7 * ((s2.getName()+" ").length() + "Desc_2".length()) + 3}, //same labels and sequence, different description
260   
261    };
262    }
263    }