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;
25 import static org.testng.Assert.assertNotNull;
26 import static org.testng.Assert.assertTrue;
28 import java.awt.Container;
29 import java.awt.Dimension;
31 import java.awt.FontMetrics;
32 import java.awt.event.MouseEvent;
33 import java.lang.reflect.InvocationTargetException;
35 import javax.swing.SwingUtilities;
37 import org.testng.annotations.BeforeMethod;
38 import org.testng.annotations.Test;
40 import jalview.api.AlignViewportI;
41 import jalview.bin.Cache;
42 import jalview.bin.Jalview;
43 import jalview.datamodel.AlignmentAnnotation;
44 import jalview.datamodel.SequenceI;
45 import jalview.io.DataSourceType;
46 import jalview.io.FileLoader;
47 import jalview.viewmodel.ViewportRanges;
49 public class AlignmentPanelTest
53 @BeforeMethod(alwaysRun = true)
54 public void setUp() throws InvocationTargetException, InterruptedException
58 { "--nonews", "--props", "test/jalview/testProps.jvprops" });
60 Cache.applicationProperties.setProperty("SHOW_IDENTITY",
61 Boolean.TRUE.toString());
62 af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
66 * ensure the panel has been repainted and so ViewportRanges set
68 SwingUtilities.invokeAndWait(new Runnable()
78 * wait for Consensus thread to complete
85 } catch (InterruptedException x)
88 } while (af.getViewport().getCalcManager().isWorking());
92 * Test side effect that end residue is set correctly by setScrollValues, with
93 * or without hidden columns
95 @Test(groups = "Functional")
96 public void testSetScrollValues()
98 ViewportRanges ranges = af.getViewport().getRanges();
99 af.alignPanel.setScrollValues(0, 0);
101 int oldres = ranges.getEndRes();
102 af.alignPanel.setScrollValues(-1, 5);
104 // setting -ve x value does not change residue
105 assertEquals(ranges.getEndRes(), oldres);
107 af.alignPanel.setScrollValues(0, 5);
109 // setting 0 as x value does not change residue
110 assertEquals(ranges.getEndRes(), oldres);
112 af.alignPanel.setScrollValues(5, 5);
113 // setting x value to 5 extends endRes by 5 residues
114 assertEquals(ranges.getEndRes(), oldres + 5);
116 // scroll to position after hidden columns sets endres to oldres (width) +
119 af.getViewport().hideColumns(30, 50);
120 af.alignPanel.setScrollValues(scrollpos, 5);
121 assertEquals(ranges.getEndRes(), oldres + scrollpos);
123 // scroll to position within hidden columns, still sets endres to oldres +
125 // not sure if this is actually correct behaviour but this is what Jalview
128 af.getViewport().showAllHiddenColumns();
129 af.getViewport().hideColumns(30, 50);
130 af.alignPanel.setScrollValues(scrollpos, 5);
131 assertEquals(ranges.getEndRes(), oldres + scrollpos);
133 // scroll to position within <width> distance of the end of the alignment
134 // endRes should be set to width of alignment - 1
136 af.getViewport().showAllHiddenColumns();
137 af.alignPanel.setScrollValues(scrollpos, 5);
138 assertEquals(ranges.getEndRes(),
139 af.getViewport().getAlignment().getWidth() - 1);
141 // now hide some columns, and scroll to position within <width>
142 // distance of the end of the alignment
143 // endRes should be set to width of alignment - 1 - the number of hidden
145 af.getViewport().hideColumns(30, 50);
146 af.alignPanel.setScrollValues(scrollpos, 5);
147 assertEquals(ranges.getEndRes(),
148 af.getViewport().getAlignment().getWidth() - 1 - 21); // 21 is the
155 * Test that update layout reverts to original (unwrapped) values for endRes
156 * when switching from wrapped back to unwrapped mode (JAL-2739)
158 @Test(groups = "Functional")
159 public void testUpdateLayout_endRes()
161 // get details of original alignment dimensions
162 ViewportRanges ranges = af.getViewport().getRanges();
163 int endres = ranges.getEndRes();
166 af.alignPanel.getAlignViewport().setWrapAlignment(true);
167 af.alignPanel.updateLayout();
169 // endRes has changed
170 assertNotEquals(ranges.getEndRes(), endres);
173 af.alignPanel.getAlignViewport().setWrapAlignment(false);
174 af.alignPanel.updateLayout();
176 // endRes back to original value
177 assertEquals(ranges.getEndRes(), endres);
181 * Test the variant of calculateIdWidth that only recomputes the width if it
182 * is not already saved in the viewport (initial value is -1)
184 @Test(groups = "Functional")
185 public void testCalculateIdWidth_noArgs()
187 AlignViewportI av = af.alignPanel.getAlignViewport();
188 av.setShowJVSuffix(true);
189 av.setFont(new Font("Courier", Font.PLAIN, 15), true);
192 Dimension d = af.alignPanel.calculateIdWidth();
193 assertEquals(d.width, 0);
194 assertEquals(d.height, 0);
197 d = af.alignPanel.calculateIdWidth();
198 assertEquals(d.width, 99);
199 assertEquals(d.height, 0);
202 * note 4 pixels padding are added to the longest sequence name width
204 av.setIdWidth(-1); // force recalculation
205 d = af.alignPanel.calculateIdWidth();
206 assertEquals(d.width, 166); // 4 + pixel width of "Q93Z60_ARATH/1-118"
207 assertEquals(d.height, 12);
208 assertEquals(d.width, av.getIdWidth());
212 * Test the variant of calculateIdWidth that computes the longest of any
213 * sequence name or annotation label width FIXME: JAL-4291: test needs
214 * updating for JAL-244 and JAL-4091
216 @Test(groups = "Functional", enabled = false)
217 public void testCalculateIdWidth_withMaxWidth()
219 AlignViewportI av = af.alignPanel.getAlignViewport();
220 av.setShowJVSuffix(true);
221 av.setFont(new Font("Courier", Font.PLAIN, 15), true);
222 av.setShowAnnotation(false);
225 FontMetrics fmfor = new Container()
226 .getFontMetrics(new Font(af.viewport.font.getName(),
227 Font.ITALIC, af.viewport.font.getSize()));
230 * note 4 pixels 'padding' are added to the longest seq name/annotation label
232 Dimension d = af.alignPanel.calculateIdWidth(2000);
233 // Assumption ID_WIDTH_PADDING == 4
234 int expwidth = 3 + fmfor.stringWidth("Conservation");
236 assertEquals(d.width, 166); // 4 + pixel width of "Q93Z60_ARATH/1-118"
237 assertEquals(d.height, 12); // fixed value (not used?)
238 assertEquals(av.getIdWidth(), expwidth); // not changed by this method
241 * make the longest sequence name longer
243 SequenceI seq = af.viewport.getAlignment()
244 .findSequenceMatch("Q93Z60_ARATH")[0];
245 seq.setName(seq.getName() + "MMMMM");
246 d = af.alignPanel.calculateIdWidth(2000);
247 assertEquals(d.width, 211); // 4 + pixel width of "Q93Z60_ARATHMMMMM/1-118"
248 assertEquals(d.height, 12);
249 assertEquals(av.getIdWidth(), 18); // unchanged
252 * make the longest annotation name even longer
253 * note this is checked even if annotations are not shown
255 AlignmentAnnotation aa = av.getAlignment().getAlignmentAnnotation()[0];
256 aa.label = "THIS IS A VERY LONG LABEL INDEED";
257 d = af.alignPanel.calculateIdWidth(2000);
258 // Assumption ID_WIDTH_PADDING == 3
259 expwidth = 3 + fmfor.stringWidth(aa.label);
261 assertEquals(d.width, expwidth); // 228 == ID_WIDTH_PADDING + pixel width of
262 // "THIS IS A VERY LONG LABEL INDEED"
263 assertEquals(d.height, 12);
266 * override with maxwidth
267 * note the 4 pixels padding is added to this value
269 d = af.alignPanel.calculateIdWidth(213);
270 assertEquals(d.width, 217);
271 assertEquals(d.height, 12);
274 @Test(groups = { "Functional", "Not-bamboo" })
275 public void testGetVisibleWidth()
277 double scaling = jalview.gui.JvSwingUtilsTest.getScaling(af.alignPanel);
279 * width for onscreen rendering is IDPanel width
281 int w = af.alignPanel.getVisibleIdWidth(true);
282 assertEquals(w, af.alignPanel.getIdPanel().getWidth());
284 // different scaling (1.0, 2.0) gives different results
285 assertEquals(w, scaling == 1.0 ? 112 : 107);
288 * width for offscreen rendering is the same
289 * if no fixed id width is specified in preferences
291 Cache.setProperty("FIGURE_AUTOIDWIDTH", Boolean.FALSE.toString());
292 Cache.removeProperty("FIGURE_FIXEDIDWIDTH");
293 assertEquals(w, af.alignPanel.getVisibleIdWidth(false));
296 * preference for fixed id width - note 4 pixels padding is added
298 Cache.setProperty("FIGURE_FIXEDIDWIDTH", "120");
299 assertEquals(124, af.alignPanel.getVisibleIdWidth(false));
302 * preference for auto id width overrides fixed width
304 Cache.setProperty("FIGURE_AUTOIDWIDTH", Boolean.TRUE.toString());
305 w = af.alignPanel.getVisibleIdWidth(false);
306 // allow some leeway for different OS renderings
307 assertTrue(w > 105 && w < 120);
308 // different scaling (1.0, 2.0) gives different results
309 assertEquals(w, scaling == 1.0 ? 112 : 107);
312 @Test(groups = { "Functional", "Not-bamboo" })
313 public void testresetIdWidth()
315 double scaling = jalview.gui.JvSwingUtilsTest.getScaling(af.alignPanel);
317 * width for onscreen rendering is IDPanel width
319 int w = af.alignPanel.getVisibleIdWidth(true);
320 int actual = af.alignPanel.getIdPanel().getWidth();
321 assertEquals(w, actual);
322 // allow some leeway for different OS renderings
323 assertTrue(w > 105 && w < 120);
324 // different scaling (1.0, 2.0) gives different results
325 assertEquals(w, scaling == 1.0 ? 112 : 107);
328 af.viewport.setIdWidth(200);
329 // fake mouse drag sets manuallyAdjusted to true (0,0 not moving mouse)
330 MouseEvent drag = new MouseEvent(af.alignPanel,
331 MouseEvent.MOUSE_DRAGGED, System.currentTimeMillis(), 0, 0, 0,
332 MouseEvent.BUTTON1, false);
333 af.alignPanel.idwidthAdjuster.mouseDragged(drag);
334 af.alignPanel.paintComponent(af.alignPanel.getGraphics());
335 w = af.alignPanel.calculateIdWidth().width;
337 af.alignPanel.getIdPanel().getIdCanvas().isManuallyAdjusted());
338 actual = af.alignPanel.getIdPanel().getWidth();
339 assertEquals(w, actual);
341 af.viewport.setIdWidth(-1);
342 af.alignPanel.calculateIdWidth();
343 af.alignPanel.getIdPanel().getIdCanvas().setManuallyAdjusted(false);
344 w = af.alignPanel.calculateIdWidth().width;
345 af.alignPanel.paintComponent(af.alignPanel.getGraphics());
347 actual = af.alignPanel.getIdPanel().getWidth();
348 assertEquals(w, actual);
350 // setting a negative IdWidth and then running calculateIdWidth resets width
351 // to optimal id width
352 // allow some leeway for different OS renderings
353 assertTrue(w > 105 && w < 120);
354 // different scaling (1.0, 2.0) gives different results
355 assertEquals(w, scaling == 1.0 ? 112 : 107);
358 @Test(groups = "Functional")
359 public void testSetOverviewTitle()
361 OverviewPanel ov1 = this.af.openOverviewPanel(true);
362 String alignFrameTitle = af.getTitle();
363 assertEquals(ov1.getTitle(), "Overview " + alignFrameTitle);
366 * on New View, existing overview should get " Original" added to title
367 * and new view's overview should get " View 1" added
369 af.newView_actionPerformed(null);
370 assertEquals(ov1.getTitle(),
371 "Overview " + alignFrameTitle + " Original");
372 OverviewPanel ov2 = this.af.openOverviewPanel(true);
373 assertEquals(ov2.getTitle(), "Overview " + alignFrameTitle + " View 1");
376 @Test(groups = "Functional")
377 public void testSetOverviewTitle_automaticOverview()
379 Cache.setProperty("SHOW_OVERVIEW", "true");
380 AlignFrame alignFrame = new FileLoader().LoadFileWaitTillLoaded(
381 "examples/uniref50.fa", DataSourceType.FILE);
382 OverviewPanel ov1 = alignFrame.alignPanel.getOverviewPanel();
384 String alignFrameTitle = alignFrame.getTitle();
385 assertEquals(ov1.getTitle(), "Overview " + alignFrameTitle);
388 * on New View, existing overview should get " Original" added to title
389 * and new view's automatic overview should have " View 1" added
391 alignFrame.newView_actionPerformed(null);
392 assertEquals(ov1.getTitle(),
393 "Overview " + alignFrameTitle + " Original");
394 OverviewPanel ov2 = alignFrame.alignPanel.getOverviewPanel();
396 assertEquals(ov2.getTitle(), "Overview " + alignFrameTitle + " View 1");