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.assertTrue;
26 import jalview.bin.Cache;
27 import jalview.bin.Jalview;
28 import jalview.datamodel.Alignment;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceGroup;
32 import jalview.datamodel.SequenceI;
33 import jalview.io.DataSourceType;
34 import jalview.io.FileLoader;
35 import jalview.util.MessageManager;
36 import jalview.viewmodel.ViewportRanges;
38 import java.awt.event.MouseEvent;
40 import javax.swing.JMenuItem;
41 import javax.swing.JPopupMenu;
43 import org.testng.annotations.BeforeClass;
44 import org.testng.annotations.Test;
46 public class ScalePanelTest
48 @BeforeClass(alwaysRun = true)
49 public void setUpJvOptionPane()
51 JvOptionPane.setInteractiveMode(false);
52 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
55 @Test(groups = "Functional")
56 public void testPreventNegativeStartColumn()
58 SequenceI seq1 = new Sequence("Seq1", "MATRESS");
59 SequenceI seq2 = new Sequence("Seq2", "MADNESS");
60 AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
62 AlignFrame alignFrame = new AlignFrame(al, al.getWidth(),
64 ScalePanel scalePanel = alignFrame.alignPanel.getScalePanel();
66 MouseEvent mouse = new MouseEvent(scalePanel, 0, 1, 0, 4, 0, 1, false);
67 scalePanel.mousePressed(mouse);
68 scalePanel.mouseDragged(mouse);
70 // simulate dragging selection leftwards beyond the sequences giving
72 mouse = new MouseEvent(scalePanel, 0, 1, 0, -30, 0, 1, false);
74 scalePanel.mouseReleased(mouse);
76 SequenceGroup sg = scalePanel.av.getSelectionGroup();
77 int startCol = sg.getStartRes();
79 assertTrue(startCol >= 0);
85 @Test(groups = "Functional")
86 public void testSelectColumns_withHidden()
88 String seq1 = ">Seq1\nANTOFAGASTAVALPARAISOMONTEVIDEOANTANANARIVO";
89 AlignFrame alignFrame = new FileLoader().LoadFileWaitTillLoaded(seq1,
90 DataSourceType.PASTE);
91 ScalePanel scalePanel = alignFrame.alignPanel.getScalePanel();
94 * hide columns 1-20 (of 43); then 'drag' to select columns 30-31;
95 * 31 is 51 in absolute columns but bug JAL-3212 reduces it to
98 AlignViewport viewport = alignFrame.getViewport();
99 ViewportRanges ranges = viewport.getRanges();
100 assertEquals(ranges.getStartRes(), 0);
101 assertEquals(ranges.getEndRes(), 42);
102 viewport.hideColumns(0, 19);
103 alignFrame.alignPanel.updateLayout();
104 assertEquals(ranges.getStartRes(), 0);
105 assertEquals(ranges.getEndRes(), 22);
107 int cw = viewport.getCharWidth();
108 int xPos = 9 * cw + 2;
109 MouseEvent mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1,
111 scalePanel.mousePressed(mouse);
112 scalePanel.mouseDragged(mouse);
114 mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false);
115 scalePanel.mouseReleased(mouse);
117 SequenceGroup sg = scalePanel.av.getSelectionGroup();
118 assertEquals(sg.getStartRes(), 29);
119 assertEquals(sg.getEndRes(), 30);
122 @Test(groups = "Functional")
123 public void testBuildPopupMenu()
125 final String hide = MessageManager.getString("label.hide_columns");
126 final String reveal = MessageManager.getString("label.reveal");
127 final String revealAll = MessageManager.getString("action.reveal_all");
129 String seq1 = ">Seq1\nANTOFAGASTA";
130 AlignFrame alignFrame = new FileLoader().LoadFileWaitTillLoaded(seq1,
131 DataSourceType.PASTE);
132 ScalePanel scalePanel = alignFrame.alignPanel.getScalePanel();
133 AlignViewport viewport = alignFrame.getViewport();
134 int cw = viewport.getCharWidth();
137 * hide columns 3-4 (counting from 0)
139 viewport.hideColumns(3, 4);
140 alignFrame.alignPanel.updateLayout();
143 * verify popup menu left/right of hidden column marker
144 * NB need to call mouseMoved first as this sets field 'reveal'
146 int xPos = 1 * cw + 2; // 2 columns left of hidden marker
147 MouseEvent mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1,
149 scalePanel.mouseMoved(mouse);
150 JPopupMenu popup = scalePanel.buildPopupMenu(1);
151 assertEquals(popup.getSubElements().length, 0);
154 * popup just left of hidden marker has 'Reveal'
157 mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false);
158 scalePanel.mouseMoved(mouse);
159 popup = scalePanel.buildPopupMenu(2);
160 assertEquals(popup.getSubElements().length, 1);
161 assertEquals(((JMenuItem) popup.getSubElements()[0]).getText(), reveal);
164 * popup just right of hidden marker has 'Reveal'
167 mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false);
168 scalePanel.mouseMoved(mouse);
169 popup = scalePanel.buildPopupMenu(5); // allowing for 2 hidden columns
170 assertEquals(popup.getSubElements().length, 1);
171 assertEquals(((JMenuItem) popup.getSubElements()[0]).getText(), reveal);
174 * popup further right is empty
177 mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false);
178 scalePanel.mouseMoved(mouse);
179 popup = scalePanel.buildPopupMenu(6); // allowing for 2 hidden columns
180 assertEquals(popup.getSubElements().length, 0);
183 * 'drag' to select columns around the hidden column marker
186 mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false);
187 scalePanel.mousePressed(mouse);
188 scalePanel.mouseDragged(mouse);
190 mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false);
191 scalePanel.mouseReleased(mouse);
194 * popup 2 columns left of marker: 'Hide' only
197 mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false);
198 scalePanel.mouseMoved(mouse);
199 popup = scalePanel.buildPopupMenu(1);
200 assertEquals(popup.getSubElements().length, 1);
201 assertEquals(((JMenuItem) popup.getSubElements()[0]).getText(), hide);
204 * popup just left of marker: 'Reveal' and 'Hide'
207 mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false);
208 scalePanel.mouseMoved(mouse);
209 popup = scalePanel.buildPopupMenu(1);
210 assertEquals(popup.getSubElements().length, 2);
211 assertEquals(((JMenuItem) popup.getSubElements()[0]).getText(), reveal);
212 assertEquals(((JMenuItem) popup.getSubElements()[1]).getText(), hide);
215 * popup just right of marker: 'Reveal' and 'Hide'
218 mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false);
219 scalePanel.mouseMoved(mouse);
220 popup = scalePanel.buildPopupMenu(5);
221 assertEquals(popup.getSubElements().length, 2);
222 assertEquals(((JMenuItem) popup.getSubElements()[0]).getText(), reveal);
223 assertEquals(((JMenuItem) popup.getSubElements()[1]).getText(), hide);
226 * hiding a second region adds option 'Reveal all' to 'Reveal'
228 viewport.hideColumns(6, 7);
229 alignFrame.alignPanel.updateLayout();
231 mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false);
232 scalePanel.mouseMoved(mouse);
233 popup = scalePanel.buildPopupMenu(5);
234 assertEquals(popup.getSubElements().length, 3);
235 assertEquals(((JMenuItem) popup.getSubElements()[0]).getText(), reveal);
236 assertEquals(((JMenuItem) popup.getSubElements()[1]).getText(),
238 assertEquals(((JMenuItem) popup.getSubElements()[2]).getText(), hide);
240 alignFrame.deselectAllSequenceMenuItem_actionPerformed(null);
241 popup = scalePanel.buildPopupMenu(5);
242 assertEquals(popup.getSubElements().length, 2);
243 assertEquals(((JMenuItem) popup.getSubElements()[0]).getText(), reveal);
244 assertEquals(((JMenuItem) popup.getSubElements()[1]).getText(),
248 @BeforeClass(alwaysRun = true)
249 public static void setUpBeforeClass() throws Exception
252 * use read-only test properties file
254 Cache.loadProperties("test/jalview/io/testProps.jvprops");
255 Jalview.main(new String[] { "--nonews" });