JAL-2465 unit test for bugfix
[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.testng.AssertJUnit.assertTrue;
24
25 import jalview.api.structures.JalviewStructureDisplayI;
26 import jalview.bin.Cache;
27 import jalview.bin.Jalview;
28 import jalview.datamodel.SequenceI;
29 import jalview.gui.AlignFrame;
30 import jalview.gui.JvOptionPane;
31 import jalview.gui.Preferences;
32 import jalview.gui.StructureViewer;
33 import jalview.gui.StructureViewer.ViewerType;
34 import jalview.io.DataSourceType;
35
36 import org.testng.Assert;
37 import org.testng.annotations.AfterClass;
38 import org.testng.annotations.BeforeClass;
39 import org.testng.annotations.Test;
40
41 @Test(singleThreaded = true)
42 public class JmolViewerTest
43 {
44
45   @BeforeClass(alwaysRun = true)
46   public void setUpJvOptionPane()
47   {
48     JvOptionPane.setInteractiveMode(false);
49     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
50   }
51
52   /**
53    * @throws java.lang.Exception
54    */
55   @BeforeClass(alwaysRun = true)
56   public static void setUpBeforeClass() throws Exception
57   {
58     Jalview.main(new String[] { "-noquestionnaire", "-nonews", "-props",
59         "test/jalview/ext/rbvi/chimera/testProps.jvprops" });
60   }
61
62   /**
63    * @throws java.lang.Exception
64    */
65   @AfterClass(alwaysRun = true)
66   public static void tearDownAfterClass() throws Exception
67   {
68     jalview.gui.Desktop.instance.closeAll_actionPerformed(null);
69   }
70
71   @Test(groups = { "Functional" })
72   public void testSingleSeqViewJMol()
73   {
74     Cache.setProperty(Preferences.STRUCTURE_DISPLAY, ViewerType.JMOL.name());
75     String inFile = "examples/1gaq.txt";
76     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
77             inFile, DataSourceType.FILE);
78     assertTrue("Didn't read input file " + inFile, af != null);
79     for (SequenceI sq : af.getViewport().getAlignment().getSequences())
80     {
81       SequenceI dsq = sq.getDatasetSequence();
82       while (dsq.getDatasetSequence() != null)
83       {
84         dsq = dsq.getDatasetSequence();
85       }
86       if (dsq.getAllPDBEntries() != null
87               && dsq.getAllPDBEntries().size() > 0)
88       {
89         for (int q = 0; q < dsq.getAllPDBEntries().size(); q++)
90         {
91           final StructureViewer structureViewer = new StructureViewer(af
92                   .getViewport().getStructureSelectionManager());
93           structureViewer.setViewerType(ViewerType.JMOL);
94           JalviewStructureDisplayI jmolViewer = structureViewer
95                   .viewStructures(dsq.getAllPDBEntries().elementAt(q),
96                           new SequenceI[] { sq }, af.getCurrentView()
97                                   .getAlignPanel());
98           /*
99            * Wait for viewer load thread to complete
100            */
101           try
102           {
103             while (!jmolViewer.getBinding().isFinishedInit())
104             {
105               Thread.sleep(500);
106             }
107           } catch (InterruptedException e)
108           {
109           }
110
111           jmolViewer.closeViewer(true);
112           // todo: break here means only once through this loop?
113           break;
114         }
115         break;
116       }
117     }
118   }
119
120   @Test(groups = { "Functional, Network" })
121   public void testStructureLoadingViaURL()
122   {
123     Cache.setProperty(Preferences.STRUCTURE_DISPLAY, ViewerType.JMOL.name());
124     String inFile = "http://www.jalview.org/builds/develop/examples/3W5V.pdb";
125     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
126             inFile, DataSourceType.URL);
127     assertTrue("Didn't read input file " + inFile, af != null);
128     for (SequenceI sq : af.getViewport().getAlignment().getSequences())
129     {
130       SequenceI dsq = sq.getDatasetSequence();
131       while (dsq.getDatasetSequence() != null)
132       {
133         dsq = dsq.getDatasetSequence();
134       }
135       if (dsq.getAllPDBEntries() != null
136               && dsq.getAllPDBEntries().size() > 0)
137       {
138         for (int q = 0; q < dsq.getAllPDBEntries().size(); q++)
139         {
140           final StructureViewer structureViewer = new StructureViewer(af
141                   .getViewport().getStructureSelectionManager());
142           structureViewer.setViewerType(ViewerType.JMOL);
143           JalviewStructureDisplayI jmolViewer = structureViewer
144                   .viewStructures(dsq.getAllPDBEntries().elementAt(q),
145                           new SequenceI[] { sq }, af.getCurrentView()
146                                   .getAlignPanel());
147           /*
148           * Wait for viewer load thread to complete
149           */
150           try
151           {
152             while (!jmolViewer.getBinding().isFinishedInit())
153             {
154               Thread.sleep(500);
155             }
156           } catch (InterruptedException e)
157           {
158           }
159           // System.out.println(">>>>>>>>>>>>>>>>> "
160           // + jmolViewer.getBinding().getPdbFile());
161           String[] expectedModelFiles = new String[] { "http://www.jalview.org/builds/develop/examples/3W5V.pdb" };
162           String[] actualModelFiles = jmolViewer.getBinding().getStructureFiles();
163           Assert.assertEqualsNoOrder(actualModelFiles, expectedModelFiles);
164           jmolViewer.closeViewer(true);
165           // todo: break here means only once through this loop?
166           break;
167         }
168         break;
169       }
170     }
171   }
172 }