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(
57 "label" + i, "desc" + i, 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[] {});
248 annotation.setCalcId("PDB");
249 annotation.visible = true;
250 seqs.get(0).addAlignmentAnnotation(annotation);
251 parentPanel.getAlignment().addAnnotation(annotation);
253 // JMOL.secondary structure on Sequence0 - hidden
254 annotation = new AlignmentAnnotation("secondary structure", "",
255 new Annotation[] {});
256 annotation.setCalcId("JMOL");
257 annotation.visible = false;
258 seqs.get(0).addAlignmentAnnotation(annotation);
259 parentPanel.getAlignment().addAnnotation(annotation);
261 // Jpred.SSP on Sequence0 - hidden
262 annotation = new AlignmentAnnotation("SSP", "", new Annotation[] {});
263 annotation.setCalcId("JPred");
264 annotation.visible = false;
265 seqs.get(0).addAlignmentAnnotation(annotation);
266 parentPanel.getAlignment().addAnnotation(annotation);
268 // PDB.Temp on Sequence1
269 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
270 annotation.setCalcId("PDB");
271 annotation.visible = true;
272 seqs.get(1).addAlignmentAnnotation(annotation);
273 parentPanel.getAlignment().addAnnotation(annotation);
276 * Expect menu options to show "secondary structure" and "SSP", and to hide
277 * "secondary structure" and "Temp". Tooltip should be calcId.
279 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
281 assertTrue(showMenu.isEnabled());
282 assertTrue(hideMenu.isEnabled());
284 Component[] showOptions = showMenu.getMenuComponents();
285 Component[] hideOptions = hideMenu.getMenuComponents();
287 assertEquals(4, showOptions.length); // includes 'All' and separator
288 assertEquals(4, hideOptions.length);
289 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
290 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
291 assertEquals(JSeparator.HORIZONTAL,
292 ((JSeparator) showOptions[1]).getOrientation());
293 assertEquals("secondary structure",
294 ((JMenuItem) showOptions[2]).getText());
295 assertEquals("JMOL", ((JMenuItem) showOptions[2]).getToolTipText());
296 assertEquals("SSP", ((JMenuItem) showOptions[3]).getText());
297 assertEquals("JPred", ((JMenuItem) showOptions[3]).getToolTipText());
299 assertEquals("All", ((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.
313 @Test(groups = { "Functional" })
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[] {});
325 annotation.setCalcId("PDB");
326 annotation.visible = true;
327 seqs.get(0).addAlignmentAnnotation(annotation);
328 parentPanel.getAlignment().addAnnotation(annotation);
330 // PDB.Temp on Sequence1
331 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
332 annotation.setCalcId("PDB");
333 annotation.visible = true;
334 seqs.get(1).addAlignmentAnnotation(annotation);
335 parentPanel.getAlignment().addAnnotation(annotation);
338 * Expect menu options to hide "secondary structure" and "Temp". Tooltip
339 * should be calcId. 'Show' menu should be disabled.
341 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
343 assertFalse(showMenu.isEnabled());
344 assertTrue(hideMenu.isEnabled());
346 Component[] showOptions = showMenu.getMenuComponents();
347 Component[] hideOptions = hideMenu.getMenuComponents();
349 assertEquals(2, showOptions.length); // includes 'All' and separator
350 assertEquals(4, hideOptions.length);
351 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
352 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
353 assertEquals(JSeparator.HORIZONTAL,
354 ((JSeparator) showOptions[1]).getOrientation());
356 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
357 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
358 assertEquals(JSeparator.HORIZONTAL,
359 ((JSeparator) hideOptions[1]).getOrientation());
360 assertEquals("secondary structure",
361 ((JMenuItem) hideOptions[2]).getText());
362 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
363 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
364 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
368 * Test for building menu options with only 'show' annotation types enabled.
370 @Test(groups = { "Functional" })
371 public void testBuildAnnotationTypesMenus_hideDisabled()
373 JMenu showMenu = new JMenu();
374 JMenu hideMenu = new JMenu();
375 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
377 // make up new annotations and add to sequences and to the alignment
379 // PDB.secondary structure on Sequence0
380 AlignmentAnnotation annotation = new AlignmentAnnotation(
381 "secondary structure", "", new Annotation[] {});
382 annotation.setCalcId("PDB");
383 annotation.visible = false;
384 seqs.get(0).addAlignmentAnnotation(annotation);
385 parentPanel.getAlignment().addAnnotation(annotation);
387 // PDB.Temp on Sequence1
388 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
389 annotation.setCalcId("PDB2");
390 annotation.visible = false;
391 seqs.get(1).addAlignmentAnnotation(annotation);
392 parentPanel.getAlignment().addAnnotation(annotation);
395 * Expect menu options to show "secondary structure" and "Temp". Tooltip
396 * should be calcId. 'hide' menu should be disabled.
398 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
400 assertTrue(showMenu.isEnabled());
401 assertFalse(hideMenu.isEnabled());
403 Component[] showOptions = showMenu.getMenuComponents();
404 Component[] hideOptions = hideMenu.getMenuComponents();
406 assertEquals(4, showOptions.length); // includes 'All' and separator
407 assertEquals(2, hideOptions.length);
408 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
409 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
410 assertEquals(JSeparator.HORIZONTAL,
411 ((JSeparator) showOptions[1]).getOrientation());
412 assertEquals("secondary structure",
413 ((JMenuItem) showOptions[2]).getText());
414 assertEquals("PDB", ((JMenuItem) showOptions[2]).getToolTipText());
415 assertEquals("Temp", ((JMenuItem) showOptions[3]).getText());
416 assertEquals("PDB2", ((JMenuItem) showOptions[3]).getToolTipText());
418 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
419 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
420 assertEquals(JSeparator.HORIZONTAL,
421 ((JSeparator) hideOptions[1]).getOrientation());