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 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;
35 import java.awt.Dimension;
37 import java.awt.FontMetrics;
38 import java.awt.Toolkit;
40 import org.testng.annotations.BeforeMethod;
41 import org.testng.annotations.Test;
43 public class AlignmentPanelTest
47 @BeforeMethod(alwaysRun = true)
50 Jalview.main(new String[] { "-nonews", "-props",
51 "test/jalview/testProps.jvprops" });
53 Cache.applicationProperties.setProperty("SHOW_IDENTITY",
54 Boolean.TRUE.toString());
55 af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
59 * wait for Consensus thread to complete
68 } catch (InterruptedException e)
71 } while (af.getViewport().isCalcInProgress()
72 || Toolkit.getDefaultToolkit().getSystemEventQueue()
74 || !af.alignPanel.isValid());
79 * Test side effect that end residue is set correctly by setScrollValues, with
80 * or without hidden columns
82 @Test(groups = "Functional")
83 public void testSetScrollValues()
85 ViewportRanges ranges = af.getViewport().getRanges();
86 af.alignPanel.setScrollValues(0, 0);
88 int oldres = ranges.getEndRes();
89 af.alignPanel.setScrollValues(-1, 5);
91 // setting -ve x value does not change residue
92 assertEquals(ranges.getEndRes(), oldres);
94 af.alignPanel.setScrollValues(0, 5);
96 // setting 0 as x value does not change residue
97 assertEquals(ranges.getEndRes(), oldres);
99 af.alignPanel.setScrollValues(5, 5);
100 // setting x value to 5 extends endRes by 5 residues
101 assertEquals(ranges.getEndRes(), oldres + 5);
103 // scroll to position after hidden columns sets endres to oldres (width) +
106 af.getViewport().hideColumns(30, 50);
107 af.alignPanel.setScrollValues(scrollpos, 5);
108 assertEquals(ranges.getEndRes(), oldres + scrollpos);
110 // scroll to position within hidden columns, still sets endres to oldres +
112 // not sure if this is actually correct behaviour but this is what Jalview
115 af.getViewport().showAllHiddenColumns();
116 af.getViewport().hideColumns(30, 50);
117 af.alignPanel.setScrollValues(scrollpos, 5);
118 assertEquals(ranges.getEndRes(), oldres + scrollpos);
120 // scroll to position within <width> distance of the end of the alignment
121 // endRes should be set to width of alignment - 1
123 af.getViewport().showAllHiddenColumns();
124 af.alignPanel.setScrollValues(scrollpos, 5);
125 assertEquals(ranges.getEndRes(), af.getViewport()
126 .getAlignment().getWidth() - 1);
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
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
140 * Test that update layout reverts to original (unwrapped) values for endRes
141 * when switching from wrapped back to unwrapped mode (JAL-2739)
143 @Test(groups = "Functional")
144 public void testUpdateLayout_endRes()
146 // get details of original alignment dimensions
147 ViewportRanges ranges = af.getViewport().getRanges();
148 int endres = ranges.getEndRes();
151 af.alignPanel.getAlignViewport().setWrapAlignment(true);
152 af.alignPanel.updateLayout();
154 // endRes has changed
155 assertNotEquals(ranges.getEndRes(), endres);
158 af.alignPanel.getAlignViewport().setWrapAlignment(false);
159 af.alignPanel.updateLayout();
161 // endRes back to original value
162 assertEquals(ranges.getEndRes(), endres);
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)
169 @Test(groups = "Functional")
170 public void testCalculateIdWidth_noArgs()
172 AlignViewportI av = af.alignPanel.getAlignViewport();
173 av.setShowJVSuffix(true);
174 av.setFont(new Font("Courier", Font.PLAIN, 15), true);
177 Dimension d = af.alignPanel.calculateIdWidth();
178 assertEquals(d.width, 0);
179 assertEquals(d.height, 0);
182 d = af.alignPanel.calculateIdWidth();
183 assertEquals(d.width, 99);
184 assertEquals(d.height, 0);
187 * note 4 pixels padding are added to the longest sequence name width
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());
197 * Test the variant of calculateIdWidth that computes the longest of any
198 * sequence name or annotation label width
200 @Test(groups = "Functional")
201 public void testCalculateIdWidth_withMaxWidth()
203 AlignViewportI av = af.alignPanel.getAlignViewport();
204 av.setShowJVSuffix(true);
205 av.setFont(new Font("Courier", Font.PLAIN, 15), true);
206 av.setShowAnnotation(false);
210 * note 4 pixels 'padding' are added to the longest seq name/annotation label
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
218 * make the longest sequence name longer
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
229 * make the longest annotation name even longer
230 * note this is checked even if annotations are not shown
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);
243 * override with maxwidth
244 * note the 4 pixels padding is added to this value
246 d = af.alignPanel.calculateIdWidth(213);
247 assertEquals(d.width, 217);
248 assertEquals(d.height, 12);
251 @Test(groups = { "Functional", "Not-bamboo" })
252 public void testGetVisibleWidth()
255 * width for onscreen rendering is IDPanel width
257 int w = af.alignPanel.getVisibleIdWidth(true);
258 assertEquals(w, af.alignPanel.getIdPanel().getWidth());
259 assertEquals(w, 115);
262 * width for offscreen rendering is the same
263 * if no fixed id width is specified in preferences
265 Cache.setProperty("FIGURE_AUTOIDWIDTH", Boolean.FALSE.toString());
266 Cache.removeProperty("FIGURE_FIXEDIDWIDTH");
267 assertEquals(w, af.alignPanel.getVisibleIdWidth(false));
270 * preference for fixed id width - note 4 pixels padding is added
272 Cache.setProperty("FIGURE_FIXEDIDWIDTH", "120");
273 assertEquals(124, af.alignPanel.getVisibleIdWidth(false));
276 * preference for auto id width overrides fixed width
278 Cache.setProperty("FIGURE_AUTOIDWIDTH", Boolean.TRUE.toString());
279 assertEquals(115, af.alignPanel.getVisibleIdWidth(false));