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