JAL-4427 Added test cases and a minor correction bug/JAL-4427_HTML_code_in_sequence_ID_text
authorRenia Correya <rcorreya001@dundee.ac.uk>
Mon, 16 Sep 2024 14:59:55 +0000 (20:29 +0530)
committerRenia Correya <rcorreya001@dundee.ac.uk>
Mon, 16 Sep 2024 14:59:55 +0000 (20:29 +0530)
src/jalview/gui/AnnotationLabels.java
test/jalview/gui/AnnotationLabelsTest.java

index 27417f0..05cf745 100755 (executable)
@@ -1464,7 +1464,7 @@ public class AnnotationLabels extends JPanel
             lastLabel = label;
             // next label is the same as this label
             //JAL-4427
-            if(!(lastDescription != null && lastDescription.equals(description))) {
+            if((lastDescription != null && !lastDescription.equals(description))) {
               if(aa[nexAA].sequenceRef == aa[i].sequenceRef) {
                 label = description;
               }
index 2a6e5b9..d3fe2db 100644 (file)
@@ -23,9 +23,15 @@ package jalview.gui;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNull;
 
+import java.awt.Font;
+import java.awt.FontMetrics;
+
+import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.Sequence;
 
+import org.mockito.Mockito;
+import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
 public class AnnotationLabelsTest
@@ -170,4 +176,88 @@ public class AnnotationLabelsTest
             AnnotationLabels.getStatusMessage(aa, new AlignmentAnnotation[]
             { aa, aa2 }), "IUPredWS Long, IUPredWS Short");
   }
+  
+  
+  @Test(groups = "Functional", dataProvider = "TestDrawLabelsData")
+  public void testDrawLabels(AlignmentAnnotation[] annotations, int expectedWidth) {
+
+      FontMetrics mockFontMetrics = Mockito.mock(FontMetrics.class);
+      Mockito.when(mockFontMetrics.getHeight()).thenReturn(10);
+      Mockito.when(mockFontMetrics.getDescent()).thenReturn(2);
+      Mockito.when(mockFontMetrics.stringWidth(Mockito.anyString())).thenAnswer(invocation -> {
+          String str = invocation.getArgument(0);
+          return str.length() * 7;  
+      });
+
+      Font mockFont = new Font("Arial", Font.PLAIN, 12);  
+      Mockito.when(mockFontMetrics.getFont()).thenReturn(mockFont);
+
+      boolean actuallyDraw = false;
+      boolean clip = false;
+      int width = 200;
+      boolean forGUI = true;
+      boolean includeHidden = true;
+
+      AlignViewport av = Mockito.mock(AlignViewport.class);
+      Alignment alignment = Mockito.mock(Alignment.class);
+      Mockito.when(alignment.getAlignmentAnnotation()).thenReturn(annotations);
+      Mockito.when(av.getAlignment()).thenReturn(alignment);
+      Mockito.when(av.getFont()).thenReturn(new Font("Arial", Font.PLAIN, 12));  // Mock font
+
+      AnnotationLabels annotationLabels = new AnnotationLabels(av);
+
+      int resultWidth = annotationLabels.drawLabels(null, clip, width, actuallyDraw, forGUI, mockFontMetrics, includeHidden);
+
+      assertEquals(resultWidth, expectedWidth);
+      Mockito.reset(mockFontMetrics, av, alignment);
+  }
+  
+  @DataProvider(name = "TestDrawLabelsData")
+  public static Object[][] provideSecondaryStructureAnnotation() {
+
+
+    Sequence s1 = new Sequence("Seq1", "MLRIQST");
+    Sequence s2 = new Sequence("Sequence2", "MLRIQST");
+    
+    String label1 = "Label1";
+    String label2 = "Label_2";
+    String label3 = "Label_three";
+    String description1 = "Desc1";
+    String description2 = "<html><h1>Desc_2</h1></html>";
+    String description3 = "Description_three";
+    
+    
+    AlignmentAnnotation a1 = new AlignmentAnnotation(label1, description1, null);
+    AlignmentAnnotation a2 = new AlignmentAnnotation(label2, description2, null);
+    AlignmentAnnotation a3 = new AlignmentAnnotation(label3, description3, null);
+    AlignmentAnnotation a4 = new AlignmentAnnotation(label3, description3, null);
+    AlignmentAnnotation a5 = new AlignmentAnnotation(label1, description2, null);
+    AlignmentAnnotation a6 = new AlignmentAnnotation(label1, description2, null);
+    
+    a1.sequenceRef = s1;
+    a2.sequenceRef = s1;
+    a3.sequenceRef = s1;
+    a4.sequenceRef = s2;
+    a5.sequenceRef = s1;
+    a6.sequenceRef = s2;
+    
+    a1.visible = true;
+    a2.visible = true;
+    a3.visible = true;
+    a4.visible = true;
+    a5.visible = true;
+    a6.visible = true;
+    
+    return new Object[][]{
+
+        {new AlignmentAnnotation[]{a2, a2, a1}, 7 * ((s1.getName()+" ").length() + label2.length()) + 3},
+        {new AlignmentAnnotation[]{a1, a2, a3}, 7 * (label3.length()) + 3}, //Different labels and descriptions, same sequenceRef
+        {new AlignmentAnnotation[]{a1, a2, a4}, 7 * ((s2.getName()+" ").length() + label3.length()) + 3}, //Different labels, descriptions, sequenceRef
+        {new AlignmentAnnotation[]{a1, a1, a1}, 7 * ((s1.getName()+" ").length() + label1.length()) + 3}, //Same labels, descriptions, sequenceRef
+        {new AlignmentAnnotation[]{a1, a1, a2}, 7 * ((s1.getName()+" ").length() + label1.length()) + 3}, 
+        {new AlignmentAnnotation[]{a1, a5, a2}, 7 * ((s1.getName()+" ").length() + "Desc_2".length()) + 3}, //same labels and sequence, different description
+        {new AlignmentAnnotation[]{a1, a6, a2}, 7 * ((s2.getName()+" ").length() + "Desc_2".length()) + 3}, //same labels and sequence, different description
+
+    };
+  }
 }