JAL-1830 unit tests for removing (or not) mappings on close frame
[jalview.git] / test / jalview / gui / AlignViewportTest.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.assertNotNull;
6 import static org.testng.AssertJUnit.assertSame;
7 import static org.testng.AssertJUnit.assertTrue;
8
9 import jalview.datamodel.AlignedCodonFrame;
10 import jalview.datamodel.Alignment;
11 import jalview.datamodel.AlignmentI;
12 import jalview.datamodel.PDBEntry;
13 import jalview.datamodel.PDBEntry.Type;
14 import jalview.datamodel.Sequence;
15 import jalview.datamodel.SequenceI;
16 import jalview.io.FileLoader;
17 import jalview.io.FormatAdapter;
18 import jalview.structure.StructureSelectionManager;
19
20 import java.util.LinkedHashSet;
21 import java.util.Set;
22
23 import org.testng.annotations.BeforeClass;
24 import org.testng.annotations.BeforeMethod;
25 import org.testng.annotations.Test;
26
27 public class AlignViewportTest
28 {
29
30   AlignmentI al;
31   AlignViewport testee;
32
33   @BeforeClass(alwaysRun = true)
34   public static void setUpBeforeClass() throws Exception
35   {
36     jalview.bin.Jalview.main(new String[] { "-props",
37         "test/jalview/testProps.jvprops" });
38   }
39
40  @BeforeMethod(alwaysRun = true)
41   public void setUp()
42   {
43     SequenceI seq1 = new Sequence("Seq1", "ABC");
44     SequenceI seq2 = new Sequence("Seq2", "ABC");
45     SequenceI seq3 = new Sequence("Seq3", "ABC");
46     SequenceI[] seqs = new SequenceI[]
47     { seq1, seq2, seq3 };
48     al = new Alignment(seqs);
49     al.setDataset(null);
50     testee = new AlignViewport(al);
51   }
52
53   @Test(groups ={ "Functional" })
54   public void testCollateForPdb()
55   {
56     /*
57      * Set up sequence pdb ids
58      */
59     PDBEntry pdb1 = new PDBEntry("1ABC", "A", Type.PDB, "1ABC.pdb");
60     PDBEntry pdb2 = new PDBEntry("2ABC", "A", Type.PDB, "2ABC.pdb");
61     PDBEntry pdb3 = new PDBEntry("3ABC", "A", Type.PDB, "3ABC.pdb");
62
63     /*
64      * seq1 and seq3 refer to 1ABC, seq2 to 2ABC, none to 3ABC
65      */
66     al.getSequenceAt(0).getDatasetSequence()
67             .addPDBId(
68             new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb"));
69     al.getSequenceAt(2).getDatasetSequence()
70             .addPDBId(
71             new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb"));
72     al.getSequenceAt(1).getDatasetSequence()
73             .addPDBId(
74             new PDBEntry("2ABC", "C", Type.PDB, "2ABC.pdb"));
75     /*
76      * Add a second chain PDB xref to Seq2 - should not result in a duplicate in
77      * the results
78      */
79     al.getSequenceAt(1).getDatasetSequence()
80             .addPDBId(new PDBEntry("2ABC", "D", Type.PDB, "2ABC.pdb"));
81     /*
82      * Seq3 refers to 3abc - this does not match 3ABC (as the code stands)
83      */
84     al.getSequenceAt(2).getDatasetSequence()
85             .addPDBId(new PDBEntry("3abc", "D", Type.PDB, "3ABC.pdb"));
86
87     /*
88      * run method under test
89      */
90     SequenceI[][] seqs = testee.collateForPDB(new PDBEntry[]
91     { pdb1, pdb2, pdb3 });
92
93     // seq1 and seq3 refer to PDBEntry[0]
94     assertEquals(2, seqs[0].length);
95     assertSame(al.getSequenceAt(0), seqs[0][0]);
96     assertSame(al.getSequenceAt(2), seqs[0][1]);
97
98     // seq2 refers to PDBEntry[1]
99     assertEquals(1, seqs[1].length);
100     assertSame(al.getSequenceAt(1), seqs[1][0]);
101
102     // no sequence refers to PDBEntry[2]
103     assertEquals(0, seqs[2].length);
104   }
105
106   /**
107    * Test that a mapping is not deregistered when a second view is closed but
108    * the first still holds a reference to the mapping
109    */
110   @Test(groups ={ "Functional" })
111   public void testDeregisterMapping_onCloseView()
112   {
113     /*
114      * alignment with reference to mappings
115      */
116     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
117             ">Seq1\nCAGT\n", FormatAdapter.PASTE);
118   
119     AlignedCodonFrame acf1 = new AlignedCodonFrame();
120     AlignedCodonFrame acf2 = new AlignedCodonFrame();
121   
122     Set<AlignedCodonFrame> mappings = new LinkedHashSet<AlignedCodonFrame>();
123     mappings.add(acf1);
124     mappings.add(acf2);
125     af1.getViewport().getAlignment().setCodonFrames(mappings);
126     af1.newView_actionPerformed(null);
127   
128     /*
129      * Verify that creating the alignment for the new View has registered the
130      * mappings
131      */
132     StructureSelectionManager ssm = StructureSelectionManager
133             .getStructureSelectionManager(Desktop.instance);
134     assertEquals(2, ssm.seqmappings.size());
135     assertTrue(ssm.seqmappings.contains(acf1));
136     assertTrue(ssm.seqmappings.contains(acf2));
137   
138     /*
139      * Close the second view. Verify that mappings are not removed as the first
140      * view still holds a reference to them.
141      */
142     af1.closeMenuItem_actionPerformed(false);
143     assertEquals(2, ssm.seqmappings.size());
144     assertTrue(ssm.seqmappings.contains(acf1));
145     assertTrue(ssm.seqmappings.contains(acf2));
146   }
147
148   /**
149    * Test that a mapping is deregistered if no alignment holds a reference to it
150    */
151   @Test(groups ={ "Functional" })
152   public void testDeregisterMapping_withNoReference()
153   {
154     Desktop d = Desktop.instance;
155     assertNotNull(d);
156     StructureSelectionManager ssm = StructureSelectionManager
157             .getStructureSelectionManager(Desktop.instance);
158     ssm.resetAll();
159
160     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
161             ">Seq1\nRSVQ\n", FormatAdapter.PASTE);
162     AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
163             ">Seq2\nDGEL\n", FormatAdapter.PASTE);
164   
165     AlignedCodonFrame acf1 = new AlignedCodonFrame();
166     AlignedCodonFrame acf2 = new AlignedCodonFrame();
167     AlignedCodonFrame acf3 = new AlignedCodonFrame();
168   
169     Set<AlignedCodonFrame> mappings1 = new LinkedHashSet<AlignedCodonFrame>();
170     mappings1.add(acf1);
171     af1.getViewport().getAlignment().setCodonFrames(mappings1);
172
173     Set<AlignedCodonFrame> mappings2 = new LinkedHashSet<AlignedCodonFrame>();
174     mappings2.add(acf2);
175     mappings2.add(acf3);
176     af2.getViewport().getAlignment().setCodonFrames(mappings2);
177   
178     /*
179      * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
180      */
181
182     Set<AlignedCodonFrame> ssmMappings = ssm.seqmappings;
183     assertEquals(0, ssmMappings.size());
184     ssm.registerMapping(acf1);
185     assertEquals(1, ssmMappings.size());
186     ssm.registerMapping(acf2);
187     assertEquals(2, ssmMappings.size());
188     ssm.registerMapping(acf3);
189     assertEquals(3, ssmMappings.size());
190
191     /*
192      * Closing AlignFrame2 should remove its mappings from
193      * StructureSelectionManager, since AlignFrame1 has no reference to them
194      */
195     af2.closeMenuItem_actionPerformed(true);
196     assertEquals(1, ssmMappings.size());
197     assertTrue(ssmMappings.contains(acf1));
198   }
199
200   /**
201    * Test that a mapping is not deregistered if another alignment holds a
202    * reference to it
203    */
204   @Test(groups ={ "Functional" })
205   public void testDeregisterMapping_withReference()
206   {
207     Desktop d = Desktop.instance;
208     assertNotNull(d);
209     StructureSelectionManager ssm = StructureSelectionManager
210             .getStructureSelectionManager(Desktop.instance);
211     ssm.resetAll();
212   
213     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
214             ">Seq1\nRSVQ\n", FormatAdapter.PASTE);
215     AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
216             ">Seq2\nDGEL\n", FormatAdapter.PASTE);
217   
218     AlignedCodonFrame acf1 = new AlignedCodonFrame();
219     AlignedCodonFrame acf2 = new AlignedCodonFrame();
220     AlignedCodonFrame acf3 = new AlignedCodonFrame();
221   
222     Set<AlignedCodonFrame> mappings1 = new LinkedHashSet<AlignedCodonFrame>();
223     mappings1.add(acf1);
224     mappings1.add(acf2);
225     af1.getViewport().getAlignment().setCodonFrames(mappings1);
226   
227     Set<AlignedCodonFrame> mappings2 = new LinkedHashSet<AlignedCodonFrame>();
228     mappings2.add(acf2);
229     mappings2.add(acf3);
230     af2.getViewport().getAlignment().setCodonFrames(mappings2);
231   
232     /*
233      * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
234      */
235   
236     Set<AlignedCodonFrame> ssmMappings = ssm.seqmappings;
237     assertEquals(0, ssmMappings.size());
238     ssm.registerMapping(acf1);
239     assertEquals(1, ssmMappings.size());
240     ssm.registerMapping(acf2);
241     assertEquals(2, ssmMappings.size());
242     ssm.registerMapping(acf3);
243     assertEquals(3, ssmMappings.size());
244   
245     /*
246      * Closing AlignFrame2 should remove mapping acf3 from
247      * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
248      * reference to it
249      */
250     af2.closeMenuItem_actionPerformed(true);
251     assertEquals(2, ssmMappings.size());
252     assertTrue(ssmMappings.contains(acf1));
253     assertTrue(ssmMappings.contains(acf2));
254     assertFalse(ssmMappings.contains(acf3));
255   }
256 }