JAL-1264 refactor to add menu options to _both_ sequence and selection
[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.util.MessageManager;
11
12 import java.awt.Component;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import javax.swing.JMenu;
18 import javax.swing.JMenuItem;
19 import javax.swing.JPopupMenu;
20 import javax.swing.JSeparator;
21
22 import org.junit.Before;
23 import org.junit.Test;
24
25 public class PopupMenuTest
26 {
27   // 4 sequences x 13 positions
28   final static String TEST_DATA = ">FER_CAPAA Ferredoxin\n"
29           + "TIETHKEAELVG-\n"
30           + ">FER_CAPAN Ferredoxin, chloroplast precursor\n"
31           + "TIETHKEAELVG-\n"
32           + ">FER1_SOLLC Ferredoxin-1, chloroplast precursor\n"
33           + "TIETHKEEELTA-\n" + ">Q93XJ9_SOLTU Ferredoxin I precursor\n"
34           + "TIETHKEEELTA-\n";
35
36   AlignmentI alignment;
37
38   AlignmentPanel parentPanel;
39
40   PopupMenu testee = null;
41
42   @Before
43   public void setUp() throws IOException
44   {
45     alignment = new jalview.io.FormatAdapter().readFile(TEST_DATA,
46             AppletFormatAdapter.PASTE, "FASTA");
47     AlignFrame af = new AlignFrame(alignment, 700, 500);
48     parentPanel = new AlignmentPanel(af, af.getViewport());
49     testee = new PopupMenu(parentPanel, null, null);
50     int i = 0;
51     for (SequenceI seq : alignment.getSequences())
52     {
53       final AlignmentAnnotation annotation = new AlignmentAnnotation("label" + i,
54               "desc" + i, i);
55       annotation.setCalcId("calcId" + i);
56       seq.addAlignmentAnnotation(annotation);
57       annotation.setSequenceRef(seq);
58     }
59   }
60
61   @Test
62   public void testConfigureReferenceAnnotationsMenu_noSequenceSelected()
63   {
64     JMenuItem menu = new JMenuItem();
65     List<SequenceI> seqs = new ArrayList<SequenceI>();
66     testee.configureReferenceAnnotationsMenu(menu, seqs);
67     assertFalse(menu.isEnabled());
68     assertEquals(
69             MessageManager.getString("label.add_reference_annotations"),
70             menu.getText());
71     // now try null list
72     menu.setEnabled(true);
73     testee.configureReferenceAnnotationsMenu(menu, seqs);
74     assertFalse(menu.isEnabled());
75   }
76
77   /**
78    * Test building the 'add reference annotations' menu for the case where there
79    * are no reference annotations to add to the alignment. The menu item should
80    * be disabled.
81    */
82   @Test
83   public void testConfigureReferenceAnnotationsMenu_noReferenceAnnotations()
84   {
85     JMenuItem menu = new JMenuItem();
86     List<SequenceI> seqs = new ArrayList<SequenceI>();
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     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 = new ArrayList<SequenceI>();
109
110     seqs = parentPanel.getAlignment().getSequences();
111     // copy annotation from sequence to dataset
112     seqs.get(1).getDatasetSequence()
113             .addAlignmentAnnotation(seqs.get(1).getAnnotation()[0]);
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     // make up new annotations and add to dataset sequences
130
131     // PDB.secondary structure on Sequence0
132     AlignmentAnnotation annotation = new AlignmentAnnotation(
133             "secondary structure", "", 0);
134     annotation.setCalcId("PBD");
135     seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
136
137     // PDB.Temp on Sequence1
138     annotation = new AlignmentAnnotation("Temp", "", 0);
139     annotation.setCalcId("PBD");
140     seqs.get(1).getDatasetSequence().addAlignmentAnnotation(annotation);
141
142     // JMOL.secondary structure on Sequence0
143     annotation = new AlignmentAnnotation("secondary structure", "", 0);
144     annotation.setCalcId("JMOL");
145     seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
146
147     testee.configureReferenceAnnotationsMenu(menu, seqs);
148     assertTrue(menu.isEnabled());
149     String expected = "<html><table width=350 border=0><tr><td>Add annotations for<br/>JMOL/secondary structure<br/>PBD/Temp</td></tr></table></html>";
150     assertEquals(expected, menu.getToolTipText());
151   }
152
153   /**
154    * Test for building menu options including 'show' and 'hide' annotation
155    * types.
156    */
157   @Test
158   public void testBuildAnnotationTypesMenus()
159   {
160     JMenu showMenu = new JMenu();
161     JMenu hideMenu = new JMenu();
162     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
163
164     // make up new annotations and add to sequences and to the alignment
165
166     // PDB.secondary structure on Sequence0
167     AlignmentAnnotation annotation = new AlignmentAnnotation(
168             "secondary structure", "", 0);
169     annotation.setCalcId("PDB");
170     annotation.visible = true;
171     seqs.get(0).addAlignmentAnnotation(annotation);
172     parentPanel.getAlignment().addAnnotation(annotation);
173
174     // JMOL.secondary structure on Sequence0 - hidden
175     annotation = new AlignmentAnnotation("secondary structure", "", 0);
176     annotation.setCalcId("JMOL");
177     annotation.visible = false;
178     seqs.get(0).addAlignmentAnnotation(annotation);
179     parentPanel.getAlignment().addAnnotation(annotation);
180
181     // Jpred.SSP on Sequence0 - hidden
182     annotation = new AlignmentAnnotation("SSP", "", 0);
183     annotation.setCalcId("JPred");
184     annotation.visible = false;
185     seqs.get(0).addAlignmentAnnotation(annotation);
186     parentPanel.getAlignment().addAnnotation(annotation);
187
188     // PDB.Temp on Sequence1
189     annotation = new AlignmentAnnotation("Temp", "", 0);
190     annotation.setCalcId("PDB");
191     annotation.visible = true;
192     seqs.get(1).addAlignmentAnnotation(annotation);
193     parentPanel.getAlignment().addAnnotation(annotation);
194
195     /*
196      * Expect menu options to show "secondary structure" and "SSP", and to hide
197      * "secondary structure" and "Temp". Tooltip should be calcId.
198      */
199     testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
200
201     assertTrue(showMenu.isEnabled());
202     assertTrue(hideMenu.isEnabled());
203
204     Component[] showOptions = showMenu.getMenuComponents();
205     Component[] hideOptions = hideMenu.getMenuComponents();
206
207     assertEquals(4, showOptions.length); // includes 'All' and separator
208     assertEquals(4, hideOptions.length);
209     assertEquals("All",
210             ((JMenuItem) showOptions[0]).getText());
211     assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
212     assertEquals(JSeparator.HORIZONTAL,
213             ((JSeparator) showOptions[1]).getOrientation());
214     assertEquals("secondary structure",
215             ((JMenuItem) showOptions[2]).getText());
216     assertEquals("JMOL", ((JMenuItem) showOptions[2]).getToolTipText());
217     assertEquals("SSP", ((JMenuItem) showOptions[3]).getText());
218     assertEquals("JPred", ((JMenuItem) showOptions[3]).getToolTipText());
219
220     assertEquals("All",
221             ((JMenuItem) hideOptions[0]).getText());
222     assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
223     assertEquals(JSeparator.HORIZONTAL,
224             ((JSeparator) hideOptions[1]).getOrientation());
225     assertEquals("secondary structure",
226             ((JMenuItem) hideOptions[2]).getText());
227     assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
228     assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
229     assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
230   }
231
232   /**
233    * Test for building menu options with only 'hide' annotation types enabled.
234    */
235   @Test
236   public void testBuildAnnotationTypesMenus_showDisabled()
237   {
238     JMenu showMenu = new JMenu();
239     JMenu hideMenu = new JMenu();
240     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
241
242     // make up new annotations and add to sequences and to the alignment
243
244     // PDB.secondary structure on Sequence0
245     AlignmentAnnotation annotation = new AlignmentAnnotation(
246             "secondary structure", "", 0);
247     annotation.setCalcId("PDB");
248     annotation.visible = true;
249     seqs.get(0).addAlignmentAnnotation(annotation);
250     parentPanel.getAlignment().addAnnotation(annotation);
251
252     // PDB.Temp on Sequence1
253     annotation = new AlignmentAnnotation("Temp", "", 0);
254     annotation.setCalcId("PDB");
255     annotation.visible = true;
256     seqs.get(1).addAlignmentAnnotation(annotation);
257     parentPanel.getAlignment().addAnnotation(annotation);
258
259     /*
260      * Expect menu options to hide "secondary structure" and "Temp". Tooltip
261      * should be calcId. 'Show' menu should be disabled.
262      */
263     testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
264
265     assertFalse(showMenu.isEnabled());
266     assertTrue(hideMenu.isEnabled());
267
268     Component[] showOptions = showMenu.getMenuComponents();
269     Component[] hideOptions = hideMenu.getMenuComponents();
270
271     assertEquals(2, showOptions.length); // includes 'All' and separator
272     assertEquals(4, hideOptions.length);
273     assertEquals("All", ((JMenuItem) showOptions[0]).getText());
274     assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
275     assertEquals(JSeparator.HORIZONTAL,
276             ((JSeparator) showOptions[1]).getOrientation());
277
278     assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
279     assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
280     assertEquals(JSeparator.HORIZONTAL,
281             ((JSeparator) hideOptions[1]).getOrientation());
282     assertEquals("secondary structure",
283             ((JMenuItem) hideOptions[2]).getText());
284     assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
285     assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
286     assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
287   }
288
289   /**
290    * Test for building menu options with only 'show' annotation types enabled.
291    */
292   @Test
293   public void testBuildAnnotationTypesMenus_hideDisabled()
294   {
295     JMenu showMenu = new JMenu();
296     JMenu hideMenu = new JMenu();
297     List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
298
299     // make up new annotations and add to sequences and to the alignment
300
301     // PDB.secondary structure on Sequence0
302     AlignmentAnnotation annotation = new AlignmentAnnotation(
303             "secondary structure", "", 0);
304     annotation.setCalcId("PDB");
305     annotation.visible = false;
306     seqs.get(0).addAlignmentAnnotation(annotation);
307     parentPanel.getAlignment().addAnnotation(annotation);
308
309     // PDB.Temp on Sequence1
310     annotation = new AlignmentAnnotation("Temp", "", 0);
311     annotation.setCalcId("PDB2");
312     annotation.visible = false;
313     seqs.get(1).addAlignmentAnnotation(annotation);
314     parentPanel.getAlignment().addAnnotation(annotation);
315
316     /*
317      * Expect menu options to show "secondary structure" and "Temp". Tooltip
318      * should be calcId. 'hide' menu should be disabled.
319      */
320     testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
321
322     assertTrue(showMenu.isEnabled());
323     assertFalse(hideMenu.isEnabled());
324
325     Component[] showOptions = showMenu.getMenuComponents();
326     Component[] hideOptions = hideMenu.getMenuComponents();
327
328     assertEquals(4, showOptions.length); // includes 'All' and separator
329     assertEquals(2, hideOptions.length);
330     assertEquals("All", ((JMenuItem) showOptions[0]).getText());
331     assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
332     assertEquals(JSeparator.HORIZONTAL,
333             ((JSeparator) showOptions[1]).getOrientation());
334     assertEquals("secondary structure",
335             ((JMenuItem) showOptions[2]).getText());
336     assertEquals("PDB", ((JMenuItem) showOptions[2]).getToolTipText());
337     assertEquals("Temp", ((JMenuItem) showOptions[3]).getText());
338     assertEquals("PDB2", ((JMenuItem) showOptions[3]).getToolTipText());
339
340     assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
341     assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
342     assertEquals(JSeparator.HORIZONTAL,
343             ((JSeparator) hideOptions[1]).getOrientation());
344   }
345 }