Add a public wxModalDialogHook class for intercepting modal dialogs.

Extract wxModalDialogHook from wx/testing.h into its own wx/modalhook.h,
extend it to allow to be notified not only about showing modal dialogs but
also about dismissing them and document it and show its use in the dialogs
sample.

Also replace all the WX_TESTING_SHOW_MODAL_HOOK macros occurrences with
WX_HOOK_MODAL_DIALOG.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74037 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2013-05-20 13:15:41 +00:00
parent 9bc3af3e64
commit 691745ab41
64 changed files with 712 additions and 194 deletions

View File

@@ -13,56 +13,11 @@
#include "wx/debug.h"
#include "wx/string.h"
#include "wx/modalhook.h"
class WXDLLIMPEXP_FWD_CORE wxDialog;
class WXDLLIMPEXP_FWD_CORE wxMessageDialogBase;
class WXDLLIMPEXP_FWD_CORE wxFileDialogBase;
// ----------------------------------------------------------------------------
// implementation helpers
// ----------------------------------------------------------------------------
// Helper hook class used to redirect ShowModal() to testing code.
// Instead of showing a dialog modally, hook code is called to simulate what
// the user would do and return appropriate ID from ShowModal().
class WXDLLIMPEXP_CORE wxModalDialogHook
{
public:
wxModalDialogHook() {}
virtual ~wxModalDialogHook() {}
/// Returns currently active hook object or NULL.
static wxModalDialogHook *Get() { return ms_instance; }
/// Set the hook and returns the previously set one.
static wxModalDialogHook *Set(wxModalDialogHook *hook)
{
wxModalDialogHook *old = ms_instance;
ms_instance = hook;
return old;
}
/// Entry point that is called from ShowModal().
virtual int Invoke(wxDialog *dlg) = 0;
private:
static wxModalDialogHook *ms_instance;
wxDECLARE_NO_COPY_CLASS(wxModalDialogHook);
};
// This macro needs to be used at the top of every implementation of
// ShowModal() in order for the above modal dialogs testing code to work.
#define WX_TESTING_SHOW_MODAL_HOOK() \
if ( wxModalDialogHook::Get() ) \
{ \
int rc = wxModalDialogHook::Get()->Invoke(this); \
if ( rc != wxID_NONE ) \
return rc; \
} \
struct wxDummyTestingStruct /* just to force a semicolon */
// ----------------------------------------------------------------------------
// testing API
// ----------------------------------------------------------------------------
@@ -233,15 +188,42 @@ class wxTestingModalHook : public wxModalDialogHook
public:
wxTestingModalHook()
{
m_prevHook = wxModalDialogHook::Set(this);
Register();
}
virtual ~wxTestingModalHook()
// Called to verify that all expectations were met. This cannot be done in
// the destructor, because ReportFailure() may throw (either because it's
// overriden or because wx's assertions handling is, globally). And
// throwing from the destructor would introduce all sort of problems,
// including messing up the order of errors in some cases.
void CheckUnmetExpectations()
{
wxModalDialogHook::Set(m_prevHook);
while ( !m_expectations.empty() )
{
const wxModalExpectation *expect = m_expectations.front();
m_expectations.pop();
if ( expect->IsOptional() )
continue;
ReportFailure
(
wxString::Format
(
"Expected %s dialog was not shown.",
expect->GetDescription()
)
);
break;
}
}
virtual int Invoke(wxDialog *dlg)
void AddExpectation(const wxModalExpectation& e)
{
m_expectations.push(&e);
}
protected:
virtual int Enter(wxDialog *dlg)
{
while ( !m_expectations.empty() )
{
@@ -281,37 +263,6 @@ public:
return wxID_NONE;
}
// Called to verify that all expectations were met. This cannot be done in
// the destructor, because ReportFailure() may throw (either because it's
// overriden or because wx's assertions handling is, globally). And
// throwing from the destructor would introduce all sort of problems,
// including messing up the order of errors in some cases.
void CheckUnmetExpectations()
{
while ( !m_expectations.empty() )
{
const wxModalExpectation *expect = m_expectations.front();
m_expectations.pop();
if ( expect->IsOptional() )
continue;
ReportFailure
(
wxString::Format
(
"Expected %s dialog was not shown.",
expect->GetDescription()
)
);
break;
}
}
void AddExpectation(const wxModalExpectation& e)
{
m_expectations.push(&e);
}
protected:
virtual void ReportFailure(const wxString& msg)
{
@@ -319,7 +270,6 @@ protected:
}
private:
wxModalDialogHook *m_prevHook;
std::queue<const wxModalExpectation*> m_expectations;
wxDECLARE_NO_COPY_CLASS(wxTestingModalHook);