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