JAL-2386 unit tests for change colour (and minor refactoring for
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 23 Jan 2017 13:26:24 +0000 (13:26 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 23 Jan 2017 13:26:24 +0000 (13:26 +0000)
testability)

src/jalview/gui/AlignFrame.java
src/jalview/gui/PopupMenu.java
src/jalview/gui/SliderPanel.java
src/jalview/jbgui/GAlignFrame.java
src/jalview/viewmodel/AlignmentViewport.java
test/jalview/gui/AlignFrameTest.java

index 5cc5111..266cbcb 100644 (file)
@@ -1254,8 +1254,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     {
       FileFormatI format = fileFormat;
       cap.setText(new FormatAdapter(alignPanel, exportData.getSettings())
-              .formatSequences(format,
-                      exportData.getAlignment(),
+              .formatSequences(format, exportData.getAlignment(),
                       exportData.getOmitHidden(),
                       exportData.getStartEndPostions(),
                       viewport.getColumnSelection()));
@@ -1846,8 +1845,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     }
 
     String output = new FormatAdapter().formatSequences(FileFormat.Fasta,
-            seqs,
-            omitHidden, null);
+            seqs, omitHidden, null);
 
     StringSelection ss = new StringSelection(output);
 
@@ -3300,11 +3298,12 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * colour scheme to all groups. If unchecked, groups may have their own
    * independent colour schemes.
    * 
+   * @param selected
    */
   @Override
-  protected void applyToAllGroups_actionPerformed()
+  protected void applyToAllGroups_actionPerformed(boolean selected)
   {
-    viewport.setColourAppliesToAllGroups(applyToAllGroups.isSelected());
+    viewport.setColourAppliesToAllGroups(selected);
   }
 
   /**
@@ -3359,12 +3358,9 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   @Override
   protected void modifyPID_actionPerformed()
   {
-    if (viewport.getAbovePIDThreshold())
-    {
-      SliderPanel.setPIDSliderSource(alignPanel,
-              viewport.getViewportColourScheme(), "Background");
-      SliderPanel.showPIDSlider();
-    }
+    SliderPanel.setPIDSliderSource(alignPanel,
+            viewport.getViewportColourScheme(), "Background");
+    SliderPanel.showPIDSlider();
   }
 
   /**
@@ -3373,22 +3369,17 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   @Override
   protected void modifyConservation_actionPerformed()
   {
-    if (viewport.getConservationSelected()
-            && viewport.getGlobalColourScheme() != null)
-    {
-      SliderPanel.setConservationSlider(alignPanel,
-              viewport.getViewportColourScheme(), "Background");
-      SliderPanel.showConservationSlider();
-    }
+    SliderPanel.setConservationSlider(alignPanel,
+            viewport.getViewportColourScheme(), "Background");
+    SliderPanel.showConservationSlider();
   }
 
   /**
    * Action on selecting or deselecting (Colour) By Conservation
    */
   @Override
-  protected void conservationMenuItem_actionPerformed()
+  protected void conservationMenuItem_actionPerformed(boolean selected)
   {
-    boolean selected = conservationMenuItem.isSelected();
     modifyConservation.setEnabled(selected);
     viewport.setConservationSelected(selected);
     viewport.getViewportColourScheme().setConservationApplied(selected);
@@ -3408,9 +3399,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * Action on selecting or deselecting (Colour) Above PID Threshold
    */
   @Override
-  public void abovePIDThreshold_actionPerformed()
+  public void abovePIDThreshold_actionPerformed(boolean selected)
   {
-    boolean selected = abovePIDThreshold.isSelected();
     modifyPID.setEnabled(selected);
     viewport.setAbovePIDThreshold(selected);
 
@@ -4768,8 +4758,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
           }
           if (FileFormat.Jnet.equals(format))
           {
-            JPredFile predictions = new JPredFile(
-                    file, sourceType);
+            JPredFile predictions = new JPredFile(file, sourceType);
             new JnetAnnotationMaker();
             JnetAnnotationMaker.add_annotation(predictions,
                     viewport.getAlignment(), 0, false);
index e186145..37cd8b3 100644 (file)
@@ -1261,7 +1261,7 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        abovePIDColour_actionPerformed();
+        abovePIDColour_actionPerformed(abovePIDColour.isSelected());
       }
     });
 
@@ -1283,7 +1283,8 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        conservationMenuItem_actionPerformed();
+        conservationMenuItem_actionPerformed(conservationMenuItem
+                .isSelected());
       }
     });
 
@@ -1528,10 +1529,12 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener
   /**
    * DOCUMENT ME!
    * 
+   * @param selected
+   * 
    * @param e
    *          DOCUMENT ME!
    */
-  protected void abovePIDColour_actionPerformed()
+  protected void abovePIDColour_actionPerformed(boolean selected)
   {
     SequenceGroup sg = getGroup();
     if (sg.cs == null)
@@ -1539,7 +1542,6 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener
       return;
     }
 
-    boolean selected = abovePIDColour.isSelected();
     if (selected)
     {
       sg.cs.setConsensus(AAFrequency.calculate(
@@ -1583,7 +1585,7 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener
    * @param e
    *          DOCUMENT ME!
    */
-  protected void conservationMenuItem_actionPerformed()
+  protected void conservationMenuItem_actionPerformed(boolean selected)
   {
     SequenceGroup sg = getGroup();
     if (sg.cs == null)
@@ -1591,7 +1593,6 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener
       return;
     }
 
-    boolean selected = conservationMenuItem.isSelected();
     if (selected)
     {
       // JBPNote: Conservation name shouldn't be i18n translated
@@ -1999,6 +2000,8 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener
   /**
    * Action on user selecting an item from the colour menu (that does not have
    * its bespoke action handler)
+   * 
+   * @return
    */
   @Override
   public void changeColour_actionPerformed(String colourSchemeName)
index 4600ab7..40f86b8 100755 (executable)
@@ -54,6 +54,18 @@ public class SliderPanel extends GSliderPanel
 
   CollectionColourSchemeI cs;
 
+  private static SliderPanel sliderPanel;
+
+  /**
+   * Returns the currently active slider panel (or null if none).
+   * 
+   * @return
+   */
+  public static SliderPanel getSliderPanel()
+  {
+    return sliderPanel;
+  }
+
   /**
    * Creates a new SliderPanel object.
    * 
@@ -128,20 +140,20 @@ public class SliderPanel extends GSliderPanel
   public static int setConservationSlider(AlignmentPanel ap,
           CollectionColourSchemeI ccs, String source)
   {
-    SliderPanel sp = null;
+    sliderPanel = null;
 
     if (conservationSlider == null)
     {
-      sp = new SliderPanel(ap, ccs.getConservationInc(), true, ccs);
+      sliderPanel = new SliderPanel(ap, ccs.getConservationInc(), true, ccs);
       conservationSlider = new JInternalFrame();
-      conservationSlider.setContentPane(sp);
+      conservationSlider.setContentPane(sliderPanel);
       conservationSlider.setLayer(JLayeredPane.PALETTE_LAYER);
     }
     else
     {
-      sp = (SliderPanel) conservationSlider.getContentPane();
-      sp.valueField.setText(String.valueOf(ccs.getConservationInc()));
-      sp.cs = ccs;
+      sliderPanel = (SliderPanel) conservationSlider.getContentPane();
+      sliderPanel.valueField.setText(String.valueOf(ccs.getConservationInc()));
+      sliderPanel.cs = ccs;
     }
 
     conservationSlider
@@ -151,14 +163,14 @@ public class SliderPanel extends GSliderPanel
 
     if (ap.av.getAlignment().getGroups() != null)
     {
-      sp.setAllGroupsCheckEnabled(true);
+      sliderPanel.setAllGroupsCheckEnabled(true);
     }
     else
     {
-      sp.setAllGroupsCheckEnabled(false);
+      sliderPanel.setAllGroupsCheckEnabled(false);
     }
 
-    return sp.getValue();
+    return sliderPanel.getValue();
   }
 
   /**
@@ -234,22 +246,20 @@ public class SliderPanel extends GSliderPanel
   public static int setPIDSliderSource(AlignmentPanel ap,
           CollectionColourSchemeI ccs, String source)
   {
-    SliderPanel pid = null;
-
     int threshold = ccs.getThreshold();
 
     if (PIDSlider == null)
     {
-      pid = new SliderPanel(ap, threshold, false, ccs);
+      sliderPanel = new SliderPanel(ap, threshold, false, ccs);
       PIDSlider = new JInternalFrame();
-      PIDSlider.setContentPane(pid);
+      PIDSlider.setContentPane(sliderPanel);
       PIDSlider.setLayer(JLayeredPane.PALETTE_LAYER);
     }
     else
     {
-      pid = (SliderPanel) PIDSlider.getContentPane();
-      pid.cs = ccs;
-      pid.valueField.setText(String.valueOf(ccs.getThreshold()));
+      sliderPanel = (SliderPanel) PIDSlider.getContentPane();
+      sliderPanel.cs = ccs;
+      sliderPanel.valueField.setText(String.valueOf(ccs.getThreshold()));
     }
 
     PIDSlider
@@ -259,20 +269,22 @@ public class SliderPanel extends GSliderPanel
 
     if (ap.av.getAlignment().getGroups() != null)
     {
-      pid.setAllGroupsCheckEnabled(true);
+      sliderPanel.setAllGroupsCheckEnabled(true);
     }
     else
     {
-      pid.setAllGroupsCheckEnabled(false);
+      sliderPanel.setAllGroupsCheckEnabled(false);
     }
 
-    return pid.getValue();
+    return sliderPanel.getValue();
   }
 
   /**
    * DOCUMENT ME!
+   * 
+   * @return
    */
-  public static void showPIDSlider()
+  public static JInternalFrame showPIDSlider()
   {
     hideConservationSlider();
 
@@ -291,6 +303,7 @@ public class SliderPanel extends GSliderPanel
       });
       PIDSlider.setLayer(JLayeredPane.PALETTE_LAYER);
     }
+    return PIDSlider;
   }
 
   /**
@@ -418,4 +431,37 @@ public class SliderPanel extends GSliderPanel
     return getValue(PIDSlider);
   }
 
+  /**
+   * Answers true if the SliderPanel is for Conservation, false if it is for PID
+   * threshold
+   * 
+   * @return
+   */
+  public boolean isForConservation()
+  {
+    return forConservation;
+  }
+
+  /**
+   * Answers the title for the slider panel; this may include 'Background' if
+   * for the alignment, or the group id if for a group
+   * 
+   * @return
+   */
+  public String getTitle()
+  {
+    String title = null;
+    if (isForConservation())
+    {
+      if (conservationSlider != null)
+      {
+        title = conservationSlider.getTitle();
+      }
+    }
+    else if (PIDSlider != null)
+    {
+      title = PIDSlider.getTitle();
+    }
+    return title;
+  }
 }
index c8aa94f..b39f4a8 100755 (executable)
@@ -752,17 +752,6 @@ public class GAlignFrame extends JInternalFrame
     };
     addMenuActionAndAccelerator(keyStroke, redoMenuItem, al);
 
-    conservationMenuItem.setText(MessageManager
-            .getString("action.by_conservation"));
-    conservationMenuItem.addActionListener(new ActionListener()
-    {
-      @Override
-      public void actionPerformed(ActionEvent e)
-      {
-        conservationMenuItem_actionPerformed();
-      }
-    });
-
     wrapMenuItem.setText(MessageManager.getString("label.wrap"));
     wrapMenuItem.addActionListener(new ActionListener()
     {
@@ -815,17 +804,6 @@ public class GAlignFrame extends JInternalFrame
     };
     addMenuActionAndAccelerator(keyStroke, findMenuItem, al);
 
-    abovePIDThreshold.setText(MessageManager
-            .getString("label.above_identity_threshold"));
-    abovePIDThreshold.addActionListener(new ActionListener()
-    {
-      @Override
-      public void actionPerformed(ActionEvent e)
-      {
-        abovePIDThreshold_actionPerformed();
-      }
-    });
-
     showSeqFeatures.setText(MessageManager
             .getString("label.show_sequence_features"));
     showSeqFeatures.addActionListener(new ActionListener()
@@ -1217,27 +1195,6 @@ public class GAlignFrame extends JInternalFrame
 
     });
 
-    modifyPID = new JMenuItem(
-            MessageManager.getString("label.modify_identity_threshold"));
-    modifyPID.addActionListener(new ActionListener()
-    {
-      @Override
-      public void actionPerformed(ActionEvent e)
-      {
-        modifyPID_actionPerformed();
-      }
-    });
-    modifyConservation.setText(MessageManager
-            .getString("label.modify_conservation_threshold"));
-    modifyConservation.addActionListener(new ActionListener()
-    {
-      @Override
-      public void actionPerformed(ActionEvent e)
-      {
-        modifyConservation_actionPerformed();
-      }
-    });
-
     sortByTreeMenu
             .setText(MessageManager.getString("action.by_tree_order"));
     sort.setText(MessageManager.getString("action.sort"));
@@ -1960,7 +1917,7 @@ public class GAlignFrame extends JInternalFrame
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        applyToAllGroups_actionPerformed();
+        applyToAllGroups_actionPerformed(applyToAllGroups.isSelected());
       }
     });
 
@@ -1982,7 +1939,8 @@ public class GAlignFrame extends JInternalFrame
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        conservationMenuItem_actionPerformed();
+        conservationMenuItem_actionPerformed(conservationMenuItem
+                .isSelected());
       }
     });
 
@@ -1993,7 +1951,7 @@ public class GAlignFrame extends JInternalFrame
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        abovePIDThreshold_actionPerformed();
+        abovePIDThreshold_actionPerformed(abovePIDThreshold.isSelected());
       }
     });
     modifyPID = new JMenuItem(
@@ -2385,7 +2343,7 @@ public class GAlignFrame extends JInternalFrame
   {
   }
 
-  protected void conservationMenuItem_actionPerformed()
+  protected void conservationMenuItem_actionPerformed(boolean selected)
   {
   }
 
@@ -2401,7 +2359,7 @@ public class GAlignFrame extends JInternalFrame
   {
   }
 
-  protected void abovePIDThreshold_actionPerformed()
+  protected void abovePIDThreshold_actionPerformed(boolean selected)
   {
   }
 
@@ -2441,7 +2399,7 @@ public class GAlignFrame extends JInternalFrame
   {
   }
 
-  protected void applyToAllGroups_actionPerformed()
+  protected void applyToAllGroups_actionPerformed(boolean selected)
   {
   }
 
index 9f53f02..a9d3b77 100644 (file)
@@ -634,6 +634,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
               || cs instanceof Blosum62ColourScheme)
       {
         globalColourScheme.setConservation(hconservation);
+        globalColourScheme
+                .setConservationApplied(getConservationSelected());
       }
       globalColourScheme.alignmentChanged(alignment, hiddenRepSequences);
     }
index 60db9dd..d71fe63 100644 (file)
@@ -22,21 +22,33 @@ package jalview.gui;
 
 import static org.testng.AssertJUnit.assertEquals;
 import static org.testng.AssertJUnit.assertFalse;
+import static org.testng.AssertJUnit.assertSame;
 import static org.testng.AssertJUnit.assertTrue;
 
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
+import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
+import jalview.io.DataSourceType;
+import jalview.io.FileLoader;
+import jalview.schemes.BuriedColourScheme;
+import jalview.schemes.HelixColourScheme;
+import jalview.schemes.JalviewColourScheme;
+import jalview.schemes.StrandColourScheme;
+import jalview.schemes.TurnColourScheme;
+import jalview.util.MessageManager;
 
 import java.util.List;
 
 import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 public class AlignFrameTest
 {
+  AlignFrame af;
 
   @BeforeClass(alwaysRun = true)
   public void setUpJvOptionPane()
@@ -95,4 +107,137 @@ public class AlignFrameTest
     assertEquals(6, hidden.get(1)[0]);
     assertEquals(8, hidden.get(1)[1]);
   }
+
+  @BeforeMethod
+  public void setUp()
+  {
+    af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
+            DataSourceType.FILE);
+  }
+
+  /**
+   * Test that changing background (alignment) colour scheme
+   * <ul>
+   * <li>with Apply Colour to All Groups not selected, does not change group
+   * colours</li>
+   * <li>with Apply Colour to All Groups selected, does change group colours</li>
+   * <li>in neither case, changes alignment or group colour thresholds (PID or
+   * Conservation)</li>
+   * </ul>
+   */
+  @Test
+  public void testChangeColour_background_groupsAndThresholds()
+  {
+    AlignViewport av = af.getViewport();
+    AlignmentI al = av.getAlignment();
+
+    /*
+     * Colour alignment by Buried Index
+     */
+    af.applyToAllGroups_actionPerformed(false);
+    af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
+    assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
+    assertFalse(av.getViewportColourScheme().conservationApplied());
+    assertEquals(av.getViewportColourScheme().getThreshold(), 0);
+
+    /*
+     * Apply Conservation 20%
+     */
+    af.conservationMenuItem_actionPerformed(true);
+    SliderPanel sp = SliderPanel.getSliderPanel();
+    assertEquals(sp.getTitle(), MessageManager.formatMessage(
+            "label.conservation_colour_increment",
+            new String[] { "Background" }));
+    assertTrue(sp.isForConservation());
+    sp.valueChanged(20);
+    assertTrue(av.getViewportColourScheme().conservationApplied());
+    assertEquals(av.getViewportColourScheme().getConservationInc(), 20);
+
+    /*
+     * Apply PID threshold 10% (conservation still applies as well)
+     */
+    af.abovePIDThreshold_actionPerformed(true);
+    sp = SliderPanel.getSliderPanel();
+    assertFalse(sp.isForConservation());
+    assertEquals(sp.getTitle(), MessageManager.formatMessage(
+            "label.percentage_identity_threshold",
+            new String[] { "Background" }));
+    sp.valueChanged(10);
+    assertEquals(av.getViewportColourScheme().getThreshold(), 10);
+    assertTrue(av.getViewportColourScheme().conservationApplied());
+    assertEquals(av.getViewportColourScheme().getConservationInc(), 20);
+
+    /*
+     * create a group with Strand colouring, 30% Conservation
+     * and 40% PID threshold
+     */
+    SequenceGroup sg = new SequenceGroup();
+    sg.addSequence(al.getSequenceAt(0), false);
+    sg.setStartRes(15);
+    sg.setEndRes(25);
+    av.setSelectionGroup(sg);
+
+    /*
+     * apply 30% Conservation to group
+     */
+    PopupMenu popupMenu = new PopupMenu(af.alignPanel, null, null);
+    popupMenu.changeColour_actionPerformed(JalviewColourScheme.Strand
+            .toString());
+    assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
+    assertEquals(al.getGroups().size(), 1);
+    assertSame(al.getGroups().get(0), sg);
+    popupMenu.conservationMenuItem_actionPerformed(true);
+    sp = SliderPanel.getSliderPanel();
+    assertTrue(sp.isForConservation());
+    assertEquals(sp.getTitle(), MessageManager.formatMessage(
+            "label.conservation_colour_increment",
+            new String[] { sg.getName() }));
+    sp.valueChanged(30);
+    assertTrue(sg.getGroupColourScheme().conservationApplied());
+    assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
+
+    /*
+     * apply 40% PID threshold to group
+     */
+    popupMenu.abovePIDColour_actionPerformed(true);
+    sp = SliderPanel.getSliderPanel();
+    assertFalse(sp.isForConservation());
+    assertEquals(sp.getTitle(), MessageManager.formatMessage(
+            "label.percentage_identity_threshold",
+            new String[] { sg.getName() }));
+    sp.valueChanged(40);
+    assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
+    // conservation threshold is unchanged:
+    assertTrue(sg.getGroupColourScheme().conservationApplied());
+    assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
+
+    /*
+     * change alignment colour - group colour, and all thresholds,
+     * should be unaffected
+     */
+    af.changeColour_actionPerformed(JalviewColourScheme.Turn.toString());
+    assertTrue(av.getGlobalColourScheme() instanceof TurnColourScheme);
+    assertTrue(av.getViewportColourScheme().conservationApplied());
+    assertEquals(av.getViewportColourScheme().getConservationInc(), 20);
+    assertEquals(av.getViewportColourScheme().getThreshold(), 10);
+    assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
+    assertTrue(sg.getGroupColourScheme().conservationApplied());
+    assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
+    assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
+
+    /*
+     * Now change alignment colour with Apply Colour To All Groups
+     * - group colour should change, but not colour thresholds
+     */
+    af.applyToAllGroups_actionPerformed(true);
+    af.changeColour_actionPerformed(JalviewColourScheme.Helix.toString());
+    assertTrue(av.getGlobalColourScheme() instanceof HelixColourScheme);
+    assertTrue(av.getViewportColourScheme().conservationApplied());
+    assertEquals(av.getViewportColourScheme().getConservationInc(), 20);
+    assertEquals(av.getViewportColourScheme().getThreshold(), 10);
+    assertTrue(sg.getColourScheme() instanceof HelixColourScheme);
+    assertTrue(sg.getGroupColourScheme().conservationApplied());
+    assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
+    assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
+  }
 }