3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertFalse;
5 import static org.testng.AssertJUnit.assertTrue;
7 import jalview.datamodel.AlignmentAnnotation;
8 import jalview.datamodel.AlignmentI;
9 import jalview.datamodel.Annotation;
10 import jalview.datamodel.SequenceI;
11 import jalview.io.AppletFormatAdapter;
12 import jalview.io.FormatAdapter;
14 import java.awt.Component;
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.List;
19 import javax.swing.JMenu;
20 import javax.swing.JMenuItem;
21 import javax.swing.JPopupMenu;
22 import javax.swing.JSeparator;
24 import org.testng.annotations.BeforeMethod;
25 import org.testng.annotations.Test;
27 public class PopupMenuTest
29 // 4 sequences x 13 positions
30 final static String TEST_DATA = ">FER_CAPAA Ferredoxin\n"
32 + ">FER_CAPAN Ferredoxin, chloroplast precursor\n"
34 + ">FER1_SOLLC Ferredoxin-1, chloroplast precursor\n"
35 + "TIETHKEEELTA-\n" + ">Q93XJ9_SOLTU Ferredoxin I precursor\n"
40 AlignmentPanel parentPanel;
42 PopupMenu testee = null;
45 public void setUp() throws IOException
47 alignment = new FormatAdapter().readFile(TEST_DATA,
48 AppletFormatAdapter.PASTE, "FASTA");
49 AlignFrame af = new AlignFrame(alignment, 700, 500);
50 parentPanel = new AlignmentPanel(af, af.getViewport());
51 testee = new PopupMenu(parentPanel, null, null);
53 for (SequenceI seq : alignment.getSequences())
55 final AlignmentAnnotation annotation = new AlignmentAnnotation("label" + i,
57 annotation.setCalcId("calcId" + i);
58 seq.addAlignmentAnnotation(annotation);
59 annotation.setSequenceRef(seq);
64 public void testConfigureReferenceAnnotationsMenu_noSequenceSelected()
66 JMenuItem menu = new JMenuItem();
67 List<SequenceI> seqs = new ArrayList<SequenceI>();
68 testee.configureReferenceAnnotationsMenu(menu, seqs);
69 assertFalse(menu.isEnabled());
71 menu.setEnabled(true);
72 testee.configureReferenceAnnotationsMenu(menu, null);
73 assertFalse(menu.isEnabled());
77 * Test building the 'add reference annotations' menu for the case where there
78 * are no reference annotations to add to the alignment. The menu item should
82 public void testConfigureReferenceAnnotationsMenu_noReferenceAnnotations()
84 JMenuItem menu = new JMenuItem();
87 * Initial state is that sequences have annotations, and have dataset
88 * sequences, but the dataset sequences have no annotations. Hence nothing
91 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
93 testee.configureReferenceAnnotationsMenu(menu, seqs);
94 assertFalse(menu.isEnabled());
98 * Test building the 'add reference annotations' menu for the case where all
99 * reference annotations are already on the alignment. The menu item should be
103 public void testConfigureReferenceAnnotationsMenu_alreadyAdded()
105 JMenuItem menu = new JMenuItem();
106 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
108 // make up new annotations and add to dataset sequences, sequences and
110 attachReferenceAnnotations(seqs, true, true);
112 testee.configureReferenceAnnotationsMenu(menu, seqs);
113 assertFalse(menu.isEnabled());
117 * Test building the 'add reference annotations' menu for the case where
118 * several reference annotations are on the dataset but not on the sequences.
119 * The menu item should be enabled, and acquire a tooltip which lists the
120 * annotation sources (calcIds) and type (labels).
123 public void testConfigureReferenceAnnotationsMenu()
125 JMenuItem menu = new JMenuItem();
126 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
128 // make up new annotations and add to dataset sequences
129 attachReferenceAnnotations(seqs, false, false);
131 testee.configureReferenceAnnotationsMenu(menu, seqs);
132 assertTrue(menu.isEnabled());
133 String expected = "<html><table width=350 border=0><tr><td align=justify>Add annotations for<br/>JMOL/secondary structure<br/>PBD/Temp</td></tr></table></html>";
134 assertEquals(expected, menu.getToolTipText());
138 * Test building the 'add reference annotations' menu for the case where
139 * several reference annotations are on the dataset and the sequences but not
140 * on the alignment. The menu item should be enabled, and acquire a tooltip
141 * which lists the annotation sources (calcIds) and type (labels).
144 public void testConfigureReferenceAnnotationsMenu_notOnAlignment()
146 JMenuItem menu = new JMenuItem();
147 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
149 // make up new annotations and add to dataset sequences and sequences
150 attachReferenceAnnotations(seqs, true, false);
152 testee.configureReferenceAnnotationsMenu(menu, seqs);
153 assertTrue(menu.isEnabled());
154 String expected = "<html><table width=350 border=0><tr><td align=justify>Add annotations for<br/>JMOL/secondary structure<br/>PBD/Temp</td></tr></table></html>";
155 assertEquals(expected, menu.getToolTipText());
159 * Generate annotations and add to dataset sequences and (optionally)
160 * sequences and/or alignment
163 * @param addToSequence
164 * @param addToAlignment
166 private void attachReferenceAnnotations(List<SequenceI> seqs,
167 boolean addToSequence, boolean addToAlignment)
169 // PDB.secondary structure on Sequence0
170 AlignmentAnnotation annotation = new AlignmentAnnotation(
171 "secondary structure", "", 0);
172 annotation.setCalcId("PBD");
173 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
176 seqs.get(0).addAlignmentAnnotation(annotation);
180 this.alignment.addAnnotation(annotation);
183 // PDB.Temp on Sequence1
184 annotation = new AlignmentAnnotation("Temp", "", 0);
185 annotation.setCalcId("PBD");
186 seqs.get(1).getDatasetSequence().addAlignmentAnnotation(annotation);
189 seqs.get(1).addAlignmentAnnotation(annotation);
193 this.alignment.addAnnotation(annotation);
196 // JMOL.secondary structure on Sequence0
197 annotation = new AlignmentAnnotation("secondary structure", "", 0);
198 annotation.setCalcId("JMOL");
199 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
202 seqs.get(0).addAlignmentAnnotation(annotation);
206 this.alignment.addAnnotation(annotation);
211 * Test building the 'add reference annotations' menu for the case where there
212 * are two alignment views:
214 * <li>in one view, reference annotations have been added (are on the
215 * datasets, sequences and alignment)</li>
216 * <li>in the current view, reference annotations are on the dataset and
217 * sequence, but not the alignment</li>
219 * The menu item should be enabled, and acquire a tooltip which lists the
220 * annotation sources (calcIds) and type (labels).
223 public void testConfigureReferenceAnnotationsMenu_twoViews()
228 * Test for building menu options including 'show' and 'hide' annotation
232 public void testBuildAnnotationTypesMenus()
234 JMenu showMenu = new JMenu();
235 JMenu hideMenu = new JMenu();
236 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
238 // make up new annotations and add to sequences and to the alignment
240 // PDB.secondary structure on Sequence0
241 AlignmentAnnotation annotation = new AlignmentAnnotation(
242 "secondary structure", "", new Annotation[]
244 annotation.setCalcId("PDB");
245 annotation.visible = true;
246 seqs.get(0).addAlignmentAnnotation(annotation);
247 parentPanel.getAlignment().addAnnotation(annotation);
249 // JMOL.secondary structure on Sequence0 - hidden
250 annotation = new AlignmentAnnotation("secondary structure", "", new Annotation[]
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", "", new Annotation[]
260 annotation.setCalcId("JPred");
261 annotation.visible = false;
262 seqs.get(0).addAlignmentAnnotation(annotation);
263 parentPanel.getAlignment().addAnnotation(annotation);
265 // PDB.Temp on Sequence1
266 annotation = new AlignmentAnnotation("Temp", "", new Annotation[]
268 annotation.setCalcId("PDB");
269 annotation.visible = true;
270 seqs.get(1).addAlignmentAnnotation(annotation);
271 parentPanel.getAlignment().addAnnotation(annotation);
274 * Expect menu options to show "secondary structure" and "SSP", and to hide
275 * "secondary structure" and "Temp". Tooltip should be calcId.
277 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
279 assertTrue(showMenu.isEnabled());
280 assertTrue(hideMenu.isEnabled());
282 Component[] showOptions = showMenu.getMenuComponents();
283 Component[] hideOptions = hideMenu.getMenuComponents();
285 assertEquals(4, showOptions.length); // includes 'All' and separator
286 assertEquals(4, hideOptions.length);
288 ((JMenuItem) showOptions[0]).getText());
289 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
290 assertEquals(JSeparator.HORIZONTAL,
291 ((JSeparator) showOptions[1]).getOrientation());
292 assertEquals("secondary structure",
293 ((JMenuItem) showOptions[2]).getText());
294 assertEquals("JMOL", ((JMenuItem) showOptions[2]).getToolTipText());
295 assertEquals("SSP", ((JMenuItem) showOptions[3]).getText());
296 assertEquals("JPred", ((JMenuItem) showOptions[3]).getToolTipText());
299 ((JMenuItem) hideOptions[0]).getText());
300 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
301 assertEquals(JSeparator.HORIZONTAL,
302 ((JSeparator) hideOptions[1]).getOrientation());
303 assertEquals("secondary structure",
304 ((JMenuItem) hideOptions[2]).getText());
305 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
306 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
307 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
311 * Test for building menu options with only 'hide' annotation types enabled.
314 public void testBuildAnnotationTypesMenus_showDisabled()
316 JMenu showMenu = new JMenu();
317 JMenu hideMenu = new JMenu();
318 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
320 // make up new annotations and add to sequences and to the alignment
322 // PDB.secondary structure on Sequence0
323 AlignmentAnnotation annotation = new AlignmentAnnotation(
324 "secondary structure", "", new Annotation[]
326 annotation.setCalcId("PDB");
327 annotation.visible = true;
328 seqs.get(0).addAlignmentAnnotation(annotation);
329 parentPanel.getAlignment().addAnnotation(annotation);
331 // PDB.Temp on Sequence1
332 annotation = new AlignmentAnnotation("Temp", "", new Annotation[]
334 annotation.setCalcId("PDB");
335 annotation.visible = true;
336 seqs.get(1).addAlignmentAnnotation(annotation);
337 parentPanel.getAlignment().addAnnotation(annotation);
340 * Expect menu options to hide "secondary structure" and "Temp". Tooltip
341 * should be calcId. 'Show' menu should be disabled.
343 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
345 assertFalse(showMenu.isEnabled());
346 assertTrue(hideMenu.isEnabled());
348 Component[] showOptions = showMenu.getMenuComponents();
349 Component[] hideOptions = hideMenu.getMenuComponents();
351 assertEquals(2, showOptions.length); // includes 'All' and separator
352 assertEquals(4, hideOptions.length);
353 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
354 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
355 assertEquals(JSeparator.HORIZONTAL,
356 ((JSeparator) showOptions[1]).getOrientation());
358 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
359 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
360 assertEquals(JSeparator.HORIZONTAL,
361 ((JSeparator) hideOptions[1]).getOrientation());
362 assertEquals("secondary structure",
363 ((JMenuItem) hideOptions[2]).getText());
364 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
365 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
366 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
370 * Test for building menu options with only 'show' annotation types enabled.
373 public void testBuildAnnotationTypesMenus_hideDisabled()
375 JMenu showMenu = new JMenu();
376 JMenu hideMenu = new JMenu();
377 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
379 // make up new annotations and add to sequences and to the alignment
381 // PDB.secondary structure on Sequence0
382 AlignmentAnnotation annotation = new AlignmentAnnotation(
383 "secondary structure", "", new Annotation[]
385 annotation.setCalcId("PDB");
386 annotation.visible = false;
387 seqs.get(0).addAlignmentAnnotation(annotation);
388 parentPanel.getAlignment().addAnnotation(annotation);
390 // PDB.Temp on Sequence1
391 annotation = new AlignmentAnnotation("Temp", "", new Annotation[]
393 annotation.setCalcId("PDB2");
394 annotation.visible = false;
395 seqs.get(1).addAlignmentAnnotation(annotation);
396 parentPanel.getAlignment().addAnnotation(annotation);
399 * Expect menu options to show "secondary structure" and "Temp". Tooltip
400 * should be calcId. 'hide' menu should be disabled.
402 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
404 assertTrue(showMenu.isEnabled());
405 assertFalse(hideMenu.isEnabled());
407 Component[] showOptions = showMenu.getMenuComponents();
408 Component[] hideOptions = hideMenu.getMenuComponents();
410 assertEquals(4, showOptions.length); // includes 'All' and separator
411 assertEquals(2, hideOptions.length);
412 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
413 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
414 assertEquals(JSeparator.HORIZONTAL,
415 ((JSeparator) showOptions[1]).getOrientation());
416 assertEquals("secondary structure",
417 ((JMenuItem) showOptions[2]).getText());
418 assertEquals("PDB", ((JMenuItem) showOptions[2]).getToolTipText());
419 assertEquals("Temp", ((JMenuItem) showOptions[3]).getText());
420 assertEquals("PDB2", ((JMenuItem) showOptions[3]).getToolTipText());
422 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
423 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
424 assertEquals(JSeparator.HORIZONTAL,
425 ((JSeparator) hideOptions[1]).getOrientation());