| 1 | package tmcsim.cadsimulator; |
|---|
| 2 | |
|---|
| 3 | import java.io.File; |
|---|
| 4 | import java.rmi.RemoteException; |
|---|
| 5 | import java.util.logging.Level; |
|---|
| 6 | import java.util.logging.Logger; |
|---|
| 7 | import static junit.framework.Assert.assertEquals; |
|---|
| 8 | import static junit.framework.Assert.fail; |
|---|
| 9 | import static org.mockito.Mockito.*; |
|---|
| 10 | import org.uispec4j.*; |
|---|
| 11 | import org.uispec4j.interception.WindowInterceptor; |
|---|
| 12 | import tmcsim.cadsimulator.managers.ParamicsSimulationManager; |
|---|
| 13 | import tmcsim.common.CADEnums; |
|---|
| 14 | import tmcsim.common.ScriptException; |
|---|
| 15 | import tmcsim.common.SimulationException; |
|---|
| 16 | import tmcsim.interfaces.CADClientInterface; |
|---|
| 17 | import tmcsim.interfaces.SimulationManagerInterface; |
|---|
| 18 | |
|---|
| 19 | /** |
|---|
| 20 | * Test of CADSimulator GUI |
|---|
| 21 | * |
|---|
| 22 | * @author jdalbey |
|---|
| 23 | */ |
|---|
| 24 | public class CADSimulatorGUITest extends UISpecTestCase |
|---|
| 25 | { |
|---|
| 26 | |
|---|
| 27 | static final String configData = |
|---|
| 28 | "CADClientPort = 4444 \n" |
|---|
| 29 | + "CoordinatorRMIPort = 4445 \n" |
|---|
| 30 | + "CADRmiPort = 4446 \n" |
|---|
| 31 | + "UserInterface = tmcsim.cadsimulator.viewer.CADSimulatorViewer\n" |
|---|
| 32 | + "ParamicsProperties = pconfig.txt\n" |
|---|
| 33 | + "ATMSProperties = empty.txt\n" |
|---|
| 34 | + "MediaProperties = empty.txt\n"; |
|---|
| 35 | |
|---|
| 36 | public CADSimulatorGUITest(String testName) |
|---|
| 37 | { |
|---|
| 38 | super(testName); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | @Override |
|---|
| 42 | public void tearDown() throws java.io.IOException |
|---|
| 43 | { |
|---|
| 44 | File removeMe = new File("config.txt"); |
|---|
| 45 | removeMe.delete(); |
|---|
| 46 | removeMe = new File("pconfig.txt"); |
|---|
| 47 | removeMe.delete(); |
|---|
| 48 | removeMe = new File("empty.txt"); |
|---|
| 49 | removeMe.delete(); |
|---|
| 50 | } |
|---|
| 51 | /** |
|---|
| 52 | * Test of main method, of class CADSimulator. |
|---|
| 53 | */ |
|---|
| 54 | CADServer app; |
|---|
| 55 | |
|---|
| 56 | public void testConstructor() throws SimulationException, RemoteException, ScriptException |
|---|
| 57 | { |
|---|
| 58 | System.out.println("CADSimulator constructor"); |
|---|
| 59 | CADSimulatorFixture.writeConfigData(); |
|---|
| 60 | CADSimulatorFixture.writedata("config.txt", configData); |
|---|
| 61 | Window cadwindow = null; |
|---|
| 62 | // try |
|---|
| 63 | // { |
|---|
| 64 | // app = new CADSimulator("config.txt"); |
|---|
| 65 | // } catch (Exception e) |
|---|
| 66 | // { |
|---|
| 67 | // fail("Couldn't launch CADSimulator" + e.getMessage()); |
|---|
| 68 | // } |
|---|
| 69 | |
|---|
| 70 | System.setProperty("CONFIG_DIR", "config/"); |
|---|
| 71 | if (System.getProperty("CONFIG_DIR") != null) |
|---|
| 72 | { |
|---|
| 73 | cadwindow = WindowInterceptor.run(new Trigger() |
|---|
| 74 | { |
|---|
| 75 | public void run() |
|---|
| 76 | { |
|---|
| 77 | try |
|---|
| 78 | { |
|---|
| 79 | app = new CADServer(System.getProperty("CONFIG_DIR") + System.getProperty("file.separator") + "cad_simulator_config.properties"); |
|---|
| 80 | |
|---|
| 81 | } catch (Exception e) |
|---|
| 82 | { |
|---|
| 83 | fail("Couldn't launch CADSimulator" + e.getMessage()); |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | }); |
|---|
| 87 | } |
|---|
| 88 | else |
|---|
| 89 | { |
|---|
| 90 | fail("CONFIG_DIR system property not defined."); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | System.out.println("Tests for text fields."); |
|---|
| 94 | // Create a UISpec window from the CADSimulator's Viewer (gui) |
|---|
| 95 | // cadwindow = new Window((CADSimulatorViewer) app.theViewer); |
|---|
| 96 | assertTrue("Title bar incorrect", cadwindow.getTitle().startsWith("CAD Server revision:")); |
|---|
| 97 | Panel mainPanel = cadwindow.getPanel("contentPane"); |
|---|
| 98 | TextBox txtStatus = mainPanel.getTextBox("simulationStatus"); |
|---|
| 99 | assertEquals("simulation status should say No Script", "No Script", txtStatus.getText().trim()); |
|---|
| 100 | TextBox terminals = mainPanel.getTextBox("termConnectedTF"); |
|---|
| 101 | assertEquals("should be 0 terminals", "0", terminals.getText().trim()); |
|---|
| 102 | assertEquals("mgr connected should be no", "No", mainPanel.getTextBox("managerConnectedTF").getText().trim()); |
|---|
| 103 | assertEquals("paramics connected should be no", "No", mainPanel.getTextBox("paramicsConnectedTF").getText().trim()); |
|---|
| 104 | assertEquals("initial time should be 0:00:00", "0:00:00", mainPanel.getTextBox("simulationClockLabel").getText().trim()); |
|---|
| 105 | |
|---|
| 106 | CADClientInterface ci = mock(CADClientInterface.class); |
|---|
| 107 | app.theCoordinator.registerForCallback(ci); |
|---|
| 108 | assertEquals("should be 1 terminal", "1", terminals.getText().trim()); |
|---|
| 109 | app.theCoordinator.registerForCallback(ci); |
|---|
| 110 | assertEquals("should be 2 terminals", "2", terminals.getText().trim()); |
|---|
| 111 | |
|---|
| 112 | SimulationManagerInterface si = mock(SimulationManagerInterface.class); |
|---|
| 113 | |
|---|
| 114 | app.theCoordinator.registerForCallback(si); |
|---|
| 115 | assertEquals("mgr connected should be yes", "Yes", mainPanel.getTextBox("managerConnectedTF").getText().trim()); |
|---|
| 116 | |
|---|
| 117 | Logger cadSimLogger = Logger.getLogger("tmcsim.cadsimulator"); |
|---|
| 118 | cadSimLogger.logp(Level.INFO, "", "", "Sample Info Message."); |
|---|
| 119 | |
|---|
| 120 | System.out.println("Tests for message panes."); |
|---|
| 121 | Panel infoPane = mainPanel.getPanel("infoMessagesPane"); |
|---|
| 122 | TextBox infoText = infoPane.getTextBox("infoMessagesTA"); |
|---|
| 123 | assertEquals("wrong info msg text", ". = Sample Info Message.", infoText.getText().trim()); |
|---|
| 124 | |
|---|
| 125 | cadSimLogger.logp(Level.SEVERE, "", "", "Sample Error Message."); |
|---|
| 126 | |
|---|
| 127 | Panel errPane = mainPanel.getPanel("errorMessagesPane"); |
|---|
| 128 | TextBox errText = errPane.getTextBox("errorMessagesTA"); |
|---|
| 129 | assertEquals("wrong error msg text", ". = Sample Error Message.", errText.getText().trim()); |
|---|
| 130 | |
|---|
| 131 | System.out.println("Tests for status changes."); |
|---|
| 132 | app.theCoordinator.setParamicsStatus(CADEnums.PARAMICS_STATUS.CONNECTED); |
|---|
| 133 | CADSimulatorFixture.pause(500); |
|---|
| 134 | assertEquals("paramics connected should be yes", "Yes", mainPanel.getTextBox("paramicsConnectedTF").getText().trim()); |
|---|
| 135 | assertEquals("network id should be None", "None", mainPanel.getTextBox("networkLoadedTF").getText().trim()); |
|---|
| 136 | |
|---|
| 137 | // app.theCoordinator.setScriptStatus(CADEnums.SCRIPT_STATUS.SCRIPT_RUNNING); |
|---|
| 138 | // assertEquals("Running", mainPanel.getTextBox("simulationStatus").getText().trim()); |
|---|
| 139 | |
|---|
| 140 | // Load a script file |
|---|
| 141 | String autoloadScriptname = "scripts/one-incident.xml"; |
|---|
| 142 | app.theCoordinator.loadScriptFile(new File(autoloadScriptname)); |
|---|
| 143 | // The status should now say Ready |
|---|
| 144 | assertEquals("sim status should be ready", "Ready", mainPanel.getTextBox("simulationStatus").getText().trim()); |
|---|
| 145 | |
|---|
| 146 | app.theCoordinator.startSimulation(); |
|---|
| 147 | CADSimulatorFixture.pause(900); |
|---|
| 148 | assertEquals("sim status should be running", "Running", mainPanel.getTextBox("simulationStatus").getText().trim()); |
|---|
| 149 | assertEquals("sim time should be 0:00:01", "0:00:01", mainPanel.getTextBox("simulationClockLabel").getText().trim()); |
|---|
| 150 | |
|---|
| 151 | ParamicsSimulationManager psm = mock(ParamicsSimulationManager.class); |
|---|
| 152 | when(psm.isConnected()).thenReturn(Boolean.TRUE); |
|---|
| 153 | when(psm.getParamicsNetworkLoaded()).thenReturn(1); // provide the network ID to return |
|---|
| 154 | app.theParamicsSimMgr = psm; |
|---|
| 155 | // this will tell the model it has a new network ID |
|---|
| 156 | app.theCoordinator.setParamicsStatus(CADEnums.PARAMICS_STATUS.LOADED); |
|---|
| 157 | CADSimulatorFixture.pause(500); |
|---|
| 158 | assertEquals("network id should be 1", "1", mainPanel.getTextBox("networkLoadedTF").getText().trim()); |
|---|
| 159 | //mainPanel.getMenu().getItem("Exit").click(); |
|---|
| 160 | boolean breakpoint = true; |
|---|
| 161 | } |
|---|
| 162 | } |
|---|