Add unit test for wxTextCtrl::HitTest() for single line controls

Note that the control needs to be created with a reasonable size for
HitTest() to work correctly (at least under MSW, but probably not only),
so change the test set up function to use some fixed size for all kinds
of controls instead of doing it only for the multi-line ones.
This commit is contained in:
Vadim Zeitlin
2018-06-03 16:58:38 +02:00
parent 690b95646b
commit 06f554775c

View File

@@ -59,6 +59,7 @@ private:
WXUISIM_TEST( MaxLength ); WXUISIM_TEST( MaxLength );
CPPUNIT_TEST( PositionToXYSingleLine ); CPPUNIT_TEST( PositionToXYSingleLine );
CPPUNIT_TEST( XYToPositionSingleLine ); CPPUNIT_TEST( XYToPositionSingleLine );
CPPUNIT_TEST( HitTestSingleLine );
SINGLE_AND_MULTI_TESTS(); SINGLE_AND_MULTI_TESTS();
// Now switch to the multi-line text controls. // Now switch to the multi-line text controls.
@@ -112,6 +113,7 @@ private:
void MaxLength(); void MaxLength();
void StreamInput(); void StreamInput();
void Redirector(); void Redirector();
void HitTestSingleLine();
//void ProcessEnter(); //void ProcessEnter();
void Url(); void Url();
void Style(); void Style();
@@ -164,12 +166,8 @@ long TextCtrlTestCase::ms_style = 0;
void TextCtrlTestCase::CreateText(long extraStyles) void TextCtrlTestCase::CreateText(long extraStyles)
{ {
wxSize size;
if ( ms_style == wxTE_MULTILINE )
size = wxSize(400, TEXT_HEIGHT);
m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY, "", m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY, "",
wxDefaultPosition, size, wxDefaultPosition, wxSize(400, TEXT_HEIGHT),
ms_style | extraStyles); ms_style | extraStyles);
} }
@@ -342,6 +340,51 @@ void TextCtrlTestCase::Redirector()
#endif #endif
} }
void TextCtrlTestCase::HitTestSingleLine()
{
m_text->ChangeValue("Hit me");
// We don't know the size of the text borders, so we can't really do any
// exact tests, just try to verify that the results are roughly as
// expected.
const wxSize sizeChar = m_text->GetTextExtent("X");
const int yMid = sizeChar.y / 2;
long pos = -1;
// Hitting a point near the left side of the control should find one of the
// first few characters under it.
SECTION("Normal")
{
REQUIRE( m_text->HitTest(wxPoint(2*sizeChar.x, yMid), &pos) == wxTE_HT_ON_TEXT );
CHECK( pos >= 0 );
CHECK( pos < 3 );
}
// Hitting a point well beyond the end of the text shouldn't find any valid
// character.
SECTION("Beyond")
{
REQUIRE( m_text->HitTest(wxPoint(20*sizeChar.x, yMid), &pos) == wxTE_HT_BEYOND );
CHECK( pos == m_text->GetLastPosition() );
}
// Making the control scroll, by ensuring that its contents is too long to
// show inside its window, should change the hit test result for the same
// position as used above.
SECTION("Scrolled")
{
m_text->ChangeValue(wxString(200, 'X'));
m_text->SetInsertionPointEnd();
// wxGTK must be given an opportunity to lay the text out.
wxYield();
REQUIRE( m_text->HitTest(wxPoint(2*sizeChar.x, yMid), &pos) == wxTE_HT_ON_TEXT );
CHECK( pos > 3 );
}
}
#if 0 #if 0
void TextCtrlTestCase::ProcessEnter() void TextCtrlTestCase::ProcessEnter()
{ {