Merge branch 'develop' into features/JAL-4219_extended_fasta_rna_ss
[jalview.git] / test / jalview / gui / JvSwingUtilsTest.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.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25
26 import java.awt.Component;
27 import java.awt.Graphics2D;
28 import java.awt.geom.AffineTransform;
29
30 import javax.swing.JScrollBar;
31
32 import org.testng.annotations.BeforeClass;
33 import org.testng.annotations.Test;
34
35 public class JvSwingUtilsTest
36 {
37
38   @BeforeClass(alwaysRun = true)
39   public void setUpJvOptionPane()
40   {
41     JvOptionPane.setInteractiveMode(false);
42     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
43   }
44
45   @Test(groups = { "Functional" })
46   public void testGetScrollBarProportion()
47   {
48     /*
49      * orientation, value, extent (width), min, max
50      */
51     JScrollBar sb = new JScrollBar(0, 125, 50, 0, 450);
52
53     /*
54      * operating range is 25 - 425 (400 wide) so value 125 is 100/400ths of this
55      * range
56      */
57     assertEquals(0.25f, JvSwingUtils.getScrollBarProportion(sb), 0.001f);
58   }
59
60   @Test(groups = { "Functional" })
61   public void testGetScrollValueForProportion()
62   {
63     /*
64      * orientation, value, extent (width), min, max
65      */
66     JScrollBar sb = new JScrollBar(0, 125, 50, 0, 450);
67
68     /*
69      * operating range is 25 - 425 (400 wide) so value 125 is a quarter of this
70      * range
71      */
72     assertEquals(125, JvSwingUtils.getScrollValueForProportion(sb, 0.25f));
73   }
74
75   /**
76    * Test wrap tooltip where it is less than or equal to 60 characters long - no
77    * wrap should be applied
78    */
79   @Test(groups = { "Functional" })
80   public void testWrapTooltip_shortText()
81   {
82     String tip = "hello world";
83     assertEquals(tip, JvSwingUtils.wrapTooltip(false, tip));
84     assertEquals("<html>" + tip + "</html>",
85             JvSwingUtils.wrapTooltip(true, tip));
86
87     tip = "012345678901234567890123456789012345678901234567890123456789"; // 60
88     assertEquals(tip, JvSwingUtils.wrapTooltip(false, tip));
89     assertEquals("<html>" + tip + "</html>",
90             JvSwingUtils.wrapTooltip(true, tip));
91
92     tip = "0123456789012345678901234567890123456789012345678901234567890"; // 61
93     assertFalse(tip.equals(JvSwingUtils.wrapTooltip(false, tip)));
94     assertFalse(("<html>" + tip + "</html>")
95             .equals(JvSwingUtils.wrapTooltip(true, tip)));
96   }
97
98   /**
99    * Test wrap tooltip where it is more than one line (separated by &lt;br&gt;
100    * tags) of less than or equal to 60 characters long - no wrap should be
101    * applied
102    */
103   @Test(groups = { "Functional" })
104   public void testWrapTooltip_multilineShortText()
105   {
106     String tip = "Now is the winter of our discontent<br>Made glorious summer by this sun of York";
107     assertEquals(tip, JvSwingUtils.wrapTooltip(false, tip));
108     assertEquals("<html>" + tip + "</html>",
109             JvSwingUtils.wrapTooltip(true, tip));
110   }
111
112   /**
113    * Test wrap tooltip where it is more than 60 characters long - word break and
114    * word wrap styling should be applied
115    */
116   @Test(groups = { "Functional" })
117   public void testWrapTooltip_longText()
118   {
119     String tip = "Now is the winter of our discontent made glorious summer by this sun of York";
120     String expected = "<style> div.ttip {width:350px;white-space:pre-wrap;padding:2px;overflow-wrap:break-word;}</style>"
121             + "<div class=\"ttip\">" + tip + " </div>";
122     assertEquals("<html>" + expected + "</html>",
123             JvSwingUtils.wrapTooltip(true, tip));
124     assertEquals(expected, JvSwingUtils.wrapTooltip(false, tip));
125   }
126
127   public static double getScaling(Component c)
128   {
129     Graphics2D g = (Graphics2D) c.getGraphics();
130     if (g == null)
131     {
132       return 0.0;
133     }
134     AffineTransform t = g.getTransform();
135     double scaling = t.getScaleX(); // Assuming square pixels :P
136     return scaling;
137   }
138
139 }