Merge branch 'releases/Release_2_11_3_Branch'
[jalview.git] / test / jalview / ext / jmol / JmolViewerTest.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.ext.jmol;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.testng.Assert.assertEquals;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import java.lang.reflect.InvocationTargetException;
28
29 import org.testng.annotations.AfterClass;
30 import org.testng.annotations.BeforeClass;
31 import org.testng.annotations.Test;
32
33 import jalview.api.structures.JalviewStructureDisplayI;
34 import jalview.bin.Cache;
35 import jalview.bin.Jalview;
36 import jalview.datamodel.PDBEntry;
37 import jalview.datamodel.SearchResultsI;
38 import jalview.datamodel.SequenceI;
39 import jalview.gui.AlignFrame;
40 import jalview.gui.Desktop;
41 import jalview.gui.JvOptionPane;
42 import jalview.gui.Preferences;
43 import jalview.gui.StructureViewer;
44 import jalview.gui.StructureViewer.ViewerType;
45 import jalview.io.DataSourceType;
46 import jalview.io.FileFormat;
47 import jalview.io.FileLoader;
48
49 @Test(singleThreaded = true)
50 public class JmolViewerTest
51 {
52
53   @BeforeClass(alwaysRun = true)
54   public void setUpJvOptionPane()
55   {
56     JvOptionPane.setInteractiveMode(false);
57     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
58   }
59
60   /**
61    * @throws java.lang.Exception
62    */
63   @BeforeClass(alwaysRun = true)
64   public static void setUpBeforeClass() throws Exception
65   {
66     Jalview.main(
67             new String[]
68             { "--noquestionnaire", "--nonews", "--props",
69                 "test/jalview/ext/rbvi/chimera/testProps.jvprops" });
70   }
71
72   /**
73    * @throws java.lang.Exception
74    */
75   @AfterClass(alwaysRun = true)
76   public static void tearDownAfterClass() throws Exception
77   {
78     if (Desktop.instance != null)
79       Desktop.instance.closeAll_actionPerformed(null);
80   }
81
82   @Test(groups = { "Functional" })
83   public void testSingleSeqViewJMol()
84   {
85     Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
86             ViewerType.JMOL.name());
87     String inFile = "examples/1gaq.txt";
88     AlignFrame af = new jalview.io.FileLoader()
89             .LoadFileWaitTillLoaded(inFile, DataSourceType.FILE);
90     assertTrue("Didn't read input file " + inFile, af != null);
91     for (SequenceI sq : af.getViewport().getAlignment().getSequences())
92     {
93       SequenceI dsq = sq.getDatasetSequence();
94       while (dsq.getDatasetSequence() != null)
95       {
96         dsq = dsq.getDatasetSequence();
97       }
98       if (dsq.getAllPDBEntries() != null
99               && dsq.getAllPDBEntries().size() > 0)
100       {
101         for (int q = 0; q < dsq.getAllPDBEntries().size(); q++)
102         {
103           final StructureViewer structureViewer = new StructureViewer(
104                   af.getViewport().getStructureSelectionManager());
105           structureViewer.setViewerType(ViewerType.JMOL);
106           JalviewStructureDisplayI jmolViewer = structureViewer
107                   .viewStructures(dsq.getAllPDBEntries().elementAt(q),
108                           new SequenceI[]
109                           { sq }, af.getCurrentView().getAlignPanel());
110           /*
111            * Wait for viewer load thread to complete
112            */
113           try
114           {
115             while (!jmolViewer.getBinding().isFinishedInit())
116             {
117               Thread.sleep(500);
118             }
119           } catch (InterruptedException e)
120           {
121           }
122
123           jmolViewer.closeViewer(true);
124           // todo: break here means only once through this loop?
125           break;
126         }
127         break;
128       }
129     }
130   }
131
132   @Test(groups = { "Functional" })
133   public void testAddStrToSingleSeqViewJMol()
134           throws InvocationTargetException, InterruptedException
135   {
136     Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
137             ViewerType.JMOL.name());
138     String inFile = "examples/1gaq.txt";
139     AlignFrame af = new jalview.io.FileLoader(true)
140             .LoadFileWaitTillLoaded(inFile, DataSourceType.FILE);
141     assertTrue("Didn't read input file " + inFile, af != null);
142     // show a structure for 4th Sequence
143     SequenceI sq1 = af.getViewport().getAlignment().getSequences().get(0);
144     final StructureViewer structureViewer = new StructureViewer(
145             af.getViewport().getStructureSelectionManager());
146     structureViewer.setViewerType(ViewerType.JMOL);
147     JalviewStructureDisplayI jmolViewer = structureViewer.viewStructures(
148             sq1.getDatasetSequence().getAllPDBEntries().elementAt(0),
149             new SequenceI[]
150             { sq1 }, af.getCurrentView().getAlignPanel());
151     /*
152      * Wait for viewer load thread to complete
153      */
154     try
155     {
156       while (!jmolViewer.getBinding().isFinishedInit())
157       {
158         Thread.sleep(500);
159       }
160     } catch (InterruptedException e)
161     {
162     }
163
164     assertTrue(jmolViewer.isVisible());
165
166     // add another pdb file and add it to view
167     final String _inFile = "examples/3W5V.pdb";
168     inFile = _inFile;
169     FileLoader fl = new FileLoader();
170     fl.LoadFile(af.getCurrentView(), _inFile, DataSourceType.FILE,
171             FileFormat.PDB);
172     try
173     {
174       int time = 0;
175       do
176       {
177         Thread.sleep(50); // hope we can avoid race condition
178
179       } while (++time < 30
180               && af.getViewport().getAlignment().getHeight() == 3);
181     } catch (Exception q)
182     {
183     }
184     ;
185     assertTrue("Didn't paste additional structure" + inFile,
186             af.getViewport().getAlignment().getHeight() > 3);
187     SequenceI sq2 = af.getViewport().getAlignment().getSequenceAt(3);
188     PDBEntry pdbe = sq2.getDatasetSequence().getAllPDBEntries().get(0);
189     assertTrue(pdbe.getFile().contains(inFile));
190     structureViewer.viewStructures(pdbe, new SequenceI[] { sq2 },
191             af.alignPanel);
192     /*
193      * Wait for viewer load thread to complete
194      */
195     try
196     {
197       while (structureViewer.isBusy())
198       {
199         Thread.sleep(500);
200       }
201     } catch (InterruptedException e)
202     {
203     }
204     assertEquals(jmolViewer.getBinding().getPdbCount(), 2);
205     String mouseOverTest = "[GLY]293:A.CA/2.1 #2164";
206     ((JalviewJmolBinding) jmolViewer.getBinding()).mouseOverStructure(2164,
207             mouseOverTest);
208     SearchResultsI highlight = af.alignPanel.getSeqPanel()
209             .getLastSearchResults();
210     assertNotNull("Didn't find highlight from second structure mouseover",
211             highlight.getResults(sq2, sq2.getStart(), sq2.getEnd()));
212   }
213
214 }