From: gmungoc Date: Thu, 7 Mar 2019 12:57:28 +0000 (+0000) Subject: JAL-2717 JAL-2360 belated unit tests for ColourMenuHelper X-Git-Tag: Release_2_11_0~17^2~57 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=2118127751e6cc5450b6bda3cc8329b286d3bae8;p=jalview.git JAL-2717 JAL-2360 belated unit tests for ColourMenuHelper --- diff --git a/test/jalview/gui/ColourMenuHelperTest.java b/test/jalview/gui/ColourMenuHelperTest.java new file mode 100644 index 0000000..9d4ed6c --- /dev/null +++ b/test/jalview/gui/ColourMenuHelperTest.java @@ -0,0 +1,263 @@ +package jalview.gui; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertSame; +import static org.testng.Assert.assertTrue; + +import jalview.bin.Cache; +import jalview.datamodel.Alignment; +import jalview.datamodel.AlignmentI; +import jalview.datamodel.Sequence; +import jalview.datamodel.SequenceI; +import jalview.schemes.ClustalxColourScheme; +import jalview.schemes.ColourSchemeI; +import jalview.schemes.ColourSchemes; +import jalview.schemes.NucleotideColourScheme; +import jalview.schemes.PIDColourScheme; +import jalview.schemes.ResidueColourScheme; +import jalview.util.MessageManager; + +import java.util.Enumeration; +import java.util.Iterator; + +import javax.swing.AbstractButton; +import javax.swing.ButtonGroup; +import javax.swing.JMenu; +import javax.swing.JMenuItem; + +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class ColourMenuHelperTest +{ + /** + * Use a properties file with a user-defined colour scheme + */ + @BeforeClass + public void setUp() + { + Cache.loadProperties("test/jalview/io/testProps.jvprops"); + } + + @Test + public void testAddMenuItems_peptide() + { + SequenceI s1 = new Sequence("s1", "KFRQSILM"); + AlignmentI al = new Alignment(new SequenceI[] { s1 }); + JMenu menu = new JMenu(); + + ButtonGroup bg = ColourMenuHelper.addMenuItems(menu, null, al, false); + Enumeration bgElements = bg.getElements(); + + /* + * first entry is 'No Colour' option + */ + JMenuItem item = menu.getItem(0); + assertEquals(item.getName(), ResidueColourScheme.NONE); + assertEquals(item.getText(), MessageManager.getString("label.none")); + AbstractButton bgItem = bgElements.nextElement(); + assertSame(bgItem, item); + + /* + * check that each registered colour scheme is in the menu, + * and in the button group; + * nucleotide-only schemes should be disabled menu items + */ + Iterator colourSchemes = ColourSchemes.getInstance() + .getColourSchemes().iterator(); + final int items = menu.getItemCount(); + for (int i = 1; i < items; i++) + { + item = menu.getItem(i); + bgItem = bgElements.nextElement(); + assertSame(bgItem, item); + ColourSchemeI cs = colourSchemes.next(); + String name = cs.getSchemeName(); + assertEquals(item.getName(), name); + boolean enabled = item.isEnabled(); + assertEquals(enabled, cs.isApplicableTo(al)); + if (cs instanceof NucleotideColourScheme) // nucleotide only + { + assertFalse(enabled); + } + if (cs instanceof ClustalxColourScheme) // peptide only + { + assertTrue(enabled); + } + if (cs instanceof PIDColourScheme) // nucleotide or peptide + { + assertTrue(enabled); + } + + /* + * check i18n for display name + */ + String label = MessageManager.getStringOrReturn("label.colourScheme_", + name.toLowerCase().replace(" ", "_")); + assertEquals(item.getText(), label); + } + + /* + * check nothing left over + */ + assertFalse(colourSchemes.hasNext()); + assertFalse(bgElements.hasMoreElements()); + } + + @Test + public void testAddMenuItems_nucleotide() + { + SequenceI s1 = new Sequence("s1", "GAATAATCCATAACAG"); + AlignmentI al = new Alignment(new SequenceI[] { s1 }); + JMenu menu = new JMenu(); + AlignFrame af = new AlignFrame(al, 500, 500); + + /* + * menu for SequenceGroup excludes 'User Defined Colour' + */ + PopupMenu popup = new PopupMenu(af.alignPanel, s1, null); + ButtonGroup bg = ColourMenuHelper.addMenuItems(menu, popup, al, false); + Enumeration bgElements = bg.getElements(); + + /* + * first entry is 'No Colour' option + */ + JMenuItem item = menu.getItem(0); + assertEquals(item.getName(), ResidueColourScheme.NONE); + assertEquals(item.getText(), MessageManager.getString("label.none")); + AbstractButton bgItem = bgElements.nextElement(); + assertSame(bgItem, item); + + /* + * check that each registered colour scheme is in the menu, + * and in the button group; + * nucleotide-only schemes should be disabled menu items + */ + Iterator colourSchemes = ColourSchemes.getInstance() + .getColourSchemes().iterator(); + final int items = menu.getItemCount(); + for (int i = 1; i < items; i++) + { + item = menu.getItem(i); + bgItem = bgElements.nextElement(); + assertSame(bgItem, item); + ColourSchemeI cs = colourSchemes.next(); + String name = cs.getSchemeName(); + assertEquals(item.getName(), name); + boolean enabled = item.isEnabled(); + assertEquals(enabled, cs.isApplicableTo(al)); + if (cs instanceof NucleotideColourScheme) // nucleotide only + { + assertTrue(enabled); + } + if (cs instanceof ClustalxColourScheme) // peptide only + { + assertFalse(enabled); + } + if (cs instanceof PIDColourScheme) // nucleotide or peptide + { + assertTrue(enabled); + } + + /* + * check i18n for display name + */ + String label = MessageManager.getStringOrReturn("label.colourScheme_", + name.toLowerCase().replace(" ", "_")); + assertEquals(item.getText(), label); + } + + /* + * check nothing left over + */ + assertFalse(colourSchemes.hasNext()); + assertFalse(bgElements.hasMoreElements()); + } + + /** + * 'Simple only' mode constructs colour menu for structures + *
    + *
  • no 'No Colour' option
  • + *
  • only simple colour schemes (colour per residue)
  • + *
+ */ + @Test + public void testAddMenuItems_simpleOnly() + { + SequenceI s1 = new Sequence("s1", "KFRQSILM"); + AlignmentI al = new Alignment(new SequenceI[] { s1 }); + JMenu menu = new JMenu(); + + ButtonGroup bg = ColourMenuHelper.addMenuItems(menu, null, al, true); + Enumeration bgElements = bg.getElements(); + + /* + * check that only 'simple' colour schemes are included + */ + Iterator colourSchemes = ColourSchemes.getInstance() + .getColourSchemes().iterator(); + int i = 0; + while (colourSchemes.hasNext()) + { + ColourSchemeI cs = colourSchemes.next(); + if (!cs.isSimple()) + { + continue; + } + JMenuItem item = menu.getItem(i++); + AbstractButton bgItem = bgElements.nextElement(); + assertSame(bgItem, item); + } + + /* + * check nothing left over + */ + assertEquals(i, menu.getItemCount()); + assertFalse(bgElements.hasMoreElements()); + } + + /* + * menu for AlignFrame includes 'User Defined Colour' + */ + @Test + public void testAddMenuItems_forAlignFrame() + { + SequenceI s1 = new Sequence("s1", "KFRQSILM"); + AlignmentI al = new Alignment(new SequenceI[] { s1 }); + AlignFrame af = new AlignFrame(al, 500, 500); + JMenu menu = new JMenu(); + + ButtonGroup bg = ColourMenuHelper.addMenuItems(menu, af, al, false); + Enumeration bgElements = bg.getElements(); + + /* + * check that each registered colour scheme is in the menu, + * (skipping over No Colour which is the first menu item), + * and in the button group + */ + bgElements.nextElement(); // skip No Colour + Iterator colourSchemes = ColourSchemes.getInstance() + .getColourSchemes().iterator(); + final int items = menu.getItemCount(); + for (int i = 1; i < items - 1; i++) + { + JMenuItem item = menu.getItem(i); + AbstractButton bgItem = bgElements.nextElement(); + assertSame(bgItem, item); + ColourSchemeI cs = colourSchemes.next(); + assertEquals(item.getName(), cs.getSchemeName()); + } + + /* + * check menu also has User Defined Colour + */ + assertFalse(colourSchemes.hasNext()); + JMenuItem item = menu.getItem(items - 1); + AbstractButton bgItem = bgElements.nextElement(); + assertSame(bgItem, item); + assertEquals(item.getName(), ResidueColourScheme.USER_DEFINED_MENU); + assertEquals(item.getText(), + MessageManager.getString("action.user_defined")); + } +}