Merge branch 'radiobox-without-items'

Handle wxRadiobox without items correctly in wxOSX.

See #22755.
This commit is contained in:
Vadim Zeitlin 2022-08-30 12:20:54 +02:00
commit 2545cf5b8d
2 changed files with 84 additions and 94 deletions

View file

@ -59,18 +59,21 @@ wxRadioBox::~wxRadioBox()
wxRadioButton *next, *current;
current = m_radioButtonCycle->NextInCycle();
current = m_radioButtonCycle;
if (current != NULL)
{
while (current != m_radioButtonCycle)
// We need to start deleting the buttons from the second one because
// deleting the first one would change the pointers stored in them.
for (current = current->NextInCycle();;)
{
next = current->NextInCycle();
delete current;
if (next == m_radioButtonCycle)
break;
current = next;
}
delete current;
}
}
@ -339,6 +342,9 @@ void wxRadioBox::Command( wxCommandEvent& event )
//
void wxRadioBox::SetFocus()
{
if (!m_radioButtonCycle)
return;
wxRadioButton *current;
current = m_radioButtonCycle;

View file

@ -16,51 +16,21 @@
#include "wx/radiobox.h"
#endif // WX_PRECOMP
#include "wx/scopedptr.h"
#include "wx/tooltip.h"
class RadioBoxTestCase : public CppUnit::TestCase
class RadioBoxTestCase
{
public:
RadioBoxTestCase() { }
void setUp() wxOVERRIDE;
void tearDown() wxOVERRIDE;
private:
CPPUNIT_TEST_SUITE( RadioBoxTestCase );
CPPUNIT_TEST( FindString );
CPPUNIT_TEST( RowColCount );
CPPUNIT_TEST( Enable );
CPPUNIT_TEST( Show );
CPPUNIT_TEST( HelpText );
CPPUNIT_TEST( ToolTip );
CPPUNIT_TEST( Selection );
CPPUNIT_TEST( Count );
CPPUNIT_TEST( SetString );
CPPUNIT_TEST_SUITE_END();
void FindString();
void RowColCount();
void Enable();
void Show();
void HelpText();
void ToolTip();
void Selection();
void Count();
void SetString();
protected:
RadioBoxTestCase();
~RadioBoxTestCase();
wxRadioBox* m_radio;
wxDECLARE_NO_COPY_CLASS(RadioBoxTestCase);
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( RadioBoxTestCase );
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RadioBoxTestCase, "RadioBoxTestCase" );
void RadioBoxTestCase::setUp()
RadioBoxTestCase::RadioBoxTestCase()
{
wxArrayString choices;
choices.push_back("item 0");
@ -71,20 +41,20 @@ void RadioBoxTestCase::setUp()
wxDefaultPosition, wxDefaultSize, choices);
}
void RadioBoxTestCase::tearDown()
RadioBoxTestCase::~RadioBoxTestCase()
{
wxTheApp->GetTopWindow()->DestroyChildren();
}
void RadioBoxTestCase::FindString()
TEST_CASE_METHOD(RadioBoxTestCase, "RadioBox::FindString", "[radiobox][find]")
{
CPPUNIT_ASSERT_EQUAL(wxNOT_FOUND, m_radio->FindString("not here"));
CPPUNIT_ASSERT_EQUAL(1, m_radio->FindString("item 1"));
CPPUNIT_ASSERT_EQUAL(2, m_radio->FindString("ITEM 2"));
CPPUNIT_ASSERT_EQUAL(wxNOT_FOUND, m_radio->FindString("ITEM 2", true));
CHECK( m_radio->FindString("not here") == wxNOT_FOUND );
CHECK( m_radio->FindString("item 1") == 1 );
CHECK( m_radio->FindString("ITEM 2") == 2 );
CHECK( m_radio->FindString("ITEM 2", true) == wxNOT_FOUND );
}
void RadioBoxTestCase::RowColCount()
TEST_CASE_METHOD(RadioBoxTestCase, "RadioBox::RowColCount", "[radiobox]")
{
#ifndef __WXGTK__
wxArrayString choices;
@ -95,132 +65,146 @@ void RadioBoxTestCase::RowColCount()
m_radio = new wxRadioBox(wxTheApp->GetTopWindow(), wxID_ANY, "RadioBox",
wxDefaultPosition, wxDefaultSize, choices, 2);
CPPUNIT_ASSERT_EQUAL(2, m_radio->GetColumnCount());
CPPUNIT_ASSERT_EQUAL(2, m_radio->GetRowCount());
CHECK( m_radio->GetColumnCount() == 2 );
CHECK( m_radio->GetRowCount() == 2 );
m_radio = new wxRadioBox(wxTheApp->GetTopWindow(), wxID_ANY, "RadioBox",
wxDefaultPosition, wxDefaultSize, choices, 1,
wxRA_SPECIFY_ROWS);
CPPUNIT_ASSERT_EQUAL(3, m_radio->GetColumnCount());
CPPUNIT_ASSERT_EQUAL(1, m_radio->GetRowCount());
CHECK( m_radio->GetColumnCount() == 3 );
CHECK( m_radio->GetRowCount() == 1 );
#endif
}
void RadioBoxTestCase::Enable()
TEST_CASE_METHOD(RadioBoxTestCase, "RadioBox::Enable", "[radiobox][enable]")
{
#ifndef __WXOSX__
m_radio->Enable(false);
CPPUNIT_ASSERT(!m_radio->IsItemEnabled(0));
CHECK(!m_radio->IsItemEnabled(0));
m_radio->Enable(1, true);
CPPUNIT_ASSERT(!m_radio->IsItemEnabled(0));
CPPUNIT_ASSERT(m_radio->IsItemEnabled(1));
CPPUNIT_ASSERT(!m_radio->IsItemEnabled(2));
CHECK(!m_radio->IsItemEnabled(0));
CHECK(m_radio->IsItemEnabled(1));
CHECK(!m_radio->IsItemEnabled(2));
m_radio->Enable(true);
CPPUNIT_ASSERT(m_radio->IsItemEnabled(0));
CPPUNIT_ASSERT(m_radio->IsItemEnabled(1));
CPPUNIT_ASSERT(m_radio->IsItemEnabled(2));
CHECK(m_radio->IsItemEnabled(0));
CHECK(m_radio->IsItemEnabled(1));
CHECK(m_radio->IsItemEnabled(2));
m_radio->Enable(0, false);
CPPUNIT_ASSERT(!m_radio->IsItemEnabled(0));
CPPUNIT_ASSERT(m_radio->IsItemEnabled(1));
CPPUNIT_ASSERT(m_radio->IsItemEnabled(2));
CHECK(!m_radio->IsItemEnabled(0));
CHECK(m_radio->IsItemEnabled(1));
CHECK(m_radio->IsItemEnabled(2));
#endif
}
void RadioBoxTestCase::Show()
TEST_CASE_METHOD(RadioBoxTestCase, "RadioBox::Show", "[radiobox][show]")
{
m_radio->Show(false);
CPPUNIT_ASSERT(!m_radio->IsItemShown(0));
CHECK(!m_radio->IsItemShown(0));
m_radio->Show(1, true);
CPPUNIT_ASSERT(!m_radio->IsItemShown(0));
CPPUNIT_ASSERT(m_radio->IsItemShown(1));
CPPUNIT_ASSERT(!m_radio->IsItemShown(2));
CHECK(!m_radio->IsItemShown(0));
CHECK(m_radio->IsItemShown(1));
CHECK(!m_radio->IsItemShown(2));
m_radio->Show(true);
CPPUNIT_ASSERT(m_radio->IsItemShown(0));
CPPUNIT_ASSERT(m_radio->IsItemShown(1));
CPPUNIT_ASSERT(m_radio->IsItemShown(2));
CHECK(m_radio->IsItemShown(0));
CHECK(m_radio->IsItemShown(1));
CHECK(m_radio->IsItemShown(2));
m_radio->Show(0, false);
CPPUNIT_ASSERT(!m_radio->IsItemShown(0));
CPPUNIT_ASSERT(m_radio->IsItemShown(1));
CPPUNIT_ASSERT(m_radio->IsItemShown(2));
CHECK(!m_radio->IsItemShown(0));
CHECK(m_radio->IsItemShown(1));
CHECK(m_radio->IsItemShown(2));
}
void RadioBoxTestCase::HelpText()
TEST_CASE_METHOD(RadioBoxTestCase, "RadioBox::HelpText", "[radiobox][help]")
{
CPPUNIT_ASSERT_EQUAL(wxEmptyString, m_radio->GetItemHelpText(0));
CHECK( m_radio->GetItemHelpText(0) == wxEmptyString );
m_radio->SetItemHelpText(1, "Item 1 help");
CPPUNIT_ASSERT_EQUAL("Item 1 help", m_radio->GetItemHelpText(1));
CHECK( m_radio->GetItemHelpText(1) == "Item 1 help" );
m_radio->SetItemHelpText(1, "");
CPPUNIT_ASSERT_EQUAL(wxEmptyString, m_radio->GetItemHelpText(1));
CHECK( m_radio->GetItemHelpText(1) == wxEmptyString );
}
void RadioBoxTestCase::ToolTip()
TEST_CASE_METHOD(RadioBoxTestCase, "RadioBox::ToolTip", "[radiobox][tooltip]")
{
#if defined (__WXMSW__) || defined(__WXGTK__)
//GetItemToolTip returns null if there is no tooltip set
CPPUNIT_ASSERT(!m_radio->GetItemToolTip(0));
CHECK(!m_radio->GetItemToolTip(0));
m_radio->SetItemToolTip(1, "Item 1 help");
CPPUNIT_ASSERT_EQUAL("Item 1 help", m_radio->GetItemToolTip(1)->GetTip());
CHECK( m_radio->GetItemToolTip(1)->GetTip() == "Item 1 help" );
m_radio->SetItemToolTip(1, "");
//However if we set a blank tip this does count as a tooltip
CPPUNIT_ASSERT(!m_radio->GetItemToolTip(1));
CHECK(!m_radio->GetItemToolTip(1));
#endif
}
void RadioBoxTestCase::Selection()
TEST_CASE_METHOD(RadioBoxTestCase, "RadioBox::Selection", "[radiobox][selection]")
{
//Until other item containers the first item is selected by default
CPPUNIT_ASSERT_EQUAL(0, m_radio->GetSelection());
CPPUNIT_ASSERT_EQUAL("item 0", m_radio->GetStringSelection());
CHECK( m_radio->GetSelection() == 0 );
CHECK( m_radio->GetStringSelection() == "item 0" );
m_radio->SetSelection(1);
CPPUNIT_ASSERT_EQUAL(1, m_radio->GetSelection());
CPPUNIT_ASSERT_EQUAL("item 1", m_radio->GetStringSelection());
CHECK( m_radio->GetSelection() == 1 );
CHECK( m_radio->GetStringSelection() == "item 1" );
m_radio->SetStringSelection("item 2");
CPPUNIT_ASSERT_EQUAL(2, m_radio->GetSelection());
CPPUNIT_ASSERT_EQUAL("item 2", m_radio->GetStringSelection());
CHECK( m_radio->GetSelection() == 2 );
CHECK( m_radio->GetStringSelection() == "item 2" );
}
void RadioBoxTestCase::Count()
TEST_CASE_METHOD(RadioBoxTestCase, "RadioBox::Count", "[radiobox]")
{
//A trivial test for the item count as items can neither
//be added or removed
CPPUNIT_ASSERT_EQUAL(3, m_radio->GetCount());
CPPUNIT_ASSERT(!m_radio->IsEmpty());
CHECK( m_radio->GetCount() == 3 );
CHECK(!m_radio->IsEmpty());
}
void RadioBoxTestCase::SetString()
TEST_CASE_METHOD(RadioBoxTestCase, "RadioBox::SetString", "[radiobox]")
{
m_radio->SetString(0, "new item 0");
m_radio->SetString(2, "");
CPPUNIT_ASSERT_EQUAL("new item 0", m_radio->GetString(0));
CPPUNIT_ASSERT_EQUAL("", m_radio->GetString(2));
CHECK( m_radio->GetString(0) == "new item 0" );
CHECK( m_radio->GetString(2) == "" );
}
TEST_CASE("RadioBox::NoItems", "[radiobox]")
{
wxScopedPtr<wxRadioBox>
radio(new wxRadioBox(wxTheApp->GetTopWindow(), wxID_ANY, "Empty",
wxDefaultPosition, wxDefaultSize,
0, NULL,
1, wxRA_SPECIFY_COLS));
CHECK( radio->GetCount() == 0 );
CHECK( radio->IsEmpty() );
CHECK_NOTHROW( radio->SetFocus() );
}
#endif // wxUSE_RADIOBOX