JAL-3438 spotless for 2.11.2.0
[jalview.git] / test / jalview / gui / AnnotationChooserTest.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.analysis.AnnotationSorter.SequenceAnnotationOrder;
28 import jalview.bin.Cache;
29 import jalview.datamodel.AlignmentAnnotation;
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.Annotation;
32 import jalview.datamodel.SequenceGroup;
33 import jalview.datamodel.SequenceI;
34 import jalview.io.DataSourceType;
35 import jalview.io.FileFormat;
36 import jalview.io.FormatAdapter;
37 import jalview.util.MessageManager;
38
39 import java.awt.BorderLayout;
40 import java.awt.Checkbox;
41 import java.awt.Component;
42 import java.awt.Container;
43 import java.awt.FlowLayout;
44 import java.awt.event.ItemEvent;
45 import java.io.IOException;
46 import java.util.List;
47
48 import javax.swing.JButton;
49 import javax.swing.JPanel;
50
51 import org.testng.annotations.BeforeClass;
52 import org.testng.annotations.BeforeMethod;
53 import org.testng.annotations.Test;
54
55 /**
56  * Unit tests for AnnotationChooser
57  * 
58  * @author gmcarstairs
59  *
60  */
61 public class AnnotationChooserTest
62 {
63
64   @BeforeClass(alwaysRun = true)
65   public void setUpJvOptionPane()
66   {
67     JvOptionPane.setInteractiveMode(false);
68     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
69   }
70
71   // 4 sequences x 13 positions
72   final static String TEST_DATA = ">FER_CAPAA Ferredoxin\n"
73           + "TIETHKEAELVG-\n"
74           + ">FER_CAPAN Ferredoxin, chloroplast precursor\n"
75           + "TIETHKEAELVG-\n"
76           + ">FER1_SOLLC Ferredoxin-1, chloroplast precursor\n"
77           + "TIETHKEEELTA-\n" + ">Q93XJ9_SOLTU Ferredoxin I precursor\n"
78           + "TIETHKEEELTA-\n";
79
80   AnnotationChooser testee;
81
82   AlignmentPanel parentPanel;
83
84   AlignFrame af;
85
86   @BeforeMethod(alwaysRun = true)
87   public void setUp() throws IOException
88   {
89     Cache.loadProperties("test/jalview/io/testProps.jvprops");
90     // pin down annotation sort order for test
91     Cache.applicationProperties.setProperty(Preferences.SORT_ANNOTATIONS,
92             SequenceAnnotationOrder.NONE.name());
93     final String TRUE = Boolean.TRUE.toString();
94     Cache.applicationProperties.setProperty(Preferences.SHOW_AUTOCALC_ABOVE,
95             TRUE);
96     Cache.applicationProperties.setProperty("SHOW_QUALITY", TRUE);
97     Cache.applicationProperties.setProperty("SHOW_CONSERVATION", TRUE);
98     Cache.applicationProperties.setProperty("SHOW_IDENTITY", TRUE);
99
100     AlignmentI al = new FormatAdapter().readFile(TEST_DATA,
101             DataSourceType.PASTE, FileFormat.Fasta);
102     af = new AlignFrame(al, 700, 500);
103     parentPanel = new AlignmentPanel(af, af.getViewport());
104     addAnnotations();
105   }
106
107   /**
108    * Add 4 annotations, 3 of them sequence-specific.
109    * 
110    * <PRE>
111    * ann1 - for sequence 0 - label 'IUPRED' ann2 - not sequence related - label
112    * 'Beauty' ann3 - for sequence 3 - label 'JMol' ann4 - for sequence 2 - label
113    * 'IUPRED' ann5 - for sequence 1 - label 'JMol'
114    */
115   private void addAnnotations()
116   {
117     Annotation an = new Annotation(2f);
118     Annotation[] anns = new Annotation[] { an, an, an };
119     AlignmentAnnotation ann0 = new AlignmentAnnotation("IUPRED", "", anns);
120     AlignmentAnnotation ann1 = new AlignmentAnnotation("Beauty", "", anns);
121     AlignmentAnnotation ann2 = new AlignmentAnnotation("JMol", "", anns);
122     AlignmentAnnotation ann3 = new AlignmentAnnotation("IUPRED", "", anns);
123     AlignmentAnnotation ann4 = new AlignmentAnnotation("JMol", "", anns);
124     SequenceI[] seqs = parentPanel.getAlignment().getSequencesArray();
125     ann0.setSequenceRef(seqs[0]);
126     ann2.setSequenceRef(seqs[3]);
127     ann3.setSequenceRef(seqs[2]);
128     ann4.setSequenceRef(seqs[1]);
129     parentPanel.getAlignment().addAnnotation(ann0);
130     parentPanel.getAlignment().addAnnotation(ann1);
131     parentPanel.getAlignment().addAnnotation(ann2);
132     parentPanel.getAlignment().addAnnotation(ann3);
133     parentPanel.getAlignment().addAnnotation(ann4);
134   }
135
136   /**
137    * Test creation of panel with OK and Cancel buttons
138    */
139   @Test(groups = { "Functional" })
140   public void testBuildActionButtonsPanel()
141   {
142     testee = new AnnotationChooser(parentPanel);
143     JPanel jp = testee.buildActionButtonsPanel();
144     assertTrue("Wrong layout", jp.getLayout() instanceof FlowLayout);
145
146     Component[] comps = jp.getComponents();
147     assertEquals("Not 2 action buttons", 2, comps.length);
148
149     final Component jb1 = comps[0];
150     final Component jb2 = comps[1];
151
152     assertEquals("Not 'OK' button", MessageManager.getString("action.ok"),
153             ((JButton) jb1).getText());
154     assertEquals("Wrong button font", JvSwingUtils.getLabelFont(),
155             jb1.getFont());
156
157     assertEquals("Not 'Cancel' button",
158             MessageManager.getString("action.cancel"),
159             ((JButton) jb2).getText());
160     assertEquals("Wrong button font", JvSwingUtils.getLabelFont(),
161             jb2.getFont());
162   }
163
164   /**
165    * Test 'Apply to' has 3 radio buttons enabled, 'Selected Sequences' selected,
166    * when there is a current selection group.
167    */
168   @Test(groups = { "Functional" })
169   public void testBuildApplyToOptionsPanel_withSelectionGroup()
170   {
171     selectSequences(0, 2, 3);
172     testee = new AnnotationChooser(parentPanel);
173
174     JPanel jp = testee.buildApplyToOptionsPanel();
175     Component[] comps = jp.getComponents();
176     assertEquals("Not 3 radio buttons", 3, comps.length);
177
178     final Checkbox cb1 = (Checkbox) comps[0];
179     final Checkbox cb2 = (Checkbox) comps[1];
180     final Checkbox cb3 = (Checkbox) comps[2];
181
182     assertTrue("Not enabled", cb1.isEnabled());
183     assertTrue("Not enabled", cb2.isEnabled());
184     assertTrue("Not enabled", cb3.isEnabled());
185     assertEquals("Option not selected", cb2,
186             cb2.getCheckboxGroup().getSelectedCheckbox());
187
188     // check state variables match checkbox selection
189     assertTrue(testee.isApplyToSelectedSequences());
190     assertFalse(testee.isApplyToUnselectedSequences());
191   }
192
193   /**
194    * Add a sequence group to the alignment with the specified sequences (base 0)
195    * in it
196    * 
197    * @param i
198    * @param more
199    */
200   private void selectSequences(int... selected)
201   {
202     SequenceI[] seqs = parentPanel.getAlignment().getSequencesArray();
203     SequenceGroup sg = new SequenceGroup();
204     for (int i : selected)
205     {
206       sg.addSequence(seqs[i], false);
207     }
208     parentPanel.av.setSelectionGroup(sg);
209   }
210
211   /**
212    * Test 'Apply to' has 1 radio button enabled, 'All Sequences' selected, when
213    * there is no current selection group.
214    */
215   @Test(groups = { "Functional" })
216   public void testBuildApplyToOptionsPanel_noSelectionGroup()
217   {
218     testee = new AnnotationChooser(parentPanel);
219     JPanel jp = testee.buildApplyToOptionsPanel();
220     verifyApplyToOptionsPanel_noSelectionGroup(jp);
221   }
222
223   protected void verifyApplyToOptionsPanel_noSelectionGroup(JPanel jp)
224   {
225     assertTrue("Wrong layout", jp.getLayout() instanceof FlowLayout);
226     Component[] comps = jp.getComponents();
227     assertEquals("Not 3 radio buttons", 3, comps.length);
228
229     final Checkbox cb1 = (Checkbox) comps[0];
230     final Checkbox cb2 = (Checkbox) comps[1];
231     final Checkbox cb3 = (Checkbox) comps[2];
232
233     assertTrue("Not enabled", cb1.isEnabled());
234     assertFalse("Enabled", cb2.isEnabled());
235     assertFalse("Enabled", cb3.isEnabled());
236     assertEquals("Not selected", cb1,
237             cb1.getCheckboxGroup().getSelectedCheckbox());
238
239     // check state variables match checkbox selection
240     assertTrue(testee.isApplyToSelectedSequences());
241     assertTrue(testee.isApplyToUnselectedSequences());
242
243     assertEquals("Wrong text",
244             MessageManager.getString("label.all_sequences"),
245             cb1.getLabel());
246     assertEquals("Wrong text",
247             MessageManager.getString("label.selected_sequences"),
248             cb2.getLabel());
249     assertEquals("Wrong text",
250             MessageManager.getString("label.except_selected_sequences"),
251             cb3.getLabel());
252   }
253
254   /**
255    * Test Show and Hide radio buttons created, with Hide initially selected.
256    */
257   @Test(groups = { "Functional" })
258   public void testBuildShowHidePanel()
259   {
260     testee = new AnnotationChooser(parentPanel);
261     JPanel jp = testee.buildShowHidePanel();
262     verifyShowHidePanel(jp);
263
264   }
265
266   protected void verifyShowHidePanel(JPanel jp)
267   {
268     assertTrue("Wrong layout", jp.getLayout() instanceof FlowLayout);
269     Component[] comps = jp.getComponents();
270     assertEquals("Not 2 radio buttons", 2, comps.length);
271
272     final Checkbox cb1 = (Checkbox) comps[0];
273     final Checkbox cb2 = (Checkbox) comps[1];
274
275     assertTrue("Show not enabled", cb1.isEnabled());
276     assertTrue("Hide not enabled", cb2.isEnabled());
277
278     // Hide (button 2) selected; note this may change to none (null)
279     assertEquals("Not selected", cb2,
280             cb2.getCheckboxGroup().getSelectedCheckbox());
281
282     assertTrue("Show is flagged", !testee.isShowSelected());
283
284     assertEquals("Wrong text",
285             MessageManager.getString("label.show_selected_annotations"),
286             cb1.getLabel());
287     assertEquals("Wrong text",
288             MessageManager.getString("label.hide_selected_annotations"),
289             cb2.getLabel());
290   }
291
292   /**
293    * Test construction of panel containing two sub-panels
294    */
295   @Test(groups = { "Functional" })
296   public void testBuildShowHideOptionsPanel()
297   {
298     testee = new AnnotationChooser(parentPanel);
299     JPanel jp = testee.buildShowHideOptionsPanel();
300     assertTrue("Wrong layout", jp.getLayout() instanceof BorderLayout);
301     Component[] comps = jp.getComponents();
302     assertEquals("Not 2 sub-panels", 2, comps.length);
303
304     verifyShowHidePanel((JPanel) comps[0]);
305     verifyApplyToOptionsPanel_noSelectionGroup((JPanel) comps[1]);
306   }
307
308   /**
309    * Test that annotation types are (uniquely) identified.
310    * 
311    */
312   @Test(groups = { "Functional" })
313   public void testGetAnnotationTypes()
314   {
315     selectSequences(1);
316     testee = new AnnotationChooser(parentPanel);
317     // selection group should make no difference to the result
318     // as all annotation types for the alignment are considered
319
320     List<String> types = AnnotationChooser
321             .getAnnotationTypes(parentPanel.getAlignment(), true);
322     assertEquals("Not two annotation types", 2, types.size());
323     assertTrue("IUPRED missing", types.contains("IUPRED"));
324     assertTrue("JMol missing", types.contains("JMol"));
325
326     types = AnnotationChooser.getAnnotationTypes(parentPanel.getAlignment(),
327             false);
328     assertEquals("Not six annotation types", 7, types.size());
329     assertTrue("IUPRED missing", types.contains("IUPRED"));
330     assertTrue("JMol missing", types.contains("JMol"));
331     assertTrue("Beauty missing", types.contains("Beauty"));
332     // These are added by viewmodel.AlignViewport.initAutoAnnotation():
333     assertTrue("Consensus missing", types.contains("Consensus"));
334     assertTrue("Quality missing", types.contains("Quality"));
335     assertTrue("Conservation missing", types.contains("Conservation"));
336     assertTrue("Occupancy missing", types.contains("Occupancy"));
337   }
338
339   /**
340    * Test result of selecting an annotation type, with 'Hide for all sequences'.
341    * 
342    * We expect all annotations of that type to be set hidden. Other annotations
343    * should be left visible.
344    */
345   @Test(groups = { "Functional" })
346   public void testSelectType_hideForAll()
347   {
348     selectSequences(1, 2);
349     testee = new AnnotationChooser(parentPanel);
350     final Checkbox hideCheckbox = (Checkbox) getComponent(testee, 1, 0, 1);
351     setSelected(hideCheckbox, true);
352
353     final Checkbox allSequencesCheckbox = (Checkbox) getComponent(testee, 1,
354             1, 0);
355     setSelected(allSequencesCheckbox, true);
356
357     AlignmentAnnotation[] anns = parentPanel.getAlignment()
358             .getAlignmentAnnotation();
359
360     int autocalc = countAutocalc(anns);
361     assertTrue(anns[autocalc + 2].visible); // JMol for seq3
362     assertTrue(anns[autocalc + 4].visible); // JMol for seq1
363
364     setSelected(getTypeCheckbox("JMol"), true);
365     assertTrue(anns[0].visible); // Conservation
366     assertTrue(anns[1].visible); // Quality
367     assertTrue(anns[2].visible); // Consensus
368     assertTrue(anns[3].visible); // Occupancy
369     assertTrue(anns[4].visible); // IUPred for seq0
370     assertTrue(anns[5].visible); // Beauty
371     assertFalse(anns[6].visible); // JMol for seq3 - not selected but hidden
372     assertTrue(anns[7].visible); // IUPRED for seq2
373     assertFalse(anns[8].visible); // JMol for seq1 - selected and hidden
374   }
375
376   /**
377    * Test result of selecting an annotation type, with 'Hide for selected
378    * sequences'.
379    * 
380    * We expect the annotations of that type, linked to the sequence group, to be
381    * set hidden. Other annotations should be left visible.
382    */
383   @Test(groups = { "Functional" })
384   public void testSelectType_hideForSelected()
385   {
386     selectSequences(1, 2);
387     testee = new AnnotationChooser(parentPanel);
388     final Checkbox hideCheckbox = (Checkbox) getComponent(testee, 1, 0, 1);
389     setSelected(hideCheckbox, true);
390
391     /*
392      * Don't set the 'selected sequences' radio button since this would trigger
393      * an update, including unselected sequences / annotation types
394      */
395     // setSelected(getSelectedSequencesCheckbox());
396
397     AlignmentAnnotation[] anns = parentPanel.getAlignment()
398             .getAlignmentAnnotation();
399
400     int autocalc = countAutocalc(anns);
401     assertTrue(anns[autocalc + 4].visible); // JMol for seq1
402
403     setSelected(getTypeCheckbox("JMol"), true);
404     assertTrue(anns[0].visible); // Conservation
405     assertTrue(anns[1].visible); // Quality
406     assertTrue(anns[2].visible); // Consensus
407     assertTrue(anns[3].visible); // Occupancy
408     assertTrue(anns[4].visible); // IUPred for seq0
409     assertTrue(anns[5].visible); // Beauty
410     assertTrue(anns[6].visible); // JMol for seq3 not in selection group
411     assertTrue(anns[7].visible); // IUPRED for seq2
412     assertFalse(anns[8].visible); // JMol for seq1 in selection group
413   }
414
415   /**
416    * Test result of deselecting an annotation type, with 'Hide for all
417    * sequences'.
418    * 
419    * We expect all annotations of that type to be set visible. Other annotations
420    * should be left unchanged.
421    */
422   @Test(groups = { "Functional" })
423   public void testDeselectType_hideForAll()
424   {
425     selectSequences(1, 2);
426     testee = new AnnotationChooser(parentPanel);
427
428     final Checkbox hideCheckbox = (Checkbox) getComponent(testee, 1, 0, 1);
429     setSelected(hideCheckbox, true);
430
431     final Checkbox allSequencesCheckbox = (Checkbox) getComponent(testee, 1,
432             1, 0);
433     setSelected(allSequencesCheckbox, true);
434
435     AlignmentAnnotation[] anns = parentPanel.getAlignment()
436             .getAlignmentAnnotation();
437
438     final Checkbox typeCheckbox = getTypeCheckbox("JMol");
439
440     // select JMol - all hidden
441     setSelected(typeCheckbox, true);
442     int autocalc = countAutocalc(anns);
443     assertFalse(anns[autocalc + 2].visible); // JMol for seq3
444     assertFalse(anns[autocalc + 4].visible); // JMol for seq1
445
446     // deselect JMol - all unhidden
447     setSelected(typeCheckbox, false);
448     for (AlignmentAnnotation ann : anns)
449     {
450       assertTrue(ann.visible);
451     }
452   }
453
454   /**
455    * Returns a count of autocalculated annotations in the set provided
456    * 
457    * @param anns
458    * @return
459    */
460   private int countAutocalc(AlignmentAnnotation[] anns)
461   {
462     int count = 0;
463     for (AlignmentAnnotation ann : anns)
464     {
465       if (ann.autoCalculated)
466       {
467         count++;
468       }
469     }
470     return count;
471   }
472
473   /**
474    * Test result of deselecting an annotation type, with 'Hide for selected
475    * sequences'.
476    * 
477    * We expect the annotations of that type, linked to the sequence group, to be
478    * set visible. Other annotations should be left unchanged.
479    */
480   @Test(groups = { "Functional" })
481   public void testDeselectType_hideForSelected()
482   {
483     selectSequences(1, 2);
484     testee = new AnnotationChooser(parentPanel);
485     final Checkbox hideCheckbox = (Checkbox) getComponent(testee, 1, 0, 1);
486     setSelected(hideCheckbox, true);
487
488     /*
489      * Don't set the 'selected sequences' radio button since this would trigger
490      * an update, including unselected sequences / annotation types
491      */
492     // setSelected(getSelectedSequencesCheckbox());
493
494     setSelected(getTypeCheckbox("JMol"), true);
495     setSelected(getTypeCheckbox("JMol"), false);
496
497     AlignmentAnnotation[] anns = parentPanel.getAlignment()
498             .getAlignmentAnnotation();
499     assertTrue(anns[0].visible); // Conservation
500     assertTrue(anns[1].visible); // Quality
501     assertTrue(anns[2].visible); // Consensus
502     assertTrue(anns[3].visible); // IUPred for seq0
503     assertTrue(anns[4].visible); // Beauty
504     assertTrue(anns[5].visible); // JMol for seq3 not in selection group
505     assertTrue(anns[6].visible); // IUPRED for seq2
506     assertTrue(anns[7].visible); // JMol for seq1 in selection group
507   }
508
509   /**
510    * Test result of selecting an annotation type, with 'Show for all sequences'.
511    * 
512    * We expect all annotations of that type to be set visible. Other annotations
513    * should be left unchanged
514    */
515   @Test(groups = { "Functional" })
516   public void testSelectType_showForAll()
517   {
518     selectSequences(1, 2);
519     testee = new AnnotationChooser(parentPanel);
520     final Checkbox showCheckbox = (Checkbox) getComponent(testee, 1, 0, 0);
521     final Checkbox hideCheckbox = (Checkbox) getComponent(testee, 1, 0, 1);
522
523     final Checkbox allSequencesCheckbox = (Checkbox) getComponent(testee, 1,
524             1, 0);
525
526     AlignmentAnnotation[] anns = parentPanel.getAlignment()
527             .getAlignmentAnnotation();
528
529     // hide all JMol annotations
530     setSelected(allSequencesCheckbox, true);
531     setSelected(hideCheckbox, true);
532     setSelected(getTypeCheckbox("JMol"), true);
533     int autocalc = countAutocalc(anns);
534     assertFalse(anns[autocalc + 2].visible); // JMol for seq3
535     assertFalse(anns[autocalc + 4].visible); // JMol for seq1
536     // ...now show them...
537     setSelected(showCheckbox, true);
538     for (AlignmentAnnotation ann : anns)
539     {
540       assertTrue(ann.visible);
541     }
542   }
543
544   /**
545    * Test result of selecting an annotation type, with 'Show for selected
546    * sequences'.
547    * 
548    * We expect all annotations of that type, linked to the sequence group, to be
549    * set visible. Other annotations should be left unchanged
550    */
551   @Test(groups = { "Functional" })
552   public void testSelectType_showForSelected()
553   {
554     // sequences 1 and 2 have annotations IUPred and Jmol
555     selectSequences(1, 2);
556     testee = new AnnotationChooser(parentPanel);
557     final Checkbox showCheckbox = (Checkbox) getComponent(testee, 1, 0, 0);
558     final Checkbox hideCheckbox = (Checkbox) getComponent(testee, 1, 0, 1);
559
560     final Checkbox selectedSequencesCheckbox = (Checkbox) getComponent(
561             testee, 1, 1, 1);
562
563     AlignmentAnnotation[] anns = parentPanel.getAlignment()
564             .getAlignmentAnnotation();
565
566     // hide all JMol annotations in the selection region (== annotation 7)
567     setSelected(selectedSequencesCheckbox, true);
568     setSelected(hideCheckbox, true);
569     setSelected(getTypeCheckbox("JMol"), true);
570
571     int autocalc = countAutocalc(anns);
572     assertTrue(anns[autocalc + 2].visible); // JMol for seq3
573     assertFalse(anns[autocalc + 4].visible); // JMol for seq1
574     // ...now show them...
575     setSelected(showCheckbox, true);
576
577     for (AlignmentAnnotation ann : anns)
578     {
579       assertTrue(ann.visible);
580     }
581   }
582
583   /**
584    * Test result of deselecting an annotation type, with 'Show for all
585    * sequences'.
586    * 
587    * We expect all annotations of that type to be set hidden. Other annotations
588    * should be left unchanged.
589    */
590   @Test(groups = { "Functional" })
591   public void testDeselectType_showForAll()
592   {
593     selectSequences(1, 2);
594     testee = new AnnotationChooser(parentPanel);
595
596     final Checkbox showCheckbox = (Checkbox) getComponent(testee, 1, 0, 0);
597     setSelected(showCheckbox, true);
598
599     final Checkbox allSequencesCheckbox = (Checkbox) getComponent(testee, 1,
600             1, 0);
601     setSelected(allSequencesCheckbox, true);
602
603     AlignmentAnnotation[] anns = parentPanel.getAlignment()
604             .getAlignmentAnnotation();
605
606     final Checkbox typeCheckbox = getTypeCheckbox("JMol");
607     // select JMol - all shown
608     setSelected(typeCheckbox, true);
609     int autocalc = countAutocalc(anns);
610     assertTrue(anns[autocalc + 2].visible); // JMol for seq3
611     assertTrue(anns[autocalc + 4].visible); // JMol for seq1
612
613     // deselect JMol - all hidden
614     setSelected(typeCheckbox, false);
615     assertTrue(anns[0].visible); // Conservation
616     assertTrue(anns[1].visible); // Quality
617     assertTrue(anns[2].visible); // Consensus
618     assertTrue(anns[3].visible); // Occupancy
619     assertTrue(anns[4].visible); // IUPred for seq0
620     assertTrue(anns[5].visible); // Beauty
621     assertFalse(anns[6].visible); // JMol for seq3
622     assertTrue(anns[7].visible); // IUPRED for seq2
623     assertFalse(anns[8].visible); // JMol for seq1
624   }
625
626   /**
627    * Test result of deselecting an annotation type, with 'Show for selected
628    * sequences'.
629    * 
630    * We expect the annotations of that type, linked to the sequence group, to be
631    * set hidden. Other annotations should be left unchanged.
632    */
633   @Test(groups = { "Functional" })
634   public void testDeselectType_showForSelected()
635   {
636     selectSequences(1, 2);
637     testee = new AnnotationChooser(parentPanel);
638     final Checkbox showCheckbox = (Checkbox) getComponent(testee, 1, 0, 0);
639     setSelected(showCheckbox, true);
640
641     /*
642      * Don't set the 'selected sequences' radio button since this would trigger
643      * an update, including unselected sequences / annotation types
644      */
645     // setSelected(getSelectedSequencesCheckbox());
646
647     AlignmentAnnotation[] anns = parentPanel.getAlignment()
648             .getAlignmentAnnotation();
649
650     // select JMol - should remain visible
651     setSelected(getTypeCheckbox("JMol"), true);
652     int autocalc = countAutocalc(anns);
653     assertTrue(anns[autocalc + 2].visible); // JMol for seq3
654     assertTrue(anns[autocalc + 4].visible); // JMol for seq1
655
656     // deselect JMol - should be hidden for selected sequences only
657     setSelected(getTypeCheckbox("JMol"), false);
658     assertTrue(anns[0].visible); // Conservation
659     assertTrue(anns[1].visible); // Quality
660     assertTrue(anns[2].visible); // Consensus
661     assertTrue(anns[3].visible); // Occupancy
662     assertTrue(anns[4].visible); // IUPred for seq0
663     assertTrue(anns[5].visible); // Beauty
664     assertTrue(anns[6].visible); // JMol for seq3 not in selection group
665     assertTrue(anns[7].visible); // IUPRED for seq2
666     assertFalse(anns[8].visible); // JMol for seq1 in selection group
667   }
668
669   /**
670    * Helper method to drill down to a sub-component in a Container hierarchy.
671    * 
672    * @param cont
673    * @param i
674    * @param j
675    * @param k
676    * @return
677    */
678   public static Component getComponent(Container cont, int... positions)
679   {
680     Component comp = cont;
681     for (int i : positions)
682     {
683       comp = ((Container) comp).getComponent(i);
684     }
685     return comp;
686   }
687
688   /**
689    * Helper method to set or unset a checkbox and fire its action listener.
690    * 
691    * @param cb
692    * @param select
693    */
694   protected void setSelected(Checkbox cb, boolean select)
695   {
696     // TODO refactor to a test utility class
697     cb.setState(select);
698     // have to manually fire the action listener
699     cb.getItemListeners()[0].itemStateChanged(
700             new ItemEvent(cb, ItemEvent.ITEM_STATE_CHANGED, cb,
701                     select ? ItemEvent.SELECTED : ItemEvent.DESELECTED));
702   }
703
704   /**
705    * Helper method to drill down to the 'Annotation type' checkbox with given
706    * label.
707    * 
708    * @return
709    */
710   private Checkbox getTypeCheckbox(String forLabel)
711   {
712     Component[] cbs = ((JPanel) testee.getComponent(0)).getComponents();
713     for (Component comp : cbs)
714     {
715       final Checkbox cb = (Checkbox) comp;
716       if (cb.getLabel().equals(forLabel))
717       {
718         return cb;
719       }
720     }
721     return null;
722   }
723
724   /**
725    * Test isInActionScope for the case where the scope is selected sequences.
726    * Test cases include sequences in the selection group, and others not in the
727    * group.
728    */
729   @Test(groups = { "Functional" })
730   public void testIsInActionScope_selectedScope()
731   {
732     // sequences 1 and 2 have annotations 4 and 3 respectively
733     selectSequences(1, 2);
734     testee = new AnnotationChooser(parentPanel);
735
736     final Checkbox selectedSequencesCheckbox = (Checkbox) getComponent(
737             testee, 1, 1, 1);
738     setSelected(selectedSequencesCheckbox, true);
739
740     AlignmentAnnotation[] anns = parentPanel.getAlignment()
741             .getAlignmentAnnotation();
742     int autocalc = countAutocalc(anns);
743     assertFalse(testee.isInActionScope(anns[autocalc]));
744     assertFalse(testee.isInActionScope(anns[autocalc + 1]));
745     assertFalse(testee.isInActionScope(anns[autocalc + 2]));
746     assertTrue(testee.isInActionScope(anns[autocalc + 3]));
747     assertTrue(testee.isInActionScope(anns[autocalc + 4]));
748   }
749
750   /**
751    * Test isInActionScope for the case where the scope is unselected sequences.
752    * Test cases include sequences in the selection group, and others not in the
753    * group.
754    */
755   @Test(groups = { "Functional" })
756   public void testIsInActionScope_unselectedScope()
757   {
758     // sequences 1 and 2 have annotations 4 and 3 respectively
759     selectSequences(1, 2);
760     testee = new AnnotationChooser(parentPanel);
761
762     final Checkbox unselectedSequencesCheckbox = (Checkbox) getComponent(
763             testee, 1, 1, 2);
764     setSelected(unselectedSequencesCheckbox, true);
765
766     AlignmentAnnotation[] anns = parentPanel.getAlignment()
767             .getAlignmentAnnotation();
768     int autocalc = countAutocalc(anns);
769     assertTrue(testee.isInActionScope(anns[autocalc]));
770     assertTrue(testee.isInActionScope(anns[autocalc + 1]));
771     assertTrue(testee.isInActionScope(anns[autocalc + 2]));
772     assertFalse(testee.isInActionScope(anns[autocalc + 3]));
773     assertFalse(testee.isInActionScope(anns[autocalc + 4]));
774   }
775
776   /**
777    * Test that the reset method restores previous visibility flags.
778    */
779   @Test(groups = { "Functional" })
780   public void testResetOriginalState()
781   {
782     testee = new AnnotationChooser(parentPanel);
783
784     AlignmentAnnotation[] anns = parentPanel.getAlignment()
785             .getAlignmentAnnotation();
786     // all start visible
787     for (int i = 0; i < anns.length; i++)
788     {
789       assertTrue(i + "'th sequence not visible", anns[i].visible);
790     }
791
792     /*
793      * check options to hide JMol and IUPRED annotations for all sequences
794      */
795     final Checkbox hideCheckbox = (Checkbox) getComponent(testee, 1, 0, 1);
796     setSelected(hideCheckbox, true);
797
798     final Checkbox allSequencesCheckbox = (Checkbox) getComponent(testee, 1,
799             1, 0);
800     setSelected(allSequencesCheckbox, true);
801
802     setSelected(getTypeCheckbox("JMol"), true);
803     setSelected(getTypeCheckbox("IUPRED"), true);
804
805     assertTrue(anns[0].visible); // Conservation
806     assertTrue(anns[1].visible); // Quality
807     assertTrue(anns[2].visible); // Consensus
808     assertTrue(anns[3].visible); // Occupancy
809     assertFalse(anns[4].visible); // IUPRED
810     assertTrue(anns[5].visible); // Beauty (not seq-related)
811     assertFalse(anns[6].visible); // JMol
812     assertFalse(anns[7].visible); // IUPRED
813     assertFalse(anns[8].visible); // JMol
814
815     // reset - should all be visible
816     testee.resetOriginalState();
817     for (int i = 0; i < anns.length; i++)
818     {
819       assertTrue(i + "'th sequence not visible", anns[i].visible);
820     }
821   }
822 }