package tmcsim.simulationmanager;

import java.io.File;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.fail;
import org.uispec4j.*;
import org.uispec4j.interception.WindowInterceptor;
import tmcsim.cadsimulator.CADSimulator;
import tmcsim.common.ScriptException;
import tmcsim.common.SimulationException;

/**
 * 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;
    CADSimulator engine;

    public SimulationManagerSmokeTest(String testName)
    {
        super(testName);
    }

    /**
     * Call constructors for both classes
     */
    public void testBothGUIs() throws ScriptException, SimulationException
    {
        System.out.println("Smoke Test Sim Mgr & CADSimulator");

        Window cadwindow = null;
        System.setProperty("CAD_SIM_PROPERTIES", "config/cad_simulator_config.properties");
        if (System.getProperty("CAD_SIM_PROPERTIES") != null)
        {
            cadwindow = WindowInterceptor.run(new Trigger()
            {
                public void run()
                {
                    try
                    {
                        engine = new CADSimulator(System.getProperty("CAD_SIM_PROPERTIES"));
                    } catch (Exception e)
                    {
                        fail("Couldn't launch CADSimulator");
                    }
                }
            });
        } else
        {
            fail("CAD_SIM_PROPERTIES system property not defined.");
        }

        // Check CAD Simulator appears with no script and nothing connected
        assertEquals("CAD Simulator", cadwindow.getTitle());
        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_config.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());

        // Quit
        engine = null;
        simMgrApp = null;
    }
}
