From: gmungoc Date: Mon, 7 Nov 2016 15:21:01 +0000 (+0000) Subject: JAL-2272 unit tests added for Platform.isControlDown X-Git-Tag: Release_2_10_1^2~8^2~3 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=a703cb429a2f2b417b9850ce3a8fd6f271d182d3;p=jalview.git JAL-2272 unit tests added for Platform.isControlDown --- diff --git a/src/jalview/util/Platform.java b/src/jalview/util/Platform.java index 9df2060..49dc7ff 100644 --- a/src/jalview/util/Platform.java +++ b/src/jalview/util/Platform.java @@ -30,6 +30,10 @@ import java.awt.event.MouseEvent; */ public class Platform { + private static Boolean isAMac = null; + + private static Boolean isHeadless = null; + /** * sorry folks - Macs really are different * @@ -37,15 +41,21 @@ public class Platform */ public static boolean isAMac() { - return java.lang.System.getProperty("os.name").indexOf("Mac") > -1; + if (isAMac == null) + { + isAMac = System.getProperty("os.name").indexOf("Mac") > -1; + } + return isAMac.booleanValue(); } public static boolean isHeadless() { - String hdls = java.lang.System.getProperty("java.awt.headless"); - - return hdls != null && hdls.equals("true"); + if (isHeadless == null) + { + isHeadless = "true".equals(System.getProperty("java.awt.headless")); + } + return isHeadless; } /** @@ -89,7 +99,20 @@ public class Platform */ public static boolean isControlDown(MouseEvent e) { - if (isAMac()) + boolean aMac = isAMac(); + return isControlDown(e, aMac); + } + + /** + * Overloaded version of method (to allow unit testing) + * + * @param e + * @param aMac + * @return + */ + protected static boolean isControlDown(MouseEvent e, boolean aMac) + { + if (aMac) { /* * answer false for right mouse button diff --git a/test/jalview/util/PlatformTest.java b/test/jalview/util/PlatformTest.java new file mode 100644 index 0000000..492df42 --- /dev/null +++ b/test/jalview/util/PlatformTest.java @@ -0,0 +1,74 @@ +package jalview.util; + +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + +import java.awt.Button; +import java.awt.Event; +import java.awt.event.MouseEvent; + +import org.testng.annotations.Test; + +public class PlatformTest +{ + Button b = new Button(); + + /** + * isControlDown for Mac should answer true for Meta-down, but not for right + * mouse (popup trigger) + */ + @Test(groups = "Functional") + public void testIsControlDown_mac() + { + int clickCount = 1; + boolean isPopupTrigger = false; + int buttonNo = MouseEvent.BUTTON1; + boolean mac = true; + + int mods = 0; + // not concerned with MouseEvent id, when, x, y, xAbs, yAbs values + assertFalse(Platform.isControlDown(new MouseEvent(b, 0, 0L, mods, 0, 0, + 0, 0, clickCount, isPopupTrigger, buttonNo), mac)); + + mods = Event.CTRL_MASK; + assertFalse(Platform.isControlDown(new MouseEvent(b, 0, 0L, mods, 0, 0, + 0, 0, clickCount, isPopupTrigger, buttonNo), mac)); + + mods = Event.META_MASK; + assertTrue(Platform.isControlDown(new MouseEvent(b, 0, 0L, mods, 0, 0, + 0, 0, clickCount, isPopupTrigger, buttonNo), mac)); + + isPopupTrigger = true; + assertFalse(Platform.isControlDown(new MouseEvent(b, 0, 0L, mods, 0, 0, + 0, 0, clickCount, isPopupTrigger, buttonNo), mac)); + } + + /** + * If not a Mac, we only care whether CTRL_MASK modifier is set on the mouse + * event + */ + @Test(groups = "Functional") + public void testIsControlDown_notMac() + { + int clickCount = 1; + boolean isPopupTrigger = false; + int buttonNo = MouseEvent.BUTTON1; + boolean mac = false; + + int mods = 0; + // not concerned with MouseEvent id, when, x, y, xAbs, yAbs values + assertFalse(Platform.isControlDown(new MouseEvent(b, 0, 0L, mods, 0, 0, + 0, 0, clickCount, isPopupTrigger, buttonNo), mac)); + + mods = Event.CTRL_MASK; + assertTrue(Platform.isControlDown(new MouseEvent(b, 0, 0L, mods, 0, 0, + 0, 0, clickCount, isPopupTrigger, buttonNo), mac)); + + mods = Event.CTRL_MASK | Event.SHIFT_MASK | Event.ALT_MASK; + clickCount = 2; + buttonNo = 2; + isPopupTrigger = true; + assertTrue(Platform.isControlDown(new MouseEvent(b, 0, 0L, mods, 0, 0, + 0, 0, clickCount, isPopupTrigger, buttonNo), mac)); + } +}