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