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