package tmcsim.simulationmanager; import java.awt.Component; import java.io.File; import java.util.Arrays; import javax.swing.JButton; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.fail; import org.uispec4j.Panel; import org.uispec4j.TextBox; import org.uispec4j.Trigger; import org.uispec4j.UISpecTestCase; import org.uispec4j.Window; import org.uispec4j.interception.WindowInterceptor; import tmcsim.cadsimulator.CADServer; import tmcsim.common.ScriptException; import tmcsim.common.SimulationException; import static tmcsim.simulationmanager.SimulationManager.SCENARIOS_DIR; /** * Smoke Test of system. Start the CAD server, Start the Sim Mgr, see if the Sim * Mgr view shows "No Script". * * @author jdalbey */ public class SimulationManagerSmokeTest extends UISpecTestCase { SimulationManager simMgrApp; CADServer engine; public SimulationManagerSmokeTest(String testName) { super(testName); } /** * Call constructors for both classes */ public void testBothGUIs() throws ScriptException, SimulationException, InterruptedException { System.out.println("Smoke Test Sim Mgr & CADSimulator"); Window cadwindow = null; System.setProperty("CAD_SIM_PROPERTIES", "config/cad_server.properties"); if (System.getProperty("CAD_SIM_PROPERTIES") != null) { cadwindow = WindowInterceptor.run(new Trigger() { public void run() { try { engine = new CADServer(System.getProperty("CAD_SIM_PROPERTIES")); } catch (Exception e) { e.printStackTrace(); fail("Couldn't launch CADSimulator"); } } }); } else { fail("CAD_SIM_PROPERTIES system property not defined."); } // Check CAD Simulator appears with no script and nothing connected assertTrue("Window title incorrect", cadwindow.getTitle().startsWith("CAD Server")); Panel mainPanel = cadwindow.getPanel("contentPane"); TextBox txtStatus = mainPanel.getTextBox("simulationStatus"); assertEquals("No Script", txtStatus.getText()); TextBox terms = mainPanel.getTextBox("termConnectedTF"); assertEquals("0", terms.getText().trim()); assertEquals("No", mainPanel.getTextBox("managerConnectedTF").getText().trim()); Window simMgrWindow = null; System.setProperty("SIM_MGR_PROPERTIES", "config/sim_manager.properties"); if (System.getProperty("SIM_MGR_PROPERTIES") != null) { simMgrWindow = WindowInterceptor.run(new Trigger() { public void run() { try { simMgrApp = new SimulationManager(System.getProperty("SIM_MGR_PROPERTIES")); } catch (Exception ex) { fail("Couldn't launch Simulation Manager"); } } }); } else { fail("SIM_MGR_PROPERTIES system property not defined."); } // Check that the Sim Mgr GUI appears without a script loaded yet SimulationManagerView view = simMgrApp.theSimManagerView; assertFalse(view.isSimulationStarted()); Window win = new Window(view); Panel contentPanel = win.getPanel("contentPane"); TextBox txtSimStatus = contentPanel.getTextBox("simulationStatusText"); assertEquals("No Script", txtSimStatus.getText()); assertEquals("Yes", mainPanel.getTextBox("managerConnectedTF").getText().trim()); // Load a script file String autoloadScriptname = SCENARIOS_DIR+"/practice_script_2016.xml"; SimulationManagerModel simMgrModel = simMgrApp.theSimManagerModel; simMgrModel.loadScript(new File(autoloadScriptname)); // The status should now say Ready assertEquals("Ready", txtSimStatus.getText()); // Click "Start" win.getButton("Start").click(); Thread.sleep(200); assertEquals("Running", txtSimStatus.getText()); java.util.ArrayList expectedButtons = new java.util.ArrayList(Arrays.asList("Load Script","Start","Pause","Reset","Connect to Paramics", "Load Network","Reschedule","Trigger","Delete","Add New Incident","Divert Traffic")); // Check for appearance of buttons in the GUI win.getButton("Load Script"); win.getButton("Reset"); win.getButton("Add New Incident"); win.getButton("Reschedule"); win.getButton("Trigger"); win.getButton("Delete"); //Find all available buttons - obsolete, replaced by stmts above // Component[] buttons = win.getSwingComponents(JButton.class); // java.util.List actualButtons = Arrays.asList(buttons); // for (Component actualBtn: actualButtons) // { // if (actualBtn instanceof JButton) // { // String btnName = ((JButton) actualBtn).getText(); // if (btnName.length()>0) // { // if (expectedButtons.contains(btnName)) // { // expectedButtons.remove(btnName); // } // else // { // fail("GUI has an unexpected button: "+btnName); // } // } // } // } // if (expectedButtons.size() > 0) // { // fail("GUI is missing a button: "+expectedButtons); // } // Pause the simulation win.getButton("Pause").click(); Thread.sleep(200); win.getButton("Resume"); // resume should appear when paused // Quit engine = null; win.getMenuBar().getMenu("File").getSubMenu("Exit").click(); //simMgrApp = null; } }