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