package jmri.jmrit.beantable; import jmri.util.gui.GuiLafPreferencesManager; import javax.annotation.Nonnull; import jmri.InstanceManager; import jmri.Turnout; import jmri.TurnoutManager; import jmri.jmrit.beantable.turnout.TurnoutTableDataModel; import jmri.jmrix.internal.InternalSystemConnectionMemo; import jmri.jmrix.internal.InternalTurnoutManager; import jmri.swing.ManagerComboBox; import jmri.util.ThreadingUtil; import jmri.util.JUnitUtil; import jmri.util.swing.JemmyUtil; import org.junit.Assert; import org.junit.jupiter.api.*; import org.junit.jupiter.api.condition.DisabledIfSystemProperty; import org.netbeans.jemmy.operators.*; import org.netbeans.jemmy.util.NameComponentChooser; /** * Tests for the jmri.jmrit.beantable.TurnoutTableAction class. * * @author Paul Bender Copyright (C) 2017 */ public class TurnoutTableActionTest extends AbstractTableActionBase { @Test public void testCTor() { Assert.assertNotNull("exists", a); } @Override public String getTableFrameName() { return Bundle.getMessage("TitleTurnoutTable"); } @Override @Test public void testGetClassDescription() { Assert.assertEquals("Turnout Table Action class description", "Turnout Table", a.getClassDescription()); } /** * Check the return value of includeAddButton. *

* The table generated by this action includes an Add button. */ @Override @Test public void testIncludeAddButton() { Assert.assertTrue("Default include Add... button", a.includeAddButton()); } /** * Check Turnout Table GUI, menus and graphic state presentation. * * @since 4.7.4 */ @DisabledIfSystemProperty(named = "java.awt.headless", matches = "true") @Test public void testAddAndInvoke() { ThreadingUtil.runOnGUI(() -> { a.actionPerformed(null); // show table }); // create 2 turnouts and see if they exist Turnout it1 = InstanceManager.turnoutManagerInstance().provideTurnout("IT1"); Turnout it2 = InstanceManager.turnoutManagerInstance().provideTurnout("IT2"); it1.setCommandedState(Turnout.THROWN); it2.setCommandedState(Turnout.CLOSED); // set graphic state column display preference to false, read by createModel() InstanceManager.getDefault(GuiLafPreferencesManager.class).setGraphicTableState(false); TurnoutTableAction _tTable = new TurnoutTableAction(); Assert.assertNotNull("found TurnoutTable frame", _tTable); // prevent there are 2 menubars with the same name _tTable.dispose(); // set to true, use icons InstanceManager.getDefault(GuiLafPreferencesManager.class).setGraphicTableState(true); TurnoutTableAction _t1Table = new TurnoutTableAction(); Assert.assertNotNull("found TurnoutTable1 frame", _t1Table); JFrameOperator t1Frame = new JFrameOperator(Bundle.getMessage("TitleTurnoutTable")); // test Add pane ThreadingUtil.runOnGUI(() -> { _t1Table.addPressed(null); }); JFrameOperator af = new JFrameOperator(Bundle.getMessage("TitleAddTurnout")); Assert.assertNotNull("found Add frame", af); // close Add pane ThreadingUtil.runOnGUI(() -> { _t1Table.cancelPressed(null); }); af.waitClosed(); // more Turnout Add pane tests are in TurnoutTableWindowTest _t1Table.dispose(); JUnitUtil.dispose(t1Frame.getWindow()); t1Frame.waitClosed(); } @DisabledIfSystemProperty(named = "java.awt.headless", matches = "true") @Test public void testSpeedsMenu() { ThreadingUtil.runOnGUI(() -> a.actionPerformed(null) ); // Open Speed pane to test Speed menu, which displays a JmriJOptionPane // log.debug("Speed pane started at {}", java.time.LocalTime.now()); // debug JFrameOperator main = new JFrameOperator(Bundle.getMessage("TitleTurnoutTable")); // Use GUI menu to open Speeds pane: //This is a modal JmriJOptionPane, so create a thread to dismiss it. Thread t = new Thread(() -> { try { JemmyUtil.confirmJOptionPane(main, Bundle.getMessage("TurnoutGlobalSpeedMessageTitle"), "", "OK"); } catch (org.netbeans.jemmy.TimeoutExpiredException tee) { // we're waiting for this thread to finish in the main method, // so any exception here means we failed. Assert.fail("caught timeout exception while waiting for modal dialog" + tee.getMessage()); } }); t.setName("Default Speeds Dialog Close Thread"); t.start(); // pushMenuNoBlock is used, because dialog is modal JMenuBarOperator mainbar = new JMenuBarOperator(main); mainbar.pushMenu(Bundle.getMessage("SpeedsMenu")); // stops at top level JMenuOperator jmo = new JMenuOperator(mainbar, Bundle.getMessage("SpeedsMenu")); JPopupMenuOperator jpmo = new JPopupMenuOperator(jmo.getPopupMenu()); new JMenuItemOperator(jpmo, Bundle.getMessage("SpeedsMenuItemDefaults")).pushNoBlock(); // wait for the dismiss thread to finish JUnitUtil.waitFor(() -> { return !t.isAlive(); }, "Dismiss Default Speeds Thread finished"); // clean up JUnitUtil.dispose(main.getWindow()); main.waitClosed(); } @Override public String getAddFrameName() { return Bundle.getMessage("TitleAddTurnout"); } @DisabledIfSystemProperty(named = "java.awt.headless", matches = "true") @Test @Override public void testAddButton() { Assert.assertTrue(a.includeAddButton()); ThreadingUtil.runOnGUI(() -> { a.actionPerformed(null); // show table }); JFrameOperator f = new JFrameOperator(getTableFrameName()); // find the "Add... " button and press it. JemmyUtil.pressButton(f, Bundle.getMessage("ButtonAdd")); JFrameOperator f1 = new JFrameOperator(getAddFrameName()); JemmyUtil.pressButton(f1, Bundle.getMessage("ButtonClose")); // not sure why this is close in this frame. f1.waitClosed(); JUnitUtil.dispose(f.getWindow()); f.waitClosed(); } @DisabledIfSystemProperty(named = "java.awt.headless", matches = "true") @Test public void testAddFailureCreate() { InstanceManager.setDefault(TurnoutManager.class, new AlwaysExceptionCreateNewTurnout()); a = new TurnoutTableAction(); Assert.assertTrue(a.includeAddButton()); ThreadingUtil.runOnGUI(() -> a.actionPerformed(null) ); JFrameOperator f = new JFrameOperator(getTableFrameName()); // find the "Add... " button and press it. JemmyUtil.pressButton(f, Bundle.getMessage("ButtonAdd")); JFrameOperator f1 = new JFrameOperator(getAddFrameName()); JTextFieldOperator jtfo = new JTextFieldOperator(f1, new NameComponentChooser("hwAddressTextField")); Assert.assertNotNull("hwAddressTextField", jtfo); // set to "1" jtfo.setText("1"); Thread add1 = JemmyUtil.createModalDialogOperatorThread( Bundle.getMessage("ErrorBeanCreateFailed", "Turnout","IT1"), Bundle.getMessage("ButtonOK")); // NOI18N //and press create JemmyUtil.pressButton(f1, Bundle.getMessage("ButtonCreate")); f1.getQueueTool().waitEmpty(); JUnitUtil.waitFor(()-> !(add1.isAlive()), "dialog finished"); // NOI18N f1.getQueueTool().waitEmpty(); JemmyUtil.pressButton(f1, Bundle.getMessage("ButtonClose")); // not sure why this is close in this frame. f1.getQueueTool().waitEmpty(); f1.waitClosed(); JUnitUtil.dispose(f.getWindow()); f.waitClosed(); } private static class AlwaysExceptionCreateNewTurnout extends InternalTurnoutManager { protected AlwaysExceptionCreateNewTurnout() { super(InstanceManager.getDefault(InternalSystemConnectionMemo.class)); } /** {@inheritDoc} */ @Override @Nonnull protected Turnout createNewTurnout(@Nonnull String systemName, String userName) throws IllegalArgumentException { throw new IllegalArgumentException("createNewTurnout Exception Text"); } } @DisabledIfSystemProperty(named = "java.awt.headless", matches = "true") @Test @Override public void testEditButton() { Assert.assertTrue(a.includeAddButton()); ThreadingUtil.runOnGUI(() -> a.actionPerformed(null)); JFrameOperator jfo = new JFrameOperator(getTableFrameName()); // find the "Add... " button and press it. JemmyUtil.pressButton(jfo, Bundle.getMessage("ButtonAdd")); jfo.getQueueTool().waitEmpty(); JFrameOperator f1 = new JFrameOperator(getAddFrameName()); //Enter 1 in the text field labeled "Hardware address:" JTextFieldOperator hwAddressField = new JTextFieldOperator(f1, new NameComponentChooser("hwAddressTextField")); Assert.assertNotNull("hwAddressTextField", hwAddressField); // set to "1" hwAddressField.typeText("1"); //and press create JemmyUtil.pressButton(f1, Bundle.getMessage("ButtonCreate")); f1.getQueueTool().waitEmpty(); JTableOperator tbl = new JTableOperator(jfo, 0); // find the "Edit" button and press it. This is in the table body. tbl.clickOnCell(0, TurnoutTableDataModel.EDITCOL); JFrameOperator f2 = new JFrameOperator(getEditFrameName()); JemmyUtil.pressButton(f2, Bundle.getMessage("ButtonCancel")); f2.waitClosed(); f1.requestClose(); f1.waitClosed(); JUnitUtil.dispose(jfo.getWindow()); jfo.waitClosed(); } @Test public void testConfigureManagerComboBox() { TurnoutManager j = new InternalTurnoutManager(new InternalSystemConnectionMemo("J", "Juliet")); InstanceManager.setTurnoutManager(j); ManagerComboBox box = new ManagerComboBox<>(); Assert.assertEquals("empty box", 0, box.getItemCount()); a.configureManagerComboBox(box, j, TurnoutManager.class); Assert.assertEquals("full box", 2, box.getItemCount()); Assert.assertEquals("selection", j, box.getSelectedItem()); } @Override public String getEditFrameName() { return "Edit Turnout IT1"; } @BeforeEach @Override public void setUp() { JUnitUtil.setUp(); JUnitUtil.resetProfileManager(); JUnitUtil.initInternalTurnoutManager(); JUnitUtil.initDefaultUserMessagePreferences(); helpTarget = "package.jmri.jmrit.beantable.TurnoutTable"; a = new TurnoutTableAction(); } @AfterEach @Override public void tearDown() { if ( a != null ) { a.dispose(); a = null; } JUnitUtil.tearDown(); } }