3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
7 import java.awt.Component;
8 import java.io.IOException;
9 import java.util.ArrayList;
10 import java.util.List;
12 import javax.swing.JMenu;
13 import javax.swing.JMenuItem;
14 import javax.swing.JPopupMenu;
15 import javax.swing.JSeparator;
17 import org.junit.Before;
18 import org.junit.Test;
20 import jalview.datamodel.AlignmentAnnotation;
21 import jalview.datamodel.AlignmentI;
22 import jalview.datamodel.SequenceI;
23 import jalview.io.AppletFormatAdapter;
24 import jalview.io.FormatAdapter;
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 menu.setEnabled(true);
71 testee.configureReferenceAnnotationsMenu(menu, null);
72 assertFalse(menu.isEnabled());
76 * Test building the 'add reference annotations' menu for the case where there
77 * are no reference annotations to add to the alignment. The menu item should
81 public void testConfigureReferenceAnnotationsMenu_noReferenceAnnotations()
83 JMenuItem menu = new JMenuItem();
86 * Initial state is that sequences have annotations, and have dataset
87 * sequences, but the dataset sequences have no annotations. Hence nothing
90 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
92 testee.configureReferenceAnnotationsMenu(menu, seqs);
93 assertFalse(menu.isEnabled());
97 * Test building the 'add reference annotations' menu for the case where all
98 * reference annotations are already on the alignment. The menu item should be
102 public void testConfigureReferenceAnnotationsMenu_alreadyAdded()
104 JMenuItem menu = new JMenuItem();
105 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
107 // make up new annotations and add to dataset sequences, sequences and
109 attachReferenceAnnotations(seqs, true, true);
111 testee.configureReferenceAnnotationsMenu(menu, seqs);
112 assertFalse(menu.isEnabled());
116 * Test building the 'add reference annotations' menu for the case where
117 * several reference annotations are on the dataset but not on the sequences.
118 * The menu item should be enabled, and acquire a tooltip which lists the
119 * annotation sources (calcIds) and type (labels).
122 public void testConfigureReferenceAnnotationsMenu()
124 JMenuItem menu = new JMenuItem();
125 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
127 // make up new annotations and add to dataset sequences
128 attachReferenceAnnotations(seqs, false, false);
130 testee.configureReferenceAnnotationsMenu(menu, seqs);
131 assertTrue(menu.isEnabled());
132 String expected = "<html><table width=350 border=0><tr><td>Add annotations for<br/>JMOL/secondary structure<br/>PBD/Temp</td></tr></table></html>";
133 assertEquals(expected, menu.getToolTipText());
137 * Test building the 'add reference annotations' menu for the case where
138 * several reference annotations are on the dataset and the sequences but not
139 * on the alignment. The menu item should be enabled, and acquire a tooltip
140 * which lists the annotation sources (calcIds) and type (labels).
143 public void testConfigureReferenceAnnotationsMenu_notOnAlignment()
145 JMenuItem menu = new JMenuItem();
146 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
148 // make up new annotations and add to dataset sequences and sequences
149 attachReferenceAnnotations(seqs, true, false);
151 testee.configureReferenceAnnotationsMenu(menu, seqs);
152 assertTrue(menu.isEnabled());
153 String expected = "<html><table width=350 border=0><tr><td>Add annotations for<br/>JMOL/secondary structure<br/>PBD/Temp</td></tr></table></html>";
154 assertEquals(expected, menu.getToolTipText());
158 * Generate annotations and add to dataset sequences and (optionally)
159 * sequences and/or alignment
162 * @param addToSequence
163 * @param addToAlignment
165 private void attachReferenceAnnotations(List<SequenceI> seqs,
166 boolean addToSequence, boolean addToAlignment)
168 // PDB.secondary structure on Sequence0
169 AlignmentAnnotation annotation = new AlignmentAnnotation(
170 "secondary structure", "", 0);
171 annotation.setCalcId("PBD");
172 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
175 seqs.get(0).addAlignmentAnnotation(annotation);
179 this.alignment.addAnnotation(annotation);
182 // PDB.Temp on Sequence1
183 annotation = new AlignmentAnnotation("Temp", "", 0);
184 annotation.setCalcId("PBD");
185 seqs.get(1).getDatasetSequence().addAlignmentAnnotation(annotation);
188 seqs.get(1).addAlignmentAnnotation(annotation);
192 this.alignment.addAnnotation(annotation);
195 // JMOL.secondary structure on Sequence0
196 annotation = new AlignmentAnnotation("secondary structure", "", 0);
197 annotation.setCalcId("JMOL");
198 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
201 seqs.get(0).addAlignmentAnnotation(annotation);
205 this.alignment.addAnnotation(annotation);
210 * Test building the 'add reference annotations' menu for the case where there
211 * are two alignment views:
213 * <li>in one view, reference annotations have been added (are on the
214 * datasets, sequences and alignment)</li>
215 * <li>in the current view, reference annotations are on the dataset and
216 * sequence, but not the alignment</li>
218 * The menu item should be enabled, and acquire a tooltip which lists the
219 * annotation sources (calcIds) and type (labels).
222 public void testConfigureReferenceAnnotationsMenu_twoViews()
227 * Test for building menu options including 'show' and 'hide' annotation
231 public void testBuildAnnotationTypesMenus()
233 JMenu showMenu = new JMenu();
234 JMenu hideMenu = new JMenu();
235 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
237 // make up new annotations and add to sequences and to the alignment
239 // PDB.secondary structure on Sequence0
240 AlignmentAnnotation annotation = new AlignmentAnnotation(
241 "secondary structure", "", 0);
242 annotation.setCalcId("PDB");
243 annotation.visible = true;
244 seqs.get(0).addAlignmentAnnotation(annotation);
245 parentPanel.getAlignment().addAnnotation(annotation);
247 // JMOL.secondary structure on Sequence0 - hidden
248 annotation = new AlignmentAnnotation("secondary structure", "", 0);
249 annotation.setCalcId("JMOL");
250 annotation.visible = false;
251 seqs.get(0).addAlignmentAnnotation(annotation);
252 parentPanel.getAlignment().addAnnotation(annotation);
254 // Jpred.SSP on Sequence0 - hidden
255 annotation = new AlignmentAnnotation("SSP", "", 0);
256 annotation.setCalcId("JPred");
257 annotation.visible = false;
258 seqs.get(0).addAlignmentAnnotation(annotation);
259 parentPanel.getAlignment().addAnnotation(annotation);
261 // PDB.Temp on Sequence1
262 annotation = new AlignmentAnnotation("Temp", "", 0);
263 annotation.setCalcId("PDB");
264 annotation.visible = true;
265 seqs.get(1).addAlignmentAnnotation(annotation);
266 parentPanel.getAlignment().addAnnotation(annotation);
269 * Expect menu options to show "secondary structure" and "SSP", and to hide
270 * "secondary structure" and "Temp". Tooltip should be calcId.
272 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
274 assertTrue(showMenu.isEnabled());
275 assertTrue(hideMenu.isEnabled());
277 Component[] showOptions = showMenu.getMenuComponents();
278 Component[] hideOptions = hideMenu.getMenuComponents();
280 assertEquals(4, showOptions.length); // includes 'All' and separator
281 assertEquals(4, hideOptions.length);
283 ((JMenuItem) showOptions[0]).getText());
284 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
285 assertEquals(JSeparator.HORIZONTAL,
286 ((JSeparator) showOptions[1]).getOrientation());
287 assertEquals("secondary structure",
288 ((JMenuItem) showOptions[2]).getText());
289 assertEquals("JMOL", ((JMenuItem) showOptions[2]).getToolTipText());
290 assertEquals("SSP", ((JMenuItem) showOptions[3]).getText());
291 assertEquals("JPred", ((JMenuItem) showOptions[3]).getToolTipText());
294 ((JMenuItem) hideOptions[0]).getText());
295 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
296 assertEquals(JSeparator.HORIZONTAL,
297 ((JSeparator) hideOptions[1]).getOrientation());
298 assertEquals("secondary structure",
299 ((JMenuItem) hideOptions[2]).getText());
300 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
301 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
302 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
306 * Test for building menu options with only 'hide' annotation types enabled.
309 public void testBuildAnnotationTypesMenus_showDisabled()
311 JMenu showMenu = new JMenu();
312 JMenu hideMenu = new JMenu();
313 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
315 // make up new annotations and add to sequences and to the alignment
317 // PDB.secondary structure on Sequence0
318 AlignmentAnnotation annotation = new AlignmentAnnotation(
319 "secondary structure", "", 0);
320 annotation.setCalcId("PDB");
321 annotation.visible = true;
322 seqs.get(0).addAlignmentAnnotation(annotation);
323 parentPanel.getAlignment().addAnnotation(annotation);
325 // PDB.Temp on Sequence1
326 annotation = new AlignmentAnnotation("Temp", "", 0);
327 annotation.setCalcId("PDB");
328 annotation.visible = true;
329 seqs.get(1).addAlignmentAnnotation(annotation);
330 parentPanel.getAlignment().addAnnotation(annotation);
333 * Expect menu options to hide "secondary structure" and "Temp". Tooltip
334 * should be calcId. 'Show' menu should be disabled.
336 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
338 assertFalse(showMenu.isEnabled());
339 assertTrue(hideMenu.isEnabled());
341 Component[] showOptions = showMenu.getMenuComponents();
342 Component[] hideOptions = hideMenu.getMenuComponents();
344 assertEquals(2, showOptions.length); // includes 'All' and separator
345 assertEquals(4, hideOptions.length);
346 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
347 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
348 assertEquals(JSeparator.HORIZONTAL,
349 ((JSeparator) showOptions[1]).getOrientation());
351 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
352 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
353 assertEquals(JSeparator.HORIZONTAL,
354 ((JSeparator) hideOptions[1]).getOrientation());
355 assertEquals("secondary structure",
356 ((JMenuItem) hideOptions[2]).getText());
357 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
358 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
359 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
363 * Test for building menu options with only 'show' annotation types enabled.
366 public void testBuildAnnotationTypesMenus_hideDisabled()
368 JMenu showMenu = new JMenu();
369 JMenu hideMenu = new JMenu();
370 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
372 // make up new annotations and add to sequences and to the alignment
374 // PDB.secondary structure on Sequence0
375 AlignmentAnnotation annotation = new AlignmentAnnotation(
376 "secondary structure", "", 0);
377 annotation.setCalcId("PDB");
378 annotation.visible = false;
379 seqs.get(0).addAlignmentAnnotation(annotation);
380 parentPanel.getAlignment().addAnnotation(annotation);
382 // PDB.Temp on Sequence1
383 annotation = new AlignmentAnnotation("Temp", "", 0);
384 annotation.setCalcId("PDB2");
385 annotation.visible = false;
386 seqs.get(1).addAlignmentAnnotation(annotation);
387 parentPanel.getAlignment().addAnnotation(annotation);
390 * Expect menu options to show "secondary structure" and "Temp". Tooltip
391 * should be calcId. 'hide' menu should be disabled.
393 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
395 assertTrue(showMenu.isEnabled());
396 assertFalse(hideMenu.isEnabled());
398 Component[] showOptions = showMenu.getMenuComponents();
399 Component[] hideOptions = hideMenu.getMenuComponents();
401 assertEquals(4, showOptions.length); // includes 'All' and separator
402 assertEquals(2, hideOptions.length);
403 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
404 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
405 assertEquals(JSeparator.HORIZONTAL,
406 ((JSeparator) showOptions[1]).getOrientation());
407 assertEquals("secondary structure",
408 ((JMenuItem) showOptions[2]).getText());
409 assertEquals("PDB", ((JMenuItem) showOptions[2]).getToolTipText());
410 assertEquals("Temp", ((JMenuItem) showOptions[3]).getText());
411 assertEquals("PDB2", ((JMenuItem) showOptions[3]).getToolTipText());
413 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
414 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
415 assertEquals(JSeparator.HORIZONTAL,
416 ((JSeparator) hideOptions[1]).getOrientation());