JAL-2326 added setup method for JvOptionPane in all Jalveiw test classes to enable...
[jalview.git] / test / jalview / gui / PopupMenuTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.gui;
22
23 import static jalview.util.UrlConstants.DB_ACCESSION;
24 import static jalview.util.UrlConstants.SEQUENCE_ID;
25 import static org.testng.AssertJUnit.assertEquals;
26 import static org.testng.AssertJUnit.assertFalse;
27 import static org.testng.AssertJUnit.assertTrue;
28
29 import jalview.datamodel.AlignmentAnnotation;
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.Annotation;
32 import jalview.datamodel.DBRefEntry;
33 import jalview.datamodel.DBRefSource;
34 import jalview.datamodel.Sequence;
35 import jalview.datamodel.SequenceI;
36 import jalview.io.AppletFormatAdapter;
37 import jalview.io.FormatAdapter;
38 import jalview.util.MessageManager;
39
40 import java.awt.Component;
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.List;
44
45 import javax.swing.JMenu;
46 import javax.swing.JMenuItem;
47 import javax.swing.JPopupMenu;
48 import javax.swing.JSeparator;
49
50 import org.testng.annotations.BeforeClass;
51 import org.testng.annotations.BeforeMethod;
52 import org.testng.annotations.Test;
53
54 public class PopupMenuTest
55 {
56
57   @BeforeClass(alwaysRun = true)
58   public void setUpJvOptionPane()
59   {
60     JvOptionPane.setInteractiveMode(false);
61     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
62   }
63
64   // 4 sequences x 13 positions
65   final static String TEST_DATA = ">FER_CAPAA Ferredoxin\n"
66           + "TIETHKEAELVG-\n"
67           + ">FER_CAPAN Ferredoxin, chloroplast precursor\n"
68           + "TIETHKEAELVG-\n"
69           + ">FER1_SOLLC Ferredoxin-1, chloroplast precursor\n"
70           + "TIETHKEEELTA-\n" + ">Q93XJ9_SOLTU Ferredoxin I precursor\n"
71           + "TIETHKEEELTA-\n";
72
73   AlignmentI alignment;
74
75   AlignmentPanel parentPanel;
76
77   PopupMenu testee = null;
78
79   @BeforeMethod(alwaysRun = true)
80   public void setUp() throws IOException
81   {
82     alignment = new FormatAdapter().readFile(TEST_DATA,
83             AppletFormatAdapter.PASTE, "FASTA");
84     AlignFrame af = new AlignFrame(alignment, 700, 500);
85     parentPanel = new AlignmentPanel(af, af.getViewport());
86     testee = new PopupMenu(parentPanel, null, null);
87     int i = 0;
88     for (SequenceI seq : alignment.getSequences())
89     {
90       final AlignmentAnnotation annotation = new AlignmentAnnotation(
91               "label" + i, "desc" + i, i);
92       annotation.setCalcId("calcId" + i);
93       seq.addAlignmentAnnotation(annotation);
94       annotation.setSequenceRef(seq);
95     }
96   }
97
98   @Test(groups = { "Functional" })
99   public void testConfigureReferenceAnnotationsMenu_noSequenceSelected()
100   {
101     JMenuItem menu = new JMenuItem();
102     List<SequenceI> seqs = new ArrayList<SequenceI>();
103     testee.configureReferenceAnnotationsMenu(menu, seqs);
104     assertFalse(menu.isEnabled());
105     // now try null list
106     menu.setEnabled(true);
107     testee.configureReferenceAnnotationsMenu(menu, null);
108     assertFalse(menu.isEnabled());
109   }
110
111   /**
112    * Test building the 'add reference annotations' menu for the case where there
113    * are no reference annotations to add to the alignment. The menu item should
114    * be disabled.
115    */
116   @Test(groups = { "Functional" })
117   public void testConfigureReferenceAnnotationsMenu_noReferenceAnnotations()
118   {
119     JMenuItem menu = new JMenuItem();
120
121     /*
122      * Initial state is that sequences have annotations, and have dataset
123      * sequences, but the dataset sequences have no annotations. Hence nothing
124      * to add.
125      */
126     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
127
128     testee.configureReferenceAnnotationsMenu(menu, seqs);
129     assertFalse(menu.isEnabled());
130   }
131
132   /**
133    * Test building the 'add reference annotations' menu for the case where all
134    * reference annotations are already on the alignment. The menu item should be
135    * disabled.
136    */
137   @Test(groups = { "Functional" })
138   public void testConfigureReferenceAnnotationsMenu_alreadyAdded()
139   {
140     JMenuItem menu = new JMenuItem();
141     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
142
143     // make up new annotations and add to dataset sequences, sequences and
144     // alignment
145     attachReferenceAnnotations(seqs, true, true);
146
147     testee.configureReferenceAnnotationsMenu(menu, seqs);
148     assertFalse(menu.isEnabled());
149   }
150
151   /**
152    * Test building the 'add reference annotations' menu for the case where
153    * several reference annotations are on the dataset but not on the sequences.
154    * The menu item should be enabled, and acquire a tooltip which lists the
155    * annotation sources (calcIds) and type (labels).
156    */
157   @Test(groups = { "Functional" })
158   public void testConfigureReferenceAnnotationsMenu()
159   {
160     JMenuItem menu = new JMenuItem();
161     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
162
163     // make up new annotations and add to dataset sequences
164     attachReferenceAnnotations(seqs, false, false);
165
166     testee.configureReferenceAnnotationsMenu(menu, seqs);
167     assertTrue(menu.isEnabled());
168     String s = MessageManager.getString("label.add_annotations_for");
169     String expected = "<html><style> p.ttip {width: 350; text-align: justify; word-wrap: break-word;}</style><p class=\"ttip\">"
170             + s + "<br/>Jmol/secondary structure<br/>PDB/Temp</p></html>";
171     assertEquals(expected, menu.getToolTipText());
172   }
173
174   /**
175    * Test building the 'add reference annotations' menu for the case where
176    * several reference annotations are on the dataset and the sequences but not
177    * on the alignment. The menu item should be enabled, and acquire a tooltip
178    * which lists the annotation sources (calcIds) and type (labels).
179    */
180   @Test(groups = { "Functional" })
181   public void testConfigureReferenceAnnotationsMenu_notOnAlignment()
182   {
183     JMenuItem menu = new JMenuItem();
184     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
185
186     // make up new annotations and add to dataset sequences and sequences
187     attachReferenceAnnotations(seqs, true, false);
188
189     testee.configureReferenceAnnotationsMenu(menu, seqs);
190     assertTrue(menu.isEnabled());
191     String s = MessageManager.getString("label.add_annotations_for");
192     String expected = "<html><style> p.ttip {width: 350; text-align: justify; word-wrap: break-word;}</style><p class=\"ttip\">"
193             + s + "<br/>Jmol/secondary structure<br/>PDB/Temp</p></html>";
194     assertEquals(expected, menu.getToolTipText());
195   }
196
197   /**
198    * Generate annotations and add to dataset sequences and (optionally)
199    * sequences and/or alignment
200    * 
201    * @param seqs
202    * @param addToSequence
203    * @param addToAlignment
204    */
205   private void attachReferenceAnnotations(List<SequenceI> seqs,
206           boolean addToSequence, boolean addToAlignment)
207   {
208     // PDB.secondary structure on Sequence0
209     AlignmentAnnotation annotation = new AlignmentAnnotation(
210             "secondary structure", "", 0);
211     annotation.setCalcId("PDB");
212     seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
213     if (addToSequence)
214     {
215       seqs.get(0).addAlignmentAnnotation(annotation);
216     }
217     if (addToAlignment)
218     {
219       this.alignment.addAnnotation(annotation);
220     }
221
222     // PDB.Temp on Sequence1
223     annotation = new AlignmentAnnotation("Temp", "", 0);
224     annotation.setCalcId("PDB");
225     seqs.get(1).getDatasetSequence().addAlignmentAnnotation(annotation);
226     if (addToSequence)
227     {
228       seqs.get(1).addAlignmentAnnotation(annotation);
229     }
230     if (addToAlignment)
231     {
232       this.alignment.addAnnotation(annotation);
233     }
234
235     // JMOL.secondary structure on Sequence0
236     annotation = new AlignmentAnnotation("secondary structure", "", 0);
237     annotation.setCalcId("Jmol");
238     seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
239     if (addToSequence)
240     {
241       seqs.get(0).addAlignmentAnnotation(annotation);
242     }
243     if (addToAlignment)
244     {
245       this.alignment.addAnnotation(annotation);
246     }
247   }
248
249   /**
250    * Test building the 'add reference annotations' menu for the case where there
251    * are two alignment views:
252    * <ul>
253    * <li>in one view, reference annotations have been added (are on the
254    * datasets, sequences and alignment)</li>
255    * <li>in the current view, reference annotations are on the dataset and
256    * sequence, but not the alignment</li>
257    * </ul>
258    * The menu item should be enabled, and acquire a tooltip which lists the
259    * annotation sources (calcIds) and type (labels).
260    */
261   @Test(groups = { "Functional" })
262   public void testConfigureReferenceAnnotationsMenu_twoViews()
263   {
264   }
265
266   /**
267    * Test for building menu options including 'show' and 'hide' annotation
268    * types.
269    */
270   @Test(groups = { "Functional" })
271   public void testBuildAnnotationTypesMenus()
272   {
273     JMenu showMenu = new JMenu();
274     JMenu hideMenu = new JMenu();
275     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
276
277     // make up new annotations and add to sequences and to the alignment
278
279     // PDB.secondary structure on Sequence0
280     AlignmentAnnotation annotation = new AlignmentAnnotation(
281             "secondary structure", "", new Annotation[] {});
282     annotation.setCalcId("PDB");
283     annotation.visible = true;
284     seqs.get(0).addAlignmentAnnotation(annotation);
285     parentPanel.getAlignment().addAnnotation(annotation);
286
287     // JMOL.secondary structure on Sequence0 - hidden
288     annotation = new AlignmentAnnotation("secondary structure", "",
289             new Annotation[] {});
290     annotation.setCalcId("JMOL");
291     annotation.visible = false;
292     seqs.get(0).addAlignmentAnnotation(annotation);
293     parentPanel.getAlignment().addAnnotation(annotation);
294
295     // Jpred.SSP on Sequence0 - hidden
296     annotation = new AlignmentAnnotation("SSP", "", new Annotation[] {});
297     annotation.setCalcId("JPred");
298     annotation.visible = false;
299     seqs.get(0).addAlignmentAnnotation(annotation);
300     parentPanel.getAlignment().addAnnotation(annotation);
301
302     // PDB.Temp on Sequence1
303     annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
304     annotation.setCalcId("PDB");
305     annotation.visible = true;
306     seqs.get(1).addAlignmentAnnotation(annotation);
307     parentPanel.getAlignment().addAnnotation(annotation);
308
309     /*
310      * Expect menu options to show "secondary structure" and "SSP", and to hide
311      * "secondary structure" and "Temp". Tooltip should be calcId.
312      */
313     testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
314
315     assertTrue(showMenu.isEnabled());
316     assertTrue(hideMenu.isEnabled());
317
318     Component[] showOptions = showMenu.getMenuComponents();
319     Component[] hideOptions = hideMenu.getMenuComponents();
320
321     assertEquals(4, showOptions.length); // includes 'All' and separator
322     assertEquals(4, hideOptions.length);
323     assertEquals("All", ((JMenuItem) showOptions[0]).getText());
324     assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
325     assertEquals(JSeparator.HORIZONTAL,
326             ((JSeparator) showOptions[1]).getOrientation());
327     assertEquals("secondary structure",
328             ((JMenuItem) showOptions[2]).getText());
329     assertEquals("JMOL", ((JMenuItem) showOptions[2]).getToolTipText());
330     assertEquals("SSP", ((JMenuItem) showOptions[3]).getText());
331     assertEquals("JPred", ((JMenuItem) showOptions[3]).getToolTipText());
332
333     assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
334     assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
335     assertEquals(JSeparator.HORIZONTAL,
336             ((JSeparator) hideOptions[1]).getOrientation());
337     assertEquals("secondary structure",
338             ((JMenuItem) hideOptions[2]).getText());
339     assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
340     assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
341     assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
342   }
343
344   /**
345    * Test for building menu options with only 'hide' annotation types enabled.
346    */
347   @Test(groups = { "Functional" })
348   public void testBuildAnnotationTypesMenus_showDisabled()
349   {
350     JMenu showMenu = new JMenu();
351     JMenu hideMenu = new JMenu();
352     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
353
354     // make up new annotations and add to sequences and to the alignment
355
356     // PDB.secondary structure on Sequence0
357     AlignmentAnnotation annotation = new AlignmentAnnotation(
358             "secondary structure", "", new Annotation[] {});
359     annotation.setCalcId("PDB");
360     annotation.visible = true;
361     seqs.get(0).addAlignmentAnnotation(annotation);
362     parentPanel.getAlignment().addAnnotation(annotation);
363
364     // PDB.Temp on Sequence1
365     annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
366     annotation.setCalcId("PDB");
367     annotation.visible = true;
368     seqs.get(1).addAlignmentAnnotation(annotation);
369     parentPanel.getAlignment().addAnnotation(annotation);
370
371     /*
372      * Expect menu options to hide "secondary structure" and "Temp". Tooltip
373      * should be calcId. 'Show' menu should be disabled.
374      */
375     testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
376
377     assertFalse(showMenu.isEnabled());
378     assertTrue(hideMenu.isEnabled());
379
380     Component[] showOptions = showMenu.getMenuComponents();
381     Component[] hideOptions = hideMenu.getMenuComponents();
382
383     assertEquals(2, showOptions.length); // includes 'All' and separator
384     assertEquals(4, hideOptions.length);
385     assertEquals("All", ((JMenuItem) showOptions[0]).getText());
386     assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
387     assertEquals(JSeparator.HORIZONTAL,
388             ((JSeparator) showOptions[1]).getOrientation());
389
390     assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
391     assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
392     assertEquals(JSeparator.HORIZONTAL,
393             ((JSeparator) hideOptions[1]).getOrientation());
394     assertEquals("secondary structure",
395             ((JMenuItem) hideOptions[2]).getText());
396     assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
397     assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
398     assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
399   }
400
401   /**
402    * Test for building menu options with only 'show' annotation types enabled.
403    */
404   @Test(groups = { "Functional" })
405   public void testBuildAnnotationTypesMenus_hideDisabled()
406   {
407     JMenu showMenu = new JMenu();
408     JMenu hideMenu = new JMenu();
409     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
410
411     // make up new annotations and add to sequences and to the alignment
412
413     // PDB.secondary structure on Sequence0
414     AlignmentAnnotation annotation = new AlignmentAnnotation(
415             "secondary structure", "", new Annotation[] {});
416     annotation.setCalcId("PDB");
417     annotation.visible = false;
418     seqs.get(0).addAlignmentAnnotation(annotation);
419     parentPanel.getAlignment().addAnnotation(annotation);
420
421     // PDB.Temp on Sequence1
422     annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
423     annotation.setCalcId("PDB2");
424     annotation.visible = false;
425     seqs.get(1).addAlignmentAnnotation(annotation);
426     parentPanel.getAlignment().addAnnotation(annotation);
427
428     /*
429      * Expect menu options to show "secondary structure" and "Temp". Tooltip
430      * should be calcId. 'hide' menu should be disabled.
431      */
432     testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
433
434     assertTrue(showMenu.isEnabled());
435     assertFalse(hideMenu.isEnabled());
436
437     Component[] showOptions = showMenu.getMenuComponents();
438     Component[] hideOptions = hideMenu.getMenuComponents();
439
440     assertEquals(4, showOptions.length); // includes 'All' and separator
441     assertEquals(2, hideOptions.length);
442     assertEquals("All", ((JMenuItem) showOptions[0]).getText());
443     assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
444     assertEquals(JSeparator.HORIZONTAL,
445             ((JSeparator) showOptions[1]).getOrientation());
446     assertEquals("secondary structure",
447             ((JMenuItem) showOptions[2]).getText());
448     assertEquals("PDB", ((JMenuItem) showOptions[2]).getToolTipText());
449     assertEquals("Temp", ((JMenuItem) showOptions[3]).getText());
450     assertEquals("PDB2", ((JMenuItem) showOptions[3]).getToolTipText());
451
452     assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
453     assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
454     assertEquals(JSeparator.HORIZONTAL,
455             ((JSeparator) hideOptions[1]).getOrientation());
456   }
457
458   /**
459    * Test for adding feature links
460    */
461   @Test(groups = { "Functional" })
462   public void testAddFeatureLinks()
463   {
464     // sequences from the alignment
465     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
466
467     // create list of links and list of DBRefs
468     List<String> links = new ArrayList<String>();
469     List<DBRefEntry> refs = new ArrayList<DBRefEntry>();
470
471     // links as might be added into Preferences | Connections dialog
472     links.add("EMBL-EBI Search | http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$"
473             + SEQUENCE_ID + "$");
474     links.add("UNIPROT | http://www.uniprot.org/uniprot/$" + DB_ACCESSION
475             + "$");
476     links.add("INTERPRO | http://www.ebi.ac.uk/interpro/entry/$"
477             + DB_ACCESSION + "$");
478     // Gene3D entry tests for case (in)sensitivity
479     links.add("Gene3D | http://gene3d.biochem.ucl.ac.uk/Gene3D/search?sterm=$"
480             + DB_ACCESSION + "$&mode=protein");
481
482     // make seq0 dbrefs
483     refs.add(new DBRefEntry(DBRefSource.UNIPROT, "1", "P83527"));
484     refs.add(new DBRefEntry("INTERPRO", "1", "IPR001041"));
485     refs.add(new DBRefEntry("INTERPRO", "1", "IPR006058"));
486     refs.add(new DBRefEntry("INTERPRO", "1", "IPR012675"));
487     
488     // make seq1 dbrefs
489     refs.add(new DBRefEntry(DBRefSource.UNIPROT, "1", "Q9ZTS2"));
490     refs.add(new DBRefEntry("GENE3D", "1", "3.10.20.30"));
491
492     // add all the dbrefs to the sequences: Uniprot 1 each, Interpro all 3 to
493     // seq0, Gene3D to seq1
494     seqs.get(0).addDBRef(refs.get(0));
495
496     seqs.get(0).addDBRef(refs.get(1));
497     seqs.get(0).addDBRef(refs.get(2));
498     seqs.get(0).addDBRef(refs.get(3));
499     
500     seqs.get(1).addDBRef(refs.get(4));
501     seqs.get(1).addDBRef(refs.get(5));
502     
503     // get the Popup Menu for first sequence
504     testee = new PopupMenu(parentPanel, (Sequence) seqs.get(0), links);
505     Component[] seqItems = testee.sequenceMenu.getMenuComponents();
506     JMenu linkMenu = (JMenu) seqItems[6];
507     Component[] linkItems = linkMenu.getMenuComponents();
508     
509     // check the number of links are the expected number
510     assertEquals(5, linkItems.length);
511
512     // first entry is EMBL-EBI which just uses sequence id not accession id?
513     assertEquals("EMBL-EBI Search", ((JMenuItem) linkItems[0]).getText());
514
515     // sequence id for each link should match corresponding DB accession id
516     for (int i = 1; i < 4; i++)
517     {
518       assertEquals(refs.get(i - 1).getSource(), ((JMenuItem) linkItems[i])
519               .getText().split("\\|")[0]);
520       assertEquals(refs.get(i - 1).getAccessionId(),
521               ((JMenuItem) linkItems[i])
522               .getText().split("\\|")[1]);
523     }
524
525     // get the Popup Menu for second sequence
526     testee = new PopupMenu(parentPanel, (Sequence) seqs.get(1), links);
527     seqItems = testee.sequenceMenu.getMenuComponents();
528     linkMenu = (JMenu) seqItems[6];
529     linkItems = linkMenu.getMenuComponents();
530     
531     // check the number of links are the expected number
532     assertEquals(3, linkItems.length);
533
534     // first entry is EMBL-EBI which just uses sequence id not accession id?
535     assertEquals("EMBL-EBI Search", ((JMenuItem) linkItems[0]).getText());
536
537     // sequence id for each link should match corresponding DB accession id
538     for (int i = 1; i < 3; i++)
539     {
540       assertEquals(refs.get(i + 3).getSource(), ((JMenuItem) linkItems[i])
541               .getText().split("\\|")[0].toUpperCase());
542       assertEquals(refs.get(i + 3).getAccessionId(),
543               ((JMenuItem) linkItems[i]).getText().split("\\|")[1]);
544     }
545
546     // if there are no valid links the Links submenu is disabled
547     List<String> nomatchlinks = new ArrayList<String>();
548     nomatchlinks.add("NOMATCH | http://www.uniprot.org/uniprot/$"
549             + DB_ACCESSION + "$");
550
551     testee = new PopupMenu(parentPanel, (Sequence) seqs.get(0),
552             nomatchlinks);
553     seqItems = testee.sequenceMenu.getMenuComponents();
554     linkMenu = (JMenu) seqItems[6];
555     assertFalse(linkMenu.isEnabled());
556
557   }
558 }