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