| 1 | package tmcsim.cadsimulator.viewer; |
|---|
| 2 | |
|---|
| 3 | import java.util.logging.Level; |
|---|
| 4 | import java.util.logging.Logger; |
|---|
| 5 | import static junit.framework.Assert.assertEquals; |
|---|
| 6 | import static junit.framework.Assert.assertTrue; |
|---|
| 7 | import org.uispec4j.*; |
|---|
| 8 | import tmcsim.cadsimulator.viewer.model.CADSimulatorState; |
|---|
| 9 | import tmcsim.common.CADEnums; |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * |
|---|
| 13 | * @author jdalbey |
|---|
| 14 | */ |
|---|
| 15 | public class CADSimulatorViewModelTest extends junit.framework.TestCase //extends UISpecTestCase |
|---|
| 16 | { |
|---|
| 17 | |
|---|
| 18 | CADServerViewer viewer; |
|---|
| 19 | Window cadwindow; |
|---|
| 20 | CADSimulatorState model; |
|---|
| 21 | Panel mainPanel; |
|---|
| 22 | |
|---|
| 23 | public CADSimulatorViewModelTest(String testName) |
|---|
| 24 | { |
|---|
| 25 | super(testName); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | @Override |
|---|
| 29 | protected void setUp() throws Exception |
|---|
| 30 | { |
|---|
| 31 | super.setUp(); |
|---|
| 32 | viewer = new CADServerViewer("config/cad_simulator_config.properties"); |
|---|
| 33 | cadwindow = new Window(viewer); |
|---|
| 34 | model = new CADSimulatorState(); |
|---|
| 35 | model.addObserver(viewer); |
|---|
| 36 | mainPanel = cadwindow.getPanel("contentPane"); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | public void testInitialView() |
|---|
| 40 | { |
|---|
| 41 | String actual; |
|---|
| 42 | assertTrue("Title bar incorrect", cadwindow.getTitle().trim().startsWith("CAD Server")); |
|---|
| 43 | TextBox txtStatus = mainPanel.getTextBox("simulationStatus"); |
|---|
| 44 | actual = txtStatus.getText().trim(); |
|---|
| 45 | assertEquals("simulation status should say No Script", "No Script", actual); |
|---|
| 46 | TextBox terminals = mainPanel.getTextBox("termConnectedTF"); |
|---|
| 47 | actual = terminals.getText().trim(); |
|---|
| 48 | assertEquals("should be 0 terminals", "0", actual); |
|---|
| 49 | actual = mainPanel.getTextBox("managerConnectedTF").getText().trim(); |
|---|
| 50 | assertEquals("mgr connected should be no", "No", actual); |
|---|
| 51 | actual = mainPanel.getTextBox("paramicsConnectedTF").getText().trim(); |
|---|
| 52 | assertEquals("paramics connected should be no", "No", actual); |
|---|
| 53 | actual = mainPanel.getTextBox("simulationClockLabel").getText().trim(); |
|---|
| 54 | assertEquals("initial time should be 0:00:00", "0:00:00", actual); |
|---|
| 55 | actual = mainPanel.getTextBox("networkLoadedTF").getText().trim(); |
|---|
| 56 | assertEquals("network id should be None", "None", actual); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | public void testClients() |
|---|
| 60 | { |
|---|
| 61 | String actual; |
|---|
| 62 | model.connectClient(); |
|---|
| 63 | TextBox terminals = mainPanel.getTextBox("termConnectedTF"); |
|---|
| 64 | assertEquals(1, model.getNumClients()); |
|---|
| 65 | assertEquals("should be 1 terminal", "1", terminals.getText().trim()); |
|---|
| 66 | model.connectClient(); |
|---|
| 67 | assertEquals("should be 2 terminals", "2", terminals.getText().trim()); |
|---|
| 68 | model.disconnectClient(); |
|---|
| 69 | assertEquals("should be 1 terminal after disconnect", "1", terminals.getText().trim()); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | public void testSimMgr() |
|---|
| 73 | { |
|---|
| 74 | String actual; |
|---|
| 75 | model.setSimManagerStatus(true); |
|---|
| 76 | actual = mainPanel.getTextBox("managerConnectedTF").getText().trim(); |
|---|
| 77 | assertEquals("mgr connected should be yes", "Yes", actual); |
|---|
| 78 | assertTrue(model.isSimManagerConnected()); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | public void testMessagePanes() |
|---|
| 82 | { |
|---|
| 83 | String actual; |
|---|
| 84 | System.out.println("Tests for message panes."); |
|---|
| 85 | Logger cadSimLogger = Logger.getLogger("tmcsim.cadsimulator"); |
|---|
| 86 | cadSimLogger.logp(Level.INFO, "", "", "Sample Info Message."); |
|---|
| 87 | Panel infoPane = mainPanel.getPanel("infoMessagesPane"); |
|---|
| 88 | TextBox infoText = infoPane.getTextBox("infoMessagesTA"); |
|---|
| 89 | actual = infoText.getText().trim(); |
|---|
| 90 | assertEquals("wrong info msg text", ". = Sample Info Message.", actual); |
|---|
| 91 | |
|---|
| 92 | cadSimLogger.logp(Level.SEVERE, "", "", "Sample Error Message."); |
|---|
| 93 | |
|---|
| 94 | Panel errPane = mainPanel.getPanel("errorMessagesPane"); |
|---|
| 95 | TextBox errText = errPane.getTextBox("errorMessagesTA"); |
|---|
| 96 | actual = errText.getText().trim(); |
|---|
| 97 | assertEquals("wrong error msg text", ". = Sample Error Message.", actual); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | public void testStatusChg() |
|---|
| 101 | { |
|---|
| 102 | String actual; |
|---|
| 103 | System.out.println("Tests for status changes."); |
|---|
| 104 | model.setParamicsStatus(CADEnums.PARAMICS_STATUS.CONNECTED); |
|---|
| 105 | pause(500); |
|---|
| 106 | actual = mainPanel.getTextBox("paramicsConnectedTF").getText().trim(); |
|---|
| 107 | assertEquals("paramics connected should be yes", "Yes", actual); |
|---|
| 108 | actual = mainPanel.getTextBox("networkLoadedTF").getText().trim(); |
|---|
| 109 | assertEquals("network id should be None", "None", actual); |
|---|
| 110 | |
|---|
| 111 | model.setScriptStatus(CADEnums.SCRIPT_STATUS.SCRIPT_STOPPED_NOT_STARTED); |
|---|
| 112 | |
|---|
| 113 | // The status should now say Ready |
|---|
| 114 | actual = mainPanel.getTextBox("simulationStatus").getText().trim(); |
|---|
| 115 | assertEquals("sim status should be ready", "Ready", actual); |
|---|
| 116 | |
|---|
| 117 | model.setScriptStatus(CADEnums.SCRIPT_STATUS.SCRIPT_RUNNING); |
|---|
| 118 | model.setTime(61L); |
|---|
| 119 | actual = mainPanel.getTextBox("simulationStatus").getText().trim(); |
|---|
| 120 | assertEquals("sim status should be running", "Running", actual); |
|---|
| 121 | actual = mainPanel.getTextBox("simulationClockLabel").getText().trim(); |
|---|
| 122 | assertEquals("sim time should be 0:01:01", "0:01:01", actual); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | public void testNetworkID() |
|---|
| 126 | { |
|---|
| 127 | String actual; |
|---|
| 128 | // this will tell the model it has a new network ID |
|---|
| 129 | model.setParamicsNetworkLoaded("1"); |
|---|
| 130 | model.setParamicsStatus(CADEnums.PARAMICS_STATUS.LOADED); |
|---|
| 131 | pause(500); |
|---|
| 132 | actual = mainPanel.getTextBox("networkLoadedTF").getText().trim(); |
|---|
| 133 | assertEquals("network id should be 1", "1", actual); |
|---|
| 134 | |
|---|
| 135 | // model.updateDVDStatus(new DVDStatusUpdate("1", false, new Throwable())); |
|---|
| 136 | |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | public static void pause(int millis) |
|---|
| 140 | { |
|---|
| 141 | try |
|---|
| 142 | { |
|---|
| 143 | Thread.sleep(millis); |
|---|
| 144 | } catch (InterruptedException ex) |
|---|
| 145 | { |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | } |
|---|