2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertTrue;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.Annotation;
30 import jalview.datamodel.SequenceI;
31 import jalview.io.DataSourceType;
32 import jalview.io.FileFormat;
33 import jalview.io.FormatAdapter;
34 import jalview.util.MessageManager;
36 import java.awt.Component;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.List;
41 import javax.swing.JMenu;
42 import javax.swing.JMenuItem;
43 import javax.swing.JPopupMenu;
44 import javax.swing.JSeparator;
46 import org.testng.annotations.BeforeMethod;
47 import org.testng.annotations.Test;
49 public class PopupMenuTest
51 // 4 sequences x 13 positions
52 final static String TEST_DATA = ">FER_CAPAA Ferredoxin\n"
54 + ">FER_CAPAN Ferredoxin, chloroplast precursor\n"
56 + ">FER1_SOLLC Ferredoxin-1, chloroplast precursor\n"
57 + "TIETHKEEELTA-\n" + ">Q93XJ9_SOLTU Ferredoxin I precursor\n"
62 AlignmentPanel parentPanel;
64 PopupMenu testee = null;
66 @BeforeMethod(alwaysRun = true)
67 public void setUp() throws IOException
69 alignment = new FormatAdapter().readFile(TEST_DATA,
70 DataSourceType.PASTE, FileFormat.Fasta);
71 AlignFrame af = new AlignFrame(alignment, 700, 500);
72 parentPanel = new AlignmentPanel(af, af.getViewport());
73 testee = new PopupMenu(parentPanel, null, null);
75 for (SequenceI seq : alignment.getSequences())
77 final AlignmentAnnotation annotation = new AlignmentAnnotation(
78 "label" + i, "desc" + i, i);
79 annotation.setCalcId("calcId" + i);
80 seq.addAlignmentAnnotation(annotation);
81 annotation.setSequenceRef(seq);
85 @Test(groups = { "Functional" })
86 public void testConfigureReferenceAnnotationsMenu_noSequenceSelected()
88 JMenuItem menu = new JMenuItem();
89 List<SequenceI> seqs = new ArrayList<SequenceI>();
90 testee.configureReferenceAnnotationsMenu(menu, seqs);
91 assertFalse(menu.isEnabled());
93 menu.setEnabled(true);
94 testee.configureReferenceAnnotationsMenu(menu, null);
95 assertFalse(menu.isEnabled());
99 * Test building the 'add reference annotations' menu for the case where there
100 * are no reference annotations to add to the alignment. The menu item should
103 @Test(groups = { "Functional" })
104 public void testConfigureReferenceAnnotationsMenu_noReferenceAnnotations()
106 JMenuItem menu = new JMenuItem();
109 * Initial state is that sequences have annotations, and have dataset
110 * sequences, but the dataset sequences have no annotations. Hence nothing
113 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
115 testee.configureReferenceAnnotationsMenu(menu, seqs);
116 assertFalse(menu.isEnabled());
120 * Test building the 'add reference annotations' menu for the case where all
121 * reference annotations are already on the alignment. The menu item should be
124 @Test(groups = { "Functional" })
125 public void testConfigureReferenceAnnotationsMenu_alreadyAdded()
127 JMenuItem menu = new JMenuItem();
128 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
130 // make up new annotations and add to dataset sequences, sequences and
132 attachReferenceAnnotations(seqs, true, true);
134 testee.configureReferenceAnnotationsMenu(menu, seqs);
135 assertFalse(menu.isEnabled());
139 * Test building the 'add reference annotations' menu for the case where
140 * several reference annotations are on the dataset but not on the sequences.
141 * The menu item should be enabled, and acquire a tooltip which lists the
142 * annotation sources (calcIds) and type (labels).
144 @Test(groups = { "Functional" })
145 public void testConfigureReferenceAnnotationsMenu()
147 JMenuItem menu = new JMenuItem();
148 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
150 // make up new annotations and add to dataset sequences
151 attachReferenceAnnotations(seqs, false, false);
153 testee.configureReferenceAnnotationsMenu(menu, seqs);
154 assertTrue(menu.isEnabled());
155 String s = MessageManager.getString("label.add_annotations_for");
156 String expected = "<html><style> p.ttip {width: 350; text-align: justify; word-wrap: break-word;}</style><p class=\"ttip\">"
157 + s + "<br/>Jmol/secondary structure<br/>PDB/Temp</p></html>";
158 assertEquals(expected, menu.getToolTipText());
162 * Test building the 'add reference annotations' menu for the case where
163 * several reference annotations are on the dataset and the sequences but not
164 * on the alignment. The menu item should be enabled, and acquire a tooltip
165 * which lists the annotation sources (calcIds) and type (labels).
167 @Test(groups = { "Functional" })
168 public void testConfigureReferenceAnnotationsMenu_notOnAlignment()
170 JMenuItem menu = new JMenuItem();
171 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
173 // make up new annotations and add to dataset sequences and sequences
174 attachReferenceAnnotations(seqs, true, false);
176 testee.configureReferenceAnnotationsMenu(menu, seqs);
177 assertTrue(menu.isEnabled());
178 String s = MessageManager.getString("label.add_annotations_for");
179 String expected = "<html><style> p.ttip {width: 350; text-align: justify; word-wrap: break-word;}</style><p class=\"ttip\">"
180 + s + "<br/>Jmol/secondary structure<br/>PDB/Temp</p></html>";
181 assertEquals(expected, menu.getToolTipText());
185 * Generate annotations and add to dataset sequences and (optionally)
186 * sequences and/or alignment
189 * @param addToSequence
190 * @param addToAlignment
192 private void attachReferenceAnnotations(List<SequenceI> seqs,
193 boolean addToSequence, boolean addToAlignment)
195 // PDB.secondary structure on Sequence0
196 AlignmentAnnotation annotation = new AlignmentAnnotation(
197 "secondary structure", "", 0);
198 annotation.setCalcId("PDB");
199 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
202 seqs.get(0).addAlignmentAnnotation(annotation);
206 this.alignment.addAnnotation(annotation);
209 // PDB.Temp on Sequence1
210 annotation = new AlignmentAnnotation("Temp", "", 0);
211 annotation.setCalcId("PDB");
212 seqs.get(1).getDatasetSequence().addAlignmentAnnotation(annotation);
215 seqs.get(1).addAlignmentAnnotation(annotation);
219 this.alignment.addAnnotation(annotation);
222 // JMOL.secondary structure on Sequence0
223 annotation = new AlignmentAnnotation("secondary structure", "", 0);
224 annotation.setCalcId("Jmol");
225 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
228 seqs.get(0).addAlignmentAnnotation(annotation);
232 this.alignment.addAnnotation(annotation);
237 * Test building the 'add reference annotations' menu for the case where there
238 * are two alignment views:
240 * <li>in one view, reference annotations have been added (are on the
241 * datasets, sequences and alignment)</li>
242 * <li>in the current view, reference annotations are on the dataset and
243 * sequence, but not the alignment</li>
245 * The menu item should be enabled, and acquire a tooltip which lists the
246 * annotation sources (calcIds) and type (labels).
248 @Test(groups = { "Functional" })
249 public void testConfigureReferenceAnnotationsMenu_twoViews()
254 * Test for building menu options including 'show' and 'hide' annotation
257 @Test(groups = { "Functional" })
258 public void testBuildAnnotationTypesMenus()
260 JMenu showMenu = new JMenu();
261 JMenu hideMenu = new JMenu();
262 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
264 // make up new annotations and add to sequences and to the alignment
266 // PDB.secondary structure on Sequence0
267 AlignmentAnnotation annotation = new AlignmentAnnotation(
268 "secondary structure", "", new Annotation[] {});
269 annotation.setCalcId("PDB");
270 annotation.visible = true;
271 seqs.get(0).addAlignmentAnnotation(annotation);
272 parentPanel.getAlignment().addAnnotation(annotation);
274 // JMOL.secondary structure on Sequence0 - hidden
275 annotation = new AlignmentAnnotation("secondary structure", "",
276 new Annotation[] {});
277 annotation.setCalcId("JMOL");
278 annotation.visible = false;
279 seqs.get(0).addAlignmentAnnotation(annotation);
280 parentPanel.getAlignment().addAnnotation(annotation);
282 // Jpred.SSP on Sequence0 - hidden
283 annotation = new AlignmentAnnotation("SSP", "", new Annotation[] {});
284 annotation.setCalcId("JPred");
285 annotation.visible = false;
286 seqs.get(0).addAlignmentAnnotation(annotation);
287 parentPanel.getAlignment().addAnnotation(annotation);
289 // PDB.Temp on Sequence1
290 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
291 annotation.setCalcId("PDB");
292 annotation.visible = true;
293 seqs.get(1).addAlignmentAnnotation(annotation);
294 parentPanel.getAlignment().addAnnotation(annotation);
297 * Expect menu options to show "secondary structure" and "SSP", and to hide
298 * "secondary structure" and "Temp". Tooltip should be calcId.
300 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
302 assertTrue(showMenu.isEnabled());
303 assertTrue(hideMenu.isEnabled());
305 Component[] showOptions = showMenu.getMenuComponents();
306 Component[] hideOptions = hideMenu.getMenuComponents();
308 assertEquals(4, showOptions.length); // includes 'All' and separator
309 assertEquals(4, hideOptions.length);
310 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
311 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
312 assertEquals(JSeparator.HORIZONTAL,
313 ((JSeparator) showOptions[1]).getOrientation());
314 assertEquals("secondary structure",
315 ((JMenuItem) showOptions[2]).getText());
316 assertEquals("JMOL", ((JMenuItem) showOptions[2]).getToolTipText());
317 assertEquals("SSP", ((JMenuItem) showOptions[3]).getText());
318 assertEquals("JPred", ((JMenuItem) showOptions[3]).getToolTipText());
320 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
321 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
322 assertEquals(JSeparator.HORIZONTAL,
323 ((JSeparator) hideOptions[1]).getOrientation());
324 assertEquals("secondary structure",
325 ((JMenuItem) hideOptions[2]).getText());
326 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
327 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
328 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
332 * Test for building menu options with only 'hide' annotation types enabled.
334 @Test(groups = { "Functional" })
335 public void testBuildAnnotationTypesMenus_showDisabled()
337 JMenu showMenu = new JMenu();
338 JMenu hideMenu = new JMenu();
339 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
341 // make up new annotations and add to sequences and to the alignment
343 // PDB.secondary structure on Sequence0
344 AlignmentAnnotation annotation = new AlignmentAnnotation(
345 "secondary structure", "", new Annotation[] {});
346 annotation.setCalcId("PDB");
347 annotation.visible = true;
348 seqs.get(0).addAlignmentAnnotation(annotation);
349 parentPanel.getAlignment().addAnnotation(annotation);
351 // PDB.Temp on Sequence1
352 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
353 annotation.setCalcId("PDB");
354 annotation.visible = true;
355 seqs.get(1).addAlignmentAnnotation(annotation);
356 parentPanel.getAlignment().addAnnotation(annotation);
359 * Expect menu options to hide "secondary structure" and "Temp". Tooltip
360 * should be calcId. 'Show' menu should be disabled.
362 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
364 assertFalse(showMenu.isEnabled());
365 assertTrue(hideMenu.isEnabled());
367 Component[] showOptions = showMenu.getMenuComponents();
368 Component[] hideOptions = hideMenu.getMenuComponents();
370 assertEquals(2, showOptions.length); // includes 'All' and separator
371 assertEquals(4, hideOptions.length);
372 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
373 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
374 assertEquals(JSeparator.HORIZONTAL,
375 ((JSeparator) showOptions[1]).getOrientation());
377 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
378 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
379 assertEquals(JSeparator.HORIZONTAL,
380 ((JSeparator) hideOptions[1]).getOrientation());
381 assertEquals("secondary structure",
382 ((JMenuItem) hideOptions[2]).getText());
383 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
384 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
385 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
389 * Test for building menu options with only 'show' annotation types enabled.
391 @Test(groups = { "Functional" })
392 public void testBuildAnnotationTypesMenus_hideDisabled()
394 JMenu showMenu = new JMenu();
395 JMenu hideMenu = new JMenu();
396 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
398 // make up new annotations and add to sequences and to the alignment
400 // PDB.secondary structure on Sequence0
401 AlignmentAnnotation annotation = new AlignmentAnnotation(
402 "secondary structure", "", new Annotation[] {});
403 annotation.setCalcId("PDB");
404 annotation.visible = false;
405 seqs.get(0).addAlignmentAnnotation(annotation);
406 parentPanel.getAlignment().addAnnotation(annotation);
408 // PDB.Temp on Sequence1
409 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
410 annotation.setCalcId("PDB2");
411 annotation.visible = false;
412 seqs.get(1).addAlignmentAnnotation(annotation);
413 parentPanel.getAlignment().addAnnotation(annotation);
416 * Expect menu options to show "secondary structure" and "Temp". Tooltip
417 * should be calcId. 'hide' menu should be disabled.
419 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
421 assertTrue(showMenu.isEnabled());
422 assertFalse(hideMenu.isEnabled());
424 Component[] showOptions = showMenu.getMenuComponents();
425 Component[] hideOptions = hideMenu.getMenuComponents();
427 assertEquals(4, showOptions.length); // includes 'All' and separator
428 assertEquals(2, hideOptions.length);
429 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
430 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
431 assertEquals(JSeparator.HORIZONTAL,
432 ((JSeparator) showOptions[1]).getOrientation());
433 assertEquals("secondary structure",
434 ((JMenuItem) showOptions[2]).getText());
435 assertEquals("PDB", ((JMenuItem) showOptions[2]).getToolTipText());
436 assertEquals("Temp", ((JMenuItem) showOptions[3]).getText());
437 assertEquals("PDB2", ((JMenuItem) showOptions[3]).getToolTipText());
439 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
440 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
441 assertEquals(JSeparator.HORIZONTAL,
442 ((JSeparator) hideOptions[1]).getOrientation());