package tmcsim.cadsimulator;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.uispec4j.*;
import org.uispec4j.interception.WindowInterceptor;
import tmcsim.cadsimulator.managers.ATMSManager;
import tmcsim.common.ScriptException;
import tmcsim.common.SimulationException;
import tmcsim.paramicscommunicator.ParamicsCommunicator;
import tmcsim.paramicscommunicator.gui.ParamicsCommunicatorGUI;
import tmcsim.simulationmanager.SimulationManager;
/**
* Note, this is just like VisibleSystemTest except it starts it's own
* CADSimulator running on the console.
*
* @author jdalbey
*/
public class SystemConsoleTest extends UISpecTestCase
{
SimulationManager simMgrApp;
CADServer engine;
public SystemConsoleTest(String testName)
{
super(testName);
}
@Override
protected void setUp() throws Exception
{
super.setUp();
}
/**
* Test of run method, of class ParamicsCommunicator.
*/
public void testRun() throws ScriptException, SimulationException, RemoteException
{
System.out.println("System Console Test");
try
{
engine = new CADServer("config/cad_simulator_console_config.properties");
} catch (Exception e)
{
fail("Couldn't launch CADSimulator");
}
ParamicsCommunicator pc = null;
pc = new ParamicsCommunicator("config/paramics_communicator_config.properties");
ParamicsCommunicatorGUI theGUI = new ParamicsCommunicatorGUI();
pc.setGUI(theGUI);
// Note: Can't set visible ANY windows during UISpec test
// expect pcomm to say "sleeping"
Window simMgrWindow = null;
simMgrWindow = WindowInterceptor.run(new Trigger()
{
public void run()
{
try
{
simMgrApp = new SimulationManager("config/sim_manager_systest_config.properties");
} catch (Exception ex)
{
fail("Couldn't launch Simulation Manager");
}
}
});
// TODO: Check that the Sim Mgr GUI appears without a script loaded yet
Panel contentPanel = simMgrWindow.getPanel("contentPane");
TextBox txtSimStatus = contentPanel.getTextBox("simulationStatusText");
assertEquals("No Script", txtSimStatus.getText());
TextBox txtParamStatus = contentPanel.getTextBox("paramicsStatusInfoLabel");
assertEquals("Unknown", txtParamStatus.getText());
Button loadNetworkBtn = simMgrWindow.getButton("Load Network");
assertFalse(loadNetworkBtn.isEnabled());
Button paramicsBtn = simMgrWindow.getButton("Connect to Paramics");
paramicsBtn.click();
try
{
Thread.sleep(200);
} catch (Exception ex)
{
}
assertEquals("Disconnect from Paramics", paramicsBtn.getLabel());
assertEquals("Connected", txtParamStatus.getText());
pc.startReading();
loadNetworkBtn.click();
try
{
Thread.sleep(200);
} catch (Exception ex)
{
}
assertEquals("Sending Network ID", txtParamStatus.getText());
System.out.println("Sending Network ID");
assertFalse(loadNetworkBtn.isEnabled());
String warmingXML = "\n"
+ "WARMING\n"
+ "1\n"
+ "";
writedata("paramics_status.xml", warmingXML);
try
{
Thread.sleep(2100);
} catch (Exception ex)
{
}
assertEquals("Warming Up", txtParamStatus.getText());
System.out.println("Warming Up Passed");
try
{
Thread.sleep(2100);
} catch (Exception ex)
{
}
String loadedXML = "\n"
+ "LOADED"
+ "1"
+ "";
writedata("paramics_status.xml", loadedXML);
try
{
Thread.sleep(2100);
} catch (Exception ex)
{
}
//assertEquals("Network 1 Loaded", txtParamStatus.getText());
//System.out.println("Network Loaded Passed");
// Load a script file
String autoloadScriptname = "scripts/audio_systest.xml";
simMgrApp.loadScript(new File(autoloadScriptname));
try
{
Thread.sleep(500);
} catch (Exception ex)
{
}
// The status should now say Ready
assertEquals("Ready", txtSimStatus.getText());
// Mock out the ATMS manager in CADSimulator
ATMSManager atm = mock(ATMSManager.class);
try
{
when(atm.getCurrentTime()).thenReturn(29000L);
} catch (Exception ex)
{
Logger.getLogger(SystemConsoleTest.class.getName()).log(Level.SEVERE, null, ex);
}
engine.theATMSMgr = atm;
// Click "Start"
simMgrWindow.getButton("Start").click();
try
{
Thread.sleep(200);
} catch (Exception ex)
{
ex.printStackTrace();
}
assertEquals("Running", txtSimStatus.getText());
System.out.println("Running Passed");
try
{
Thread.sleep(9000);
} catch (InterruptedException ex)
{
}
simMgrWindow.getButton("Pause").click();
try
{
Thread.sleep(3000);
} catch (InterruptedException ex)
{
}
simMgrWindow.getButton("Start").click();
try
{
Thread.sleep(3000);
} catch (InterruptedException ex)
{
}
simMgrWindow.getButton("Pause").click();
simMgrWindow.getMenuBar().getMenu("File").getSubMenu("Exit").click();
System.out.println("Exiting.");
}
// Write the test data to a file
public static void writedata(String filename, String data)
{
PrintWriter writer = null;
try
{
writer = new PrintWriter(new FileWriter(filename));
writer.println(data);
writer.close();
} catch (Exception ex)
{
ex.printStackTrace();
}
}
}