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