JAL-3949 - refactor logging from jalview.bin.Cache to jalview.bin.Console
[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.assertNotNull;
28 import static org.testng.AssertJUnit.assertNull;
29 import static org.testng.AssertJUnit.assertTrue;
30
31 import java.awt.Component;
32 import java.awt.Container;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.Iterator;
37 import java.util.List;
38
39 import javax.swing.JMenu;
40 import javax.swing.JMenuItem;
41 import javax.swing.JPopupMenu;
42 import javax.swing.JSeparator;
43
44 import org.testng.annotations.BeforeClass;
45 import org.testng.annotations.BeforeMethod;
46 import org.testng.annotations.Test;
47
48 import jalview.bin.Cache;
49 import jalview.bin.Console;
50 import jalview.datamodel.AlignmentAnnotation;
51 import jalview.datamodel.AlignmentI;
52 import jalview.datamodel.Annotation;
53 import jalview.datamodel.ColumnSelection;
54 import jalview.datamodel.DBRefEntry;
55 import jalview.datamodel.DBRefSource;
56 import jalview.datamodel.HiddenColumns;
57 import jalview.datamodel.Sequence;
58 import jalview.datamodel.SequenceFeature;
59 import jalview.datamodel.SequenceGroup;
60 import jalview.datamodel.SequenceI;
61 import jalview.io.DataSourceType;
62 import jalview.io.FileFormat;
63 import jalview.io.FormatAdapter;
64 import jalview.urls.api.UrlProviderFactoryI;
65 import jalview.urls.desktop.DesktopUrlProviderFactory;
66 import jalview.util.MessageManager;
67 import jalview.util.UrlConstants;
68
69 public class PopupMenuTest
70 {
71
72   @BeforeClass(alwaysRun = true)
73   public void setUpJvOptionPane()
74   {
75     JvOptionPane.setInteractiveMode(false);
76     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
77   }
78
79   // 4 sequences x 13 positions
80   final static String TEST_DATA = ">FER_CAPAA Ferredoxin\n"
81           + "TIETHKEAELVG-\n"
82           + ">FER_CAPAN Ferredoxin, chloroplast precursor\n"
83           + "TIETHKEAELVG-\n"
84           + ">FER1_SOLLC Ferredoxin-1, chloroplast precursor\n"
85           + "TIETHKEEELTA-\n" + ">Q93XJ9_SOLTU Ferredoxin I precursor\n"
86           + "TIETHKEEELTA-\n";
87
88   AlignmentI alignment;
89
90   AlignmentPanel parentPanel;
91
92   PopupMenu testee = null;
93
94   @BeforeMethod(alwaysRun = true)
95   public void setUp() throws IOException
96   {
97     Cache.loadProperties("test/jalview/io/testProps.jvprops");
98     Console.initLogger();
99
100     String inMenuString = ("EMBL-EBI Search | http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$"
101             + SEQUENCE_ID
102             + "$"
103             + "|"
104             + "UNIPROT | http://www.uniprot.org/uniprot/$" + DB_ACCESSION + "$")
105             + "|"
106             + ("INTERPRO | http://www.ebi.ac.uk/interpro/entry/$"
107                     + DB_ACCESSION + "$")
108             + "|"
109             +
110             // Gene3D entry tests for case (in)sensitivity
111             ("Gene3D | http://gene3d.biochem.ucl.ac.uk/Gene3D/search?sterm=$"
112                     + DB_ACCESSION + "$&mode=protein");
113
114     UrlProviderFactoryI factory = new DesktopUrlProviderFactory(
115             UrlConstants.DEFAULT_LABEL, inMenuString, "");
116     Preferences.sequenceUrlLinks = factory.createUrlProvider();
117
118     alignment = new FormatAdapter().readFile(TEST_DATA,
119             DataSourceType.PASTE, FileFormat.Fasta);
120     AlignFrame af = new AlignFrame(alignment, 700, 500);
121     parentPanel = new AlignmentPanel(af, af.getViewport());
122     testee = new PopupMenu(parentPanel, alignment.getSequenceAt(0), null);
123     int i = 0;
124     for (SequenceI seq : alignment.getSequences())
125     {
126       final AlignmentAnnotation annotation = new AlignmentAnnotation(
127               "label" + i, "desc" + i, i);
128       annotation.setCalcId("calcId" + i);
129       seq.addAlignmentAnnotation(annotation);
130       annotation.setSequenceRef(seq);
131     }
132   }
133
134   @Test(groups = { "Functional" })
135   public void testConfigureReferenceAnnotationsMenu_noSequenceSelected()
136   {
137     JMenuItem menu = new JMenuItem();
138     List<SequenceI> seqs = new ArrayList<>();
139     testee.configureReferenceAnnotationsMenu(menu, seqs);
140     assertFalse(menu.isEnabled());
141     // now try null list
142     menu.setEnabled(true);
143     testee.configureReferenceAnnotationsMenu(menu, null);
144     assertFalse(menu.isEnabled());
145   }
146
147   /**
148    * Test building the 'add reference annotations' menu for the case where there
149    * are no reference annotations to add to the alignment. The menu item should
150    * be disabled.
151    */
152   @Test(groups = { "Functional" })
153   public void testConfigureReferenceAnnotationsMenu_noReferenceAnnotations()
154   {
155     JMenuItem menu = new JMenuItem();
156
157     /*
158      * Initial state is that sequences have annotations, and have dataset
159      * sequences, but the dataset sequences have no annotations. Hence nothing
160      * to add.
161      */
162     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
163
164     testee.configureReferenceAnnotationsMenu(menu, seqs);
165     assertFalse(menu.isEnabled());
166   }
167
168   /**
169    * Test building the 'add reference annotations' menu for the case where all
170    * reference annotations are already on the alignment. The menu item should be
171    * disabled.
172    */
173   @Test(groups = { "Functional" })
174   public void testConfigureReferenceAnnotationsMenu_alreadyAdded()
175   {
176     JMenuItem menu = new JMenuItem();
177     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
178
179     // make up new annotations and add to dataset sequences, sequences and
180     // alignment
181     attachReferenceAnnotations(seqs, true, true);
182
183     testee.configureReferenceAnnotationsMenu(menu, seqs);
184     assertFalse(menu.isEnabled());
185   }
186
187   /**
188    * Test building the 'add reference annotations' menu for the case where
189    * several reference annotations are on the dataset but not on the sequences.
190    * The menu item should be enabled, and acquire a tooltip which lists the
191    * annotation sources (calcIds) and type (labels).
192    */
193   @Test(groups = { "Functional" })
194   public void testConfigureReferenceAnnotationsMenu()
195   {
196     JMenuItem menu = new JMenuItem();
197     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
198
199     // make up new annotations and add to dataset sequences
200     attachReferenceAnnotations(seqs, false, false);
201
202     testee.configureReferenceAnnotationsMenu(menu, seqs);
203     assertTrue(menu.isEnabled());
204     String s = MessageManager.getString("label.add_annotations_for");
205     String expected = "<html><style> div.ttip {width:350px;white-space:pre-wrap;padding:2px;overflow-wrap:break-word;}</style>"
206             + "<div class=\"ttip\">" + s
207             + "<br/>Jmol/secondary structure<br/>PDB/Temp </div></html>";
208     assertEquals(expected, menu.getToolTipText());
209   }
210
211   /**
212    * Test building the 'add reference annotations' menu for the case where
213    * several reference annotations are on the dataset and the sequences but not
214    * on the alignment. The menu item should be enabled, and acquire a tooltip
215    * which lists the annotation sources (calcIds) and type (labels).
216    */
217   @Test(groups = { "Functional" })
218   public void testConfigureReferenceAnnotationsMenu_notOnAlignment()
219   {
220     JMenuItem menu = new JMenuItem();
221     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
222
223     // make up new annotations and add to dataset sequences and sequences
224     attachReferenceAnnotations(seqs, true, false);
225
226     testee.configureReferenceAnnotationsMenu(menu, seqs);
227     assertTrue(menu.isEnabled());
228     String s = MessageManager.getString("label.add_annotations_for");
229     String expected = "<html><style> div.ttip {width:350px;white-space:pre-wrap;padding:2px;overflow-wrap:break-word;}</style>"
230             + "<div class=\"ttip\">" + s
231             + "<br/>Jmol/secondary structure<br/>PDB/Temp </div></html>";
232     assertEquals(expected, menu.getToolTipText());
233   }
234
235   /**
236    * Generate annotations and add to dataset sequences and (optionally)
237    * sequences and/or alignment
238    * 
239    * @param seqs
240    * @param addToSequence
241    * @param addToAlignment
242    */
243   private void attachReferenceAnnotations(List<SequenceI> seqs,
244           boolean addToSequence, boolean addToAlignment)
245   {
246     // PDB.secondary structure on Sequence0
247     AlignmentAnnotation annotation = new AlignmentAnnotation(
248             "secondary structure", "", 0);
249     annotation.setCalcId("PDB");
250     seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
251     if (addToSequence)
252     {
253       seqs.get(0).addAlignmentAnnotation(annotation);
254     }
255     if (addToAlignment)
256     {
257       this.alignment.addAnnotation(annotation);
258     }
259
260     // PDB.Temp on Sequence1
261     annotation = new AlignmentAnnotation("Temp", "", 0);
262     annotation.setCalcId("PDB");
263     seqs.get(1).getDatasetSequence().addAlignmentAnnotation(annotation);
264     if (addToSequence)
265     {
266       seqs.get(1).addAlignmentAnnotation(annotation);
267     }
268     if (addToAlignment)
269     {
270       this.alignment.addAnnotation(annotation);
271     }
272
273     // JMOL.secondary structure on Sequence0
274     annotation = new AlignmentAnnotation("secondary structure", "", 0);
275     annotation.setCalcId("Jmol");
276     seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
277     if (addToSequence)
278     {
279       seqs.get(0).addAlignmentAnnotation(annotation);
280     }
281     if (addToAlignment)
282     {
283       this.alignment.addAnnotation(annotation);
284     }
285   }
286
287   /**
288    * Test building the 'add reference annotations' menu for the case where there
289    * are two alignment views:
290    * <ul>
291    * <li>in one view, reference annotations have been added (are on the
292    * datasets, sequences and alignment)</li>
293    * <li>in the current view, reference annotations are on the dataset and
294    * sequence, but not the alignment</li>
295    * </ul>
296    * The menu item should be enabled, and acquire a tooltip which lists the
297    * annotation sources (calcIds) and type (labels).
298    */
299   @Test(groups = { "Functional" })
300   public void testConfigureReferenceAnnotationsMenu_twoViews()
301   {
302   }
303
304   /**
305    * Test for building menu options including 'show' and 'hide' annotation
306    * types.
307    */
308   @Test(groups = { "Functional" })
309   public void testBuildAnnotationTypesMenus()
310   {
311     JMenu showMenu = new JMenu();
312     JMenu hideMenu = new JMenu();
313     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
314
315     // make up new annotations and add to sequences and to the alignment
316
317     // PDB.secondary structure on Sequence0
318     AlignmentAnnotation annotation = new AlignmentAnnotation(
319             "secondary structure", "", new Annotation[] {});
320     annotation.setCalcId("PDB");
321     annotation.visible = true;
322     seqs.get(0).addAlignmentAnnotation(annotation);
323     parentPanel.getAlignment().addAnnotation(annotation);
324
325     // JMOL.secondary structure on Sequence0 - hidden
326     annotation = new AlignmentAnnotation("secondary structure", "",
327             new Annotation[] {});
328     annotation.setCalcId("JMOL");
329     annotation.visible = false;
330     seqs.get(0).addAlignmentAnnotation(annotation);
331     parentPanel.getAlignment().addAnnotation(annotation);
332
333     // Jpred.SSP on Sequence0 - hidden
334     annotation = new AlignmentAnnotation("SSP", "", new Annotation[] {});
335     annotation.setCalcId("JPred");
336     annotation.visible = false;
337     seqs.get(0).addAlignmentAnnotation(annotation);
338     parentPanel.getAlignment().addAnnotation(annotation);
339
340     // PDB.Temp on Sequence1
341     annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
342     annotation.setCalcId("PDB");
343     annotation.visible = true;
344     seqs.get(1).addAlignmentAnnotation(annotation);
345     parentPanel.getAlignment().addAnnotation(annotation);
346
347     /*
348      * Expect menu options to show "secondary structure" and "SSP", and to hide
349      * "secondary structure" and "Temp". Tooltip should be calcId.
350      */
351     testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
352
353     assertTrue(showMenu.isEnabled());
354     assertTrue(hideMenu.isEnabled());
355
356     Component[] showOptions = showMenu.getMenuComponents();
357     Component[] hideOptions = hideMenu.getMenuComponents();
358
359     assertEquals(4, showOptions.length); // includes 'All' and separator
360     assertEquals(4, hideOptions.length);
361     String all = MessageManager.getString("label.all");
362     assertEquals(all, ((JMenuItem) showOptions[0]).getText());
363     assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
364     assertEquals(JSeparator.HORIZONTAL,
365             ((JSeparator) showOptions[1]).getOrientation());
366     assertEquals("secondary structure",
367             ((JMenuItem) showOptions[2]).getText());
368     assertEquals("JMOL", ((JMenuItem) showOptions[2]).getToolTipText());
369     assertEquals("SSP", ((JMenuItem) showOptions[3]).getText());
370     assertEquals("JPred", ((JMenuItem) showOptions[3]).getToolTipText());
371
372     assertEquals(all, ((JMenuItem) hideOptions[0]).getText());
373     assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
374     assertEquals(JSeparator.HORIZONTAL,
375             ((JSeparator) hideOptions[1]).getOrientation());
376     assertEquals("secondary structure",
377             ((JMenuItem) hideOptions[2]).getText());
378     assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
379     assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
380     assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
381   }
382
383   /**
384    * Test for building menu options with only 'hide' annotation types enabled.
385    */
386   @Test(groups = { "Functional" })
387   public void testBuildAnnotationTypesMenus_showDisabled()
388   {
389     JMenu showMenu = new JMenu();
390     JMenu hideMenu = new JMenu();
391     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
392
393     // make up new annotations and add to sequences and to the alignment
394
395     // PDB.secondary structure on Sequence0
396     AlignmentAnnotation annotation = new AlignmentAnnotation(
397             "secondary structure", "", new Annotation[] {});
398     annotation.setCalcId("PDB");
399     annotation.visible = true;
400     seqs.get(0).addAlignmentAnnotation(annotation);
401     parentPanel.getAlignment().addAnnotation(annotation);
402
403     // PDB.Temp on Sequence1
404     annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
405     annotation.setCalcId("PDB");
406     annotation.visible = true;
407     seqs.get(1).addAlignmentAnnotation(annotation);
408     parentPanel.getAlignment().addAnnotation(annotation);
409
410     /*
411      * Expect menu options to hide "secondary structure" and "Temp". Tooltip
412      * should be calcId. 'Show' menu should be disabled.
413      */
414     testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
415
416     assertFalse(showMenu.isEnabled());
417     assertTrue(hideMenu.isEnabled());
418
419     Component[] showOptions = showMenu.getMenuComponents();
420     Component[] hideOptions = hideMenu.getMenuComponents();
421
422     assertEquals(2, showOptions.length); // includes 'All' and separator
423     assertEquals(4, hideOptions.length);
424     String all = MessageManager.getString("label.all");
425     assertEquals(all, ((JMenuItem) showOptions[0]).getText());
426     assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
427     assertEquals(JSeparator.HORIZONTAL,
428             ((JSeparator) showOptions[1]).getOrientation());
429
430     assertEquals(all, ((JMenuItem) hideOptions[0]).getText());
431     assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
432     assertEquals(JSeparator.HORIZONTAL,
433             ((JSeparator) hideOptions[1]).getOrientation());
434     assertEquals("secondary structure",
435             ((JMenuItem) hideOptions[2]).getText());
436     assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
437     assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
438     assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
439   }
440
441   /**
442    * Test for building menu options with only 'show' annotation types enabled.
443    */
444   @Test(groups = { "Functional" })
445   public void testBuildAnnotationTypesMenus_hideDisabled()
446   {
447     JMenu showMenu = new JMenu();
448     JMenu hideMenu = new JMenu();
449     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
450
451     // make up new annotations and add to sequences and to the alignment
452
453     // PDB.secondary structure on Sequence0
454     AlignmentAnnotation annotation = new AlignmentAnnotation(
455             "secondary structure", "", new Annotation[] {});
456     annotation.setCalcId("PDB");
457     annotation.visible = false;
458     seqs.get(0).addAlignmentAnnotation(annotation);
459     parentPanel.getAlignment().addAnnotation(annotation);
460
461     // PDB.Temp on Sequence1
462     annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
463     annotation.setCalcId("PDB2");
464     annotation.visible = false;
465     seqs.get(1).addAlignmentAnnotation(annotation);
466     parentPanel.getAlignment().addAnnotation(annotation);
467
468     /*
469      * Expect menu options to show "secondary structure" and "Temp". Tooltip
470      * should be calcId. 'hide' menu should be disabled.
471      */
472     testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
473
474     assertTrue(showMenu.isEnabled());
475     assertFalse(hideMenu.isEnabled());
476
477     Component[] showOptions = showMenu.getMenuComponents();
478     Component[] hideOptions = hideMenu.getMenuComponents();
479
480     assertEquals(4, showOptions.length); // includes 'All' and separator
481     assertEquals(2, hideOptions.length);
482     String all = MessageManager.getString("label.all");
483     assertEquals(all, ((JMenuItem) showOptions[0]).getText());
484     assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
485     assertEquals(JSeparator.HORIZONTAL,
486             ((JSeparator) showOptions[1]).getOrientation());
487     assertEquals("secondary structure",
488             ((JMenuItem) showOptions[2]).getText());
489     assertEquals("PDB", ((JMenuItem) showOptions[2]).getToolTipText());
490     assertEquals("Temp", ((JMenuItem) showOptions[3]).getText());
491     assertEquals("PDB2", ((JMenuItem) showOptions[3]).getToolTipText());
492
493     assertEquals(all, ((JMenuItem) hideOptions[0]).getText());
494     assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
495     assertEquals(JSeparator.HORIZONTAL,
496             ((JSeparator) hideOptions[1]).getOrientation());
497   }
498
499   /**
500    * Test for adding sequence id, dbref and feature links
501    */
502   @Test(groups = { "Functional" })
503   public void testBuildLinkMenu()
504   {
505     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
506     final SequenceI seq0 = seqs.get(0);
507     final SequenceI seq1 = seqs.get(1);
508     final List<SequenceFeature> noFeatures = Collections
509             .<SequenceFeature> emptyList();
510     final String linkText = MessageManager.getString("action.link");
511
512     seq0.addDBRef(new DBRefEntry(DBRefSource.UNIPROT, "1", "P83527"));
513     seq0.addDBRef(new DBRefEntry("INTERPRO", "1", "IPR001041"));
514     seq0.addDBRef(new DBRefEntry("INTERPRO", "1", "IPR012675"));
515     seq0.addDBRef(new DBRefEntry("INTERPRO", "1", "IPR006058"));
516     seq1.addDBRef(new DBRefEntry(DBRefSource.UNIPROT, "1", "Q9ZTS2"));
517     seq1.addDBRef(new DBRefEntry("GENE3D", "1", "3.10.20.30"));
518     
519     /*
520      * check the Link Menu for the first sequence
521      */
522     JMenu linkMenu = PopupMenu.buildLinkMenu(seq0, noFeatures);
523     assertEquals(linkText, linkMenu.getText());
524     Component[] linkItems = linkMenu.getMenuComponents();
525     
526     /*
527      * menu items are ordered: SEQUENCE_ID search first, then dbrefs in order
528      * of database name (and within that by order of dbref addition)
529      */
530     assertEquals(5, linkItems.length);
531     assertEquals("EMBL-EBI Search", ((JMenuItem) linkItems[0]).getText());
532     assertEquals("INTERPRO|IPR001041",
533             ((JMenuItem) linkItems[1]).getText());
534     assertEquals("INTERPRO|IPR012675",
535             ((JMenuItem) linkItems[2]).getText());
536     assertEquals("INTERPRO|IPR006058",
537             ((JMenuItem) linkItems[3]).getText());
538     assertEquals("UNIPROT|P83527", ((JMenuItem) linkItems[4]).getText());
539
540     /*
541      * check the Link Menu for the second sequence
542      * note dbref GENE3D is matched to link Gene3D, the latter is displayed
543      */
544     linkMenu = PopupMenu.buildLinkMenu(seq1, noFeatures);
545     linkItems = linkMenu.getMenuComponents();
546     assertEquals(3, linkItems.length);
547     assertEquals("EMBL-EBI Search", ((JMenuItem) linkItems[0]).getText());
548     assertEquals("Gene3D|3.10.20.30", ((JMenuItem) linkItems[1]).getText());
549     assertEquals("UNIPROT|Q9ZTS2", ((JMenuItem) linkItems[2]).getText());
550
551     /*
552      * if there are no valid links the Links submenu is still shown, but
553      * reduced to the EMBL-EBI lookup only (inserted by 
554      * CustomUrlProvider.choosePrimaryUrl())
555      */
556     String unmatched = "NOMATCH|http://www.uniprot.org/uniprot/$"
557             + DB_ACCESSION + "$";
558     UrlProviderFactoryI factory = new DesktopUrlProviderFactory(null,
559             unmatched, "");
560     Preferences.sequenceUrlLinks = factory.createUrlProvider();
561
562     linkMenu = PopupMenu.buildLinkMenu(seq1, noFeatures);
563     linkItems = linkMenu.getMenuComponents();
564     assertEquals(1, linkItems.length);
565     assertEquals("EMBL-EBI Search", ((JMenuItem) linkItems[0]).getText());
566
567     /*
568      * if sequence is null, only feature links are shown (alignment popup submenu)
569      */
570     linkMenu = PopupMenu.buildLinkMenu(null, noFeatures);
571     linkItems = linkMenu.getMenuComponents();
572     assertEquals(0, linkItems.length);
573
574     List<SequenceFeature> features = new ArrayList<>();
575     SequenceFeature sf = new SequenceFeature("type", "desc", 1, 20, null);
576     features.add(sf);
577     linkMenu = PopupMenu.buildLinkMenu(null, features);
578     linkItems = linkMenu.getMenuComponents();
579     assertEquals(0, linkItems.length); // feature has no links
580
581     sf.addLink("Pfam family|http://pfam.xfam.org/family/PF00111");
582     linkMenu = PopupMenu.buildLinkMenu(null, features);
583     linkItems = linkMenu.getMenuComponents();
584     assertEquals(1, linkItems.length);
585     JMenuItem item = (JMenuItem) linkItems[0];
586     assertEquals("Pfam family", item.getText());
587     // ? no way to verify URL, compiled into link's actionListener
588   }
589
590   @Test(groups = { "Functional" })
591   public void testHideInsertions()
592   {
593     // get sequences from the alignment
594     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
595     
596     // add our own seqs to avoid problems with changes to existing sequences
597     // (gap at end of sequences varies depending on how tests are run!)
598     Sequence seqGap1 = new Sequence("GappySeq",
599             "AAAA----AA-AAAAAAA---AAA-----------AAAAAAAAAA--");
600     seqGap1.createDatasetSequence();
601     seqs.add(seqGap1);
602     Sequence seqGap2 = new Sequence("LessGappySeq",
603             "AAAAAA-AAAAA---AAA--AAAAA--AAAAAAA-AAAAAA");
604     seqGap2.createDatasetSequence();
605     seqs.add(seqGap2);
606     Sequence seqGap3 = new Sequence("AnotherGapSeq",
607             "AAAAAA-AAAAAA--AAAAAA-AAAAAAAAAAA---AAAAAAAA");
608     seqGap3.createDatasetSequence();
609     seqs.add(seqGap3);
610     Sequence seqGap4 = new Sequence("NoGaps",
611             "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
612     seqGap4.createDatasetSequence();
613     seqs.add(seqGap4);
614
615     ColumnSelection sel = new ColumnSelection();
616     parentPanel.av.getAlignment().getHiddenColumns()
617             .revealAllHiddenColumns(sel);
618
619     // get the Popup Menu for 7th sequence - no insertions
620     testee = new PopupMenu(parentPanel, seqs.get(7), null);
621     testee.hideInsertions_actionPerformed(null);
622     
623     HiddenColumns hidden = parentPanel.av.getAlignment().getHiddenColumns();
624     Iterator<int[]> it = hidden.iterator();
625     assertFalse(it.hasNext());
626
627     // get the Popup Menu for GappySeq - this time we have insertions
628     testee = new PopupMenu(parentPanel, seqs.get(4), null);
629     testee.hideInsertions_actionPerformed(null);
630     hidden = parentPanel.av.getAlignment().getHiddenColumns();
631     it = hidden.iterator();
632
633     assertTrue(it.hasNext());
634     int[] region = it.next();
635     assertEquals(region[0], 4);
636     assertEquals(region[1], 7);
637
638     assertTrue(it.hasNext());
639     region = it.next();
640     assertEquals(region[0], 10);
641     assertEquals(region[1], 10);
642
643     assertTrue(it.hasNext());
644     region = it.next();
645     assertEquals(region[0], 18);
646     assertEquals(region[1], 20);
647
648     assertTrue(it.hasNext());
649     region = it.next();
650     assertEquals(region[0], 24);
651     assertEquals(region[1], 34);
652
653     assertTrue(it.hasNext());
654     region = it.next();
655     assertEquals(region[0], 45);
656     assertEquals(region[1], 46);
657
658     assertFalse(it.hasNext());
659
660     sel = new ColumnSelection();
661     hidden.revealAllHiddenColumns(sel);
662
663     // make a sequence group and hide insertions within the group
664     SequenceGroup sg = new SequenceGroup();
665     sg.setStartRes(8);
666     sg.setEndRes(42);
667     sg.addSequence(seqGap2, false);
668     sg.addSequence(seqGap3, false);
669     parentPanel.av.setSelectionGroup(sg);
670
671     // hide columns outside and within selection
672     // only hidden columns outside the collection will be retained (unless also
673     // gaps in the selection)
674     hidden.hideColumns(1, 10);
675     hidden.hideColumns(31, 40);
676
677     // get the Popup Menu for LessGappySeq in the sequence group
678     testee = new PopupMenu(parentPanel, seqs.get(5), null);
679     testee.hideInsertions_actionPerformed(null);
680     hidden = parentPanel.av.getAlignment().getHiddenColumns();
681     it = hidden.iterator();
682
683     assertTrue(it.hasNext());
684     region = it.next();
685     assertEquals(region[0], 1);
686     assertEquals(region[1], 7);
687
688     assertTrue(it.hasNext());
689     region = it.next();
690     assertEquals(region[0], 13);
691     assertEquals(region[1], 14);
692
693     assertTrue(it.hasNext());
694     region = it.next();
695     assertEquals(region[0], 34);
696     assertEquals(region[1], 34);
697   }
698
699   @Test(groups = { "Functional" })
700   public void testAddFeatureDetails()
701   {
702     String menuText = MessageManager.getString("label.feature_details");
703
704     /*
705      * with no features, sub-menu should not be created
706      */
707     List<SequenceFeature> features = new ArrayList<>();
708     SequenceI seq = this.alignment.getSequenceAt(0); // FER_CAPAA/1-12
709     testee.addFeatureDetails(features, seq, 10);
710     JMenu menu = findMenu(testee, menuText);
711     assertNull(menu);
712
713     /*
714      * add some features; the menu item text is wrapped in html, and includes
715      * feature type, position, description, group (if not null)
716      */
717     SequenceFeature sf1 = new SequenceFeature("helix", "curly", 2, 6, null);
718     SequenceFeature sf2 = new SequenceFeature("chain", "straight", 1, 1,
719             "uniprot");
720     features.add(sf1);
721     features.add(sf2);
722     testee.addFeatureDetails(features, seq, 10);
723     menu = findMenu(testee, menuText);
724     assertNotNull(menu);
725     assertEquals(2, menu.getItemCount());
726     JMenuItem item = menu.getItem(0);
727     assertEquals("<html>helix 2-6 curly</html>", item.getText());
728     item = menu.getItem(1);
729     assertEquals("<html>chain 1 straight (uniprot)</html>", item.getText());
730
731     /*
732      * long feature descriptions are truncated to 40 characters
733      */
734     sf1.setDescription(
735             "this is a quite extraordinarily long description");
736     testee.remove(menu); // don't create the sub-menu twice
737     testee.addFeatureDetails(features, seq, 10);
738     menu = findMenu(testee, menuText);
739     item = menu.getItem(0);
740     assertEquals(
741             "<html>helix 2-6 this is a quite extraordinarily long des...</html>",
742             item.getText());
743   }
744
745   /**
746    * Returns the first component which is a JMenu with the given text
747    * 
748    * @param c
749    * @param text
750    * @return
751    */
752   private JMenu findMenu(Container c, String text)
753   {
754     for (int i = 0; i < c.getComponentCount(); i++)
755     {
756       Component comp = c.getComponent(i);
757       if ((comp instanceof JMenu) && ((JMenu) comp).getText().equals(text))
758       {
759         return (JMenu) comp;
760       }
761     }
762     return null;
763   }
764
765   @Test(groups = { "Functional" })
766   public void testAddFeatureDetails_linkedFeatures()
767   {
768     // todo tests that verify menu items for complement features
769   }
770 }