3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6 import jalview.datamodel.AlignmentAnnotation;
7 import jalview.datamodel.AlignmentI;
8 import jalview.datamodel.SequenceI;
9 import jalview.io.AppletFormatAdapter;
10 import jalview.util.MessageManager;
12 import java.awt.Component;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.List;
17 import javax.swing.JMenu;
18 import javax.swing.JMenuItem;
19 import javax.swing.JPopupMenu;
20 import javax.swing.JSeparator;
22 import org.junit.Before;
23 import org.junit.Test;
25 public class PopupMenuTest
27 // 4 sequences x 13 positions
28 final static String TEST_DATA = ">FER_CAPAA Ferredoxin\n"
30 + ">FER_CAPAN Ferredoxin, chloroplast precursor\n"
32 + ">FER1_SOLLC Ferredoxin-1, chloroplast precursor\n"
33 + "TIETHKEEELTA-\n" + ">Q93XJ9_SOLTU Ferredoxin I precursor\n"
38 AlignmentPanel parentPanel;
40 PopupMenu testee = null;
43 public void setUp() throws IOException
45 alignment = new jalview.io.FormatAdapter().readFile(TEST_DATA,
46 AppletFormatAdapter.PASTE, "FASTA");
47 AlignFrame af = new AlignFrame(alignment, 700, 500);
48 parentPanel = new AlignmentPanel(af, af.getViewport());
49 testee = new PopupMenu(parentPanel, null, null);
51 for (SequenceI seq : alignment.getSequences())
53 final AlignmentAnnotation annotation = new AlignmentAnnotation("label" + i,
55 annotation.setCalcId("calcId" + i);
56 seq.addAlignmentAnnotation(annotation);
57 annotation.setSequenceRef(seq);
62 public void testConfigureReferenceAnnotationsMenu_noSequenceSelected()
64 JMenuItem menu = new JMenuItem();
65 List<SequenceI> seqs = new ArrayList<SequenceI>();
66 testee.configureReferenceAnnotationsMenu(menu, seqs);
67 assertFalse(menu.isEnabled());
69 MessageManager.getString("label.add_reference_annotations"),
72 menu.setEnabled(true);
73 testee.configureReferenceAnnotationsMenu(menu, seqs);
74 assertFalse(menu.isEnabled());
78 * Test building the 'add reference annotations' menu for the case where there
79 * are no reference annotations to add to the alignment. The menu item should
83 public void testConfigureReferenceAnnotationsMenu_noReferenceAnnotations()
85 JMenuItem menu = new JMenuItem();
86 List<SequenceI> seqs = new ArrayList<SequenceI>();
89 * Initial state is that sequences have annotations, and have dataset
90 * sequences, but the dataset sequences have no annotations. Hence nothing
93 seqs = parentPanel.getAlignment().getSequences();
95 testee.configureReferenceAnnotationsMenu(menu, seqs);
96 assertFalse(menu.isEnabled());
100 * Test building the 'add reference annotations' menu for the case where all
101 * reference annotations are already on the alignment. The menu item should be
105 public void testConfigureReferenceAnnotationsMenu_alreadyAdded()
107 JMenuItem menu = new JMenuItem();
108 List<SequenceI> seqs = new ArrayList<SequenceI>();
110 seqs = parentPanel.getAlignment().getSequences();
111 // copy annotation from sequence to dataset
112 seqs.get(1).getDatasetSequence()
113 .addAlignmentAnnotation(seqs.get(1).getAnnotation()[0]);
114 testee.configureReferenceAnnotationsMenu(menu, seqs);
115 assertFalse(menu.isEnabled());
119 * Test building the 'add reference annotations' menu for the case where
120 * several reference annotations are on the dataset but not on the sequences.
121 * The menu item should be enabled, and acquire a tooltip which lists the
122 * annotation sources (calcIds) and type (labels).
125 public void testConfigureReferenceAnnotationsMenu()
127 JMenuItem menu = new JMenuItem();
128 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
129 // make up new annotations and add to dataset sequences
131 // PDB.secondary structure on Sequence0
132 AlignmentAnnotation annotation = new AlignmentAnnotation(
133 "secondary structure", "", 0);
134 annotation.setCalcId("PBD");
135 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
137 // PDB.Temp on Sequence1
138 annotation = new AlignmentAnnotation("Temp", "", 0);
139 annotation.setCalcId("PBD");
140 seqs.get(1).getDatasetSequence().addAlignmentAnnotation(annotation);
142 // JMOL.secondary structure on Sequence0
143 annotation = new AlignmentAnnotation("secondary structure", "", 0);
144 annotation.setCalcId("JMOL");
145 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
147 testee.configureReferenceAnnotationsMenu(menu, seqs);
148 assertTrue(menu.isEnabled());
149 String expected = "<html><table width=350 border=0><tr><td>Add annotations for<br/>JMOL/secondary structure<br/>PBD/Temp</td></tr></table></html>";
150 assertEquals(expected, menu.getToolTipText());
154 * Test for building menu options including 'show' and 'hide' annotation
158 public void testBuildAnnotationTypesMenus()
160 JMenu showMenu = new JMenu();
161 JMenu hideMenu = new JMenu();
162 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
164 // make up new annotations and add to sequences and to the alignment
166 // PDB.secondary structure on Sequence0
167 AlignmentAnnotation annotation = new AlignmentAnnotation(
168 "secondary structure", "", 0);
169 annotation.setCalcId("PDB");
170 annotation.visible = true;
171 seqs.get(0).addAlignmentAnnotation(annotation);
172 parentPanel.getAlignment().addAnnotation(annotation);
174 // JMOL.secondary structure on Sequence0 - hidden
175 annotation = new AlignmentAnnotation("secondary structure", "", 0);
176 annotation.setCalcId("JMOL");
177 annotation.visible = false;
178 seqs.get(0).addAlignmentAnnotation(annotation);
179 parentPanel.getAlignment().addAnnotation(annotation);
181 // Jpred.SSP on Sequence0 - hidden
182 annotation = new AlignmentAnnotation("SSP", "", 0);
183 annotation.setCalcId("JPred");
184 annotation.visible = false;
185 seqs.get(0).addAlignmentAnnotation(annotation);
186 parentPanel.getAlignment().addAnnotation(annotation);
188 // PDB.Temp on Sequence1
189 annotation = new AlignmentAnnotation("Temp", "", 0);
190 annotation.setCalcId("PDB");
191 annotation.visible = true;
192 seqs.get(1).addAlignmentAnnotation(annotation);
193 parentPanel.getAlignment().addAnnotation(annotation);
196 * Expect menu options to show "secondary structure" and "SSP", and to hide
197 * "secondary structure" and "Temp". Tooltip should be calcId.
199 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
201 assertTrue(showMenu.isEnabled());
202 assertTrue(hideMenu.isEnabled());
204 Component[] showOptions = showMenu.getMenuComponents();
205 Component[] hideOptions = hideMenu.getMenuComponents();
207 assertEquals(4, showOptions.length); // includes 'All' and separator
208 assertEquals(4, hideOptions.length);
210 ((JMenuItem) showOptions[0]).getText());
211 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
212 assertEquals(JSeparator.HORIZONTAL,
213 ((JSeparator) showOptions[1]).getOrientation());
214 assertEquals("secondary structure",
215 ((JMenuItem) showOptions[2]).getText());
216 assertEquals("JMOL", ((JMenuItem) showOptions[2]).getToolTipText());
217 assertEquals("SSP", ((JMenuItem) showOptions[3]).getText());
218 assertEquals("JPred", ((JMenuItem) showOptions[3]).getToolTipText());
221 ((JMenuItem) hideOptions[0]).getText());
222 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
223 assertEquals(JSeparator.HORIZONTAL,
224 ((JSeparator) hideOptions[1]).getOrientation());
225 assertEquals("secondary structure",
226 ((JMenuItem) hideOptions[2]).getText());
227 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
228 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
229 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
233 * Test for building menu options with only 'hide' annotation types enabled.
236 public void testBuildAnnotationTypesMenus_showDisabled()
238 JMenu showMenu = new JMenu();
239 JMenu hideMenu = new JMenu();
240 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
242 // make up new annotations and add to sequences and to the alignment
244 // PDB.secondary structure on Sequence0
245 AlignmentAnnotation annotation = new AlignmentAnnotation(
246 "secondary structure", "", 0);
247 annotation.setCalcId("PDB");
248 annotation.visible = true;
249 seqs.get(0).addAlignmentAnnotation(annotation);
250 parentPanel.getAlignment().addAnnotation(annotation);
252 // PDB.Temp on Sequence1
253 annotation = new AlignmentAnnotation("Temp", "", 0);
254 annotation.setCalcId("PDB");
255 annotation.visible = true;
256 seqs.get(1).addAlignmentAnnotation(annotation);
257 parentPanel.getAlignment().addAnnotation(annotation);
260 * Expect menu options to hide "secondary structure" and "Temp". Tooltip
261 * should be calcId. 'Show' menu should be disabled.
263 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
265 assertFalse(showMenu.isEnabled());
266 assertTrue(hideMenu.isEnabled());
268 Component[] showOptions = showMenu.getMenuComponents();
269 Component[] hideOptions = hideMenu.getMenuComponents();
271 assertEquals(2, showOptions.length); // includes 'All' and separator
272 assertEquals(4, hideOptions.length);
273 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
274 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
275 assertEquals(JSeparator.HORIZONTAL,
276 ((JSeparator) showOptions[1]).getOrientation());
278 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
279 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
280 assertEquals(JSeparator.HORIZONTAL,
281 ((JSeparator) hideOptions[1]).getOrientation());
282 assertEquals("secondary structure",
283 ((JMenuItem) hideOptions[2]).getText());
284 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
285 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
286 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
290 * Test for building menu options with only 'show' annotation types enabled.
293 public void testBuildAnnotationTypesMenus_hideDisabled()
295 JMenu showMenu = new JMenu();
296 JMenu hideMenu = new JMenu();
297 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
299 // make up new annotations and add to sequences and to the alignment
301 // PDB.secondary structure on Sequence0
302 AlignmentAnnotation annotation = new AlignmentAnnotation(
303 "secondary structure", "", 0);
304 annotation.setCalcId("PDB");
305 annotation.visible = false;
306 seqs.get(0).addAlignmentAnnotation(annotation);
307 parentPanel.getAlignment().addAnnotation(annotation);
309 // PDB.Temp on Sequence1
310 annotation = new AlignmentAnnotation("Temp", "", 0);
311 annotation.setCalcId("PDB2");
312 annotation.visible = false;
313 seqs.get(1).addAlignmentAnnotation(annotation);
314 parentPanel.getAlignment().addAnnotation(annotation);
317 * Expect menu options to show "secondary structure" and "Temp". Tooltip
318 * should be calcId. 'hide' menu should be disabled.
320 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
322 assertTrue(showMenu.isEnabled());
323 assertFalse(hideMenu.isEnabled());
325 Component[] showOptions = showMenu.getMenuComponents();
326 Component[] hideOptions = hideMenu.getMenuComponents();
328 assertEquals(4, showOptions.length); // includes 'All' and separator
329 assertEquals(2, hideOptions.length);
330 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
331 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
332 assertEquals(JSeparator.HORIZONTAL,
333 ((JSeparator) showOptions[1]).getOrientation());
334 assertEquals("secondary structure",
335 ((JMenuItem) showOptions[2]).getText());
336 assertEquals("PDB", ((JMenuItem) showOptions[2]).getToolTipText());
337 assertEquals("Temp", ((JMenuItem) showOptions[3]).getText());
338 assertEquals("PDB2", ((JMenuItem) showOptions[3]).getToolTipText());
340 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
341 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
342 assertEquals(JSeparator.HORIZONTAL,
343 ((JSeparator) hideOptions[1]).getOrientation());