From 675807fd3608bee0df109abe93bfeff3d96a0235 Mon Sep 17 00:00:00 2001 From: Marcus Rockwell Date: Wed, 14 Aug 2024 15:48:38 -0400 Subject: [PATCH] Set up tests for JButtons --- .../simcontrol/SimControlApplication.java | 12 +- .../StubbedSimControlApplication.java | 188 +++++++++++++++++- .../simcontrol/StubbedSimControlTests.java | 152 ++++++++++++++ 3 files changed, 334 insertions(+), 18 deletions(-) create mode 100644 trick_source/java/src/test/java/trick/simcontrol/StubbedSimControlTests.java diff --git a/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java b/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java index 206cd524..66d250a2 100644 --- a/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java +++ b/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java @@ -107,6 +107,10 @@ public class SimControlApplication extends TrickApplication implements PropertyC //======================================== // Protected data //======================================== + protected static String host; + protected static int port = -1; + protected static boolean isRestartOptionOn; + protected static boolean isAutoExitOn; /** whether automatically exit when sim is done/killed. */ //======================================== @@ -118,9 +122,6 @@ public class SimControlApplication extends TrickApplication implements PropertyC private int overrun_present ; private int message_present ; private int message_port ; - - /** whether automatically exit when sim is done/killed. */ - private static boolean isAutoExitOn; // The panel that displays the current sim state description as well as progress. private JXTitledPanel runtimeStatePanel; @@ -170,9 +171,6 @@ public class SimControlApplication extends TrickApplication implements PropertyC private SocketChannel healthStatusSocketChannel ; private JComboBox runningSimList; - private static String host; - private static int port = -1; - private static boolean isRestartOptionOn; //True if an error was encountered during the attempt to connect to Variable Server during intialize() private boolean errOnInitConnect = false; //Time out when attempting to establish connection with Variable Server in milliseconds @@ -364,7 +362,7 @@ public class SimControlApplication extends TrickApplication implements PropertyC * Sets all actions as either enabled or disabled * @param isEnabled the state to set each action as */ - private void setEnabledAllActions(boolean isEnabled) { + protected void setEnabledAllActions(boolean isEnabled) { if(actionMap == null) return; diff --git a/trick_source/java/src/test/java/trick/simcontrol/StubbedSimControlApplication.java b/trick_source/java/src/test/java/trick/simcontrol/StubbedSimControlApplication.java index 7aa1129b..d2892718 100644 --- a/trick_source/java/src/test/java/trick/simcontrol/StubbedSimControlApplication.java +++ b/trick_source/java/src/test/java/trick/simcontrol/StubbedSimControlApplication.java @@ -5,17 +5,30 @@ import org.jdesktop.application.Application; import trick.simcontrol.utils.SimControlActionController; +import static trick.simcontrol.SimControlApplication.host; +import static trick.simcontrol.SimControlApplication.isAutoExitOn; +import static trick.simcontrol.SimControlApplication.isRestartOptionOn; +import static trick.simcontrol.SimControlApplication.port; + import java.awt.*; import java.awt.event.ActionEvent; +import java.io.File; +import java.util.Arrays; +import java.util.Scanner; import java.util.Stack; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.swing.Action; +import javax.swing.text.BadLocationException; +import javax.swing.text.Document; import javax.swing.AbstractAction; public class StubbedSimControlApplication extends SimControlApplication { - public Stack actions; + public static Stack ActionRecord; + private static StubbedSimControlApplication the_stub = null; public StubbedSimControlApplication() { this("", -1); @@ -24,14 +37,9 @@ public class StubbedSimControlApplication extends SimControlApplication { public StubbedSimControlApplication(String hostname, int portNum) { super.setHostPort(hostname, portNum); actionController = new SimControlActionController(); - actions = new Stack(); - } + ActionRecord = new Stack(); - @Override - protected Action getAction(String key) { - return new AbstractAction() { - public void actionPerformed(ActionEvent UNUSED) {} - }; + the_stub = this; } @Override @@ -41,9 +49,166 @@ public class StubbedSimControlApplication extends SimControlApplication { protected void startStatusMonitors() { /* UNUSED */} @Override - public void connect() { - actions.push(ActionID.CONNECT); + protected void ready() { + super.ready(); + setEnabledAllActions(true); // enable all actions } + + @Override + public void startSim() { + ActionRecord.push(ActionID.START); + } + + @Override + public void freezeSim() { + ActionRecord.push(ActionID.FREEZE); + } + + @Override + public void connect() { + ActionRecord.push(ActionID.CONNECT); + } + + @Override + public void showStatusFont() { + ActionRecord.push(ActionID.SET_FONT); + } + + @Override + public void saveStatusMsgs() { + ActionRecord.push(ActionID.SAVE_STATUS); + } + + @Override + public void clearStatusMsgs() { + ActionRecord.push(ActionID.CLEAR_STATUS); + } + + @Override + public void startTV() { + ActionRecord.push(ActionID.TV); + } + + @Override + public void startMTV() { + ActionRecord.push(ActionID.MTV); + } + + @Override + public void freezeAt() { + ActionRecord.push(ActionID.FREEZE_AT); + } + + @Override + public void freezeIn() { + ActionRecord.push(ActionID.FREEZE_IN); + } + + @Override + public void checkpointObjects() { + ActionRecord.push(ActionID.PART_CHKPNT); + } + + @Override + public void throttle() { + ActionRecord.push(ActionID.THROTTLE); + } + + @Override + public void stepSim() { + ActionRecord.push(ActionID.STEP); + } + + @Override + public void recordingSim() { + ActionRecord.push(ActionID.RECORDING); + } + + @Override + public void realtime() { + ActionRecord.push(ActionID.REALTIME); + } + + @Override + public void shutdownSim() { + ActionRecord.push(ActionID.SHUTDOWN); + } + + @Override + public void dumpChkpntASCII() { + ActionRecord.push(ActionID.DUMP_CHKPNT); + } + + @Override + public void loadChkpnt() { + ActionRecord.push(ActionID.LOAD_CHKPNT); + } + + @Override + public void lite() { + ActionRecord.push(ActionID.LITE); + } + + @Override + public void quit(ActionEvent e) { + ActionRecord.push(ActionID.EXIT); + // super.quit(e); + } + + public static StubbedSimControlApplication getInstance() { + return the_stub; + } + + /** + * Main method for this application. + * @param args command line arguments + */ + public static void main(String[] args) { + Application.launch(StubbedSimControlApplication.class, args); + + // Arrays.toString(args) converts such as localhost 7000 -r to [localhost, 7000, -r], + // so need to remove [, ] and all white spaces. + String commandLine = (Arrays.toString(args)).replace("[","").replace("]", "").replaceAll("\\s+", ""); + + // check to see if -r or -restart is used + Pattern restartOptionPattern = Pattern.compile("(\\-r|\\-restart)(,|$)"); + Matcher matcher = restartOptionPattern.matcher(commandLine); + + // if -r | -restart is used, set the flag and then remove it from the command line + if (matcher.find()) { + isRestartOptionOn = true; + commandLine = matcher.replaceAll(""); + } + + // check to see if -auto_exit is used + Pattern autoExitOptionPattern = Pattern.compile("(\\-auto\\_exit)(,|$)"); + Matcher autoExitMatcher = autoExitOptionPattern.matcher(commandLine); + + if (autoExitMatcher.find()) { + isAutoExitOn = true; + commandLine = autoExitMatcher.replaceAll(""); + } + + Scanner commandScanner = new Scanner(commandLine).useDelimiter(","); + // now need to figure out host and port, if not specified, available host&port will be listed + if (commandScanner.hasNextInt()) { + port = commandScanner.nextInt(); + if (commandScanner.hasNext()) { + host = commandScanner.next(); + } + } else { + if (commandScanner.hasNext()) { + host = commandScanner.next(); + if (commandScanner.hasNextInt()) { + port = commandScanner.nextInt(); + } + } + } + + if (commandScanner != null) { + commandScanner.close(); + } + } } enum ActionID { @@ -65,5 +230,6 @@ enum ActionID { TV, SAVE_STATUS, CLEAR_STATUS, - SET_FONT + SET_FONT, + EXIT } \ No newline at end of file diff --git a/trick_source/java/src/test/java/trick/simcontrol/StubbedSimControlTests.java b/trick_source/java/src/test/java/trick/simcontrol/StubbedSimControlTests.java new file mode 100644 index 00000000..1226b710 --- /dev/null +++ b/trick_source/java/src/test/java/trick/simcontrol/StubbedSimControlTests.java @@ -0,0 +1,152 @@ +package trick.simcontrol; + +import trick.simcontrol.SimControlApplication; +import trick.simcontrol.StubbedSimControlApplication; + +import java.awt.Frame; + +import javax.swing.JButton; + +import org.assertj.swing.core.GenericTypeMatcher; +import org.assertj.swing.exception.ComponentLookupException; +import org.assertj.swing.fixture.ComponentContainerFixture; +import org.assertj.swing.fixture.MouseInputSimulationFixture; +import org.assertj.swing.fixture.JButtonFixture; +import org.assertj.swing.fixture.FrameFixture; +import org.assertj.swing.junit.testcase.AssertJSwingJUnitTestCase; + +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assumptions.assumeThat; +import static org.assertj.swing.finder.WindowFinder.findFrame; +import static org.assertj.swing.launcher.ApplicationLauncher.application; + +public class StubbedSimControlTests extends AssertJSwingJUnitTestCase { + private FrameFixture mainFrame; + private StubbedSimControlApplication app = null; + + @BeforeClass + public static void onSetUpBeforeClass() { + + } + + @Override + protected void onSetUp() { + application(StubbedSimControlApplication.class).start(); + app = StubbedSimControlApplication.getInstance(); + mainFrame = getFrameByTitle("Sim Control"); + + } + + //-------------------- + // JButton Tests + //-------------------- + + @Test + public void testConnectButton() { + testJButton("Connect", ActionID.CONNECT); + } + + @Test + public void testStartButton() { + testJButton("Start", ActionID.START); + } + + @Test + public void testFreezeButton() { + testJButton("Freeze", ActionID.FREEZE); + } + + @Test + public void testStepButton() { + testJButton("Step", ActionID.STEP); + } + + @Test + public void testShutdownButton() { + testJButton("Shutdown", ActionID.SHUTDOWN); + } + + @Test + public void testExitButton() { + testJButton("Exit", ActionID.EXIT); + } + + //-------------------- + // JToggleButton Tests + //-------------------- + + // @Test + // public void testLiteButton() { + // testJButton("Lite", ActionID.LITE); + // } + + // @Test + // public void testDumpChkpntButton() { + // testJButton("Dump Chkpnt", ActionID.DUMP_CHKPNT); + // } + + // @Test + // public void testLoadChkpntButton() { + // testJButton("Load Chkpnt", ActionID.LOAD_CHKPNT); + // } + + private void testJButton(String text, ActionID action) { + JButtonFixture button = getButtonByText(text); + assumeThat(button).withFailMessage("Button with text\"%s\" not found\n", text) + .isNotNull(); + + testConnectedAction(button, action); + } + + private void testConnectedAction(MouseInputSimulationFixture clickable, ActionID expected) { + // ARRANGE + ActionID actual; + int initialLogSize, logSize; + + // ACT + initialLogSize = StubbedSimControlApplication.ActionRecord.size(); + clickable.click(); + + assumeThat(StubbedSimControlApplication.ActionRecord.size() > 0).isTrue(); + actual = StubbedSimControlApplication.ActionRecord.peek(); + logSize = StubbedSimControlApplication.ActionRecord.size(); + + // ASSERT + assertThat(actual).isEqualTo(expected); + assertThat(logSize).isEqualTo(initialLogSize + 1); + } + + protected FrameFixture getFrameByTitle(String title) { + FrameFixture frame = findFrame(new GenericTypeMatcher(Frame.class) { + protected boolean isMatching(Frame frame) { + return title.equals(frame.getTitle()) && frame.isShowing(); + } + }).using(robot()); + + return frame; + } + + protected JButtonFixture getButtonByText(ComponentContainerFixture container, String text) { + JButtonFixture button; + + try { + button = container.button(new GenericTypeMatcher(JButton.class) { + @Override + protected boolean isMatching(JButton button) { + return text.equals(button.getText()); + } + }); + } catch (ComponentLookupException e) { + return null; + } + + return button; + } + + protected JButtonFixture getButtonByText(String text) { + return getButtonByText(mainFrame, text); + } +}