Merge branch 'develop' into features/JAL-4219_extended_fasta_rna_ss
[jalview.git] / test / jalview / gui / AnnotationLabelsTest2.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.gui;
22
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertFalse;
25 import static org.testng.Assert.assertTrue;
26
27 import java.io.File;
28
29 import org.testng.annotations.AfterMethod;
30 import org.testng.annotations.BeforeClass;
31 import org.testng.annotations.BeforeMethod;
32 import org.testng.annotations.DataProvider;
33 import org.testng.annotations.Test;
34
35 import jalview.bin.Cache;
36 import jalview.bin.Jalview;
37 import jalview.datamodel.AlignmentI;
38 import jalview.datamodel.SequenceI;
39 import jalview.gui.StructureViewer.ViewerType;
40 import jalview.io.DataSourceType;
41 import jalview.io.FileLoader;
42 import jalview.structure.StructureImportSettings.TFType;
43 import jalview.util.Platform;
44
45 public class AnnotationLabelsTest2
46 {
47   private static double scaling;
48
49   @BeforeClass(alwaysRun = true)
50   public static void setUpBeforeClass() throws Exception
51   {
52     if (Desktop.instance != null)
53       Desktop.instance.closeAll_actionPerformed(null);
54
55     setUpJvOptionPane();
56     /*
57      * use read-only test properties file
58      */
59     Cache.loadProperties("test/jalview/io/testProps.jvprops");
60     Jalview.main(new String[] { "--nonews", "--nosplash", });
61
62     scaling = JvSwingUtilsTest.getScaling(Desktop.instance);
63   }
64
65   @AfterMethod(alwaysRun = true)
66   public void tearDown()
67   {
68     if (Desktop.instance != null)
69       Desktop.instance.closeAll_actionPerformed(null);
70   }
71
72   /**
73    * configure (read-only) properties for test to ensure Consensus is computed
74    * for colour Above PID testing
75    */
76   @BeforeMethod(alwaysRun = true)
77   public void setUp()
78   {
79     Cache.loadProperties("test/jalview/io/testProps.jvprops");
80     Cache.applicationProperties.setProperty("SHOW_IDENTITY",
81             Boolean.TRUE.toString());
82
83   }
84
85   public static void setUpJvOptionPane()
86   {
87     JvOptionPane.setInteractiveMode(false);
88     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
89   }
90
91   @Test(
92     groups =
93     { "Functional", "testTask1" },
94     dataProvider = "openFilesWithIdWidthChanges")
95   public void testIdWidthChanges(String alignmentFilename, boolean wrap,
96           int idWidth1min, int idWidth1max, int manualWidth,
97           String structureFilename, String paeFilename,
98           boolean secondaryStructureView, TFType temperatureFactorType,
99           ViewerType viewerType, int idWidth2min, int idWidth2max)
100   {
101     AlignFrame af = new FileLoader()
102             .LoadFileWaitTillLoaded(alignmentFilename, DataSourceType.FILE);
103
104     try
105     {
106       Thread.sleep(200); // to allow alignment annotations to open
107     } catch (InterruptedException e)
108     {
109       // TODO Auto-generated catch block
110       e.printStackTrace();
111     }
112     AlignViewport av = af.getCurrentView();
113
114     int idWidth = 0;
115
116     idWidth = av.getIdWidth();
117     assertTrue(idWidth > idWidth1min,
118             "idWidth (" + idWidth + ") is not greater than " + idWidth1min);
119     assertTrue(idWidth < idWidth1max, "idWidth (" + idWidth
120             + ") is not narrower than " + idWidth1max);
121
122     // set wrap
123     if (wrap)
124     {
125       af.setWrapFormat(true, false);
126       idWidth = av.getIdWidth();
127       assertTrue(idWidth > idWidth1min, "After wrap idWidth (" + idWidth
128               + ") is not greater than " + idWidth1min);
129       assertTrue(idWidth < idWidth1max, "After wrap idWidth (" + idWidth
130               + ") is not narrower than" + idWidth1max);
131     }
132
133     AlignmentI al = av.getAlignment();
134     SequenceI s = al.getSequenceAt(0);
135     AlignmentPanel ap = af.alignPanel;
136
137     File structureFile = new File(structureFilename);
138     File paeFile = new File(paeFilename);
139
140     StructureViewer sv = StructureChooser.openStructureFileForSequence(null,
141             null, ap, s, false, structureFile.getAbsolutePath(),
142             temperatureFactorType, paeFile.getAbsolutePath(), true,
143             secondaryStructureView, false, viewerType);
144     // give time for annotations to open
145     try
146     {
147       Thread.sleep(200); // to allow alignment annotations to open
148     } catch (InterruptedException e)
149     {
150       // TODO Auto-generated catch block
151       e.printStackTrace();
152     }
153
154     // idWidth = ap.getIdPanel().getWidth();
155     idWidth = av.getIdWidth();
156     assertTrue(idWidth >= idWidth2min,
157             "idWidth (" + idWidth + ") is not greater than " + idWidth2min);
158     assertTrue(idWidth <= idWidth2max, "idWidth (" + idWidth
159             + ") is not narrower than " + idWidth2max);
160   }
161
162   @Test(
163     groups =
164     { "Functional", "testTask1" },
165     dataProvider = "openFilesWithIdWidthChanges")
166   public void testIdWidthNoChanges(String alignmentFilename, boolean wrap,
167           int idWidth1min, int idWidth1max, int manualWidth,
168           String structureFilename, String paeFilename,
169           boolean secondaryStructureView, TFType temperatureFactorType,
170           ViewerType viewerType, int idWidth2min, int idWidth2max)
171   {
172     AlignFrame af = new FileLoader()
173             .LoadFileWaitTillLoaded(alignmentFilename, DataSourceType.FILE);
174     try
175     {
176       Thread.sleep(200); // to allow alignment annotations to open
177     } catch (InterruptedException e)
178     {
179       // TODO Auto-generated catch block
180       e.printStackTrace();
181     }
182     AlignViewport av = af.getCurrentView();
183
184     int idWidth = 0;
185     idWidth = av.getIdWidth();
186     assertTrue(idWidth > idWidth1min,
187             "idWidth (" + idWidth + ") is not greater than " + idWidth1min);
188     assertTrue(idWidth < idWidth1max,
189             "idWidth (" + idWidth + ") is not narrower than" + idWidth1max);
190
191     AlignmentI al = av.getAlignment();
192     SequenceI s = al.getSequenceAt(0);
193     AlignmentPanel ap = af.alignPanel;
194
195     // set width manually
196     av.setIdWidth(manualWidth);
197     ap.validateAnnotationDimensions(false);
198     ap.paintAlignment(true, false);
199     ap.getIdPanel().getIdCanvas().setManuallyAdjusted(true);
200
201     idWidth = av.getIdWidth();
202     assertEquals(idWidth, manualWidth,
203             "idWidth is not set to the manually set width " + manualWidth);
204
205     File structureFile = new File(structureFilename);
206     File paeFile = new File(paeFilename);
207
208     StructureViewer sv = StructureChooser.openStructureFileForSequence(null,
209             null, ap, s, false, structureFile.getAbsolutePath(),
210             temperatureFactorType, paeFile.getAbsolutePath(), false,
211             secondaryStructureView, false, viewerType);
212
213     idWidth = ap.getIdPanel().getWidth();// av.getIdWidth();
214     idWidth = av.getIdWidth();
215     assertEquals(idWidth, manualWidth,
216             "idWidth is not set to the manually set width " + manualWidth
217                     + " after adding structure annotations");
218     assertFalse(idWidth > idWidth2min,
219             "idWidth (" + idWidth + ") is greater than " + idWidth2min);
220   }
221
222   @DataProvider(name = "openFilesWithIdWidthChanges")
223   public Object[][] openFilesWithIdWidthChanges()
224   {
225     /*
226       String alignmentFilename,
227       boolean wrap,
228       int idWidth1min,
229       int idWidth1max,
230       int manualWidth, // ignored by testIdWidthChanges()
231       String structureFilename,
232       String paeFilename,
233       boolean secondaryStructureView,
234       TFType temperatureFactorType,
235       ViewerType viewerType,
236       int idWidth2min,
237       int idWidth2max,
238      */
239     int idWidth2min = scaling == 1.0 ? 114 : 108;
240     int idWidth2max = scaling == 1.0 ? 117 : 114; // was 130
241     if (Platform.isMac())
242     {
243       idWidth2max = 122;
244     }
245
246     return new Object[][] {
247         //
248         /*
249          */
250         { "./test/files/annotation_label_width/sample.a2m", false, 50, 70,
251             100,
252             "./examples/test_fab41.result/test_fab41_unrelaxed_rank_1_model_3.pdb",
253             "./examples/test_fab41.result/test_fab41_unrelaxed_rank_1_model_3_scores.json",
254             true, TFType.PLDDT, null, idWidth2min, idWidth2max },
255         { "./test/files/annotation_label_width/sample.a2m", true, 50, 70,
256             100,
257             "./examples/test_fab41.result/test_fab41_unrelaxed_rank_1_model_3.pdb",
258             "./examples/test_fab41.result/test_fab41_unrelaxed_rank_1_model_3_scores.json",
259             true, TFType.PLDDT, null, idWidth2min, idWidth2max },
260         /*
261          */
262     };
263   }
264
265 }