From 158c1787f2a13ff60bbf881b452dfd63c99444d9 Mon Sep 17 00:00:00 2001 From: Marcus Rockwell Date: Thu, 15 Aug 2024 17:40:14 -0400 Subject: [PATCH] Fixed tests for Checkpoint buttons --- .../StubbedSimControlApplication.java | 1 + .../simcontrol/StubbedSimControlTests.java | 81 ++++++++++++++++--- 2 files changed, 72 insertions(+), 10 deletions(-) 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 d2892718..f7f4d4f0 100644 --- a/trick_source/java/src/test/java/trick/simcontrol/StubbedSimControlApplication.java +++ b/trick_source/java/src/test/java/trick/simcontrol/StubbedSimControlApplication.java @@ -147,6 +147,7 @@ public class StubbedSimControlApplication extends SimControlApplication { @Override public void lite() { ActionRecord.push(ActionID.LITE); + super.lite(); } @Override diff --git a/trick_source/java/src/test/java/trick/simcontrol/StubbedSimControlTests.java b/trick_source/java/src/test/java/trick/simcontrol/StubbedSimControlTests.java index 1226b710..8fa7eee1 100644 --- a/trick_source/java/src/test/java/trick/simcontrol/StubbedSimControlTests.java +++ b/trick_source/java/src/test/java/trick/simcontrol/StubbedSimControlTests.java @@ -6,12 +6,14 @@ import trick.simcontrol.StubbedSimControlApplication; import java.awt.Frame; import javax.swing.JButton; +import javax.swing.JToggleButton; 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.JToggleButtonFixture; import org.assertj.swing.fixture.FrameFixture; import org.assertj.swing.junit.testcase.AssertJSwingJUnitTestCase; @@ -38,6 +40,8 @@ public class StubbedSimControlTests extends AssertJSwingJUnitTestCase { app = StubbedSimControlApplication.getInstance(); mainFrame = getFrameByTitle("Sim Control"); + sleep(1000); + updateState(); } //-------------------- @@ -80,18 +84,22 @@ public class StubbedSimControlTests extends AssertJSwingJUnitTestCase { // @Test // public void testLiteButton() { - // testJButton("Lite", ActionID.LITE); + // testJToggleButton("Lite", ActionID.LITE); // } - // @Test - // public void testDumpChkpntButton() { - // testJButton("Dump Chkpnt", ActionID.DUMP_CHKPNT); - // } + @Test + public void testDumpChkpntButton() { + testJToggleButton("Dump Chkpnt", ActionID.DUMP_CHKPNT); + } - // @Test - // public void testLoadChkpntButton() { - // testJButton("Load Chkpnt", ActionID.LOAD_CHKPNT); - // } + @Test + public void testLoadChkpntButton() { + testJToggleButton("Load Chkpnt", ActionID.LOAD_CHKPNT); + } + + public static void sleep(long ms) { + try {Thread.sleep(ms);} catch(Exception ignored) {} + } private void testJButton(String text, ActionID action) { JButtonFixture button = getButtonByText(text); @@ -101,6 +109,36 @@ public class StubbedSimControlTests extends AssertJSwingJUnitTestCase { testConnectedAction(button, action); } + private void updateState() { + JToggleButtonFixture liteFixture, recordFixture, realFixture; + + liteFixture = getToggleButtonByText("Lite"); + if (liteFixture == null) + liteFixture = getToggleButtonByText("Full"); + + } + + private void testJToggleButton(String text, ActionID action) { testJToggleButton(text, action, text, action); } + + private void testJToggleButton(String initText, ActionID initAction, String toggleText, ActionID toggleAction) { + // ARRANGE + JToggleButtonFixture button = getToggleButtonByText(initText); + String buttonText, newButtonText; + assumeThat(button).withFailMessage("Toggle Button with text\"%s\" not found\n", initText) + .isNotNull(); + + // ACT + testConnectedAction(button, initAction); // Toggle On + newButtonText = button.target().getText(); + + testConnectedAction(button, toggleAction); // Toggle Off + buttonText = button.target().getText(); + + // ASSERT + assertThat(newButtonText).isEqualTo(toggleText); + assertThat(buttonText).isEqualTo(initText); + } + private void testConnectedAction(MouseInputSimulationFixture clickable, ActionID expected) { // ARRANGE ActionID actual; @@ -110,7 +148,9 @@ public class StubbedSimControlTests extends AssertJSwingJUnitTestCase { initialLogSize = StubbedSimControlApplication.ActionRecord.size(); clickable.click(); - assumeThat(StubbedSimControlApplication.ActionRecord.size() > 0).isTrue(); + assumeThat(StubbedSimControlApplication.ActionRecord.size() > 0) + .withFailMessage("\"%s\" Action ID Not Registered", expected.name()) + .isTrue(); actual = StubbedSimControlApplication.ActionRecord.peek(); logSize = StubbedSimControlApplication.ActionRecord.size(); @@ -146,6 +186,27 @@ public class StubbedSimControlTests extends AssertJSwingJUnitTestCase { return button; } + protected JToggleButtonFixture getToggleButtonByText(String text) { + return getToggleButtonByText(mainFrame, text); + } + + protected JToggleButtonFixture getToggleButtonByText(ComponentContainerFixture container, String text) { + JToggleButtonFixture button; + + try { + button = container.toggleButton(new GenericTypeMatcher(JToggleButton.class) { + @Override + protected boolean isMatching(JToggleButton button) { + return text.equals(button.getText()); + } + }); + } catch (ComponentLookupException e) { + return null; + } + + return button; + } + protected JButtonFixture getButtonByText(String text) { return getButtonByText(mainFrame, text); }