00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00012
00013 #ifdef __GNUG__
00014 #pragma implementation "WxGuiTestHelper.h"
00015 #endif
00016
00017 #include <wxGuiTest/WxGuiTestHelper.h>
00018
00019 #include <wxGuiTest/WarningAsserterInterface.h>
00020 #include <wxGuiTest/ProvokedWarningRegistry.h>
00021
00022
00023
00024
00025 namespace wxTst {
00026
00027
00028
00029 bool WxGuiTestHelper::s_interactive = false;
00030
00031 bool WxGuiTestHelper::s_isGuiLessUnitTest = false;
00032
00033 bool WxGuiTestHelper::s_showModalDialogsNonModal = false;
00034 bool WxGuiTestHelper::s_showPopupMenus = true;
00035
00036 bool WxGuiTestHelper::s_disableTestInteractivity = false;
00037
00038 bool WxGuiTestHelper::s_popupWarningForFailingAssert = true;
00039
00040 bool WxGuiTestHelper::s_checkForProvokedWarnings = false;
00041
00042 WarningAsserterInterface *WxGuiTestHelper::s_warningAsserter = NULL;
00043
00044 WxGuiTestHelper::PopupMenuMap WxGuiTestHelper::s_popupMenuMap;
00045
00046 wxString WxGuiTestHelper::s_fileOfFirstTestFailure;
00047 int WxGuiTestHelper::s_lineNmbOfFirstTestFailure = -1;
00048 wxString WxGuiTestHelper::s_shortDescriptionOfFirstTestFailure;
00049 wxString WxGuiTestHelper::s_accTestFailures;
00050
00051
00052 WxGuiTestHelper::WxGuiTestHelper ()
00053 {
00054
00055 }
00056
00057
00058 WxGuiTestHelper::~WxGuiTestHelper ()
00059 {
00060 s_popupMenuMap.clear ();
00061 if (s_warningAsserter != NULL) {
00062
00063 delete s_warningAsserter;
00064 s_warningAsserter = NULL;
00065 }
00066 }
00067
00068
00069 int WxGuiTestHelper::FlushEventQueue ()
00070 {
00071
00072 s_fileOfFirstTestFailure.Clear ();
00073 s_lineNmbOfFirstTestFailure = -1;
00074 s_accTestFailures.Clear ();
00075
00076 bool oldInteractive = s_interactive;
00077 int retCode;
00078
00079 s_interactive = false;
00080
00081 retCode = wxTheApp->MainLoop ();
00082
00083 s_interactive = oldInteractive;
00084
00085
00086 if (!s_accTestFailures.IsEmpty ()) {
00087
00088 wxASSERT (s_warningAsserter != NULL);
00089 s_warningAsserter->FailAssert (s_fileOfFirstTestFailure,
00090 s_lineNmbOfFirstTestFailure,
00091 s_shortDescriptionOfFirstTestFailure, s_accTestFailures);
00092 }
00093
00094 return retCode;
00095 }
00096
00097
00098 int WxGuiTestHelper::Show (wxWindow *wdw, bool show, bool isModal)
00099 {
00100 return WxGuiTestHelper::Show (wdw, show, isModal,
00101 WxGuiTestHelper::s_isGuiLessUnitTest);
00102 }
00103
00104
00105 int WxGuiTestHelper::Show (wxWindow *wdw, bool show, bool isModal,
00106 bool isGuiLessUnitTest)
00107 {
00108 wxASSERT (wdw != NULL);
00109
00110 int ret = -1;
00111
00112 if (isModal) {
00113
00114
00115 wxDialog *dlg = wxDynamicCast (wdw, wxDialog);
00116 wxASSERT (dlg != NULL);
00117
00118 if (isGuiLessUnitTest) {
00119
00120
00121
00122 } else {
00123
00124 if (show) {
00125
00126 if (WxGuiTestHelper::s_showModalDialogsNonModal) {
00127
00128 ret = dlg->Show ();
00129
00130 } else {
00131
00132 ret = dlg->ShowModal ();
00133 }
00134
00135 } else {
00136
00137 if (WxGuiTestHelper::s_showModalDialogsNonModal) {
00138
00139 ret = dlg->Hide ();
00140
00141 } else {
00142
00143 dlg->EndModal (show);
00144 }
00145 }
00146 }
00147
00148 } else {
00149
00150 if (!isGuiLessUnitTest) {
00151
00152 if (show) {
00153
00154 ret = wdw->Show (show);
00155
00156 } else {
00157
00158 ret = wdw->Hide ();
00159 }
00160 }
00161 }
00162
00163 return ret;
00164 }
00165
00166
00167 bool WxGuiTestHelper::PopupMenu (wxWindow *wdw, wxMenu *menu,
00168 const wxPoint &pos, const wxString &cacheMapKey)
00169 {
00170 return WxGuiTestHelper::PopupMenu (wdw, menu, pos, cacheMapKey,
00171 WxGuiTestHelper::s_isGuiLessUnitTest);
00172 }
00173
00174
00175 bool WxGuiTestHelper::PopupMenu (wxWindow *wdw, wxMenu *menu,
00176 const wxPoint &pos, const wxString &cacheMapKey, bool isGuiLessUnitTest)
00177 {
00178 bool ret = false;
00179
00180
00181 WxGuiTestHelper::s_popupMenuMap[cacheMapKey] = std::make_pair (menu, wdw);
00182
00183 if (isGuiLessUnitTest) {
00184
00185
00186
00187 } else {
00188
00189 if (WxGuiTestHelper::s_showPopupMenus) {
00190
00191 ret = wdw->PopupMenu (menu, pos);
00192
00193 } else {
00194
00195
00196 }
00197 }
00198
00199 return ret;
00200 }
00201
00202
00203 void WxGuiTestHelper::BreakTestToShowCurrentGui ()
00204 {
00205 if (WxGuiTestHelper::GetDisableTestInteractivity ()) {
00206
00207 return;
00208 }
00209
00210 ::wxMessageBox (_T("Continue unit testing?"),
00211 _T("wxGui CppUnit Testing Suspended"), wxOK | wxICON_QUESTION);
00212 }
00213
00214
00215 void WxGuiTestHelper::SetInteractive (bool interactive)
00216 {
00217 s_interactive = interactive;
00218 }
00219
00220
00221 bool WxGuiTestHelper::GetInteractive ()
00222 {
00223 return s_interactive;
00224 }
00225
00226 void WxGuiTestHelper::SetIsGuiLessUnitTestFlag (bool isGuiLess)
00227 {
00228 s_isGuiLessUnitTest = isGuiLess;
00229 }
00230
00231
00232 bool WxGuiTestHelper::IsGuiLessUnitTestFlag ()
00233 {
00234 return s_isGuiLessUnitTest;
00235 }
00236
00237
00238 void WxGuiTestHelper::SetShowModalDialogsNonModalFlag (bool showNonModal)
00239 {
00240 s_showModalDialogsNonModal = showNonModal;
00241 }
00242
00243
00244 bool WxGuiTestHelper::GetShowModalDialogsNonModalFlag ()
00245 {
00246 return s_showModalDialogsNonModal;
00247 }
00248
00249
00250 void WxGuiTestHelper::SetShowPopupMenusFlag (bool showPopupMenus)
00251 {
00252 s_showPopupMenus = showPopupMenus;
00253 }
00254
00255
00256 bool WxGuiTestHelper::GetShowPopupMenusFlag ()
00257 {
00258 return s_showPopupMenus;
00259 }
00260
00261
00262 void WxGuiTestHelper::SetDisableTestInteractivity (bool disable)
00263 {
00264 s_disableTestInteractivity = disable;
00265 }
00266
00267
00268 bool WxGuiTestHelper::GetDisableTestInteractivity ()
00269 {
00270 return s_disableTestInteractivity;
00271 }
00272
00273
00274 void WxGuiTestHelper::SetPopupWarningForFailingAssert (bool popup)
00275 {
00276 s_popupWarningForFailingAssert = popup;
00277 }
00278
00279
00280 bool WxGuiTestHelper::GetPopupWarningForFailingAssert ()
00281 {
00282 return s_popupWarningForFailingAssert;
00283 }
00284
00285
00286 wxMenu *WxGuiTestHelper::FindPopupMenu (const wxString &key)
00287 {
00288 PopupMenuMap::const_iterator it;
00289
00290 it = s_popupMenuMap.find (key);
00291 return (it != s_popupMenuMap.end () ? (*it).second.first : NULL);
00292 }
00293
00294
00295 wxString WxGuiTestHelper::FindPopupMenuKey (wxMenu *menu)
00296 {
00297 bool found = false;
00298 PopupMenuMap::const_iterator it = s_popupMenuMap.begin ();
00299 while ((!found) && (it != s_popupMenuMap.end ())) {
00300
00301 if ((*it).second.first == menu) {
00302
00303 found = true;
00304
00305 } else {
00306
00307 it++;
00308 }
00309 }
00310 return (found ? (*it).first : _T(""));
00311 }
00312
00313
00314 wxWindow *WxGuiTestHelper::FindPopupMenuEvtHandlerWdw (const wxString &key)
00315 {
00316 PopupMenuMap::const_iterator it;
00317
00318 it = s_popupMenuMap.find (key);
00319 return (it != s_popupMenuMap.end () ? (*it).second.second : NULL);
00320 }
00321
00322
00323 bool WxGuiTestHelper::IsProvokedWarning (const wxString &caption,
00324 const wxString &message)
00325 {
00326 bool isProvoked = false;
00327
00328 if (WxGuiTestHelper::GetCheckForProvokedWarnings ()) {
00329
00330 ProvokedWarningRegistry &provWarningRegistry =
00331 ProvokedWarningRegistry::GetInstance ();
00332 const ProvokedWarning *warning =
00333 provWarningRegistry.FindRegisteredWarning (caption, message);
00334 if ((warning != NULL) && (
00335 provWarningRegistry.IsRegisteredAndInTime (*warning))) {
00336
00337
00338 provWarningRegistry.SetWarningAsDetected (*warning);
00339 isProvoked = true;
00340
00341 } else {
00342
00343
00344 wxString failMsg = wxString::Format (
00345 _T("Caption \"%s\", message \"%s\" occured"), caption.c_str (),
00346 message.c_str ());
00347 WxGuiTestHelper::AddTestFailure (_T(""), -1,
00348 _T("Unexpected App application warning detected"), failMsg);
00349
00350 wxTheApp->ExitMainLoop ();
00351 }
00352 }
00353
00354 return isProvoked;
00355 }
00356
00357
00358 void WxGuiTestHelper::SetCheckForProvokedWarnings (bool check)
00359 {
00360 WxGuiTestHelper::s_checkForProvokedWarnings = check;
00361 }
00362
00363
00364 bool WxGuiTestHelper::GetCheckForProvokedWarnings ()
00365 {
00366 return WxGuiTestHelper::s_checkForProvokedWarnings;
00367 }
00368
00369
00370 void WxGuiTestHelper::SetWarningAsserter (
00371 WarningAsserterInterface *warningAsserter)
00372 {
00373
00374
00375
00376 s_warningAsserter = warningAsserter;
00377 }
00378
00379
00380 void WxGuiTestHelper::AddTestFailure (const wxString &file, const int line,
00381 const wxString &shortDescription, const wxString &message)
00382 {
00383 if (s_accTestFailures.IsEmpty ()) {
00384
00385 s_fileOfFirstTestFailure = file;
00386 s_lineNmbOfFirstTestFailure = line;
00387 s_shortDescriptionOfFirstTestFailure = shortDescription;
00388
00389 } else {
00390
00391 s_accTestFailures += _T("\nAND SUBSEQUENTLY:");
00392 if (!shortDescription.IsEmpty ()) {
00393
00394 s_accTestFailures += _T("\n");
00395 s_accTestFailures += shortDescription;
00396 }
00397 }
00398 s_accTestFailures += _T("\n");
00399 s_accTestFailures += message;
00400 }
00401
00402 }
00403