package jmri.jmrit.beantable; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import javax.swing.JFrame; import javax.swing.JTextField; import jmri.InstanceManager; import jmri.SignalGroup; import jmri.SignalHead; import jmri.Turnout; import jmri.util.JUnitUtil; import jmri.util.junit.annotations.*; import jmri.util.swing.JemmyUtil; import org.junit.jupiter.api.*; import org.netbeans.jemmy.operators.*; /** * Tests for the jmri.jmrit.beantable.SignalGroupTableAction class * * @author Egbert Broerse Copyright 2017 */ public class SignalGroupTableActionTest extends AbstractTableActionBase { @Test public void testCreate() { assertNotNull(a); } @Override public String getTableFrameName() { return Bundle.getMessage("TitleSignalGroupTable"); } @Override @Test public void testGetClassDescription() { assertEquals( "Signal Group Table", a.getClassDescription(), "Signal Group Table Action class description"); } /** * Check the return value of includeAddButton. The table generated by this * action includes an Add Button. */ @Override @Test public void testIncludeAddButton() { assertTrue( a.includeAddButton(), "Default include add button"); } @Test @DisabledIfHeadless public void testAdd() { var vsm = new jmri.implementation.VirtualSignalMast("IF$vsm:AAR-1946:CPL($0002)", "VM1"); assertNotNull(vsm); // create a Turnout Turnout it1 = InstanceManager.turnoutManagerInstance().provideTurnout("IT1"); // create a signal head SignalHead sh = new jmri.implementation.SingleTurnoutSignalHead("IH1", new jmri.NamedBeanHandle<>("IT1", it1), SignalHead.LUNAR, SignalHead.DARK); // on state + off state InstanceManager.getDefault(jmri.SignalHeadManager.class).register(sh); // open Signal Group Table SignalGroupTableAction _sGroupTable; _sGroupTable = new SignalGroupTableAction(); assertNotNull( _sGroupTable, "found SignalGroupTable frame"); _sGroupTable.addPressed(null); JFrame af = JFrameOperator.waitJFrame(Bundle.getMessage("AddSignalGroup"), true, true); assertNotNull( af, "found Add frame"); // create a new signal group _sGroupTable._userName.setText("TestGroup"); assertEquals( "TestGroup", _sGroupTable._userName.getText(), "user name"); _sGroupTable._systemName.setText("IF1"); assertEquals( "IF1", _sGroupTable._systemName.getText(), "system name"); _sGroupTable.mainSignalComboBox.setSelectedItemByName("VM1"); SignalGroup g = _sGroupTable.checkNamesOK(); _sGroupTable.setValidSignalMastAspects(); // add the head to the group: g.addSignalHead(sh); // NPE when bypassing the GUI to open an Edit Head pane: was fixed by registering sh in SignalHeadManager (line 63) // open Edit head pane SignalGroupSubTableAction editSignalHead = new SignalGroupSubTableAction(); editSignalHead.editHead(g, "IH1"); editSignalHead.cancelSubPressed(null); // close edit head pane _sGroupTable.cancelPressed(null); // calling updatePressed() complains about duplicate group name // clean up (new JFrameOperator(af)).requestClose(); JUnitUtil.dispose(af); g.dispose(); _sGroupTable.dispose(); sh.dispose(); JUnitUtil.dispose(editSignalHead.addSubFrame); } @Override public String getAddFrameName() { return "Add Signal Group"; } @Test @Override @DisabledIfHeadless public void testAddThroughDialog() { assertTrue(a.includeAddButton()); a.actionPerformed(null); JFrame f = JFrameOperator.waitJFrame(getTableFrameName(), true, true); // find the "Add... " button and press it. JemmyUtil.pressButton(new JFrameOperator(f), Bundle.getMessage("ButtonAdd")); new org.netbeans.jemmy.QueueTool().waitEmpty(); JFrame f1 = JFrameOperator.waitJFrame(getAddFrameName(), true, true); JFrameOperator jf = new JFrameOperator(f1); //Enter 1 in the text field labeled "System Name:" JLabelOperator jlo = new JLabelOperator(jf, Bundle.getMessage("LabelSystemName")); ((JTextField) jlo.getLabelFor()).setText("1"); //and press create // pops dialog No Signal Mast Present Thread t = JemmyUtil.createModalDialogOperatorThread("Error", "OK"); JemmyUtil.pressButton(jf, Bundle.getMessage("ButtonCreate")); JUnitUtil.waitThreadTerminated(t); JUnitUtil.dispose(f1); JUnitUtil.dispose(f); jf.waitClosed(); } @Test @Disabled("needs further setup") @ToDo("To Edit, the signal group needs a mast added to it") @Override @DisabledIfHeadless public void testEditButton() { assertTrue(a.includeAddButton()); a.actionPerformed(null); JFrame f = JFrameOperator.waitJFrame(getTableFrameName(), true, true); // find the "Add... " button and press it. jmri.util.swing.JemmyUtil.pressButton(new JFrameOperator(f), Bundle.getMessage("ButtonAdd")); JFrame f1 = JFrameOperator.waitJFrame(getAddFrameName(), true, true); JFrameOperator jf = new JFrameOperator(f1); //Enter 1 in the text field labeled "System Name:" JLabelOperator jlo = new JLabelOperator(jf, Bundle.getMessage("LabelSystemName")); ((JTextField) jlo.getLabelFor()).setText("1"); //and press create jmri.util.swing.JemmyUtil.pressButton(jf, Bundle.getMessage("ButtonCreate")); new org.netbeans.jemmy.QueueTool().waitEmpty(); // find the "Edit" button and press it. This may be in the table body. jmri.util.swing.JemmyUtil.pressButton(new JFrameOperator(f), Bundle.getMessage("ButtonEdit")); JFrame f2 = JFrameOperator.waitJFrame(getAddFrameName(), true, true); jmri.util.swing.JemmyUtil.pressButton(new JFrameOperator(f2), Bundle.getMessage("ButtonCancel")); JUnitUtil.dispose(f2); JUnitUtil.dispose(f1); JUnitUtil.dispose(f); } @BeforeEach @Override public void setUp() { JUnitUtil.setUp(); JUnitUtil.resetProfileManager(); JUnitUtil.initDefaultUserMessagePreferences(); JUnitUtil.initInternalSignalHeadManager(); helpTarget = "package.jmri.jmrit.beantable.SignalGroupTable"; a = new SignalGroupTableAction(); } @AfterEach @Override public void tearDown() { a = null; JUnitUtil.tearDown(); } }