JAL-1541 BioJs MSA Viewer export option implementation
[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.io.IOException;
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import javax.swing.JMenuItem;
17
18 import org.junit.Before;
19 import org.junit.Test;
20
21 public class PopupMenuTest
22 {
23   // 4 sequences x 13 positions
24   final static String TEST_DATA = ">FER_CAPAA Ferredoxin\n"
25           + "TIETHKEAELVG-\n"
26           + ">FER_CAPAN Ferredoxin, chloroplast precursor\n"
27           + "TIETHKEAELVG-\n"
28           + ">FER1_SOLLC Ferredoxin-1, chloroplast precursor\n"
29           + "TIETHKEEELTA-\n" + ">Q93XJ9_SOLTU Ferredoxin I precursor\n"
30           + "TIETHKEEELTA-\n";
31
32   AlignmentI alignment;
33
34   AlignmentPanel parentPanel;
35
36   PopupMenu testee = null;
37
38   @Before
39   public void setUp() throws IOException
40   {
41     alignment = new jalview.io.FormatAdapter().readFile(TEST_DATA,
42             AppletFormatAdapter.PASTE, "FASTA");
43     AlignFrame af = new AlignFrame(alignment, 700, 500);
44     parentPanel = new AlignmentPanel(af, af.getViewport());
45     testee = new PopupMenu(parentPanel, null, null);
46     int i = 0;
47     for (SequenceI seq : alignment.getSequences())
48     {
49       final AlignmentAnnotation annotation = new AlignmentAnnotation("label" + i,
50               "desc" + i, i);
51       annotation.setCalcId("calcId" + i);
52       seq.addAlignmentAnnotation(annotation);
53       annotation.setSequenceRef(seq);
54     }
55   }
56
57   @Test
58   public void testConfigureReferenceAnnotationsMenu_noSequenceSelected()
59   {
60     JMenuItem menu = new JMenuItem();
61     List<SequenceI> seqs = new ArrayList<SequenceI>();
62     testee.configureReferenceAnnotationsMenu(menu, seqs);
63     assertFalse(menu.isEnabled());
64     assertEquals(
65             MessageManager.getString("label.add_reference_annotations"),
66             menu.getText());
67     // now try null list
68     menu.setEnabled(true);
69     testee.configureReferenceAnnotationsMenu(menu, seqs);
70     assertFalse(menu.isEnabled());
71   }
72
73   /**
74    * Test building the 'add reference annotations' menu for the case where there
75    * are no reference annotations to add to the alignment. The menu item should
76    * be disabled.
77    */
78   @Test
79   public void testConfigureReferenceAnnotationsMenu_noReferenceAnnotations()
80   {
81     JMenuItem menu = new JMenuItem();
82     List<SequenceI> seqs = new ArrayList<SequenceI>();
83
84     /*
85      * Initial state is that sequences have annotations, and have dataset
86      * sequences, but the dataset sequences have no annotations. Hence nothing
87      * to add.
88      */
89     seqs = parentPanel.getAlignment().getSequences();
90
91     testee.configureReferenceAnnotationsMenu(menu, seqs);
92     assertFalse(menu.isEnabled());
93   }
94
95   /**
96    * Test building the 'add reference annotations' menu for the case where all
97    * reference annotations are already on the alignment. The menu item should be
98    * disabled.
99    */
100   @Test
101   public void testConfigureReferenceAnnotationsMenu_alreadyAdded()
102   {
103     JMenuItem menu = new JMenuItem();
104     List<SequenceI> seqs = new ArrayList<SequenceI>();
105
106     seqs = parentPanel.getAlignment().getSequences();
107     // copy annotation from sequence to dataset
108     seqs.get(1).getDatasetSequence()
109             .addAlignmentAnnotation(seqs.get(1).getAnnotation()[0]);
110     testee.configureReferenceAnnotationsMenu(menu, seqs);
111     assertFalse(menu.isEnabled());
112   }
113
114   /**
115    * Test building the 'add reference annotations' menu for the case where
116    * several reference annotations are on the dataset but not on the sequences.
117    * The menu item should be enabled, and acquire a tooltip which lists the
118    * annotation sources (calcIds) and type (labels).
119    */
120   @Test
121   public void testConfigureReferenceAnnotationsMenu()
122   {
123     JMenuItem menu = new JMenuItem();
124     List<SequenceI> seqs = new ArrayList<SequenceI>();
125
126     seqs = parentPanel.getAlignment().getSequences();
127     // make up new annotations and add to dataset sequences
128
129     // PDB.secondary structure on Sequence0
130     AlignmentAnnotation annotation = new AlignmentAnnotation(
131             "secondary structure", "", 0);
132     annotation.setCalcId("PBD");
133     seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
134
135     // PDB.Temp on Sequence1
136     annotation = new AlignmentAnnotation("Temp", "", 0);
137     annotation.setCalcId("PBD");
138     seqs.get(1).getDatasetSequence().addAlignmentAnnotation(annotation);
139
140     // JMOL.secondary structure on Sequence0
141     annotation = new AlignmentAnnotation("secondary structure", "", 0);
142     annotation.setCalcId("JMOL");
143     seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
144
145     testee.configureReferenceAnnotationsMenu(menu, seqs);
146     assertTrue(menu.isEnabled());
147     String expected = "<html><table width=350 border=0><tr><td>Add annotations for<br/>JMOL/secondary structure<br/>PBD/Temp</td></tr></table></html>";
148     assertEquals(expected, menu.getToolTipText());
149   }
150 }