X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/729f53d4213a02fc7131afb4649755f20b796270..17beda75ef9780ebef5044e41889a9b598dcf186:/tests/sizers/boxsizer.cpp diff --git a/tests/sizers/boxsizer.cpp b/tests/sizers/boxsizer.cpp index 3087f6f07c..118cacf494 100644 --- a/tests/sizers/boxsizer.cpp +++ b/tests/sizers/boxsizer.cpp @@ -20,12 +20,10 @@ #ifndef WX_PRECOMP #include "wx/app.h" #include "wx/sizer.h" + #include "wx/listbox.h" #endif // WX_PRECOMP -inline std::ostream& operator<<(std::ostream& o, const wxSize& s) -{ - return o << s.x << 'x' << s.y; -} +#include "asserthelper.h" // ---------------------------------------------------------------------------- // test class @@ -43,10 +41,18 @@ private: CPPUNIT_TEST_SUITE( BoxSizerTestCase ); CPPUNIT_TEST( Size1 ); CPPUNIT_TEST( Size3 ); + CPPUNIT_TEST( CalcMin ); + CPPUNIT_TEST( BestSizeRespectsMaxSize ); + CPPUNIT_TEST( RecalcSizesRespectsMaxSize1 ); + CPPUNIT_TEST( RecalcSizesRespectsMaxSize2 ); CPPUNIT_TEST_SUITE_END(); void Size1(); void Size3(); + void CalcMin(); + void BestSizeRespectsMaxSize(); + void RecalcSizesRespectsMaxSize1(); + void RecalcSizesRespectsMaxSize2(); wxWindow *m_win; wxSizer *m_sizer; @@ -57,7 +63,7 @@ private: // register in the unnamed registry so that these tests are run by default CPPUNIT_TEST_SUITE_REGISTRATION( BoxSizerTestCase ); -// also include in it's own registry so that these tests can be run alone +// also include in its own registry so that these tests can be run alone CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( BoxSizerTestCase, "BoxSizerTestCase" ); // ---------------------------------------------------------------------------- @@ -185,7 +191,6 @@ void BoxSizerTestCase::Size3() child[1] = new wxWindow(m_win, wxID_ANY); child[2] = new wxWindow(m_win, wxID_ANY); - int j; for ( unsigned i = 0; i < WXSIZEOF(layoutTestData); i++ ) { LayoutTestData ltd = layoutTestData[i]; @@ -219,6 +224,8 @@ void BoxSizerTestCase::Size3() } m_sizer->Clear(); + + unsigned j; for ( j = 0; j < WXSIZEOF(child); j++ ) ltd.AddToSizer(m_sizer, child[j], j); @@ -230,7 +237,7 @@ void BoxSizerTestCase::Size3() WX_ASSERT_EQUAL_MESSAGE ( ( - "test %lu, permutation #%d: wrong size for child #%d " + "test %lu, permutation #%lu: wrong size for child #%d " "for total size %d", static_cast(i), static_cast(p), @@ -247,3 +254,126 @@ void BoxSizerTestCase::Size3() } } } + +void BoxSizerTestCase::CalcMin() +{ + static const unsigned NUM_TEST_ITEM = 3; + + static const struct CalcMinTestData + { + // proportions of the elements, if one of them is -1 it means to not + // use this window at all in this test + int prop[NUM_TEST_ITEM]; + + // minimal sizes of the elements in the sizer direction + int minsize[NUM_TEST_ITEM]; + + // the expected minimal sizer size + int total; + } calcMinTestData[] = + { + { { 1, 1, -1 }, { 30, 50, 0 }, 100 }, + { { 1, 1, 0 }, { 30, 50, 20 }, 120 }, + { { 10, 10, -1 }, { 30, 50, 0 }, 100 }, + { { 1, 2, 2 }, { 50, 50, 80 }, 250 }, + { { 1, 2, 2 }, { 100, 50, 80 }, 500 }, + }; + + unsigned n; + wxWindow *child[NUM_TEST_ITEM]; + for ( n = 0; n < NUM_TEST_ITEM; n++ ) + child[n] = new wxWindow(m_win, wxID_ANY); + + for ( unsigned i = 0; i < WXSIZEOF(calcMinTestData); i++ ) + { + m_sizer->Clear(); + + const CalcMinTestData& cmtd = calcMinTestData[i]; + for ( n = 0; n < NUM_TEST_ITEM; n++ ) + { + if ( cmtd.prop[n] != -1 ) + { + child[n]->SetInitialSize(wxSize(cmtd.minsize[n], -1)); + m_sizer->Add(child[n], wxSizerFlags(cmtd.prop[n])); + } + } + + WX_ASSERT_EQUAL_MESSAGE + ( + ("In test #%u", i), + cmtd.total, m_sizer->CalcMin().x + ); + } +} + +void BoxSizerTestCase::BestSizeRespectsMaxSize() +{ + m_sizer->Clear(); + + const int maxWidth = 100; + + wxSizer* sizer = new wxBoxSizer(wxVERTICAL); + wxListBox* listbox = new wxListBox(m_win, wxID_ANY); + listbox->Append("some very very very very very very very very very very very long string"); + listbox->SetMaxSize(wxSize(maxWidth, -1)); + sizer->Add(listbox); + + m_sizer->Add(sizer); + m_win->Layout(); + + CPPUNIT_ASSERT_EQUAL(maxWidth, listbox->GetSize().GetWidth()); +} + +void BoxSizerTestCase::RecalcSizesRespectsMaxSize1() +{ + m_sizer->Clear(); + + const int maxWidth = 100; + + m_win->SetClientSize(300, 300); + + wxSizer* sizer1 = new wxBoxSizer(wxVERTICAL); + m_sizer->Add(sizer1); + + wxListBox* listbox1 = new wxListBox(m_win, wxID_ANY); + listbox1->Append("some very very very very very very very very very very very long string"); + sizer1->Add(listbox1); + + wxSizer* sizer2 = new wxBoxSizer(wxHORIZONTAL); + sizer1->Add(sizer2, wxSizerFlags().Expand()); + + wxListBox* listbox2 = new wxListBox(m_win, wxID_ANY); + listbox2->Append("some string"); + listbox2->SetMaxSize(wxSize(100, -1)); + sizer2->Add(listbox2, wxSizerFlags().Proportion(1)); + + m_win->Layout(); + + CPPUNIT_ASSERT_EQUAL(maxWidth, listbox2->GetSize().GetWidth()); +} + +void BoxSizerTestCase::RecalcSizesRespectsMaxSize2() +{ + m_sizer->Clear(); + + m_win->SetClientSize(300, 300); + + wxSizer* sizer1 = new wxBoxSizer(wxVERTICAL); + m_sizer->Add(sizer1, wxSizerFlags().Expand()); + + wxWindow* child1 = new wxWindow(m_win, wxID_ANY); + sizer1->Add(child1, wxSizerFlags().Proportion(1)); + + wxWindow* child2 = new wxWindow(m_win, wxID_ANY); + child2->SetMaxSize(wxSize(-1, 50)); + sizer1->Add(child2, wxSizerFlags().Proportion(1)); + + wxWindow* child3 = new wxWindow(m_win, wxID_ANY); + sizer1->Add(child3, wxSizerFlags().Proportion(1)); + + m_win->Layout(); + + CPPUNIT_ASSERT_EQUAL(125, child1->GetSize().GetHeight()); + CPPUNIT_ASSERT_EQUAL(50, child2->GetSize().GetHeight()); + CPPUNIT_ASSERT_EQUAL(125, child3->GetSize().GetHeight()); +}