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