/////////////////////////////////////////////////////////////////////////////
// $RCSfile: sccCADFileImportGuiTest.cpp,v $
// Original Author/Owner: reinhold
//
// $Revision: 1.1 $ $Name:  $ $State: Exp $
//
// $Date: 2006/05/02 21:50:51 $ $Author: reinhold $
//
/////////////////////////////////////////////////////////////////////////////

#ifdef __GNUG__
    #pragma implementation "sccCADFileImportGuiTest.h"
#endif

#include "sccCADFileImportGuiTest.h"

#include "wx/xrc/xmlres.h"

#include "vtkMatrix4x4.h"
#include "vtkPolyData.h"

#include "swWxGuiTestHelper.h"
#include "swWxGuiTestEventSimulationHelper.h"
#include "swWxGuiTestTimedDialogEnder.h"
#include "swWxGuiTestTempInteractive.h"

#include "swApp.h"
#include "swFrameFactory.h"
#include "swActiveDocument.h"

#include "sccDocument.h"
#include "sccCADModelDataset.h"

namespace sccTst {


// Register test suite with special name in order to be identifiable as test
// which must be run after GUI part of wxWidgets library is initialised:
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CADFileImportGuiTest, "WxGuiTest" );


void CADFileImportGuiTest::setUp ()
{
    m_cadFileDir = "../../TestData/CAD/";

    // Open a default document:
    wxString scpFilename ("../../TestData/scpv10/quickTest.scp");
    sw::App::GetInstance ()->GetDocumentManager ()->CreateDocument (scpFilename, wxDOC_SILENT);
}


void CADFileImportGuiTest::tearDown ()
{
    // Manipulate document's modify state allowing to close without any user
    // interaction:
    sw::App::GetInstance ()->GetDocumentManager ()->GetCurrentDocument ()->
            Modify (false);
    sw::App::GetInstance ()->GetDocumentManager ()->CloseDocuments ();
}


void CADFileImportGuiTest::testSTLImport ()
{
    const wxString filename = m_cadFileDir + "TesselatedImplant.stl";

    CPPUNIT_ASSERT_MESSAGE ("No document open", sw::App::GetInstance ()->
            GetDocumentManager ()->GetDocuments ().GetCount () == 1);

    scc::SCCDocument *doc = dynamic_cast< scc::SCCDocument *> (sw::ActiveDocument::
            GetInstance ()->GetActiveDocument ());
  	CPPUNIT_ASSERT_MESSAGE ("Open document is not active", doc != NULL);

    CPPUNIT_ASSERT_MESSAGE ("Invalid number of CAD models",
            doc->GetNmbCADModels () == 1);

    wxFrame *mainFrame = sw::FrameFactory::GetInstance ()->GetMainFrame ()->
            GetFrame ();
    wxMenuBar *menuBar = mainFrame->GetMenuBar ();
    CPPUNIT_ASSERT_MESSAGE ("Menubar not found", menuBar != NULL);

    int importMenuItemId = menuBar->FindMenuItem (_("File"), _("STL File..."));
    CPPUNIT_ASSERT_MESSAGE ("STL import menu item not found",
            importMenuItemId != wxNOT_FOUND);

    swTst::WxGuiTestEventSimulationHelper::SelectMenuItem (importMenuItemId, mainFrame);
    swTst::WxGuiTestHelper::FlushEventQueue ();

    wxWindow *fileSelPanelWdw = wxWindow::FindWindowByName ("sccCADFileSelectorPanel");
    CPPUNIT_ASSERT_MESSAGE ("Import dialogs file selection panel window not found",
            fileSelPanelWdw != NULL);
    wxTextCtrl *cadFilenameTextCtrl = XRCCTRL (*fileSelPanelWdw, "CADFileTextCtrl",
            wxTextCtrl);
    CPPUNIT_ASSERT_MESSAGE ("CAD filename text control not found",
            cadFilenameTextCtrl != NULL);

    // Temporary interactive test:
    swTst::WxGuiTestTempInteractive interactive;
    interactive.ShowCurrentGui ();

    swTst::WxGuiTestEventSimulationHelper::SetTextCtrlValue (cadFilenameTextCtrl, filename);
    swTst::WxGuiTestHelper::FlushEventQueue ();

    interactive.ShowCurrentGui ();

    swTst::WxGuiTestEventSimulationHelper::ClickButton (wxID_OK, fileSelPanelWdw);
    swTst::WxGuiTestHelper::FlushEventQueue ();

    interactive.ShowCurrentGui ();

    CPPUNIT_ASSERT_MESSAGE ("Invalid number of CAD models - no CAD imported successfully",
            doc->GetNmbCADModels () == 2);

    scc::CADModelDataset *stlModel = doc->GetCADModel (1);
    CPPUNIT_ASSERT_MESSAGE ("Invalid CAD model", stlModel != NULL);

    // This test is now invalid because a random colour is used instead of
    // static and potentially duplicate grey:
    //CPPUNIT_ASSERT_MESSAGE ("Colour is wrong",
    //        stlModel->GetColour () == wxColour (wxT("grey")));

    CPPUNIT_ASSERT_MESSAGE ("Filename is wrong",
            stlModel->GetFilename () == filename);

    // Test matrix for correct initialisation:
    vtkMatrix4x4 *matrix = stlModel->GetMatrix ();
    vtkMatrix4x4 *idMatrix = vtkMatrix4x4::New ();
    for (int i = 0; i < 4; i++) {

        for (int j = 0; j < 4; j++) {

            CPPUNIT_ASSERT_MESSAGE ("Element is wrong",
                    matrix->GetElement (i, j) == idMatrix->GetElement (i, j));
        }
    }
    idMatrix->Delete ();

    wxFileName filenameObj (filename);
    CPPUNIT_ASSERT_MESSAGE ("Dataset name is wrong",
            stlModel->GetName () == filenameObj.GetName ());

    vtkPolyData *tesselation = stlModel->GetTesselation ();
    CPPUNIT_ASSERT_MESSAGE ("Tesselation: number of points is wrong",
            tesselation->GetNumberOfPoints () == 1695);
    CPPUNIT_ASSERT_MESSAGE ("Tesselation: number of polygons is wrong",
            tesselation->GetNumberOfPolys () == 1762);
}

} // End namespace sccTst
