Clover icon

Coverage Report

  1. Project Clover database Wed Nov 13 2024 16:21:17 GMT
  2. Package jalview.gui

File ColourMenuHelperTest.java

 

Code metrics

22
102
5
1
283
179
16
0.16
20.4
5
3.2

Classes

Class Line # Actions
ColourMenuHelperTest 52 102 16
1.0100%
 

Contributing tests

This file is covered by 4 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.assertSame;
26    import static org.testng.Assert.assertTrue;
27   
28    import jalview.bin.Cache;
29    import jalview.datamodel.Alignment;
30    import jalview.datamodel.AlignmentI;
31    import jalview.datamodel.Sequence;
32    import jalview.datamodel.SequenceI;
33    import jalview.schemes.ClustalxColourScheme;
34    import jalview.schemes.ColourSchemeI;
35    import jalview.schemes.ColourSchemes;
36    import jalview.schemes.NucleotideColourScheme;
37    import jalview.schemes.PIDColourScheme;
38    import jalview.schemes.ResidueColourScheme;
39    import jalview.util.MessageManager;
40   
41    import java.util.Enumeration;
42    import java.util.Iterator;
43   
44    import javax.swing.AbstractButton;
45    import javax.swing.ButtonGroup;
46    import javax.swing.JMenu;
47    import javax.swing.JMenuItem;
48   
49    import org.testng.annotations.BeforeClass;
50    import org.testng.annotations.Test;
51   
 
52    public class ColourMenuHelperTest
53    {
54    /**
55    * Use a properties file with a user-defined colour scheme
56    */
 
57  1 toggle @BeforeClass(alwaysRun = true)
58    public void setUp()
59    {
60  1 Cache.loadProperties("test/jalview/io/testProps.jvprops");
61    }
62   
 
63  1 toggle @Test(groups = "Functional")
64    public void testAddMenuItems_peptide()
65    {
66  1 SequenceI s1 = new Sequence("s1", "KFRQSILM");
67  1 AlignmentI al = new Alignment(new SequenceI[] { s1 });
68  1 JMenu menu = new JMenu();
69   
70  1 ButtonGroup bg = ColourMenuHelper.addMenuItems(menu, null, al, false);
71  1 Enumeration<AbstractButton> bgElements = bg.getElements();
72   
73    /*
74    * first entry is 'No Colour' option
75    */
76  1 JMenuItem item = menu.getItem(0);
77  1 assertEquals(item.getName(), ResidueColourScheme.NONE);
78  1 assertEquals(item.getText(), MessageManager.getString("label.none"));
79  1 AbstractButton bgItem = bgElements.nextElement();
80  1 assertSame(bgItem, item);
81   
82    /*
83    * check that each registered colour scheme is in the menu,
84    * and in the button group;
85    * nucleotide-only schemes should be disabled menu items
86    */
87  1 Iterator<ColourSchemeI> colourSchemes = ColourSchemes.getInstance()
88    .getColourSchemes().iterator();
89  1 final int items = menu.getItemCount();
90  21 for (int i = 1; i < items; i++)
91    {
92  20 item = menu.getItem(i);
93  20 bgItem = bgElements.nextElement();
94  20 assertSame(bgItem, item);
95  20 ColourSchemeI cs = colourSchemes.next();
96  20 String name = cs.getSchemeName();
97  20 assertEquals(item.getName(), name);
98  20 boolean enabled = item.isEnabled();
99  20 assertEquals(enabled, cs.isApplicableTo(al));
100  20 if (cs instanceof NucleotideColourScheme) // nucleotide only
101    {
102  1 assertFalse(enabled);
103    }
104  20 if (cs instanceof ClustalxColourScheme) // peptide only
105    {
106  1 assertTrue(enabled);
107    }
108  20 if (cs instanceof PIDColourScheme) // nucleotide or peptide
109    {
110  1 assertTrue(enabled);
111    }
112   
113    /*
114    * check i18n for display name
115    */
116  20 String label = MessageManager.getStringOrReturn("label.colourScheme_",
117    name);
118  20 assertEquals(item.getText(), label);
119    }
120   
121    /*
122    * check nothing left over
123    */
124  1 assertFalse(colourSchemes.hasNext());
125  1 assertFalse(bgElements.hasMoreElements());
126    }
127   
 
128  1 toggle @Test(groups = "Functional")
129    public void testAddMenuItems_nucleotide()
130    {
131  1 SequenceI s1 = new Sequence("s1", "GAATAATCCATAACAG");
132  1 AlignmentI al = new Alignment(new SequenceI[] { s1 });
133  1 JMenu menu = new JMenu();
134  1 AlignFrame af = new AlignFrame(al, 500, 500);
135   
136    /*
137    * menu for SequenceGroup excludes 'User Defined Colour'
138    */
139  1 PopupMenu popup = new PopupMenu(af.alignPanel, s1, null);
140  1 ButtonGroup bg = ColourMenuHelper.addMenuItems(menu, popup, al, false);
141  1 Enumeration<AbstractButton> bgElements = bg.getElements();
142   
143    /*
144    * first entry is 'No Colour' option
145    */
146  1 JMenuItem item = menu.getItem(0);
147  1 assertEquals(item.getName(), ResidueColourScheme.NONE);
148  1 assertEquals(item.getText(), MessageManager.getString("label.none"));
149  1 AbstractButton bgItem = bgElements.nextElement();
150  1 assertSame(bgItem, item);
151   
152    /*
153    * check that each registered colour scheme is in the menu,
154    * and in the button group;
155    * nucleotide-only schemes should be disabled menu items
156    */
157  1 Iterator<ColourSchemeI> colourSchemes = ColourSchemes.getInstance()
158    .getColourSchemes().iterator();
159  1 final int items = menu.getItemCount();
160  21 for (int i = 1; i < items; i++)
161    {
162  20 item = menu.getItem(i);
163  20 bgItem = bgElements.nextElement();
164  20 assertSame(bgItem, item);
165  20 ColourSchemeI cs = colourSchemes.next();
166  20 String name = cs.getSchemeName();
167  20 assertEquals(item.getName(), name);
168  20 boolean enabled = item.isEnabled();
169  20 assertEquals(enabled, cs.isApplicableTo(al));
170  20 if (cs instanceof NucleotideColourScheme) // nucleotide only
171    {
172  1 assertTrue(enabled);
173    }
174  20 if (cs instanceof ClustalxColourScheme) // peptide only
175    {
176  1 assertFalse(enabled);
177    }
178  20 if (cs instanceof PIDColourScheme) // nucleotide or peptide
179    {
180  1 assertTrue(enabled);
181    }
182   
183    /*
184    * check i18n for display name
185    */
186  20 String label = MessageManager.getStringOrReturn("label.colourScheme_",
187    name);
188  20 assertEquals(item.getText(), label);
189    }
190   
191    /*
192    * check nothing left over
193    */
194  1 assertFalse(colourSchemes.hasNext());
195  1 assertFalse(bgElements.hasMoreElements());
196    }
197   
198    /**
199    * 'Simple only' mode constructs colour menu for structures
200    * <ul>
201    * <li>no 'No Colour' option</li>
202    * <li>only simple colour schemes (colour per residue)</li>
203    * </ul>
204    */
 
205  1 toggle @Test(groups = "Functional")
206    public void testAddMenuItems_simpleOnly()
207    {
208  1 SequenceI s1 = new Sequence("s1", "KFRQSILM");
209  1 AlignmentI al = new Alignment(new SequenceI[] { s1 });
210  1 JMenu menu = new JMenu();
211   
212  1 ButtonGroup bg = ColourMenuHelper.addMenuItems(menu, null, al, true);
213  1 Enumeration<AbstractButton> bgElements = bg.getElements();
214   
215    /*
216    * check that only 'simple' colour schemes are included
217    */
218  1 Iterator<ColourSchemeI> colourSchemes = ColourSchemes.getInstance()
219    .getColourSchemes().iterator();
220  1 int i = 0;
221  21 while (colourSchemes.hasNext())
222    {
223  20 ColourSchemeI cs = colourSchemes.next();
224  20 if (!cs.isSimple())
225    {
226  6 continue;
227    }
228  14 JMenuItem item = menu.getItem(i++);
229  14 AbstractButton bgItem = bgElements.nextElement();
230  14 assertSame(bgItem, item);
231    }
232   
233    /*
234    * check nothing left over
235    */
236  1 assertEquals(i, menu.getItemCount());
237  1 assertFalse(bgElements.hasMoreElements());
238    }
239   
240    /*
241    * menu for AlignFrame includes 'User Defined Colour'
242    */
 
243  1 toggle @Test(groups = "Functional")
244    public void testAddMenuItems_forAlignFrame()
245    {
246  1 SequenceI s1 = new Sequence("s1", "KFRQSILM");
247  1 AlignmentI al = new Alignment(new SequenceI[] { s1 });
248  1 AlignFrame af = new AlignFrame(al, 500, 500);
249  1 JMenu menu = new JMenu();
250   
251  1 ButtonGroup bg = ColourMenuHelper.addMenuItems(menu, af, al, false);
252  1 Enumeration<AbstractButton> bgElements = bg.getElements();
253   
254    /*
255    * check that each registered colour scheme is in the menu,
256    * (skipping over No Colour which is the first menu item),
257    * and in the button group
258    */
259  1 bgElements.nextElement(); // skip No Colour
260  1 Iterator<ColourSchemeI> colourSchemes = ColourSchemes.getInstance()
261    .getColourSchemes().iterator();
262  1 final int items = menu.getItemCount();
263  21 for (int i = 1; i < items - 1; i++)
264    {
265  20 JMenuItem item = menu.getItem(i);
266  20 AbstractButton bgItem = bgElements.nextElement();
267  20 assertSame(bgItem, item);
268  20 ColourSchemeI cs = colourSchemes.next();
269  20 assertEquals(item.getName(), cs.getSchemeName());
270    }
271   
272    /*
273    * check menu also has User Defined Colour
274    */
275  1 assertFalse(colourSchemes.hasNext());
276  1 JMenuItem item = menu.getItem(items - 1);
277  1 AbstractButton bgItem = bgElements.nextElement();
278  1 assertSame(bgItem, item);
279  1 assertEquals(item.getName(), ResidueColourScheme.USER_DEFINED_MENU);
280  1 assertEquals(item.getText(),
281    MessageManager.getString("action.user_defined"));
282    }
283    }