2012-11-01 16:45:11 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/testing.h
|
|
|
|
// Purpose: helpers for GUI testing
|
|
|
|
// Author: Vaclav Slavik
|
|
|
|
// Created: 2012-08-28
|
|
|
|
// Copyright: (c) 2012 Vaclav Slavik
|
|
|
|
// Licence: wxWindows Licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_TESTING_H_
|
|
|
|
#define _WX_TESTING_H_
|
|
|
|
|
|
|
|
#include "wx/debug.h"
|
|
|
|
#include "wx/string.h"
|
2013-05-20 13:15:41 +00:00
|
|
|
#include "wx/modalhook.h"
|
2012-11-01 16:45:11 +00:00
|
|
|
|
|
|
|
class WXDLLIMPEXP_FWD_CORE wxMessageDialogBase;
|
|
|
|
class WXDLLIMPEXP_FWD_CORE wxFileDialogBase;
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// testing API
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Don't include this code when building the library itself
|
|
|
|
#ifndef WXBUILDING
|
|
|
|
|
|
|
|
#include "wx/beforestd.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
|
|
|
#include <queue>
|
|
|
|
#include "wx/afterstd.h"
|
|
|
|
#include "wx/cpp.h"
|
2012-11-01 18:25:00 +00:00
|
|
|
#include "wx/dialog.h"
|
|
|
|
#include "wx/msgdlg.h"
|
|
|
|
#include "wx/filedlg.h"
|
2012-11-01 16:45:11 +00:00
|
|
|
|
|
|
|
class wxTestingModalHook;
|
|
|
|
|
|
|
|
// Non-template base class for wxExpectModal<T> (via wxExpectModalBase).
|
|
|
|
// Only used internally.
|
|
|
|
class wxModalExpectation
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxModalExpectation() : m_isOptional(false) {}
|
|
|
|
virtual ~wxModalExpectation() {}
|
|
|
|
|
|
|
|
bool IsOptional() const { return m_isOptional; }
|
|
|
|
|
|
|
|
virtual int Invoke(wxDialog *dlg) const = 0;
|
|
|
|
|
|
|
|
virtual wxString GetDescription() const = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Is this dialog optional, i.e. not required to be shown?
|
|
|
|
bool m_isOptional;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// This must be specialized for each type. The specialization MUST be derived
|
|
|
|
// from wxExpectModalBase<T>.
|
|
|
|
template<class T> class wxExpectModal {};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Base class for wxExpectModal<T> specializations.
|
|
|
|
|
|
|
|
Every such specialization must be derived from wxExpectModalBase; there's
|
|
|
|
no other use for this class than to serve as wxExpectModal<T>'s base class.
|
|
|
|
|
|
|
|
T must be a class derived from wxDialog.
|
|
|
|
*/
|
|
|
|
template<class T>
|
|
|
|
class wxExpectModalBase : public wxModalExpectation
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef T DialogType;
|
|
|
|
typedef wxExpectModal<DialogType> ExpectationType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns a copy of the expectation where the expected dialog is marked
|
|
|
|
as optional.
|
|
|
|
|
|
|
|
Optional dialogs aren't required to appear, it's not an error if they
|
|
|
|
don't.
|
|
|
|
*/
|
|
|
|
ExpectationType Optional() const
|
|
|
|
{
|
|
|
|
ExpectationType e(*static_cast<const ExpectationType*>(this));
|
|
|
|
e.m_isOptional = true;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual int Invoke(wxDialog *dlg) const
|
|
|
|
{
|
|
|
|
DialogType *t = dynamic_cast<DialogType*>(dlg);
|
|
|
|
if ( t )
|
|
|
|
return OnInvoked(t);
|
|
|
|
else
|
|
|
|
return wxID_NONE; // not handled
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns description of the expected dialog (by default, its class).
|
|
|
|
virtual wxString GetDescription() const
|
|
|
|
{
|
|
|
|
return wxCLASSINFO(T)->GetClassName();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
This method is called when ShowModal() was invoked on a dialog of type T.
|
|
|
|
|
|
|
|
@return Return value is used as ShowModal()'s return value.
|
|
|
|
*/
|
|
|
|
virtual int OnInvoked(DialogType *dlg) const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// wxExpectModal<T> specializations for common dialogs:
|
|
|
|
|
2014-09-10 16:51:30 +00:00
|
|
|
template<class T>
|
|
|
|
class wxExpectDismissableModal : public wxExpectModalBase<T>
|
2012-11-01 16:45:11 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-09-10 16:51:30 +00:00
|
|
|
explicit wxExpectDismissableModal(int id)
|
2012-11-01 16:45:11 +00:00
|
|
|
{
|
|
|
|
switch ( id )
|
|
|
|
{
|
|
|
|
case wxYES:
|
|
|
|
m_id = wxID_YES;
|
|
|
|
break;
|
|
|
|
case wxNO:
|
|
|
|
m_id = wxID_NO;
|
|
|
|
break;
|
|
|
|
case wxCANCEL:
|
|
|
|
m_id = wxID_CANCEL;
|
|
|
|
break;
|
|
|
|
case wxOK:
|
|
|
|
m_id = wxID_OK;
|
|
|
|
break;
|
|
|
|
case wxHELP:
|
|
|
|
m_id = wxID_HELP;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
m_id = id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-09-10 16:51:30 +00:00
|
|
|
virtual int OnInvoked(T *WXUNUSED(dlg)) const
|
2012-11-01 16:45:11 +00:00
|
|
|
{
|
|
|
|
return m_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
int m_id;
|
|
|
|
};
|
|
|
|
|
2014-09-10 16:51:30 +00:00
|
|
|
template<>
|
|
|
|
class wxExpectModal<wxMessageDialog>
|
|
|
|
: public wxExpectDismissableModal<wxMessageDialog>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit wxExpectModal(int id)
|
|
|
|
: wxExpectDismissableModal<wxMessageDialog>(id)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class wxExpectAny : public wxExpectDismissableModal<wxDialog>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit wxExpectAny(int id)
|
|
|
|
: wxExpectDismissableModal<wxDialog>(id)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-06 06:35:29 +00:00
|
|
|
#if wxUSE_FILEDLG
|
2012-11-01 16:45:11 +00:00
|
|
|
|
|
|
|
template<>
|
|
|
|
class wxExpectModal<wxFileDialog> : public wxExpectModalBase<wxFileDialog>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxExpectModal(const wxString& path, int id = wxID_OK)
|
|
|
|
: m_path(path), m_id(id)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual int OnInvoked(wxFileDialog *dlg) const
|
|
|
|
{
|
|
|
|
dlg->SetPath(m_path);
|
|
|
|
return m_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString m_path;
|
|
|
|
int m_id;
|
|
|
|
};
|
|
|
|
|
2012-11-06 06:35:29 +00:00
|
|
|
#endif
|
2012-11-01 16:45:11 +00:00
|
|
|
|
|
|
|
// Implementation of wxModalDialogHook for use in testing, with
|
|
|
|
// wxExpectModal<T> and the wxTEST_DIALOG() macro. It is not intended for
|
|
|
|
// direct use, use the macro instead.
|
|
|
|
class wxTestingModalHook : public wxModalDialogHook
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxTestingModalHook()
|
|
|
|
{
|
2013-05-20 13:15:41 +00:00
|
|
|
Register();
|
2012-11-01 16:45:11 +00:00
|
|
|
}
|
|
|
|
|
2013-05-20 13:15:41 +00:00
|
|
|
// 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()
|
2012-11-01 16:45:11 +00:00
|
|
|
{
|
2013-05-20 13:15:41 +00:00
|
|
|
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);
|
2012-11-01 16:45:11 +00:00
|
|
|
}
|
|
|
|
|
2013-05-20 13:15:41 +00:00
|
|
|
protected:
|
|
|
|
virtual int Enter(wxDialog *dlg)
|
2012-11-01 16:45:11 +00:00
|
|
|
{
|
|
|
|
while ( !m_expectations.empty() )
|
|
|
|
{
|
|
|
|
const wxModalExpectation *expect = m_expectations.front();
|
|
|
|
m_expectations.pop();
|
|
|
|
|
|
|
|
int ret = expect->Invoke(dlg);
|
|
|
|
if ( ret != wxID_NONE )
|
|
|
|
return ret; // dialog shown as expected
|
|
|
|
|
|
|
|
// not showing an optional dialog is OK, but showing an unexpected
|
|
|
|
// one definitely isn't:
|
|
|
|
if ( !expect->IsOptional() )
|
|
|
|
{
|
|
|
|
ReportFailure
|
|
|
|
(
|
|
|
|
wxString::Format
|
|
|
|
(
|
2014-09-20 22:07:31 +00:00
|
|
|
"%s was shown unexpectedly, expected %s.",
|
|
|
|
DescribeUnexpectedDialog(dlg),
|
2012-11-01 16:45:11 +00:00
|
|
|
expect->GetDescription()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return wxID_NONE;
|
|
|
|
}
|
|
|
|
// else: try the next expectation in the chain
|
|
|
|
}
|
|
|
|
|
|
|
|
ReportFailure
|
|
|
|
(
|
|
|
|
wxString::Format
|
|
|
|
(
|
2014-09-20 22:07:31 +00:00
|
|
|
"%s was shown unexpectedly.",
|
|
|
|
DescribeUnexpectedDialog(dlg)
|
2012-11-01 16:45:11 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
return wxID_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-09-20 22:07:31 +00:00
|
|
|
// This method may be overridden to provide a better description of
|
|
|
|
// (unexpected) dialogs, e.g. add knowledge of custom dialogs used by the
|
|
|
|
// program here.
|
|
|
|
virtual wxString DescribeUnexpectedDialog(wxDialog* dlg) const
|
|
|
|
{
|
|
|
|
return wxString::Format
|
|
|
|
(
|
|
|
|
"A %s with title \"%s\"",
|
|
|
|
dlg->GetClassInfo()->GetClassName(),
|
|
|
|
dlg->GetTitle()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method may be overridden to change the way test failures are
|
|
|
|
// handled. By default they result in an assertion failure which, of
|
|
|
|
// course, can itself be customized.
|
2012-11-01 16:45:11 +00:00
|
|
|
virtual void ReportFailure(const wxString& msg)
|
|
|
|
{
|
|
|
|
wxFAIL_MSG( msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::queue<const wxModalExpectation*> m_expectations;
|
|
|
|
|
|
|
|
wxDECLARE_NO_COPY_CLASS(wxTestingModalHook);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Redefining this value makes it possible to customize the hook class,
|
|
|
|
// including e.g. its error reporting.
|
|
|
|
#define wxTEST_DIALOG_HOOK_CLASS wxTestingModalHook
|
|
|
|
|
|
|
|
#define WX_TEST_IMPL_ADD_EXPECTATION(pos, expect) \
|
|
|
|
const wxModalExpectation& wx_exp##pos = expect; \
|
|
|
|
wx_hook.AddExpectation(wx_exp##pos);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Runs given code with all modal dialogs redirected to wxExpectModal<T>
|
|
|
|
hooks, instead of being shown to the user.
|
|
|
|
|
|
|
|
The first argument is any valid expression, typically a function call. The
|
|
|
|
remaining arguments are wxExpectModal<T> instances defining the dialogs
|
|
|
|
that are expected to be shown, in order of appearance.
|
|
|
|
|
|
|
|
Some typical examples:
|
|
|
|
|
|
|
|
@code
|
|
|
|
wxTEST_DIALOG
|
|
|
|
(
|
|
|
|
rc = dlg.ShowModal(),
|
|
|
|
wxExpectModal<wxFileDialog>(wxGetCwd() + "/test.txt")
|
|
|
|
);
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
Sometimes, the code may show more than one dialog:
|
|
|
|
|
|
|
|
@code
|
|
|
|
wxTEST_DIALOG
|
|
|
|
(
|
|
|
|
RunSomeFunction(),
|
|
|
|
wxExpectModal<wxMessageDialog>(wxNO),
|
|
|
|
wxExpectModal<MyConfirmationDialog>(wxYES),
|
|
|
|
wxExpectModal<wxFileDialog>(wxGetCwd() + "/test.txt")
|
|
|
|
);
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
Notice that wxExpectModal<T> has some convenience methods for further
|
|
|
|
tweaking the expectations. For example, it's possible to mark an expected
|
|
|
|
dialog as @em optional for situations when a dialog may be shown, but isn't
|
|
|
|
required to, by calling the Optional() method:
|
|
|
|
|
|
|
|
@code
|
|
|
|
wxTEST_DIALOG
|
|
|
|
(
|
|
|
|
RunSomeFunction(),
|
|
|
|
wxExpectModal<wxMessageDialog>(wxNO),
|
|
|
|
wxExpectModal<wxFileDialog>(wxGetCwd() + "/test.txt").Optional()
|
|
|
|
);
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
@note By default, errors are reported with wxFAIL_MSG(). You may customize this by
|
|
|
|
implementing a class derived from wxTestingModalHook, overriding its
|
|
|
|
ReportFailure() method and redefining the wxTEST_DIALOG_HOOK_CLASS
|
|
|
|
macro to be the name of this class.
|
|
|
|
|
|
|
|
@note Custom dialogs are supported too. All you have to do is to specialize
|
|
|
|
wxExpectModal<> for your dialog type and implement its OnInvoked()
|
|
|
|
method.
|
|
|
|
*/
|
2013-07-11 22:40:47 +00:00
|
|
|
#ifdef HAVE_VARIADIC_MACROS
|
2014-09-10 16:52:08 +00:00
|
|
|
|
|
|
|
// See wx/cpp.h for the explanations of this hack.
|
|
|
|
#if defined(__GNUC__) && __GNUC__ == 3
|
|
|
|
#pragma GCC system_header
|
|
|
|
#endif /* gcc-3.x */
|
|
|
|
|
2012-11-01 16:45:11 +00:00
|
|
|
#define wxTEST_DIALOG(codeToRun, ...) \
|
|
|
|
{ \
|
|
|
|
wxTEST_DIALOG_HOOK_CLASS wx_hook; \
|
|
|
|
wxCALL_FOR_EACH(WX_TEST_IMPL_ADD_EXPECTATION, __VA_ARGS__) \
|
|
|
|
codeToRun; \
|
|
|
|
wx_hook.CheckUnmetExpectations(); \
|
|
|
|
}
|
2013-07-11 22:40:47 +00:00
|
|
|
#endif /* HAVE_VARIADIC_MACROS */
|
2012-11-01 16:45:11 +00:00
|
|
|
|
|
|
|
#endif // !WXBUILDING
|
|
|
|
|
|
|
|
#endif // _WX_TESTING_H_
|