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;
13 import jalview.util.MessageManager;
15 import java.awt.Component;
16 import java.io.IOException;
17 import java.util.ArrayList;
18 import java.util.List;
20 import javax.swing.JMenu;
21 import javax.swing.JMenuItem;
22 import javax.swing.JPopupMenu;
23 import javax.swing.JSeparator;
25 import org.testng.annotations.BeforeMethod;
26 import org.testng.annotations.Test;
28 public class PopupMenuTest
30 // 4 sequences x 13 positions
31 final static String TEST_DATA = ">FER_CAPAA Ferredoxin\n"
33 + ">FER_CAPAN Ferredoxin, chloroplast precursor\n"
35 + ">FER1_SOLLC Ferredoxin-1, chloroplast precursor\n"
36 + "TIETHKEEELTA-\n" + ">Q93XJ9_SOLTU Ferredoxin I precursor\n"
41 AlignmentPanel parentPanel;
43 PopupMenu testee = null;
45 @BeforeMethod(alwaysRun = true)
46 public void setUp() throws IOException
48 alignment = new FormatAdapter().readFile(TEST_DATA,
49 AppletFormatAdapter.PASTE, "FASTA");
50 AlignFrame af = new AlignFrame(alignment, 700, 500);
51 parentPanel = new AlignmentPanel(af, af.getViewport());
52 testee = new PopupMenu(parentPanel, null, null);
54 for (SequenceI seq : alignment.getSequences())
56 final AlignmentAnnotation annotation = new AlignmentAnnotation("label" + i,
58 annotation.setCalcId("calcId" + i);
59 seq.addAlignmentAnnotation(annotation);
60 annotation.setSequenceRef(seq);
64 @Test(groups ={ "Functional" })
65 public void testConfigureReferenceAnnotationsMenu_noSequenceSelected()
67 JMenuItem menu = new JMenuItem();
68 List<SequenceI> seqs = new ArrayList<SequenceI>();
69 testee.configureReferenceAnnotationsMenu(menu, seqs);
70 assertFalse(menu.isEnabled());
72 menu.setEnabled(true);
73 testee.configureReferenceAnnotationsMenu(menu, null);
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
82 @Test(groups ={ "Functional" })
83 public void testConfigureReferenceAnnotationsMenu_noReferenceAnnotations()
85 JMenuItem menu = new JMenuItem();
88 * Initial state is that sequences have annotations, and have dataset
89 * sequences, but the dataset sequences have no annotations. Hence nothing
92 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
94 testee.configureReferenceAnnotationsMenu(menu, seqs);
95 assertFalse(menu.isEnabled());
99 * Test building the 'add reference annotations' menu for the case where all
100 * reference annotations are already on the alignment. The menu item should be
103 @Test(groups ={ "Functional" })
104 public void testConfigureReferenceAnnotationsMenu_alreadyAdded()
106 JMenuItem menu = new JMenuItem();
107 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
109 // make up new annotations and add to dataset sequences, sequences and
111 attachReferenceAnnotations(seqs, true, true);
113 testee.configureReferenceAnnotationsMenu(menu, seqs);
114 assertFalse(menu.isEnabled());
118 * Test building the 'add reference annotations' menu for the case where
119 * several reference annotations are on the dataset but not on the sequences.
120 * The menu item should be enabled, and acquire a tooltip which lists the
121 * annotation sources (calcIds) and type (labels).
123 @Test(groups ={ "Functional" })
124 public void testConfigureReferenceAnnotationsMenu()
126 JMenuItem menu = new JMenuItem();
127 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
129 // make up new annotations and add to dataset sequences
130 attachReferenceAnnotations(seqs, false, false);
132 testee.configureReferenceAnnotationsMenu(menu, seqs);
133 assertTrue(menu.isEnabled());
134 String s = MessageManager.getString("label.add_annotations_for");
135 String expected = "<html><style> p.ttip {width: 350; text-align: justify; word-wrap: break-word;}</style><p class=\"ttip\">"
136 + s + "<br/>Jmol/secondary structure<br/>PDB/Temp</p></html>";
137 assertEquals(expected, menu.getToolTipText());
141 * Test building the 'add reference annotations' menu for the case where
142 * several reference annotations are on the dataset and the sequences but not
143 * on the alignment. The menu item should be enabled, and acquire a tooltip
144 * which lists the annotation sources (calcIds) and type (labels).
146 @Test(groups ={ "Functional" })
147 public void testConfigureReferenceAnnotationsMenu_notOnAlignment()
149 JMenuItem menu = new JMenuItem();
150 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
152 // make up new annotations and add to dataset sequences and sequences
153 attachReferenceAnnotations(seqs, true, false);
155 testee.configureReferenceAnnotationsMenu(menu, seqs);
156 assertTrue(menu.isEnabled());
157 String s = MessageManager.getString("label.add_annotations_for");
158 String expected = "<html><style> p.ttip {width: 350; text-align: justify; word-wrap: break-word;}</style><p class=\"ttip\">"
159 + s + "<br/>Jmol/secondary structure<br/>PDB/Temp</p></html>";
160 assertEquals(expected, menu.getToolTipText());
164 * Generate annotations and add to dataset sequences and (optionally)
165 * sequences and/or alignment
168 * @param addToSequence
169 * @param addToAlignment
171 private void attachReferenceAnnotations(List<SequenceI> seqs,
172 boolean addToSequence, boolean addToAlignment)
174 // PDB.secondary structure on Sequence0
175 AlignmentAnnotation annotation = new AlignmentAnnotation(
176 "secondary structure", "", 0);
177 annotation.setCalcId("PDB");
178 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
181 seqs.get(0).addAlignmentAnnotation(annotation);
185 this.alignment.addAnnotation(annotation);
188 // PDB.Temp on Sequence1
189 annotation = new AlignmentAnnotation("Temp", "", 0);
190 annotation.setCalcId("PDB");
191 seqs.get(1).getDatasetSequence().addAlignmentAnnotation(annotation);
194 seqs.get(1).addAlignmentAnnotation(annotation);
198 this.alignment.addAnnotation(annotation);
201 // JMOL.secondary structure on Sequence0
202 annotation = new AlignmentAnnotation("secondary structure", "", 0);
203 annotation.setCalcId("Jmol");
204 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
207 seqs.get(0).addAlignmentAnnotation(annotation);
211 this.alignment.addAnnotation(annotation);
216 * Test building the 'add reference annotations' menu for the case where there
217 * are two alignment views:
219 * <li>in one view, reference annotations have been added (are on the
220 * datasets, sequences and alignment)</li>
221 * <li>in the current view, reference annotations are on the dataset and
222 * sequence, but not the alignment</li>
224 * The menu item should be enabled, and acquire a tooltip which lists the
225 * annotation sources (calcIds) and type (labels).
227 @Test(groups ={ "Functional" })
228 public void testConfigureReferenceAnnotationsMenu_twoViews()
233 * Test for building menu options including 'show' and 'hide' annotation
236 @Test(groups ={ "Functional" })
237 public void testBuildAnnotationTypesMenus()
239 JMenu showMenu = new JMenu();
240 JMenu hideMenu = new JMenu();
241 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
243 // make up new annotations and add to sequences and to the alignment
245 // PDB.secondary structure on Sequence0
246 AlignmentAnnotation annotation = new AlignmentAnnotation(
247 "secondary structure", "", new Annotation[]
249 annotation.setCalcId("PDB");
250 annotation.visible = true;
251 seqs.get(0).addAlignmentAnnotation(annotation);
252 parentPanel.getAlignment().addAnnotation(annotation);
254 // JMOL.secondary structure on Sequence0 - hidden
255 annotation = new AlignmentAnnotation("secondary structure", "", new Annotation[]
257 annotation.setCalcId("JMOL");
258 annotation.visible = false;
259 seqs.get(0).addAlignmentAnnotation(annotation);
260 parentPanel.getAlignment().addAnnotation(annotation);
262 // Jpred.SSP on Sequence0 - hidden
263 annotation = new AlignmentAnnotation("SSP", "", new Annotation[]
265 annotation.setCalcId("JPred");
266 annotation.visible = false;
267 seqs.get(0).addAlignmentAnnotation(annotation);
268 parentPanel.getAlignment().addAnnotation(annotation);
270 // PDB.Temp on Sequence1
271 annotation = new AlignmentAnnotation("Temp", "", new Annotation[]
273 annotation.setCalcId("PDB");
274 annotation.visible = true;
275 seqs.get(1).addAlignmentAnnotation(annotation);
276 parentPanel.getAlignment().addAnnotation(annotation);
279 * Expect menu options to show "secondary structure" and "SSP", and to hide
280 * "secondary structure" and "Temp". Tooltip should be calcId.
282 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
284 assertTrue(showMenu.isEnabled());
285 assertTrue(hideMenu.isEnabled());
287 Component[] showOptions = showMenu.getMenuComponents();
288 Component[] hideOptions = hideMenu.getMenuComponents();
290 assertEquals(4, showOptions.length); // includes 'All' and separator
291 assertEquals(4, hideOptions.length);
293 ((JMenuItem) showOptions[0]).getText());
294 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
295 assertEquals(JSeparator.HORIZONTAL,
296 ((JSeparator) showOptions[1]).getOrientation());
297 assertEquals("secondary structure",
298 ((JMenuItem) showOptions[2]).getText());
299 assertEquals("JMOL", ((JMenuItem) showOptions[2]).getToolTipText());
300 assertEquals("SSP", ((JMenuItem) showOptions[3]).getText());
301 assertEquals("JPred", ((JMenuItem) showOptions[3]).getToolTipText());
304 ((JMenuItem) hideOptions[0]).getText());
305 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
306 assertEquals(JSeparator.HORIZONTAL,
307 ((JSeparator) hideOptions[1]).getOrientation());
308 assertEquals("secondary structure",
309 ((JMenuItem) hideOptions[2]).getText());
310 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
311 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
312 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
316 * Test for building menu options with only 'hide' annotation types enabled.
318 @Test(groups ={ "Functional" })
319 public void testBuildAnnotationTypesMenus_showDisabled()
321 JMenu showMenu = new JMenu();
322 JMenu hideMenu = new JMenu();
323 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
325 // make up new annotations and add to sequences and to the alignment
327 // PDB.secondary structure on Sequence0
328 AlignmentAnnotation annotation = new AlignmentAnnotation(
329 "secondary structure", "", new Annotation[]
331 annotation.setCalcId("PDB");
332 annotation.visible = true;
333 seqs.get(0).addAlignmentAnnotation(annotation);
334 parentPanel.getAlignment().addAnnotation(annotation);
336 // PDB.Temp on Sequence1
337 annotation = new AlignmentAnnotation("Temp", "", new Annotation[]
339 annotation.setCalcId("PDB");
340 annotation.visible = true;
341 seqs.get(1).addAlignmentAnnotation(annotation);
342 parentPanel.getAlignment().addAnnotation(annotation);
345 * Expect menu options to hide "secondary structure" and "Temp". Tooltip
346 * should be calcId. 'Show' menu should be disabled.
348 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
350 assertFalse(showMenu.isEnabled());
351 assertTrue(hideMenu.isEnabled());
353 Component[] showOptions = showMenu.getMenuComponents();
354 Component[] hideOptions = hideMenu.getMenuComponents();
356 assertEquals(2, showOptions.length); // includes 'All' and separator
357 assertEquals(4, hideOptions.length);
358 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
359 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
360 assertEquals(JSeparator.HORIZONTAL,
361 ((JSeparator) showOptions[1]).getOrientation());
363 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
364 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
365 assertEquals(JSeparator.HORIZONTAL,
366 ((JSeparator) hideOptions[1]).getOrientation());
367 assertEquals("secondary structure",
368 ((JMenuItem) hideOptions[2]).getText());
369 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
370 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
371 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
375 * Test for building menu options with only 'show' annotation types enabled.
377 @Test(groups ={ "Functional" })
378 public void testBuildAnnotationTypesMenus_hideDisabled()
380 JMenu showMenu = new JMenu();
381 JMenu hideMenu = new JMenu();
382 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
384 // make up new annotations and add to sequences and to the alignment
386 // PDB.secondary structure on Sequence0
387 AlignmentAnnotation annotation = new AlignmentAnnotation(
388 "secondary structure", "", new Annotation[]
390 annotation.setCalcId("PDB");
391 annotation.visible = false;
392 seqs.get(0).addAlignmentAnnotation(annotation);
393 parentPanel.getAlignment().addAnnotation(annotation);
395 // PDB.Temp on Sequence1
396 annotation = new AlignmentAnnotation("Temp", "", new Annotation[]
398 annotation.setCalcId("PDB2");
399 annotation.visible = false;
400 seqs.get(1).addAlignmentAnnotation(annotation);
401 parentPanel.getAlignment().addAnnotation(annotation);
404 * Expect menu options to show "secondary structure" and "Temp". Tooltip
405 * should be calcId. 'hide' menu should be disabled.
407 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
409 assertTrue(showMenu.isEnabled());
410 assertFalse(hideMenu.isEnabled());
412 Component[] showOptions = showMenu.getMenuComponents();
413 Component[] hideOptions = hideMenu.getMenuComponents();
415 assertEquals(4, showOptions.length); // includes 'All' and separator
416 assertEquals(2, hideOptions.length);
417 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
418 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
419 assertEquals(JSeparator.HORIZONTAL,
420 ((JSeparator) showOptions[1]).getOrientation());
421 assertEquals("secondary structure",
422 ((JMenuItem) showOptions[2]).getText());
423 assertEquals("PDB", ((JMenuItem) showOptions[2]).getToolTipText());
424 assertEquals("Temp", ((JMenuItem) showOptions[3]).getText());
425 assertEquals("PDB2", ((JMenuItem) showOptions[3]).getToolTipText());
427 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
428 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
429 assertEquals(JSeparator.HORIZONTAL,
430 ((JSeparator) hideOptions[1]).getOrientation());