8871249533de207b3c002321a24c970227cfa5ce
[jalview.git] / test / jalview / gui / AlignmentPanelTest.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.assertNotEquals;
25
26 import java.awt.Dimension;
27 import java.awt.Font;
28 import java.awt.FontMetrics;
29 import java.lang.reflect.InvocationTargetException;
30
31 import javax.swing.SwingUtilities;
32
33 import org.testng.annotations.BeforeMethod;
34 import org.testng.annotations.Test;
35
36 import jalview.api.AlignViewportI;
37 import jalview.bin.Cache;
38 import jalview.bin.Jalview;
39 import jalview.datamodel.AlignmentAnnotation;
40 import jalview.datamodel.SequenceI;
41 import jalview.io.DataSourceType;
42 import jalview.io.FileLoader;
43 import jalview.viewmodel.ViewportRanges;
44
45 public class AlignmentPanelTest
46 {
47   AlignFrame af;
48
49   @BeforeMethod(alwaysRun = true)
50   public void setUp() throws InvocationTargetException, InterruptedException
51   {
52     Jalview.main(new String[] { "-nonews", "-props",
53         "test/jalview/testProps.jvprops" });
54
55     Cache.applicationProperties.setProperty("SHOW_IDENTITY",
56             Boolean.TRUE.toString());
57     af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
58             DataSourceType.FILE);
59     
60     /*
61      * ensure the panel has been repainted and so ViewportRanges set
62      */
63     SwingUtilities.invokeAndWait(new Runnable() {
64                 @Override
65                 public void run() {
66                     af.repaint();
67                 }});
68
69     /*
70      * wait for Consensus thread to complete
71      */
72     do
73     {
74       try
75       {
76         Thread.sleep(50);
77       } catch (InterruptedException x)
78       {
79       }
80     } while (af.getViewport().getCalcManager().isWorking());
81   }
82
83   /**
84    * Test side effect that end residue is set correctly by setScrollValues, with
85    * or without hidden columns
86    */
87   @Test(groups = "Functional")
88   public void testSetScrollValues()
89   {
90     ViewportRanges ranges = af.getViewport().getRanges();
91     af.alignPanel.setScrollValues(0, 0);
92
93     int oldres = ranges.getEndRes();
94     af.alignPanel.setScrollValues(-1, 5);
95
96     // setting -ve x value does not change residue
97     assertEquals(ranges.getEndRes(), oldres);
98
99     af.alignPanel.setScrollValues(0, 5);
100
101     // setting 0 as x value does not change residue
102     assertEquals(ranges.getEndRes(), oldres);
103
104     af.alignPanel.setScrollValues(5, 5);
105     // setting x value to 5 extends endRes by 5 residues
106     assertEquals(ranges.getEndRes(), oldres + 5);
107
108     // scroll to position after hidden columns sets endres to oldres (width) +
109     // position
110     int scrollpos = 60;
111     af.getViewport().hideColumns(30, 50);
112     af.alignPanel.setScrollValues(scrollpos, 5);
113     assertEquals(ranges.getEndRes(), oldres + scrollpos);
114
115     // scroll to position within hidden columns, still sets endres to oldres +
116     // position
117     // not sure if this is actually correct behaviour but this is what Jalview
118     // currently does
119     scrollpos = 40;
120     af.getViewport().showAllHiddenColumns();
121     af.getViewport().hideColumns(30, 50);
122     af.alignPanel.setScrollValues(scrollpos, 5);
123     assertEquals(ranges.getEndRes(), oldres + scrollpos);
124
125     // scroll to position within <width> distance of the end of the alignment
126     // endRes should be set to width of alignment - 1
127     scrollpos = 130;
128     af.getViewport().showAllHiddenColumns();
129     af.alignPanel.setScrollValues(scrollpos, 5);
130     assertEquals(ranges.getEndRes(), af.getViewport()
131             .getAlignment().getWidth() - 1);
132
133     // now hide some columns, and scroll to position within <width>
134     // distance of the end of the alignment
135     // endRes should be set to width of alignment - 1 - the number of hidden
136     // columns
137     af.getViewport().hideColumns(30, 50);
138     af.alignPanel.setScrollValues(scrollpos, 5);
139     assertEquals(ranges.getEndRes(), af.getViewport()
140             .getAlignment().getWidth() - 1 - 21); // 21 is the number of hidden
141                                                   // columns
142   }
143
144   /**
145    * Test that update layout reverts to original (unwrapped) values for endRes
146    * when switching from wrapped back to unwrapped mode (JAL-2739)
147    */
148   @Test(groups = "Functional")
149   public void testUpdateLayout_endRes()
150   {
151     // get details of original alignment dimensions
152     ViewportRanges ranges = af.getViewport().getRanges();
153     int endres = ranges.getEndRes();
154
155     // wrap
156     af.alignPanel.getAlignViewport().setWrapAlignment(true);
157     af.alignPanel.updateLayout();
158
159     // endRes has changed
160     assertNotEquals(ranges.getEndRes(), endres);
161
162     // unwrap
163     af.alignPanel.getAlignViewport().setWrapAlignment(false);
164     af.alignPanel.updateLayout();
165
166     // endRes back to original value
167     assertEquals(ranges.getEndRes(), endres);
168   }
169
170   /**
171    * Test the variant of calculateIdWidth that only recomputes the width if it is
172    * not already saved in the viewport (initial value is -1)
173    */
174   @Test(groups = "Functional")
175   public void testCalculateIdWidth_noArgs()
176   {
177     AlignViewportI av = af.alignPanel.getAlignViewport();
178     av.setShowJVSuffix(true);
179     av.setFont(new Font("Courier", Font.PLAIN, 15), true);
180
181     av.setIdWidth(0);
182     Dimension d = af.alignPanel.calculateIdWidth();
183     assertEquals(d.width, 0);
184     assertEquals(d.height, 0);
185
186     av.setIdWidth(99);
187     d = af.alignPanel.calculateIdWidth();
188     assertEquals(d.width, 99);
189     assertEquals(d.height, 0);
190
191     /*
192      * note 4 pixels padding are added to the longest sequence name width
193      */
194     av.setIdWidth(-1); // force recalculation
195     d = af.alignPanel.calculateIdWidth();
196     assertEquals(d.width, 166); // 4 + pixel width of "Q93Z60_ARATH/1-118"
197     assertEquals(d.height, 12);
198     assertEquals(d.width, av.getIdWidth());
199   }
200
201   /**
202    * Test the variant of calculateIdWidth that computes the longest of any
203    * sequence name or annotation label width
204    */
205   @Test(groups = "Functional")
206   public void testCalculateIdWidth_withMaxWidth()
207   {
208     AlignViewportI av = af.alignPanel.getAlignViewport();
209     av.setShowJVSuffix(true);
210     av.setFont(new Font("Courier", Font.PLAIN, 15), true);
211     av.setShowAnnotation(false);
212     av.setIdWidth(18);
213
214     /*
215      * note 4 pixels 'padding' are added to the longest seq name/annotation label
216      */
217     Dimension d = af.alignPanel.calculateIdWidth(2000);
218     assertEquals(d.width, 166); // 4 + pixel width of "Q93Z60_ARATH/1-118"
219     assertEquals(d.height, 12); // fixed value (not used?)
220     assertEquals(av.getIdWidth(), 18); // not changed by this method
221
222     /*
223      * make the longest sequence name longer
224      */
225     SequenceI seq = af.viewport.getAlignment()
226             .findSequenceMatch("Q93Z60_ARATH")[0];
227     seq.setName(seq.getName() + "MMMMM");
228     d = af.alignPanel.calculateIdWidth(2000);
229     assertEquals(d.width, 211); // 4 + pixel width of "Q93Z60_ARATHMMMMM/1-118"
230     assertEquals(d.height, 12);
231     assertEquals(av.getIdWidth(), 18); // unchanged
232
233     /*
234      * make the longest annotation name even longer
235      * note this is checked even if annotations are not shown
236      */
237     AlignmentAnnotation aa = av.getAlignment().getAlignmentAnnotation()[0];
238     aa.label = "THIS IS A VERY LONG LABEL INDEED";
239     FontMetrics fmfor = af.alignPanel
240             .getFontMetrics(af.alignPanel.getAlabels().getFont());
241     // Assumption ID_WIDTH_PADDING == 4
242     int expwidth = 4 + fmfor.stringWidth(aa.label);
243     d = af.alignPanel.calculateIdWidth(2000);
244     assertEquals(d.width, expwidth); // 228 == ID_WIDTH_PADDING + pixel width of "THIS IS A VERY LONG LABEL INDEED"
245     assertEquals(d.height, 12);
246
247     /*
248      * override with maxwidth
249      * note the 4 pixels padding is added to this value
250      */
251     d = af.alignPanel.calculateIdWidth(213);
252     assertEquals(d.width, 217);
253     assertEquals(d.height, 12);
254   }
255
256   @Test(groups = { "Functional", "Not-bamboo" })
257   public void testGetVisibleWidth()
258   {
259     /*
260      * width for onscreen rendering is IDPanel width
261      */
262     int w = af.alignPanel.getVisibleIdWidth(true);
263     assertEquals(w, af.alignPanel.getIdPanel().getWidth());
264     assertEquals(w, 115);
265
266     /*
267      * width for offscreen rendering is the same
268      * if no fixed id width is specified in preferences
269      */
270     Cache.setProperty("FIGURE_AUTOIDWIDTH", Boolean.FALSE.toString());
271     Cache.removeProperty("FIGURE_FIXEDIDWIDTH");
272     assertEquals(w, af.alignPanel.getVisibleIdWidth(false));
273
274     /*
275      * preference for fixed id width - note 4 pixels padding is added
276      */
277     Cache.setProperty("FIGURE_FIXEDIDWIDTH", "120");
278     assertEquals(124, af.alignPanel.getVisibleIdWidth(false));
279
280     /*
281      * preference for auto id width overrides fixed width
282      */
283     Cache.setProperty("FIGURE_AUTOIDWIDTH", Boolean.TRUE.toString());
284     assertEquals(115, af.alignPanel.getVisibleIdWidth(false));
285   }
286 }