2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertNotEquals;
26 import java.awt.Dimension;
28 import java.awt.FontMetrics;
29 import java.lang.reflect.InvocationTargetException;
31 import javax.swing.SwingUtilities;
33 import org.testng.annotations.BeforeMethod;
34 import org.testng.annotations.Test;
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;
45 public class AlignmentPanelTest
49 @BeforeMethod(alwaysRun = true)
50 public void setUp() throws InvocationTargetException, InterruptedException
52 Jalview.main(new String[] { "-nonews", "-props",
53 "test/jalview/testProps.jvprops" });
55 Cache.applicationProperties.setProperty("SHOW_IDENTITY",
56 Boolean.TRUE.toString());
57 af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
61 * ensure the panel has been repainted and so ViewportRanges set
63 SwingUtilities.invokeAndWait(new Runnable() {
70 * wait for Consensus thread to complete
77 } catch (InterruptedException x)
80 } while (af.getViewport().getCalcManager().isWorking());
84 * Test side effect that end residue is set correctly by setScrollValues, with
85 * or without hidden columns
87 @Test(groups = "Functional")
88 public void testSetScrollValues()
90 ViewportRanges ranges = af.getViewport().getRanges();
91 af.alignPanel.setScrollValues(0, 0);
93 int oldres = ranges.getEndRes();
94 af.alignPanel.setScrollValues(-1, 5);
96 // setting -ve x value does not change residue
97 assertEquals(ranges.getEndRes(), oldres);
99 af.alignPanel.setScrollValues(0, 5);
101 // setting 0 as x value does not change residue
102 assertEquals(ranges.getEndRes(), oldres);
104 af.alignPanel.setScrollValues(5, 5);
105 // setting x value to 5 extends endRes by 5 residues
106 assertEquals(ranges.getEndRes(), oldres + 5);
108 // scroll to position after hidden columns sets endres to oldres (width) +
111 af.getViewport().hideColumns(30, 50);
112 af.alignPanel.setScrollValues(scrollpos, 5);
113 assertEquals(ranges.getEndRes(), oldres + scrollpos);
115 // scroll to position within hidden columns, still sets endres to oldres +
117 // not sure if this is actually correct behaviour but this is what Jalview
120 af.getViewport().showAllHiddenColumns();
121 af.getViewport().hideColumns(30, 50);
122 af.alignPanel.setScrollValues(scrollpos, 5);
123 assertEquals(ranges.getEndRes(), oldres + scrollpos);
125 // scroll to position within <width> distance of the end of the alignment
126 // endRes should be set to width of alignment - 1
128 af.getViewport().showAllHiddenColumns();
129 af.alignPanel.setScrollValues(scrollpos, 5);
130 assertEquals(ranges.getEndRes(), af.getViewport()
131 .getAlignment().getWidth() - 1);
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
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
145 * Test that update layout reverts to original (unwrapped) values for endRes
146 * when switching from wrapped back to unwrapped mode (JAL-2739)
148 @Test(groups = "Functional")
149 public void testUpdateLayout_endRes()
151 // get details of original alignment dimensions
152 ViewportRanges ranges = af.getViewport().getRanges();
153 int endres = ranges.getEndRes();
156 af.alignPanel.getAlignViewport().setWrapAlignment(true);
157 af.alignPanel.updateLayout();
159 // endRes has changed
160 assertNotEquals(ranges.getEndRes(), endres);
163 af.alignPanel.getAlignViewport().setWrapAlignment(false);
164 af.alignPanel.updateLayout();
166 // endRes back to original value
167 assertEquals(ranges.getEndRes(), endres);
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)
174 @Test(groups = "Functional")
175 public void testCalculateIdWidth_noArgs()
177 AlignViewportI av = af.alignPanel.getAlignViewport();
178 av.setShowJVSuffix(true);
179 av.setFont(new Font("Courier", Font.PLAIN, 15), true);
182 Dimension d = af.alignPanel.calculateIdWidth();
183 assertEquals(d.width, 0);
184 assertEquals(d.height, 0);
187 d = af.alignPanel.calculateIdWidth();
188 assertEquals(d.width, 99);
189 assertEquals(d.height, 0);
192 * note 4 pixels padding are added to the longest sequence name width
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());
202 * Test the variant of calculateIdWidth that computes the longest of any
203 * sequence name or annotation label width
205 @Test(groups = "Functional")
206 public void testCalculateIdWidth_withMaxWidth()
208 AlignViewportI av = af.alignPanel.getAlignViewport();
209 av.setShowJVSuffix(true);
210 av.setFont(new Font("Courier", Font.PLAIN, 15), true);
211 av.setShowAnnotation(false);
215 * note 4 pixels 'padding' are added to the longest seq name/annotation label
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
223 * make the longest sequence name longer
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
234 * make the longest annotation name even longer
235 * note this is checked even if annotations are not shown
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);
248 * override with maxwidth
249 * note the 4 pixels padding is added to this value
251 d = af.alignPanel.calculateIdWidth(213);
252 assertEquals(d.width, 217);
253 assertEquals(d.height, 12);
256 @Test(groups = { "Functional", "Not-bamboo" })
257 public void testGetVisibleWidth()
260 * width for onscreen rendering is IDPanel width
262 int w = af.alignPanel.getVisibleIdWidth(true);
263 assertEquals(w, af.alignPanel.getIdPanel().getWidth());
264 assertEquals(w, 115);
267 * width for offscreen rendering is the same
268 * if no fixed id width is specified in preferences
270 Cache.setProperty("FIGURE_AUTOIDWIDTH", Boolean.FALSE.toString());
271 Cache.removeProperty("FIGURE_FIXEDIDWIDTH");
272 assertEquals(w, af.alignPanel.getVisibleIdWidth(false));
275 * preference for fixed id width - note 4 pixels padding is added
277 Cache.setProperty("FIGURE_FIXEDIDWIDTH", "120");
278 assertEquals(124, af.alignPanel.getVisibleIdWidth(false));
281 * preference for auto id width overrides fixed width
283 Cache.setProperty("FIGURE_AUTOIDWIDTH", Boolean.TRUE.toString());
284 assertEquals(115, af.alignPanel.getVisibleIdWidth(false));