From: tcofoegbu Date: Thu, 24 Nov 2016 15:40:05 +0000 (+0000) Subject: JAL-2326 added layer of abstraction between Jalview and JOptionPane to provide better... X-Git-Tag: Release_2_10_3b1~399^2^2~2 X-Git-Url: http://source.jalview.org/gitweb/?p=jalview.git;a=commitdiff_plain;h=d3913db54e57a595f182851af2f4043a88b0eb2a JAL-2326 added layer of abstraction between Jalview and JOptionPane to provide better control for running unattended test non-interactively --- diff --git a/src/jalview/gui/JvOptionPane.java b/src/jalview/gui/JvOptionPane.java new file mode 100644 index 0000000..a6192be --- /dev/null +++ b/src/jalview/gui/JvOptionPane.java @@ -0,0 +1,316 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ + +package jalview.gui; + +import java.awt.Component; +import java.awt.HeadlessException; + +import javax.swing.Icon; +import javax.swing.JOptionPane; + +public class JvOptionPane extends JOptionPane +{ + /** + * + */ + private static final long serialVersionUID = -3019167117756785229L; + + private static Object mockResponse = JvOptionPane.CANCEL_OPTION; + + private static boolean interactiveMode = true; + + public static int showConfirmDialog(Component parentComponent, + Object message) throws HeadlessException + { + return isInteractiveMode() ? JOptionPane.showConfirmDialog( + parentComponent, message) : (int) getMockResponse(); + } + + public static int showConfirmDialog(Component parentComponent, + Object message, String title, int optionType) + throws HeadlessException + { + return isInteractiveMode() ? JOptionPane.showConfirmDialog( + parentComponent, message, title, optionType) + : (int) getMockResponse(); + } + + public static int showConfirmDialog(Component parentComponent, + Object message, String title, int optionType, int messageType) + throws HeadlessException + { + return isInteractiveMode() ? JOptionPane.showConfirmDialog( + parentComponent, message, title, optionType, messageType) + : (int) getMockResponse(); + } + + public static int showConfirmDialog(Component parentComponent, + Object message, String title, int optionType, int messageType, + Icon icon) throws HeadlessException + { + return isInteractiveMode() ? JOptionPane.showConfirmDialog( + parentComponent, message, title, optionType, messageType, icon) + : (int) getMockResponse(); + } + + public static int showInternalConfirmDialog(Component parentComponent, + Object message) + { + return isInteractiveMode() ? JOptionPane.showInternalConfirmDialog( + parentComponent, message) : (int) getMockResponse(); + } + + public static int showInternalConfirmDialog(Component parentComponent, + Object message, String title, int optionType) + { + return isInteractiveMode() ? JOptionPane.showConfirmDialog( + parentComponent, message, title, optionType) + : (int) getMockResponse(); + } + + public static int showInternalConfirmDialog(Component parentComponent, + Object message, String title, int optionType, int messageType) + { + return isInteractiveMode() ? JOptionPane.showConfirmDialog( + parentComponent, message, title, optionType, messageType) + : (int) getMockResponse(); + } + + public static int showInternalConfirmDialog(Component parentComponent, + Object message, String title, int optionType, int messageType, + Icon icon) + { + return isInteractiveMode() ? JOptionPane.showInternalConfirmDialog( + parentComponent, message, title, optionType, messageType, icon) + : (int) getMockResponse(); + } + + public static int showOptionDialog(Component parentComponent, + Object message, String title, int optionType, int messageType, + Icon icon, Object[] options, Object initialValue) + throws HeadlessException + { + return isInteractiveMode() ? JOptionPane.showOptionDialog( + parentComponent, message, title, optionType, messageType, icon, + options, initialValue) : (int) getMockResponse(); + } + + public static void showMessageDialog(Component parentComponent, + Object message) throws HeadlessException + { + if (isInteractiveMode()) + { + JOptionPane.showMessageDialog(parentComponent, message); + } + else + { + outputMessage(message); + } + } + + public static void showMessageDialog(Component parentComponent, + Object message, String title, int messageType) + throws HeadlessException + { + if (isInteractiveMode()) + { + JOptionPane.showMessageDialog(parentComponent, message, title, + messageType); + } + else + { + outputMessage(message); + } + } + + public static void showMessageDialog(Component parentComponent, + Object message, String title, int messageType, Icon icon) + throws HeadlessException + { + if (isInteractiveMode()) + { + JOptionPane.showMessageDialog(parentComponent, message, title, + messageType, icon); + } + else + { + outputMessage(message); + } + } + + public static void showInternalMessageDialog(Component parentComponent, + Object message) + { + if (isInteractiveMode()) + { + JOptionPane.showMessageDialog(parentComponent, message); + } + else + { + outputMessage(message); + } + } + + public static void showInternalMessageDialog(Component parentComponent, + Object message, String title, int messageType) + { + if (isInteractiveMode()) + { + JOptionPane.showMessageDialog(parentComponent, message, title, + messageType); + } + else + { + outputMessage(message); + } + } + + public static void showInternalMessageDialog(Component parentComponent, + Object message, String title, int messageType, Icon icon) + { + if (isInteractiveMode()) + { + JOptionPane.showMessageDialog(parentComponent, message, title, + messageType, icon); + } + else + { + outputMessage(message); + } + } + + public static String showInputDialog(Object message) + throws HeadlessException + { + return isInteractiveMode() ? JOptionPane.showInputDialog(message) + : getMockResponse().toString(); + } + + public static String showInputDialog(Object message, + Object initialSelectionValue) + { + return isInteractiveMode() ? JOptionPane.showInputDialog(message, + initialSelectionValue) : getMockResponse().toString(); + } + + public static String showInputDialog(Component parentComponent, + Object message) throws HeadlessException + { + return isInteractiveMode() ? JOptionPane.showInputDialog( + parentComponent, message) : getMockResponse().toString(); + } + + public static String showInputDialog(Component parentComponent, + Object message, Object initialSelectionValue) + { + return isInteractiveMode() ? JOptionPane.showInputDialog( + parentComponent, message, initialSelectionValue) + : getMockResponse().toString(); + } + + public static String showInputDialog(Component parentComponent, + Object message, String title, int messageType) + throws HeadlessException + { + return isInteractiveMode() ? JOptionPane.showInputDialog( + parentComponent, message, title, messageType) + : getMockResponse().toString(); + } + + public static Object showInputDialog(Component parentComponent, + Object message, String title, int messageType, Icon icon, + Object[] selectionValues, Object initialSelectionValue) + throws HeadlessException + { + return isInteractiveMode() ? JOptionPane.showInputDialog( + parentComponent, message, title, messageType, icon, + selectionValues, initialSelectionValue) : getMockResponse() + .toString(); + } + + public static String showInternalInputDialog(Component parentComponent, + Object message) + { + return isInteractiveMode() ? JOptionPane.showInternalInputDialog( + parentComponent, message) : getMockResponse().toString(); + } + + public static String showInternalInputDialog(Component parentComponent, + Object message, String title, int messageType) + { + return isInteractiveMode() ? JOptionPane.showInternalInputDialog( + parentComponent, message, title, messageType) + : getMockResponse().toString(); + } + + public static Object showInternalInputDialog(Component parentComponent, + Object message, String title, int messageType, Icon icon, + Object[] selectionValues, Object initialSelectionValue) + { + return isInteractiveMode() ? JOptionPane.showInternalInputDialog( + parentComponent, message, title, messageType, icon, + selectionValues, initialSelectionValue) : getMockResponse() + .toString(); + } + + // (mockResponse != DONT_MOCK) ? mockResponse : + // if (isMockMode()) + // { + // + // } + // else + // { + // JOptionPane + // } + + private static void outputMessage(Object message) + { + System.out.println(">>> JOption Message : " + message.toString()); + } + + public static Object getMockResponse() + { + return mockResponse; + } + + public static void setMockResponse(Object mockOption) + { + JvOptionPane.mockResponse = mockOption; + } + + public static void resetMock() + { + setMockResponse(JvOptionPane.CANCEL_OPTION); + setInteractiveMode(true); + } + + public static boolean isInteractiveMode() + { + return interactiveMode; + } + + public static void setInteractiveMode(boolean interactiveMode) + { + JvOptionPane.interactiveMode = interactiveMode; + } + +} diff --git a/test/jalview/gui/JvOptionPaneTest.java b/test/jalview/gui/JvOptionPaneTest.java new file mode 100644 index 0000000..86409a4 --- /dev/null +++ b/test/jalview/gui/JvOptionPaneTest.java @@ -0,0 +1,156 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ + +package jalview.gui; + +import java.awt.Component; +import java.awt.Dimension; + +import javax.swing.Icon; +import javax.swing.JDesktopPane; +import javax.swing.JFrame; +import javax.swing.JPanel; + +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class JvOptionPaneTest +{ + + Component parentComponent = null; + + String message = "Hello World!"; + + String title = "Title"; + + int optionType = JvOptionPane.OK_CANCEL_OPTION; + + int messageType = JvOptionPane.INFORMATION_MESSAGE; + + Icon icon = null; + + Object initialSelectionValue = null; + + Object[] selectionValues = null; + + @BeforeMethod(alwaysRun = true) + public void setUp() + { + JvOptionPane.setInteractiveMode(false); + JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION); + } + + @Test(groups = { "Functional" }) + public void showConfirmDialogFamilyTest() + { + JvOptionPane.showConfirmDialog(parentComponent, message); + JvOptionPane.showConfirmDialog(parentComponent, message, title, + optionType); + JvOptionPane.showConfirmDialog(parentComponent, message, title, + optionType, messageType); + JvOptionPane.showConfirmDialog(parentComponent, message, title, + optionType, messageType, icon); + Assert.assertTrue(true); + } + + @Test(groups = { "Functional" }) + public void showInputDialogFamilyTest() + { + JvOptionPane.showInputDialog(message); + JvOptionPane.showInputDialog(parentComponent, message); + JvOptionPane.showInputDialog(message, initialSelectionValue); + JvOptionPane.showInputDialog(parentComponent, message, + initialSelectionValue); + JvOptionPane.showInputDialog(parentComponent, message, title, + messageType); + JvOptionPane.showInputDialog(parentComponent, message, title, + messageType, icon, selectionValues, initialSelectionValue); + Assert.assertTrue(true); + } + + @Test(groups = { "Functional" }) + public void showMessageDialogFamilyTest() + { + JvOptionPane.showMessageDialog(parentComponent, message); + JvOptionPane.showMessageDialog(parentComponent, message, title, + messageType); + JvOptionPane.showMessageDialog(parentComponent, message, title, + messageType, icon); + Assert.assertTrue(true); + } + + @Test(groups = { "Functional" }) + public void showInternalMessageDialogFamilyTest() + { + JvOptionPane.showInternalMessageDialog(parentComponent, message); + JvOptionPane.showInternalMessageDialog(parentComponent, message, title, + messageType); + JvOptionPane.showInternalMessageDialog(parentComponent, message, title, + messageType, icon); + Assert.assertTrue(true); + } + + @Test(groups = { "Functional" }) + public void showInternalConfirmDialogFamilyTest() + { + JvOptionPane.showInternalConfirmDialog(parentComponent, message, title, + optionType); + JvOptionPane.showInternalConfirmDialog(parentComponent, message, title, + optionType, messageType); + + JvOptionPane.showInternalConfirmDialog(getDummyDesktopPane(), message); + + JvOptionPane.showInternalConfirmDialog(getDummyDesktopPane(), message, + title, optionType, messageType, icon); + JvOptionPane.showInternalInputDialog(getDummyDesktopPane(), message); + JvOptionPane.showInternalInputDialog(getDummyDesktopPane(), message, + title, messageType); + JvOptionPane.showInternalInputDialog(getDummyDesktopPane(), message, + title, messageType, icon, selectionValues, + initialSelectionValue); + Assert.assertTrue(true); + + } + + private JDesktopPane getDummyDesktopPane() + { + JFrame frame = new JFrame("Dummy JDesktopPane"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + @SuppressWarnings("serial") + JDesktopPane jdpDesktop = new JDesktopPane() + { + @Override + public Dimension getPreferredSize() + { + return new Dimension(400, 300); + } + }; + frame.setContentPane(jdpDesktop); + JPanel panel = new JPanel(); + panel.setBounds(0, 0, 400, 300); + jdpDesktop.add(panel); + frame.pack(); + frame.setVisible(true); + panel.setVisible(true); + return jdpDesktop; + } +}