Add unit tests for checking wxTE_PROCESS_ENTER handling

Verify that pressing Enter in a dialog activates its default button when
a text-like (i.e. wxTextCtrl or wxComboBox) has focus either if it
doesn't have wxTE_PROCESS_ENTER style or if it does, but its handler
skips the event, but not if the style is used and the event is handled.
This commit is contained in:
Vadim Zeitlin
2019-07-14 01:03:11 +02:00
parent f7ead9f844
commit 13aa2a6721
4 changed files with 163 additions and 0 deletions

View File

@@ -231,4 +231,22 @@ void ComboBoxTestCase::IsEmpty()
#endif #endif
} }
TEST_CASE("wxComboBox::ProcessEnter", "[wxComboBox][enter]")
{
struct ComboBoxCreator
{
static wxControl* Do(wxWindow* parent, int style)
{
const wxString choices[] = { "foo", "bar", "baz" };
return new wxComboBox(parent, wxID_ANY, wxString(),
wxDefaultPosition, wxDefaultSize,
WXSIZEOF(choices), choices,
style);
}
};
TestProcessEnter(&ComboBoxCreator::Do);
}
#endif //wxUSE_COMBOBOX #endif //wxUSE_COMBOBOX

View File

@@ -1249,4 +1249,19 @@ void TextCtrlTestCase::XYToPositionSingleLine()
} }
} }
TEST_CASE("wxTextCtrl::ProcessEnter", "[wxTextCtrl][enter]")
{
struct TextCtrlCreator
{
static wxControl* Do(wxWindow* parent, int style)
{
return new wxTextCtrl(parent, wxID_ANY, wxString(),
wxDefaultPosition, wxDefaultSize,
style);
}
};
TestProcessEnter(&TextCtrlCreator::Do);
}
#endif //wxUSE_TEXTCTRL #endif //wxUSE_TEXTCTRL

View File

@@ -10,8 +10,12 @@
#ifndef WX_PRECOMP #ifndef WX_PRECOMP
#include "wx/app.h" #include "wx/app.h"
#include "wx/dialog.h"
#include "wx/event.h" #include "wx/event.h"
#include "wx/sizer.h"
#include "wx/textctrl.h"
#include "wx/textentry.h" #include "wx/textentry.h"
#include "wx/timer.h"
#include "wx/window.h" #include "wx/window.h"
#endif // WX_PRECOMP #endif // WX_PRECOMP
@@ -364,3 +368,122 @@ void TextEntryTestCase::UndoRedo()
} }
} }
} }
#if wxUSE_UIACTIONSIMULATOR
namespace
{
enum ProcessEnter
{
ProcessEnter_No,
ProcessEnter_ButSkip,
ProcessEnter_WithoutSkipping
};
class TestDialog : public wxDialog
{
public:
explicit TestDialog(TextLikeControlCreator controlCreator,
ProcessEnter processEnter)
: wxDialog(wxTheApp->GetTopWindow(), wxID_ANY, "Test dialog"),
m_control((*controlCreator)(this,
processEnter == ProcessEnter_No
? 0
: wxTE_PROCESS_ENTER)),
m_processEnter(processEnter),
m_gotEnter(false)
{
// We can't always bind this handler because wx will helpfully
// complain if we bind it without using wxTE_PROCESS_ENTER.
if ( processEnter != ProcessEnter_No )
m_control->Bind(wxEVT_TEXT_ENTER, &TestDialog::OnTextEnter, this);
wxSizer* const sizer = new wxBoxSizer(wxVERTICAL);
sizer->Add(m_control, wxSizerFlags().Expand());
sizer->Add(CreateStdDialogButtonSizer(wxOK));
SetSizerAndFit(sizer);
CallAfter(&TestDialog::SimulateEnter);
m_timer.Bind(wxEVT_TIMER, &TestDialog::OnTimeOut, this);
m_timer.StartOnce(2000);
}
bool GotEnter() const { return m_gotEnter; }
private:
void OnTextEnter(wxCommandEvent& e)
{
m_gotEnter = true;
switch ( m_processEnter )
{
case ProcessEnter_No:
FAIL("Shouldn't be getting wxEVT_TEXT_ENTER at all");
break;
case ProcessEnter_ButSkip:
e.Skip();
break;
case ProcessEnter_WithoutSkipping:
// Close the dialog with a different exit code than what
// pressing the OK button would have generated.
EndModal(wxID_APPLY);
break;
}
}
void OnTimeOut(wxTimerEvent&)
{
EndModal(wxID_CANCEL);
}
void SimulateEnter()
{
wxUIActionSimulator sim;
m_control->SetFocus();
sim.Char(WXK_RETURN);
}
wxControl* const m_control;
const ProcessEnter m_processEnter;
wxTimer m_timer;
bool m_gotEnter;
};
} // anonymous namespace
void TestProcessEnter(TextLikeControlCreator controlCreator)
{
SECTION("Without wxTE_PROCESS_ENTER")
{
TestDialog dlg(controlCreator, ProcessEnter_No);
REQUIRE( dlg.ShowModal() == wxID_OK );
CHECK( !dlg.GotEnter() );
}
SECTION("With wxTE_PROCESS_ENTER but skipping")
{
TestDialog dlgProcessEnter(controlCreator, ProcessEnter_ButSkip);
REQUIRE( dlgProcessEnter.ShowModal() == wxID_OK );
CHECK( dlgProcessEnter.GotEnter() );
}
SECTION("With wxTE_PROCESS_ENTER without skipping")
{
TestDialog dlgProcessEnter(controlCreator, ProcessEnter_WithoutSkipping);
REQUIRE( dlgProcessEnter.ShowModal() == wxID_APPLY );
CHECK( dlgProcessEnter.GotEnter() );
}
}
#else // !wxUSE_UIACTIONSIMULATOR
void TestProcessEnter(TextLikeControlCreator WXUNUSED(controlCreator))
{
WARN("Skipping wxTE_PROCESS_ENTER tests: wxUIActionSimulator not available");
}
#endif // wxUSE_UIACTIONSIMULATOR/!wxUSE_UIACTIONSIMULATOR

View File

@@ -75,4 +75,11 @@ private:
wxDECLARE_NO_COPY_CLASS(TextEntryTestCase); wxDECLARE_NO_COPY_CLASS(TextEntryTestCase);
}; };
// Function to call to test that wxTE_PROCESS_ENTER is handled correctly for
// the controls of the type created by the given creator function when they're
// placed in a dialog with a default button.
typedef wxControl* (*TextLikeControlCreator)(wxWindow* parent, int style);
void TestProcessEnter(TextLikeControlCreator controlCreator);
#endif // _WX_TESTS_CONTROLS_TEXTENTRYTEST_H_ #endif // _WX_TESTS_CONTROLS_TEXTENTRYTEST_H_