Merge develop to Release_2_8_3_Branch
[jalview.git] / test / jalview / gui / PopupMenuTest.java
1 package jalview.gui;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6
7 import java.awt.Component;
8 import java.io.IOException;
9 import java.util.ArrayList;
10 import java.util.List;
11
12 import javax.swing.JMenu;
13 import javax.swing.JMenuItem;
14 import javax.swing.JPopupMenu;
15 import javax.swing.JSeparator;
16
17 import org.junit.Before;
18 import org.junit.Test;
19
20 import jalview.datamodel.AlignmentAnnotation;
21 import jalview.datamodel.AlignmentI;
22 import jalview.datamodel.SequenceI;
23 import jalview.io.AppletFormatAdapter;
24 import jalview.io.FormatAdapter;
25
26 public class PopupMenuTest
27 {
28   // 4 sequences x 13 positions
29   final static String TEST_DATA = ">FER_CAPAA Ferredoxin\n"
30           + "TIETHKEAELVG-\n"
31           + ">FER_CAPAN Ferredoxin, chloroplast precursor\n"
32           + "TIETHKEAELVG-\n"
33           + ">FER1_SOLLC Ferredoxin-1, chloroplast precursor\n"
34           + "TIETHKEEELTA-\n" + ">Q93XJ9_SOLTU Ferredoxin I precursor\n"
35           + "TIETHKEEELTA-\n";
36
37   AlignmentI alignment;
38
39   AlignmentPanel parentPanel;
40
41   PopupMenu testee = null;
42
43   @Before
44   public void setUp() throws IOException
45   {
46     alignment = new FormatAdapter().readFile(TEST_DATA,
47             AppletFormatAdapter.PASTE, "FASTA");
48     AlignFrame af = new AlignFrame(alignment, 700, 500);
49     parentPanel = new AlignmentPanel(af, af.getViewport());
50     testee = new PopupMenu(parentPanel, null, null);
51     int i = 0;
52     for (SequenceI seq : alignment.getSequences())
53     {
54       final AlignmentAnnotation annotation = new AlignmentAnnotation("label" + i,
55               "desc" + i, i);
56       annotation.setCalcId("calcId" + i);
57       seq.addAlignmentAnnotation(annotation);
58       annotation.setSequenceRef(seq);
59     }
60   }
61
62   @Test
63   public void testConfigureReferenceAnnotationsMenu_noSequenceSelected()
64   {
65     JMenuItem menu = new JMenuItem();
66     List<SequenceI> seqs = new ArrayList<SequenceI>();
67     testee.configureReferenceAnnotationsMenu(menu, seqs);
68     assertFalse(menu.isEnabled());
69     // now try null list
70     menu.setEnabled(true);
71     testee.configureReferenceAnnotationsMenu(menu, null);
72     assertFalse(menu.isEnabled());
73   }
74
75   /**
76    * Test building the 'add reference annotations' menu for the case where there
77    * are no reference annotations to add to the alignment. The menu item should
78    * be disabled.
79    */
80   @Test
81   public void testConfigureReferenceAnnotationsMenu_noReferenceAnnotations()
82   {
83     JMenuItem menu = new JMenuItem();
84
85     /*
86      * Initial state is that sequences have annotations, and have dataset
87      * sequences, but the dataset sequences have no annotations. Hence nothing
88      * to add.
89      */
90     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
91
92     testee.configureReferenceAnnotationsMenu(menu, seqs);
93     assertFalse(menu.isEnabled());
94   }
95
96   /**
97    * Test building the 'add reference annotations' menu for the case where all
98    * reference annotations are already on the alignment. The menu item should be
99    * disabled.
100    */
101   @Test
102   public void testConfigureReferenceAnnotationsMenu_alreadyAdded()
103   {
104     JMenuItem menu = new JMenuItem();
105     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
106
107     // make up new annotations and add to dataset sequences, sequences and
108     // alignment
109     attachReferenceAnnotations(seqs, true, true);
110
111     testee.configureReferenceAnnotationsMenu(menu, seqs);
112     assertFalse(menu.isEnabled());
113   }
114
115   /**
116    * Test building the 'add reference annotations' menu for the case where
117    * several reference annotations are on the dataset but not on the sequences.
118    * The menu item should be enabled, and acquire a tooltip which lists the
119    * annotation sources (calcIds) and type (labels).
120    */
121   @Test
122   public void testConfigureReferenceAnnotationsMenu()
123   {
124     JMenuItem menu = new JMenuItem();
125     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
126
127     // make up new annotations and add to dataset sequences
128     attachReferenceAnnotations(seqs, false, false);
129
130     testee.configureReferenceAnnotationsMenu(menu, seqs);
131     assertTrue(menu.isEnabled());
132     String expected = "<html><table width=350 border=0><tr><td>Add annotations for<br/>JMOL/secondary structure<br/>PBD/Temp</td></tr></table></html>";
133     assertEquals(expected, menu.getToolTipText());
134   }
135
136   /**
137    * Test building the 'add reference annotations' menu for the case where
138    * several reference annotations are on the dataset and the sequences but not
139    * on the alignment. The menu item should be enabled, and acquire a tooltip
140    * which lists the annotation sources (calcIds) and type (labels).
141    */
142   @Test
143   public void testConfigureReferenceAnnotationsMenu_notOnAlignment()
144   {
145     JMenuItem menu = new JMenuItem();
146     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
147
148     // make up new annotations and add to dataset sequences and sequences
149     attachReferenceAnnotations(seqs, true, false);
150
151     testee.configureReferenceAnnotationsMenu(menu, seqs);
152     assertTrue(menu.isEnabled());
153     String expected = "<html><table width=350 border=0><tr><td>Add annotations for<br/>JMOL/secondary structure<br/>PBD/Temp</td></tr></table></html>";
154     assertEquals(expected, menu.getToolTipText());
155   }
156
157   /**
158    * Generate annotations and add to dataset sequences and (optionally)
159    * sequences and/or alignment
160    * 
161    * @param seqs
162    * @param addToSequence
163    * @param addToAlignment
164    */
165   private void attachReferenceAnnotations(List<SequenceI> seqs,
166           boolean addToSequence, boolean addToAlignment)
167   {
168     // PDB.secondary structure on Sequence0
169     AlignmentAnnotation annotation = new AlignmentAnnotation(
170             "secondary structure", "", 0);
171     annotation.setCalcId("PBD");
172     seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
173     if (addToSequence)
174     {
175       seqs.get(0).addAlignmentAnnotation(annotation);
176     }
177     if (addToAlignment)
178     {
179       this.alignment.addAnnotation(annotation);
180     }
181
182     // PDB.Temp on Sequence1
183     annotation = new AlignmentAnnotation("Temp", "", 0);
184     annotation.setCalcId("PBD");
185     seqs.get(1).getDatasetSequence().addAlignmentAnnotation(annotation);
186     if (addToSequence)
187     {
188       seqs.get(1).addAlignmentAnnotation(annotation);
189     }
190     if (addToAlignment)
191     {
192       this.alignment.addAnnotation(annotation);
193     }
194
195     // JMOL.secondary structure on Sequence0
196     annotation = new AlignmentAnnotation("secondary structure", "", 0);
197     annotation.setCalcId("JMOL");
198     seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
199     if (addToSequence)
200     {
201       seqs.get(0).addAlignmentAnnotation(annotation);
202     }
203     if (addToAlignment)
204     {
205       this.alignment.addAnnotation(annotation);
206     }
207   }
208
209   /**
210    * Test building the 'add reference annotations' menu for the case where there
211    * are two alignment views:
212    * <ul>
213    * <li>in one view, reference annotations have been added (are on the
214    * datasets, sequences and alignment)</li>
215    * <li>in the current view, reference annotations are on the dataset and
216    * sequence, but not the alignment</li>
217    * </ul>
218    * The menu item should be enabled, and acquire a tooltip which lists the
219    * annotation sources (calcIds) and type (labels).
220    */
221   @Test
222   public void testConfigureReferenceAnnotationsMenu_twoViews()
223   {
224   }
225
226   /**
227    * Test for building menu options including 'show' and 'hide' annotation
228    * types.
229    */
230   @Test
231   public void testBuildAnnotationTypesMenus()
232   {
233     JMenu showMenu = new JMenu();
234     JMenu hideMenu = new JMenu();
235     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
236
237     // make up new annotations and add to sequences and to the alignment
238
239     // PDB.secondary structure on Sequence0
240     AlignmentAnnotation annotation = new AlignmentAnnotation(
241             "secondary structure", "", 0);
242     annotation.setCalcId("PDB");
243     annotation.visible = true;
244     seqs.get(0).addAlignmentAnnotation(annotation);
245     parentPanel.getAlignment().addAnnotation(annotation);
246
247     // JMOL.secondary structure on Sequence0 - hidden
248     annotation = new AlignmentAnnotation("secondary structure", "", 0);
249     annotation.setCalcId("JMOL");
250     annotation.visible = false;
251     seqs.get(0).addAlignmentAnnotation(annotation);
252     parentPanel.getAlignment().addAnnotation(annotation);
253
254     // Jpred.SSP on Sequence0 - hidden
255     annotation = new AlignmentAnnotation("SSP", "", 0);
256     annotation.setCalcId("JPred");
257     annotation.visible = false;
258     seqs.get(0).addAlignmentAnnotation(annotation);
259     parentPanel.getAlignment().addAnnotation(annotation);
260
261     // PDB.Temp on Sequence1
262     annotation = new AlignmentAnnotation("Temp", "", 0);
263     annotation.setCalcId("PDB");
264     annotation.visible = true;
265     seqs.get(1).addAlignmentAnnotation(annotation);
266     parentPanel.getAlignment().addAnnotation(annotation);
267
268     /*
269      * Expect menu options to show "secondary structure" and "SSP", and to hide
270      * "secondary structure" and "Temp". Tooltip should be calcId.
271      */
272     testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
273
274     assertTrue(showMenu.isEnabled());
275     assertTrue(hideMenu.isEnabled());
276
277     Component[] showOptions = showMenu.getMenuComponents();
278     Component[] hideOptions = hideMenu.getMenuComponents();
279
280     assertEquals(4, showOptions.length); // includes 'All' and separator
281     assertEquals(4, hideOptions.length);
282     assertEquals("All",
283             ((JMenuItem) showOptions[0]).getText());
284     assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
285     assertEquals(JSeparator.HORIZONTAL,
286             ((JSeparator) showOptions[1]).getOrientation());
287     assertEquals("secondary structure",
288             ((JMenuItem) showOptions[2]).getText());
289     assertEquals("JMOL", ((JMenuItem) showOptions[2]).getToolTipText());
290     assertEquals("SSP", ((JMenuItem) showOptions[3]).getText());
291     assertEquals("JPred", ((JMenuItem) showOptions[3]).getToolTipText());
292
293     assertEquals("All",
294             ((JMenuItem) hideOptions[0]).getText());
295     assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
296     assertEquals(JSeparator.HORIZONTAL,
297             ((JSeparator) hideOptions[1]).getOrientation());
298     assertEquals("secondary structure",
299             ((JMenuItem) hideOptions[2]).getText());
300     assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
301     assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
302     assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
303   }
304
305   /**
306    * Test for building menu options with only 'hide' annotation types enabled.
307    */
308   @Test
309   public void testBuildAnnotationTypesMenus_showDisabled()
310   {
311     JMenu showMenu = new JMenu();
312     JMenu hideMenu = new JMenu();
313     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
314
315     // make up new annotations and add to sequences and to the alignment
316
317     // PDB.secondary structure on Sequence0
318     AlignmentAnnotation annotation = new AlignmentAnnotation(
319             "secondary structure", "", 0);
320     annotation.setCalcId("PDB");
321     annotation.visible = true;
322     seqs.get(0).addAlignmentAnnotation(annotation);
323     parentPanel.getAlignment().addAnnotation(annotation);
324
325     // PDB.Temp on Sequence1
326     annotation = new AlignmentAnnotation("Temp", "", 0);
327     annotation.setCalcId("PDB");
328     annotation.visible = true;
329     seqs.get(1).addAlignmentAnnotation(annotation);
330     parentPanel.getAlignment().addAnnotation(annotation);
331
332     /*
333      * Expect menu options to hide "secondary structure" and "Temp". Tooltip
334      * should be calcId. 'Show' menu should be disabled.
335      */
336     testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
337
338     assertFalse(showMenu.isEnabled());
339     assertTrue(hideMenu.isEnabled());
340
341     Component[] showOptions = showMenu.getMenuComponents();
342     Component[] hideOptions = hideMenu.getMenuComponents();
343
344     assertEquals(2, showOptions.length); // includes 'All' and separator
345     assertEquals(4, hideOptions.length);
346     assertEquals("All", ((JMenuItem) showOptions[0]).getText());
347     assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
348     assertEquals(JSeparator.HORIZONTAL,
349             ((JSeparator) showOptions[1]).getOrientation());
350
351     assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
352     assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
353     assertEquals(JSeparator.HORIZONTAL,
354             ((JSeparator) hideOptions[1]).getOrientation());
355     assertEquals("secondary structure",
356             ((JMenuItem) hideOptions[2]).getText());
357     assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
358     assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
359     assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
360   }
361
362   /**
363    * Test for building menu options with only 'show' annotation types enabled.
364    */
365   @Test
366   public void testBuildAnnotationTypesMenus_hideDisabled()
367   {
368     JMenu showMenu = new JMenu();
369     JMenu hideMenu = new JMenu();
370     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
371
372     // make up new annotations and add to sequences and to the alignment
373
374     // PDB.secondary structure on Sequence0
375     AlignmentAnnotation annotation = new AlignmentAnnotation(
376             "secondary structure", "", 0);
377     annotation.setCalcId("PDB");
378     annotation.visible = false;
379     seqs.get(0).addAlignmentAnnotation(annotation);
380     parentPanel.getAlignment().addAnnotation(annotation);
381
382     // PDB.Temp on Sequence1
383     annotation = new AlignmentAnnotation("Temp", "", 0);
384     annotation.setCalcId("PDB2");
385     annotation.visible = false;
386     seqs.get(1).addAlignmentAnnotation(annotation);
387     parentPanel.getAlignment().addAnnotation(annotation);
388
389     /*
390      * Expect menu options to show "secondary structure" and "Temp". Tooltip
391      * should be calcId. 'hide' menu should be disabled.
392      */
393     testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
394
395     assertTrue(showMenu.isEnabled());
396     assertFalse(hideMenu.isEnabled());
397
398     Component[] showOptions = showMenu.getMenuComponents();
399     Component[] hideOptions = hideMenu.getMenuComponents();
400
401     assertEquals(4, showOptions.length); // includes 'All' and separator
402     assertEquals(2, hideOptions.length);
403     assertEquals("All", ((JMenuItem) showOptions[0]).getText());
404     assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
405     assertEquals(JSeparator.HORIZONTAL,
406             ((JSeparator) showOptions[1]).getOrientation());
407     assertEquals("secondary structure",
408             ((JMenuItem) showOptions[2]).getText());
409     assertEquals("PDB", ((JMenuItem) showOptions[2]).getToolTipText());
410     assertEquals("Temp", ((JMenuItem) showOptions[3]).getText());
411     assertEquals("PDB2", ((JMenuItem) showOptions[3]).getToolTipText());
412
413     assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
414     assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
415     assertEquals(JSeparator.HORIZONTAL,
416             ((JSeparator) hideOptions[1]).getOrientation());
417   }
418 }