From: gmungoc Date: Thu, 20 Aug 2015 08:42:44 +0000 (+0100) Subject: JAL-1833 added unit tests (some failing) for wrapToolitp X-Git-Tag: Jalview_2_9~10^2~35 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=91ef57a97eda85f9bb7b6f68bba52e06f19c29b8;p=jalview.git JAL-1833 added unit tests (some failing) for wrapToolitp --- diff --git a/src/jalview/gui/JvSwingUtils.java b/src/jalview/gui/JvSwingUtils.java index a9d0e75..65be457 100644 --- a/src/jalview/gui/JvSwingUtils.java +++ b/src/jalview/gui/JvSwingUtils.java @@ -49,12 +49,12 @@ import javax.swing.SwingConstants; public final class JvSwingUtils { /** - * wrap a bare html safe string to around 60 characters per line using a - * - * - * field - * @param enclose TODO - * @param ttext + * wrap a bare html safe string to around 60 characters per line using a CSS + * style class specifying word-wrap and break-word + * + * @param enclose + * if true, add <html> wrapper tags + * @param ttext * * @return */ @@ -62,15 +62,20 @@ public final class JvSwingUtils { Objects.requireNonNull(ttext, "Tootip text to format must not be null!"); ttext = ttext.trim(); - boolean maxLenghtExceeded = false; + boolean maxLengthExceeded = false; + /* + * Split into lines if already separated by
tags. + * + * TODO what about
,
,
? + */ if (ttext.contains("
")) { String[] htmllines = ttext.split("
"); for (String line : htmllines) { - maxLenghtExceeded = line.length() > 60; - if (maxLenghtExceeded) + maxLengthExceeded = line.length() > 60; + if (maxLengthExceeded) { break; } @@ -78,19 +83,17 @@ public final class JvSwingUtils } else { - maxLenghtExceeded = ttext.length() > 60; + maxLengthExceeded = ttext.length() > 60; } - if (!maxLenghtExceeded) + if (!maxLengthExceeded) { return enclose ? "" + ttext + "" : ttext; } - else - { - return enclose ? "

" - + ttext + "

" - : ttext; - } + + return enclose ? "

" + + ttext + "

" + : ttext; } public static JButton makeButton(String label, String tooltip, diff --git a/test/jalview/gui/JvSwingUtilsTest.java b/test/jalview/gui/JvSwingUtilsTest.java index 8a6f63b..eb810e6 100644 --- a/test/jalview/gui/JvSwingUtilsTest.java +++ b/test/jalview/gui/JvSwingUtilsTest.java @@ -1,6 +1,7 @@ package jalview.gui; import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertFalse; import javax.swing.JScrollBar; @@ -24,7 +25,7 @@ public class JvSwingUtilsTest assertEquals(0.25f, JvSwingUtils.getScrollBarProportion(sb), 0.001f); } - @Test(groups ={ "Functional" }) + @Test(groups = { "Functional" }) public void testGetScrollValueForProportion() { /* @@ -38,4 +39,56 @@ public class JvSwingUtilsTest */ assertEquals(125, JvSwingUtils.getScrollValueForProportion(sb, 0.25f)); } + + /** + * Test wrap tooltip where it is less than or equal to 60 characters long - no + * wrap should be applied + */ + @Test(groups = { "Functional" }) + public void testWrapTooltip_shortText() + { + String tip = "hello world"; + assertEquals(tip, JvSwingUtils.wrapTooltip(false, tip)); + assertEquals("" + tip + "", + JvSwingUtils.wrapTooltip(true, tip)); + + tip = "012345678901234567890123456789012345678901234567890123456789"; // 60 + assertEquals(tip, JvSwingUtils.wrapTooltip(false, tip)); + assertEquals("" + tip + "", + JvSwingUtils.wrapTooltip(true, tip)); + + tip = "0123456789012345678901234567890123456789012345678901234567890"; // 61 + assertFalse(tip.equals(JvSwingUtils.wrapTooltip(false, tip))); + assertFalse(("" + tip + "").equals(JvSwingUtils + .wrapTooltip(true, tip))); + } + + /** + * Test wrap tooltip where it is more than one line (separated by <br> + * tags) of less than or equal to 60 characters long - no wrap should be + * applied + */ + @Test(groups = { "Functional" }) + public void testWrapTooltip_multilineShortText() + { + String tip = "Now is the winter of our discontent
Made glorious summer by this sun of York"; + assertEquals(tip, JvSwingUtils.wrapTooltip(false, tip)); + assertEquals("" + tip + "", + JvSwingUtils.wrapTooltip(true, tip)); + } + + /** + * Test wrap tooltip where it is more than 60 characters long - word break and + * word wrap styling should be applied + */ + @Test(groups = { "Functional" }) + public void testWrapTooltip_longText() + { + String tip = "Now is the winter of our discontent made glorious summer by this sun of York"; + String expected = "

" + + tip + "

"; + assertEquals("" + expected + "", + JvSwingUtils.wrapTooltip(true, tip)); + assertEquals(expected, JvSwingUtils.wrapTooltip(false, tip)); + } }