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