| 1 | package tmcsim.simulationmanager; |
|---|
| 2 | |
|---|
| 3 | import java.awt.Component; |
|---|
| 4 | import java.io.File; |
|---|
| 5 | import java.util.Arrays; |
|---|
| 6 | import javax.swing.JButton; |
|---|
| 7 | import static junit.framework.Assert.assertEquals; |
|---|
| 8 | import static junit.framework.Assert.assertFalse; |
|---|
| 9 | import static junit.framework.Assert.assertTrue; |
|---|
| 10 | import static junit.framework.Assert.fail; |
|---|
| 11 | import org.uispec4j.Panel; |
|---|
| 12 | import org.uispec4j.TextBox; |
|---|
| 13 | import org.uispec4j.Trigger; |
|---|
| 14 | import org.uispec4j.UISpecTestCase; |
|---|
| 15 | import org.uispec4j.Window; |
|---|
| 16 | import org.uispec4j.interception.WindowInterceptor; |
|---|
| 17 | import tmcsim.cadsimulator.CADServer; |
|---|
| 18 | import tmcsim.common.ScriptException; |
|---|
| 19 | import tmcsim.common.SimulationException; |
|---|
| 20 | import static tmcsim.simulationmanager.SimulationManager.SCENARIOS_DIR; |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * Smoke Test of system. Start the CAD server, Start the Sim Mgr, see if the Sim |
|---|
| 24 | * Mgr view shows "No Script". |
|---|
| 25 | * |
|---|
| 26 | * @author jdalbey |
|---|
| 27 | */ |
|---|
| 28 | public class SimulationManagerSmokeTest extends UISpecTestCase |
|---|
| 29 | { |
|---|
| 30 | |
|---|
| 31 | SimulationManager simMgrApp; |
|---|
| 32 | CADServer engine; |
|---|
| 33 | |
|---|
| 34 | public SimulationManagerSmokeTest(String testName) |
|---|
| 35 | { |
|---|
| 36 | super(testName); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * Call constructors for both classes |
|---|
| 41 | */ |
|---|
| 42 | public void testBothGUIs() throws ScriptException, SimulationException, InterruptedException |
|---|
| 43 | { |
|---|
| 44 | System.out.println("Smoke Test Sim Mgr & CADSimulator"); |
|---|
| 45 | |
|---|
| 46 | Window cadwindow = null; |
|---|
| 47 | System.setProperty("CAD_SIM_PROPERTIES", "config/cad_server.properties"); |
|---|
| 48 | if (System.getProperty("CAD_SIM_PROPERTIES") != null) |
|---|
| 49 | { |
|---|
| 50 | cadwindow = WindowInterceptor.run(new Trigger() |
|---|
| 51 | { |
|---|
| 52 | public void run() |
|---|
| 53 | { |
|---|
| 54 | try |
|---|
| 55 | { |
|---|
| 56 | engine = new CADServer(System.getProperty("CAD_SIM_PROPERTIES")); |
|---|
| 57 | } catch (Exception e) |
|---|
| 58 | { |
|---|
| 59 | e.printStackTrace(); |
|---|
| 60 | fail("Couldn't launch CADSimulator"); |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | }); |
|---|
| 64 | } |
|---|
| 65 | else |
|---|
| 66 | { |
|---|
| 67 | fail("CAD_SIM_PROPERTIES system property not defined."); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | // Check CAD Simulator appears with no script and nothing connected |
|---|
| 71 | assertTrue("Window title incorrect", cadwindow.getTitle().startsWith("CAD Server")); |
|---|
| 72 | Panel mainPanel = cadwindow.getPanel("contentPane"); |
|---|
| 73 | TextBox txtStatus = mainPanel.getTextBox("simulationStatus"); |
|---|
| 74 | assertEquals("No Script", txtStatus.getText()); |
|---|
| 75 | TextBox terms = mainPanel.getTextBox("termConnectedTF"); |
|---|
| 76 | assertEquals("0", terms.getText().trim()); |
|---|
| 77 | assertEquals("No", mainPanel.getTextBox("managerConnectedTF").getText().trim()); |
|---|
| 78 | |
|---|
| 79 | Window simMgrWindow = null; |
|---|
| 80 | System.setProperty("SIM_MGR_PROPERTIES", "config/sim_manager.properties"); |
|---|
| 81 | if (System.getProperty("SIM_MGR_PROPERTIES") != null) |
|---|
| 82 | { |
|---|
| 83 | simMgrWindow = WindowInterceptor.run(new Trigger() |
|---|
| 84 | { |
|---|
| 85 | public void run() |
|---|
| 86 | { |
|---|
| 87 | try |
|---|
| 88 | { |
|---|
| 89 | simMgrApp = new SimulationManager(System.getProperty("SIM_MGR_PROPERTIES")); |
|---|
| 90 | } catch (Exception ex) |
|---|
| 91 | { |
|---|
| 92 | fail("Couldn't launch Simulation Manager"); |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | }); |
|---|
| 96 | } |
|---|
| 97 | else |
|---|
| 98 | { |
|---|
| 99 | fail("SIM_MGR_PROPERTIES system property not defined."); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | // Check that the Sim Mgr GUI appears without a script loaded yet |
|---|
| 103 | SimulationManagerView view = simMgrApp.theSimManagerView; |
|---|
| 104 | assertFalse(view.isSimulationStarted()); |
|---|
| 105 | Window win = new Window(view); |
|---|
| 106 | Panel contentPanel = win.getPanel("contentPane"); |
|---|
| 107 | TextBox txtSimStatus = contentPanel.getTextBox("simulationStatusText"); |
|---|
| 108 | assertEquals("No Script", txtSimStatus.getText()); |
|---|
| 109 | |
|---|
| 110 | assertEquals("Yes", mainPanel.getTextBox("managerConnectedTF").getText().trim()); |
|---|
| 111 | |
|---|
| 112 | // Load a script file |
|---|
| 113 | String autoloadScriptname = SCENARIOS_DIR+"/practice_script_2016.xml"; |
|---|
| 114 | SimulationManagerModel simMgrModel = simMgrApp.theSimManagerModel; |
|---|
| 115 | simMgrModel.loadScript(new File(autoloadScriptname)); |
|---|
| 116 | // The status should now say Ready |
|---|
| 117 | assertEquals("Ready", txtSimStatus.getText()); |
|---|
| 118 | |
|---|
| 119 | // Click "Start" |
|---|
| 120 | win.getButton("Start").click(); |
|---|
| 121 | Thread.sleep(200); |
|---|
| 122 | assertEquals("Running", txtSimStatus.getText()); |
|---|
| 123 | |
|---|
| 124 | java.util.ArrayList<String> expectedButtons = new java.util.ArrayList<String>(Arrays.asList("Load Script","Start","Pause","Reset","Connect to Paramics", |
|---|
| 125 | "Load Network","Reschedule","Trigger","Delete","Add New Incident","Divert Traffic")); |
|---|
| 126 | // Check for appearance of buttons in the GUI |
|---|
| 127 | win.getButton("Load Script"); |
|---|
| 128 | win.getButton("Reset"); |
|---|
| 129 | win.getButton("Add New Incident"); |
|---|
| 130 | win.getButton("Reschedule"); |
|---|
| 131 | win.getButton("Trigger"); |
|---|
| 132 | win.getButton("Delete"); |
|---|
| 133 | //Find all available buttons - obsolete, replaced by stmts above |
|---|
| 134 | // Component[] buttons = win.getSwingComponents(JButton.class); |
|---|
| 135 | // java.util.List<Component> actualButtons = Arrays.asList(buttons); |
|---|
| 136 | // for (Component actualBtn: actualButtons) |
|---|
| 137 | // { |
|---|
| 138 | // if (actualBtn instanceof JButton) |
|---|
| 139 | // { |
|---|
| 140 | // String btnName = ((JButton) actualBtn).getText(); |
|---|
| 141 | // if (btnName.length()>0) |
|---|
| 142 | // { |
|---|
| 143 | // if (expectedButtons.contains(btnName)) |
|---|
| 144 | // { |
|---|
| 145 | // expectedButtons.remove(btnName); |
|---|
| 146 | // } |
|---|
| 147 | // else |
|---|
| 148 | // { |
|---|
| 149 | // fail("GUI has an unexpected button: "+btnName); |
|---|
| 150 | // } |
|---|
| 151 | // } |
|---|
| 152 | // } |
|---|
| 153 | // } |
|---|
| 154 | // if (expectedButtons.size() > 0) |
|---|
| 155 | // { |
|---|
| 156 | // fail("GUI is missing a button: "+expectedButtons); |
|---|
| 157 | // } |
|---|
| 158 | |
|---|
| 159 | // Pause the simulation |
|---|
| 160 | win.getButton("Pause").click(); |
|---|
| 161 | Thread.sleep(200); |
|---|
| 162 | win.getButton("Resume"); // resume should appear when paused |
|---|
| 163 | // Quit |
|---|
| 164 | engine = null; |
|---|
| 165 | win.getMenuBar().getMenu("File").getSubMenu("Exit").click(); |
|---|
| 166 | //simMgrApp = null; |
|---|
| 167 | } |
|---|
| 168 | } |
|---|