Files
JIMRI/java/test/jmri/jmrit/beantable/OBlockTableActionTest.java
2026-06-17 14:00:51 +02:00

292 lines
12 KiB
Java

package jmri.jmrit.beantable;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import javax.swing.*;
import jmri.Block;
import jmri.InstanceManager;
import jmri.SignalMastManager;
import jmri.implementation.VirtualSignalMast;
import jmri.jmrit.logix.*;
import jmri.util.JUnitUtil;
import jmri.util.JmriJFrame;
import jmri.util.ThreadingUtil;
import jmri.util.gui.GuiLafPreferencesManager;
import jmri.util.junit.annotations.DisabledIfHeadless;
import jmri.util.swing.JemmyUtil;
import org.junit.jupiter.api.*;
import org.netbeans.jemmy.QueueTool;
import org.netbeans.jemmy.operators.*;
/**
* Swing tests for the OBlock etc tables.
* Includes many of the jmri.jmrit.beantable.ablock classes via Jemmy UI tests.
*
* @author Pete Cressman Copyright 2016
* @author Egbert Broerse Copyright 2020
*/
public class OBlockTableActionTest {
protected OBlockTableAction a = null;
@Test
public void testCreate() {
assertNotNull(a);
assertNull(a.f); // frame should be null until action invoked
}
@Test
public void testGetClassDescription() {
assertEquals("Occupancy Block Table", a.getClassDescription(), "OBlock Table Action class description");
}
/**
* Check the return value of includeAddButton. The table generated by this
* action includes an Add Button.
*/
@Test
public void testIncludeAddButton() {
assertFalse(a.includeAddButton(), "Default include add button");
}
@Test
@DisabledIfHeadless
public void testInvoke() {
// use original _desktop interface
InstanceManager.getDefault(GuiLafPreferencesManager.class).setOblockEditTabbed(false);
// ask for the window to open
a.actionPerformed(new java.awt.event.ActionEvent(a, 1, ""));
// Find new table window by name
JmriJFrame doc = JmriJFrame.getFrame(jmri.jmrit.beantable.oblock.Bundle.getMessage("TitleOBlocks"));
assertNotNull(doc, "Occupancy window");
new QueueTool().waitEmpty();
JDesktopPane dt = assertInstanceOf( JDesktopPane.class, doc.getContentPane());
JInternalFrame[] fob = dt.getAllFrames();
assertNotNull(fob, "OBlock window");
assertEquals(4, fob.length);
new QueueTool().waitEmpty();
// Ask to close add window
JUnitUtil.dispose(doc);
}
@Test
@DisabledIfHeadless
public void testInvokeTabbed() {
// use _tabbed interface
InstanceManager.getDefault(GuiLafPreferencesManager.class).setOblockEditTabbed(true);
// ask for the window to open
a.actionPerformed(new java.awt.event.ActionEvent(a, 1, ""));
// Find new table window by name
JmriJFrame doc = JmriJFrame.getFrame(Bundle.getMessage("TitleOBlocksTabbedFrame"));
assertNotNull(doc, "Occupancy Blocks tabbed window");
new QueueTool().waitEmpty();
// Ask to close add window
JUnitUtil.dispose(doc);
}
@Test
@DisabledIfHeadless
public void testAddOBlock() {
// use _tabbed interface
InstanceManager.getDefault(GuiLafPreferencesManager.class).setOblockEditTabbed(true);
a.actionPerformed(new java.awt.event.ActionEvent(a, 2, "")); // show table
JFrame f = JFrameOperator.waitJFrame(Bundle.getMessage("TitleOBlocksTabbedFrame"), true, true);
assertNotNull(f);
ThreadingUtil.runOnGUI( () -> a.addOBlockPressed(null));
JFrameOperator addFrame = new JFrameOperator(Bundle.getMessage("TitleAddOBlock")); // NOI18N
assertNotNull(addFrame, "Found Add OBlock Frame"); // NOI18N
new JTextFieldOperator(addFrame, 0).setText("105"); // NOI18N
new JTextFieldOperator(addFrame, 2).setText("OBlock 105"); // NOI18N
new JButtonOperator(addFrame, Bundle.getMessage("ButtonCreate")).push(); // NOI18N
new JButtonOperator(addFrame, Bundle.getMessage("ButtonCancel")).push(); // NOI18N
Block chk105 = jmri.InstanceManager.getDefault(jmri.jmrit.logix.OBlockManager.class).getOBlock("OBlock 105"); // NOI18N
assertNotNull(chk105, "Verify OB105 Added"); // NOI18N
assertEquals("OB105", chk105.getSystemName(), "Verify system name prefix"); // NOI18N
(new JFrameOperator(f)).requestClose();
JUnitUtil.dispose(f);
}
@Test
@DisabledIfHeadless
public void testRenameOBlock() {
// use _tabbed interface
InstanceManager.getDefault(GuiLafPreferencesManager.class).setOblockEditTabbed(true);
OBlockManager obm = InstanceManager.getDefault(OBlockManager.class);
OBlock ob = obm.createNewOBlock("OB999", "OBlock Name"); // NOI18N
Assertions.assertNotNull(ob);
assertEquals("OBlock Name", ob.getUserName()); // NOI18N
OPath path = new OPath(ob, "Path");
// Open the OBlock table
a.actionPerformed(null); // show table
JFrameOperator jfo = new JFrameOperator(Bundle.getMessage("TitleOBlocksTabbedFrame")); // NOI18N
assertNotNull(jfo);
JTabbedPaneOperator tbp = new JTabbedPaneOperator(jfo);
tbp.selectPage("Occupancy Blocks");
assertNotNull(tbp);
JTableOperator tbo = new JTableOperator(tbp);
assertNotNull(tbo);
// Click on the edit button, set the user name to empty for remove
tbo.clickOnCell(0, 5);
JFrameOperator jfoEdit = new JFrameOperator("Edit Occupancy Block OBlock Name"); // NOI18N
JTextFieldOperator jtxt = new JTextFieldOperator(jfoEdit, 0);
jtxt.clickMouse();
jtxt.setText("");
// Prepare the dialog thread and click on OK //
Thread remove = JemmyUtil.createModalDialogOperatorThread("Update usage to system name", Bundle.getMessage("ButtonYes")); // NOI18N
new JButtonOperator(jfoEdit, "OK").doClick(); // NOI18N
JUnitUtil.waitFor(() -> !(remove.isAlive()), "remove finished"); // NOI18N
tbo.clickOnCell(0, 0); // deselect the edit button
// Click on the edit button, set the user name to a new value
tbo.clickOnCell(0, 5);
jfoEdit = new JFrameOperator("Edit Occupancy Block OB999"); // NOI18N
jtxt = new JTextFieldOperator(jfoEdit, 0);
jtxt.clickMouse();
jtxt.setText("New OBlock Name"); // NOI18N
// Prepare the dialog thread and click on OK
//Thread rename = JemmyUtil.createModalDialogOperatorThread("Update usage to system name", Bundle.getMessage("ButtonYes")); // NOI18N
new JButtonOperator(jfoEdit, "OK").doClick(); // NOI18N
//JUnitUtil.waitFor(() -> !(rename.isAlive()), "rename finished"); // NOI18N
tbo.clickOnCell(0, 0); // deselect the edit button
// Confirm the portal toBlock user name change
assertEquals("New OBlock Name", path.getBlock().getUserName());
JUnitUtil.dispose(jfo.getWindow());
jfo.waitClosed();
}
@Test
@DisabledIfHeadless
public void testAddPortal() {
// use _tabbed interface
InstanceManager.getDefault(GuiLafPreferencesManager.class).setOblockEditTabbed(true);
a.actionPerformed(new java.awt.event.ActionEvent(a, 2, "")); // show table
JFrame f = JFrameOperator.waitJFrame(Bundle.getMessage("TitleOBlocksTabbedFrame"), true, true);
assertNotNull(f);
a.addPortalPressed(null);
JFrameOperator addFrame = new JFrameOperator(Bundle.getMessage("TitleAddPortal")); // NOI18N
assertNotNull(addFrame, "Found Add Portal Frame"); // NOI18N
new JButtonOperator(addFrame, Bundle.getMessage("ButtonOK")).push(); // NOI18N
assertNotNull(addFrame, "Add Portal Frame still open after empty entry"); // NOI18N
PortalManager pm = InstanceManager.getDefault(PortalManager.class);
OBlockManager obm = InstanceManager.getDefault(OBlockManager.class);
obm.createNewOBlock("OB1", "ob1"); // NOI18N
obm.createNewOBlock("OB2", "ob2"); // NOI18N
new JTextFieldOperator(addFrame, 0).setText("portal1"); // NOI18N
new JComboBoxOperator(addFrame, 0).setSelectedIndex(1); // NOI18N
new JComboBoxOperator(addFrame, 1).setSelectedIndex(2); // NOI18N
new JButtonOperator(addFrame, Bundle.getMessage("ButtonOK")).push(); // NOI18N
jmri.util.JUnitAppender.assertWarnMessage("Portal portal1 needs an OBlock on each side"); // TODO prevent this from showing up when oblocks are defined
assertEquals(1, pm.getPortalCount(), "Verify Portal Added");
// TODO add Edit Portal test
(new JFrameOperator(f)).requestClose();
JUnitUtil.dispose(f);
}
@Test
@DisabledIfHeadless
public void testAddSignal() {
// use _tabbed interface
InstanceManager.getDefault(GuiLafPreferencesManager.class).setOblockEditTabbed(true);
a.actionPerformed(new java.awt.event.ActionEvent(a, 2, "")); // show table
JFrame f = JFrameOperator.waitJFrame(Bundle.getMessage("TitleOBlocksTabbedFrame"), true, true);
assertNotNull(f);
OBlockManager obm = InstanceManager.getDefault(OBlockManager.class);
OBlock ob1 = obm.createNewOBlock("OB1", "ob1"); // NOI18N
OBlock ob2 = obm.createNewOBlock("OB2", "ob2"); // NOI18N
PortalManager pm = InstanceManager.getDefault(PortalManager.class);
Portal p = pm.getPortal("IP1");
if (p != null) { // make sure it is not already there
p.dispose();
}
Portal port1 = pm.createNewPortal("IP1"); // NOI18N
port1.setFromBlock(ob1, false);
port1.setToBlock(ob2, false);
SignalMastManager l = InstanceManager.getDefault(SignalMastManager.class);
VirtualSignalMast sm1 = (VirtualSignalMast) l.provideSignalMast("IF$vsm:basic:one-searchlight($1)");
a.addSignalPressed(null);
jmri.util.JUnitAppender.assertWarnMessage("Portal IP1 needs an OBlock on each side");
JFrameOperator addFrame = new JFrameOperator(Bundle.getMessage("TitleAddSignal")); // NOI18N
assertNotNull(addFrame, "Found Add Signal Frame"); // NOI18N
new JButtonOperator(addFrame, Bundle.getMessage("ButtonOK")).push(); // NOI18N
assertNotNull(addFrame, "Add Signal Frame still open after empty entry"); // NOI18N
new JComboBoxOperator(addFrame, 0).setSelectedItem(sm1); // NOI18N select sm1
new JComboBoxOperator(addFrame, 2).setSelectedIndex(1); // NOI18N select port1
new JButtonOperator(addFrame, "Flip OBlocks").push(); // NOI18N
new JTextFieldOperator(addFrame, 0).setText("19.05"); // NOI18N
new JButtonOperator(addFrame, Bundle.getMessage("ButtonOK")).push(); // NOI18N
assertNotNull(port1.getToSignal(), "Verify Signal Added"); // NOI18N
assertEquals("IF$vsm:basic:one-searchlight($1)", port1.getToSignal().getSystemName(), "Check signal name"); // NOI18N
// TODO add Edit Signal test
(new JFrameOperator(f)).requestClose();
JUnitUtil.dispose(f);
}
@BeforeEach
public void setUp() {
JUnitUtil.setUp();
JUnitUtil.resetInstanceManager();
JUnitUtil.resetProfileManager();
JUnitUtil.initDefaultUserMessagePreferences();
InstanceManager.setDefault(jmri.jmrit.logix.OBlockManager.class, new jmri.jmrit.logix.OBlockManager());
a = new OBlockTableAction();
}
@AfterEach
public void tearDown() {
a = null;
JUnitUtil.deregisterBlockManagerShutdownTask(); // perhaps invoked via super, must be cleaned up
JUnitUtil.tearDown();
}
}