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.io.FormatAdapter;
11 import jalview.util.MessageManager;
13 import java.awt.Component;
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.List;
18 import javax.swing.JMenu;
19 import javax.swing.JMenuItem;
20 import javax.swing.JPopupMenu;
21 import javax.swing.JSeparator;
23 import org.junit.Before;
24 import org.junit.Test;
26 public class PopupMenuTest
28 // 4 sequences x 13 positions
29 final static String TEST_DATA = ">FER_CAPAA Ferredoxin\n"
31 + ">FER_CAPAN Ferredoxin, chloroplast precursor\n"
33 + ">FER1_SOLLC Ferredoxin-1, chloroplast precursor\n"
34 + "TIETHKEEELTA-\n" + ">Q93XJ9_SOLTU Ferredoxin I precursor\n"
39 AlignmentPanel parentPanel;
41 PopupMenu testee = null;
44 public void setUp() throws IOException
46 alignment = new FormatAdapter().readFile(TEST_DATA,
47 AppletFormatAdapter.PASTE, "FASTA");
48 AlignFrame af = new AlignFrame(alignment, 700, 500);
49 parentPanel = new AlignmentPanel(af, af.getViewport());
50 testee = new PopupMenu(parentPanel, null, null);
52 for (SequenceI seq : alignment.getSequences())
54 final AlignmentAnnotation annotation = new AlignmentAnnotation("label" + i,
56 annotation.setCalcId("calcId" + i);
57 seq.addAlignmentAnnotation(annotation);
58 annotation.setSequenceRef(seq);
63 public void testConfigureReferenceAnnotationsMenu_noSequenceSelected()
65 JMenuItem menu = new JMenuItem();
66 List<SequenceI> seqs = new ArrayList<SequenceI>();
67 testee.configureReferenceAnnotationsMenu(menu, seqs);
68 assertFalse(menu.isEnabled());
70 MessageManager.getString("label.add_reference_annotations"),
73 menu.setEnabled(true);
74 testee.configureReferenceAnnotationsMenu(menu, seqs);
75 assertFalse(menu.isEnabled());
79 * Test building the 'add reference annotations' menu for the case where there
80 * are no reference annotations to add to the alignment. The menu item should
84 public void testConfigureReferenceAnnotationsMenu_noReferenceAnnotations()
86 JMenuItem menu = new JMenuItem();
89 * Initial state is that sequences have annotations, and have dataset
90 * sequences, but the dataset sequences have no annotations. Hence nothing
93 List<SequenceI> 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 = parentPanel.getAlignment().getSequences();
110 // make up new annotations and add to dataset sequences, sequences and
112 attachReferenceAnnotations(seqs, true, true);
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();
130 // make up new annotations and add to dataset sequences
131 attachReferenceAnnotations(seqs, false, false);
133 testee.configureReferenceAnnotationsMenu(menu, seqs);
134 assertTrue(menu.isEnabled());
135 String expected = "<html><table width=350 border=0><tr><td>Add annotations for<br/>JMOL/secondary structure<br/>PBD/Temp</td></tr></table></html>";
136 assertEquals(expected, menu.getToolTipText());
140 * Test building the 'add reference annotations' menu for the case where
141 * several reference annotations are on the dataset and the sequences but not
142 * on the alignment. The menu item should be enabled, and acquire a tooltip
143 * which lists the annotation sources (calcIds) and type (labels).
146 public void testConfigureReferenceAnnotationsMenu_notOnAlignment()
148 JMenuItem menu = new JMenuItem();
149 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
151 // make up new annotations and add to dataset sequences and sequences
152 attachReferenceAnnotations(seqs, true, false);
154 testee.configureReferenceAnnotationsMenu(menu, seqs);
155 assertTrue(menu.isEnabled());
156 String expected = "<html><table width=350 border=0><tr><td>Add annotations for<br/>JMOL/secondary structure<br/>PBD/Temp</td></tr></table></html>";
157 assertEquals(expected, menu.getToolTipText());
161 * Generate annotations and add to dataset sequences and (optionally)
162 * sequences and/or alignment
165 * @param addToSequence
166 * @param addToAlignment
168 private void attachReferenceAnnotations(List<SequenceI> seqs,
169 boolean addToSequence, boolean addToAlignment)
171 // PDB.secondary structure on Sequence0
172 AlignmentAnnotation annotation = new AlignmentAnnotation(
173 "secondary structure", "", 0);
174 annotation.setCalcId("PBD");
175 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
178 seqs.get(0).addAlignmentAnnotation(annotation);
182 this.alignment.addAnnotation(annotation);
185 // PDB.Temp on Sequence1
186 annotation = new AlignmentAnnotation("Temp", "", 0);
187 annotation.setCalcId("PBD");
188 seqs.get(1).getDatasetSequence().addAlignmentAnnotation(annotation);
191 seqs.get(1).addAlignmentAnnotation(annotation);
195 this.alignment.addAnnotation(annotation);
198 // JMOL.secondary structure on Sequence0
199 annotation = new AlignmentAnnotation("secondary structure", "", 0);
200 annotation.setCalcId("JMOL");
201 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
204 seqs.get(0).addAlignmentAnnotation(annotation);
208 this.alignment.addAnnotation(annotation);
213 * Test building the 'add reference annotations' menu for the case where there
214 * are two alignment views:
216 * <li>in one view, reference annotations have been added (are on the
217 * datasets, sequences and alignment)</li>
218 * <li>in the current view, reference annotations are on the dataset and
219 * sequence, but not the alignment</li>
221 * The menu item should be enabled, and acquire a tooltip which lists the
222 * annotation sources (calcIds) and type (labels).
225 public void testConfigureReferenceAnnotationsMenu_twoViews()
230 * Test for building menu options including 'show' and 'hide' annotation
234 public void testBuildAnnotationTypesMenus()
236 JMenu showMenu = new JMenu();
237 JMenu hideMenu = new JMenu();
238 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
240 // make up new annotations and add to sequences and to the alignment
242 // PDB.secondary structure on Sequence0
243 AlignmentAnnotation annotation = new AlignmentAnnotation(
244 "secondary structure", "", 0);
245 annotation.setCalcId("PDB");
246 annotation.visible = true;
247 seqs.get(0).addAlignmentAnnotation(annotation);
248 parentPanel.getAlignment().addAnnotation(annotation);
250 // JMOL.secondary structure on Sequence0 - hidden
251 annotation = new AlignmentAnnotation("secondary structure", "", 0);
252 annotation.setCalcId("JMOL");
253 annotation.visible = false;
254 seqs.get(0).addAlignmentAnnotation(annotation);
255 parentPanel.getAlignment().addAnnotation(annotation);
257 // Jpred.SSP on Sequence0 - hidden
258 annotation = new AlignmentAnnotation("SSP", "", 0);
259 annotation.setCalcId("JPred");
260 annotation.visible = false;
261 seqs.get(0).addAlignmentAnnotation(annotation);
262 parentPanel.getAlignment().addAnnotation(annotation);
264 // PDB.Temp on Sequence1
265 annotation = new AlignmentAnnotation("Temp", "", 0);
266 annotation.setCalcId("PDB");
267 annotation.visible = true;
268 seqs.get(1).addAlignmentAnnotation(annotation);
269 parentPanel.getAlignment().addAnnotation(annotation);
272 * Expect menu options to show "secondary structure" and "SSP", and to hide
273 * "secondary structure" and "Temp". Tooltip should be calcId.
275 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
277 assertTrue(showMenu.isEnabled());
278 assertTrue(hideMenu.isEnabled());
280 Component[] showOptions = showMenu.getMenuComponents();
281 Component[] hideOptions = hideMenu.getMenuComponents();
283 assertEquals(4, showOptions.length); // includes 'All' and separator
284 assertEquals(4, hideOptions.length);
286 ((JMenuItem) showOptions[0]).getText());
287 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
288 assertEquals(JSeparator.HORIZONTAL,
289 ((JSeparator) showOptions[1]).getOrientation());
290 assertEquals("secondary structure",
291 ((JMenuItem) showOptions[2]).getText());
292 assertEquals("JMOL", ((JMenuItem) showOptions[2]).getToolTipText());
293 assertEquals("SSP", ((JMenuItem) showOptions[3]).getText());
294 assertEquals("JPred", ((JMenuItem) showOptions[3]).getToolTipText());
297 ((JMenuItem) hideOptions[0]).getText());
298 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
299 assertEquals(JSeparator.HORIZONTAL,
300 ((JSeparator) hideOptions[1]).getOrientation());
301 assertEquals("secondary structure",
302 ((JMenuItem) hideOptions[2]).getText());
303 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
304 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
305 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
309 * Test for building menu options with only 'hide' annotation types enabled.
312 public void testBuildAnnotationTypesMenus_showDisabled()
314 JMenu showMenu = new JMenu();
315 JMenu hideMenu = new JMenu();
316 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
318 // make up new annotations and add to sequences and to the alignment
320 // PDB.secondary structure on Sequence0
321 AlignmentAnnotation annotation = new AlignmentAnnotation(
322 "secondary structure", "", 0);
323 annotation.setCalcId("PDB");
324 annotation.visible = true;
325 seqs.get(0).addAlignmentAnnotation(annotation);
326 parentPanel.getAlignment().addAnnotation(annotation);
328 // PDB.Temp on Sequence1
329 annotation = new AlignmentAnnotation("Temp", "", 0);
330 annotation.setCalcId("PDB");
331 annotation.visible = true;
332 seqs.get(1).addAlignmentAnnotation(annotation);
333 parentPanel.getAlignment().addAnnotation(annotation);
336 * Expect menu options to hide "secondary structure" and "Temp". Tooltip
337 * should be calcId. 'Show' menu should be disabled.
339 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
341 assertFalse(showMenu.isEnabled());
342 assertTrue(hideMenu.isEnabled());
344 Component[] showOptions = showMenu.getMenuComponents();
345 Component[] hideOptions = hideMenu.getMenuComponents();
347 assertEquals(2, showOptions.length); // includes 'All' and separator
348 assertEquals(4, hideOptions.length);
349 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
350 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
351 assertEquals(JSeparator.HORIZONTAL,
352 ((JSeparator) showOptions[1]).getOrientation());
354 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
355 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
356 assertEquals(JSeparator.HORIZONTAL,
357 ((JSeparator) hideOptions[1]).getOrientation());
358 assertEquals("secondary structure",
359 ((JMenuItem) hideOptions[2]).getText());
360 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
361 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
362 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
366 * Test for building menu options with only 'show' annotation types enabled.
369 public void testBuildAnnotationTypesMenus_hideDisabled()
371 JMenu showMenu = new JMenu();
372 JMenu hideMenu = new JMenu();
373 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
375 // make up new annotations and add to sequences and to the alignment
377 // PDB.secondary structure on Sequence0
378 AlignmentAnnotation annotation = new AlignmentAnnotation(
379 "secondary structure", "", 0);
380 annotation.setCalcId("PDB");
381 annotation.visible = false;
382 seqs.get(0).addAlignmentAnnotation(annotation);
383 parentPanel.getAlignment().addAnnotation(annotation);
385 // PDB.Temp on Sequence1
386 annotation = new AlignmentAnnotation("Temp", "", 0);
387 annotation.setCalcId("PDB2");
388 annotation.visible = false;
389 seqs.get(1).addAlignmentAnnotation(annotation);
390 parentPanel.getAlignment().addAnnotation(annotation);
393 * Expect menu options to show "secondary structure" and "Temp". Tooltip
394 * should be calcId. 'hide' menu should be disabled.
396 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
398 assertTrue(showMenu.isEnabled());
399 assertFalse(hideMenu.isEnabled());
401 Component[] showOptions = showMenu.getMenuComponents();
402 Component[] hideOptions = hideMenu.getMenuComponents();
404 assertEquals(4, showOptions.length); // includes 'All' and separator
405 assertEquals(2, hideOptions.length);
406 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
407 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
408 assertEquals(JSeparator.HORIZONTAL,
409 ((JSeparator) showOptions[1]).getOrientation());
410 assertEquals("secondary structure",
411 ((JMenuItem) showOptions[2]).getText());
412 assertEquals("PDB", ((JMenuItem) showOptions[2]).getToolTipText());
413 assertEquals("Temp", ((JMenuItem) showOptions[3]).getText());
414 assertEquals("PDB2", ((JMenuItem) showOptions[3]).getToolTipText());
416 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
417 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
418 assertEquals(JSeparator.HORIZONTAL,
419 ((JSeparator) hideOptions[1]).getOrientation());