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