Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wxWidgets: How to initialize wxApp without using macros and without entering the main application loop?

We need to write unit tests for a wxWidgets application using Google Test Framework. The problem is that wxWidgets uses the macro IMPLEMENT_APP(MyApp) to initialize and enter the application main loop. This macro creates several functions including int main(). The google test framework also uses macro definitions for each test.

One of the problems is that it is not possible to call the wxWidgets macro from within the test macro, because the first one creates functions.. So, we found that we could replace the macro with the following code:

wxApp* pApp = new MyApp(); 
wxApp::SetInstance(pApp);
wxEntry(argc, argv);

That's a good replacement, but wxEntry() call enters the original application loop. If we don't call wxEntry() there are still some parts of the application not initialized.

The question is how to initialize everything required for a wxApp to run, without actually running it, so we are able to unit test portions of it?

like image 619
m_pGladiator Avatar asked Oct 16 '08 12:10

m_pGladiator


2 Answers

Just been through this myself with 2.8.10. The magic is this:

// MyWxApp derives from wxApp
wxApp::SetInstance( new MyWxApp() );
wxEntryStart( argc, argv );
wxTheApp->CallOnInit();

// you can create top level-windows here or in OnInit()
...
// do your testing here

wxTheApp->OnRun();
wxTheApp->OnExit();
wxEntryCleanup();

You can just create a wxApp instance rather than deriving your own class using the technique above.

I'm not sure how you expect to do unit testing of your application without entering the mainloop as many wxWidgets components require the delivery of events to function. The usual approach would be to run unit tests after entering the main loop.

like image 135
Daniel Paull Avatar answered Oct 22 '22 13:10

Daniel Paull


IMPLEMENT_APP_NO_MAIN(MyApp);
IMPLEMENT_WX_THEME_SUPPORT;

int main(int argc, char *argv[])
{
    wxEntryStart( argc, argv );
    wxTheApp->CallOnInit();
    wxTheApp->OnRun();

    return 0;
}
like image 9
Byllgrim Avatar answered Oct 22 '22 14:10

Byllgrim