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