initial commit
Signed-off-by: Peter Siegmund <mars3142@noreply.mars3142.dev>
This commit is contained in:
485
libs/wxWidgets-3.3.1/tests/sizers/boxsizer.cpp
Normal file
485
libs/wxWidgets-3.3.1/tests/sizers/boxsizer.cpp
Normal file
@@ -0,0 +1,485 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Name: tests/sizers/boxsizer.cpp
|
||||
// Purpose: Unit tests for wxBoxSizer
|
||||
// Author: Vadim Zeitlin
|
||||
// Created: 2010-03-06
|
||||
// Copyright: (c) 2010 Vadim Zeitlin <vadim@wxwidgets.org>
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// headers
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#include "testprec.h"
|
||||
|
||||
|
||||
#ifndef WX_PRECOMP
|
||||
#include "wx/app.h"
|
||||
#include "wx/sizer.h"
|
||||
#include "wx/listbox.h"
|
||||
#endif // WX_PRECOMP
|
||||
|
||||
#include "asserthelper.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// test fixture
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
class BoxSizerTestCase
|
||||
{
|
||||
public:
|
||||
BoxSizerTestCase()
|
||||
: m_win(new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY)),
|
||||
m_sizer(new wxBoxSizer(wxHORIZONTAL))
|
||||
{
|
||||
m_win->SetClientSize(127, 35);
|
||||
m_win->SetSizer(m_sizer);
|
||||
}
|
||||
|
||||
~BoxSizerTestCase()
|
||||
{
|
||||
delete m_win;
|
||||
}
|
||||
|
||||
protected:
|
||||
wxWindow* const m_win;
|
||||
wxSizer* const m_sizer;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// tests themselves
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE_METHOD(BoxSizerTestCase, "BoxSizer::Size1", "[sizer]")
|
||||
{
|
||||
const wxSize sizeTotal = m_win->GetClientSize();
|
||||
const wxSize sizeChild = sizeTotal / 2;
|
||||
|
||||
wxWindow * const
|
||||
child = new wxWindow(m_win, wxID_ANY, wxDefaultPosition, sizeChild);
|
||||
m_sizer->Add(child);
|
||||
m_win->Layout();
|
||||
CHECK(child->GetSize() == sizeChild);
|
||||
|
||||
m_sizer->Clear();
|
||||
m_sizer->Add(child, wxSizerFlags(1));
|
||||
m_win->Layout();
|
||||
CHECK( child->GetSize() == wxSize(sizeTotal.x, sizeChild.y) );
|
||||
|
||||
m_sizer->Clear();
|
||||
m_sizer->Add(child, wxSizerFlags(1).Expand());
|
||||
m_win->Layout();
|
||||
CHECK(child->GetSize() == sizeTotal);
|
||||
|
||||
m_sizer->Clear();
|
||||
m_sizer->Add(child, wxSizerFlags());
|
||||
m_sizer->SetItemMinSize(child, sizeTotal*2);
|
||||
m_win->Layout();
|
||||
CHECK(child->GetSize() == sizeTotal);
|
||||
|
||||
m_sizer->Clear();
|
||||
m_sizer->Add(child, wxSizerFlags().Expand());
|
||||
m_sizer->SetItemMinSize(child, sizeTotal*2);
|
||||
m_win->Layout();
|
||||
CHECK(child->GetSize() == sizeTotal);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(BoxSizerTestCase, "BoxSizer::Size3", "[sizer]")
|
||||
{
|
||||
wxGCC_WARNING_SUPPRESS(missing-field-initializers)
|
||||
|
||||
// check that various combinations of minimal sizes and proportions work as
|
||||
// expected for different window sizes
|
||||
static const struct LayoutTestData
|
||||
{
|
||||
// proportions of the elements
|
||||
int prop[3];
|
||||
|
||||
// minimal sizes of the elements in the sizer direction
|
||||
int minsize[3];
|
||||
|
||||
// total size and the expected sizes of the elements
|
||||
int x,
|
||||
sizes[3];
|
||||
|
||||
// if true, don't try the permutations of our test data
|
||||
bool dontPermute;
|
||||
|
||||
|
||||
// Add the given window to the sizer with the corresponding parameters
|
||||
void AddToSizer(wxSizer *sizer, wxWindow *win, int n) const
|
||||
{
|
||||
sizer->Add(win, wxSizerFlags(prop[n]));
|
||||
sizer->SetItemMinSize(win, wxSize(minsize[n], -1));
|
||||
}
|
||||
|
||||
} layoutTestData[] =
|
||||
{
|
||||
// some really simple cases (no need to permute those, they're
|
||||
// symmetrical anyhow)
|
||||
{ { 1, 1, 1, }, { 50, 50, 50, }, 150, { 50, 50, 50, }, true },
|
||||
{ { 2, 2, 2, }, { 50, 50, 50, }, 600, { 200, 200, 200, }, true },
|
||||
|
||||
// items with different proportions and min sizes when there is enough
|
||||
// space to lay them out
|
||||
{ { 1, 2, 3, }, { 0, 0, 0, }, 600, { 100, 200, 300, } },
|
||||
{ { 1, 2, 3, }, { 100, 100, 100, }, 600, { 100, 200, 300, } },
|
||||
{ { 1, 2, 3, }, { 100, 50, 50, }, 600, { 100, 200, 300, } },
|
||||
{ { 0, 1, 1, }, { 200, 100, 100, }, 600, { 200, 200, 200, } },
|
||||
{ { 0, 1, 2, }, { 300, 100, 100, }, 600, { 300, 100, 200, } },
|
||||
{ { 0, 1, 1, }, { 100, 50, 50, }, 300, { 100, 100, 100, } },
|
||||
{ { 0, 1, 2, }, { 100, 50, 50, }, 400, { 100, 100, 200, } },
|
||||
|
||||
// cases when there is not enough space to lay out the items correctly
|
||||
// while still respecting their min sizes
|
||||
{ { 0, 1, 1, }, { 100, 150, 50, }, 300, { 100, 150, 50, } },
|
||||
{ { 1, 2, 3, }, { 100, 100, 100, }, 300, { 100, 100, 100, } },
|
||||
{ { 1, 2, 3, }, { 100, 50, 50, }, 300, { 100, 80, 120, } },
|
||||
{ { 1, 2, 3, }, { 100, 10, 10, }, 150, { 100, 20, 30, } },
|
||||
|
||||
// cases when there is not enough space even for the min sizes (don't
|
||||
// permute in these cases as the layout does depend on the item order
|
||||
// because the first ones have priority)
|
||||
{ { 1, 2, 3, }, { 100, 50, 50, }, 150, { 100, 50, 0, }, true },
|
||||
{ { 1, 2, 3, }, { 100, 100, 100, }, 200, { 100, 100, 0, }, true },
|
||||
{ { 1, 2, 3, }, { 100, 100, 100, }, 150, { 100, 50, 0, }, true },
|
||||
{ { 1, 2, 3, }, { 100, 100, 100, }, 50, { 50, 0, 0, }, true },
|
||||
{ { 1, 2, 3, }, { 100, 100, 100, }, 0, { 0, 0, 0, }, true },
|
||||
};
|
||||
|
||||
wxGCC_WARNING_RESTORE(missing-field-initializers)
|
||||
|
||||
wxWindow *child[3];
|
||||
child[0] = new wxWindow(m_win, wxID_ANY);
|
||||
child[1] = new wxWindow(m_win, wxID_ANY);
|
||||
child[2] = new wxWindow(m_win, wxID_ANY);
|
||||
|
||||
for ( unsigned i = 0; i < WXSIZEOF(layoutTestData); i++ )
|
||||
{
|
||||
LayoutTestData ltd = layoutTestData[i];
|
||||
|
||||
// the results shouldn't depend on the order of items except in the
|
||||
// case when there is not enough space for even the fixed width items
|
||||
// (in which case the first ones might get enough of it but not the
|
||||
// last ones) so test a couple of permutations of test data unless
|
||||
// specifically disabled for this test case
|
||||
for ( unsigned p = 0; p < 3; p++)
|
||||
{
|
||||
switch ( p )
|
||||
{
|
||||
case 0:
|
||||
// nothing to do, use original data
|
||||
break;
|
||||
|
||||
case 1:
|
||||
// exchange first and last elements
|
||||
wxSwap(ltd.prop[0], ltd.prop[2]);
|
||||
wxSwap(ltd.minsize[0], ltd.minsize[2]);
|
||||
wxSwap(ltd.sizes[0], ltd.sizes[2]);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
// exchange the original third and second elements
|
||||
wxSwap(ltd.prop[0], ltd.prop[1]);
|
||||
wxSwap(ltd.minsize[0], ltd.minsize[1]);
|
||||
wxSwap(ltd.sizes[0], ltd.sizes[1]);
|
||||
break;
|
||||
}
|
||||
|
||||
m_sizer->Clear();
|
||||
|
||||
unsigned j;
|
||||
for ( j = 0; j < WXSIZEOF(child); j++ )
|
||||
ltd.AddToSizer(m_sizer, child[j], j);
|
||||
|
||||
m_win->SetClientSize(ltd.x, -1);
|
||||
m_win->Layout();
|
||||
|
||||
for ( j = 0; j < WXSIZEOF(child); j++ )
|
||||
{
|
||||
WX_ASSERT_EQUAL_MESSAGE
|
||||
(
|
||||
(
|
||||
"test %lu, permutation #%lu: wrong size for child #%d "
|
||||
"for total size %d",
|
||||
static_cast<unsigned long>(i),
|
||||
static_cast<unsigned long>(p),
|
||||
j,
|
||||
ltd.x
|
||||
),
|
||||
ltd.sizes[j], child[j]->GetSize().x
|
||||
);
|
||||
}
|
||||
|
||||
// don't try other permutations if explicitly disabled
|
||||
if ( ltd.dontPermute )
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(BoxSizerTestCase, "BoxSizer::CalcMin", "[sizer]")
|
||||
{
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(BoxSizerTestCase, "BoxSizer::SetMinSize", "[sizer]")
|
||||
{
|
||||
wxWindow* const child = new wxWindow(m_win, wxID_ANY);
|
||||
child->SetInitialSize(wxSize(10, -1));
|
||||
m_sizer->Add(child);
|
||||
|
||||
// Setting minimal size explicitly must make GetMinSize() return at least
|
||||
// this size even if it needs a much smaller one.
|
||||
m_sizer->SetMinSize(100, 0);
|
||||
CHECK(m_sizer->GetMinSize().x == 100);
|
||||
|
||||
m_sizer->Layout();
|
||||
CHECK(m_sizer->GetMinSize().x == 100);
|
||||
}
|
||||
|
||||
#if wxUSE_LISTBOX
|
||||
TEST_CASE_METHOD(BoxSizerTestCase, "BoxSizer::BestSizeRespectsMaxSize", "[sizer]")
|
||||
{
|
||||
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();
|
||||
|
||||
CHECK(listbox->GetSize().GetWidth() == maxWidth);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(BoxSizerTestCase, "BoxSizer::RecalcSizesRespectsMaxSize1", "[sizer]")
|
||||
{
|
||||
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();
|
||||
|
||||
CHECK(listbox2->GetSize().GetWidth() == maxWidth);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_CASE_METHOD(BoxSizerTestCase, "BoxSizer::RecalcSizesRespectsMaxSize2", "[sizer]")
|
||||
{
|
||||
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();
|
||||
|
||||
CHECK(child1->GetSize().GetHeight() == 125);
|
||||
CHECK(child2->GetSize().GetHeight() == 50);
|
||||
CHECK(child3->GetSize().GetHeight() == 125);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(BoxSizerTestCase, "BoxSizer::IncompatibleFlags", "[sizer]")
|
||||
{
|
||||
// This unhygienic macro relies on having a local variable called "sizer".
|
||||
#define ASSERT_SIZER_INVALID_FLAGS(f, msg) \
|
||||
WX_ASSERT_FAILS_WITH_ASSERT_MESSAGE( \
|
||||
"Expected assertion not generated for " msg, \
|
||||
std::unique_ptr<wxSizerItem> item(new wxSizerItem(10, 10, 0, f)); \
|
||||
sizer->Add(item.get()); \
|
||||
item.release() \
|
||||
)
|
||||
|
||||
#define ASSERT_SIZER_INCOMPATIBLE_FLAGS(f1, f2) \
|
||||
ASSERT_SIZER_INVALID_FLAGS(f1 | f2, \
|
||||
"using incompatible flags " #f1 " and " #f2 \
|
||||
)
|
||||
|
||||
// First check with the horizontal sizer, which is what we use by default.
|
||||
wxSizer* sizer = m_sizer;
|
||||
|
||||
// In horizontal sizers alignment is only used in vertical direction.
|
||||
ASSERT_SIZER_INVALID_FLAGS(
|
||||
wxALIGN_RIGHT,
|
||||
"using wxALIGN_RIGHT in a horizontal sizer"
|
||||
);
|
||||
|
||||
ASSERT_SIZER_INVALID_FLAGS(
|
||||
wxALIGN_CENTRE_HORIZONTAL,
|
||||
"using wxALIGN_CENTRE_HORIZONTAL in a horizontal sizer"
|
||||
);
|
||||
|
||||
// However using wxALIGN_CENTRE_HORIZONTAL together with
|
||||
// wxALIGN_CENTRE_VERTICAL as done by wxSizerFlags::Centre() should work.
|
||||
sizer->Add(10, 10, wxSizerFlags().Centre());
|
||||
|
||||
// Combining two vertical alignment flags doesn't make sense.
|
||||
ASSERT_SIZER_INCOMPATIBLE_FLAGS(wxALIGN_BOTTOM, wxALIGN_CENTRE_VERTICAL);
|
||||
|
||||
// Combining wxEXPAND with vertical alignment doesn't make sense either.
|
||||
ASSERT_SIZER_INCOMPATIBLE_FLAGS(wxEXPAND, wxALIGN_CENTRE_VERTICAL);
|
||||
ASSERT_SIZER_INCOMPATIBLE_FLAGS(wxEXPAND, wxALIGN_BOTTOM);
|
||||
|
||||
// But combining it with these flags and wxSHAPED does make sense and so
|
||||
// shouldn't result in an assert.
|
||||
CHECK_NOTHROW(
|
||||
sizer->Add(10, 10, 0, wxEXPAND | wxSHAPED | wxALIGN_CENTRE_VERTICAL)
|
||||
);
|
||||
CHECK_NOTHROW(
|
||||
sizer->Add(10, 10, 0, wxEXPAND | wxSHAPED | wxALIGN_TOP)
|
||||
);
|
||||
|
||||
|
||||
// And now exactly the same thing in the other direction.
|
||||
sizer = new wxBoxSizer(wxVERTICAL);
|
||||
m_win->SetSizer(sizer);
|
||||
|
||||
ASSERT_SIZER_INVALID_FLAGS(
|
||||
wxALIGN_BOTTOM,
|
||||
"using wxALIGN_BOTTOM in a vertical sizer"
|
||||
);
|
||||
|
||||
ASSERT_SIZER_INVALID_FLAGS(
|
||||
wxALIGN_CENTRE_VERTICAL,
|
||||
"using wxALIGN_CENTRE_VERTICAL in a vertical sizer"
|
||||
);
|
||||
|
||||
sizer->Add(10, 10, wxSizerFlags().Centre());
|
||||
|
||||
ASSERT_SIZER_INCOMPATIBLE_FLAGS(wxALIGN_RIGHT, wxALIGN_CENTRE_HORIZONTAL);
|
||||
ASSERT_SIZER_INCOMPATIBLE_FLAGS(wxEXPAND, wxALIGN_CENTRE_HORIZONTAL);
|
||||
ASSERT_SIZER_INCOMPATIBLE_FLAGS(wxEXPAND, wxALIGN_RIGHT);
|
||||
|
||||
CHECK_NOTHROW(
|
||||
sizer->Add(10, 10, 0, wxEXPAND | wxSHAPED | wxALIGN_CENTRE_HORIZONTAL)
|
||||
);
|
||||
CHECK_NOTHROW(
|
||||
sizer->Add(10, 10, 0, wxEXPAND | wxSHAPED | wxALIGN_RIGHT)
|
||||
);
|
||||
|
||||
#undef ASSERT_SIZER_INCOMPATIBLE_FLAGS
|
||||
#undef ASSERT_SIZER_INVALID_FLAGS
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(BoxSizerTestCase, "BoxSizer::Replace", "[sizer]")
|
||||
{
|
||||
m_sizer->AddSpacer(1);
|
||||
m_sizer->Replace(0, new wxSizerItem(new wxWindow(m_win, wxID_ANY)));
|
||||
}
|
||||
|
||||
TEST_CASE("Sizer::CombineFlags", "[sizer]")
|
||||
{
|
||||
// This is a compile-time test which simply verifies that we can combine
|
||||
// all the different flags without getting any warnings about doing it --
|
||||
// as would have been the case when using C++20 or later if we didn't use
|
||||
// wxALLOW_COMBINING_ENUMS() for all these enums in wx/defs.h.
|
||||
//
|
||||
// These constants belong to the following enums, respectively:
|
||||
//
|
||||
// wxALIGN_CENTER wxAlignment
|
||||
// wxBORDER_NONE wxBorder
|
||||
// wxLEFT wxDirection
|
||||
// wxCENTER wxGeometryCentre
|
||||
// wxFIXED_MINSIZE wxSizerFlagBits
|
||||
// wxEXPAND wxStretch
|
||||
//
|
||||
int n = (wxALIGN_CENTER | wxBORDER_NONE)
|
||||
| (wxALIGN_CENTER | wxLEFT)
|
||||
| (wxALIGN_CENTER | wxCENTER)
|
||||
| (wxALIGN_CENTER | wxFIXED_MINSIZE)
|
||||
| (wxALIGN_CENTER | wxEXPAND)
|
||||
| (wxBORDER_NONE | wxLEFT)
|
||||
| (wxBORDER_NONE | wxCENTER)
|
||||
| (wxBORDER_NONE | wxFIXED_MINSIZE)
|
||||
| (wxBORDER_NONE | wxEXPAND)
|
||||
| (wxLEFT | wxCENTER)
|
||||
| (wxLEFT | wxFIXED_MINSIZE)
|
||||
| (wxLEFT | wxEXPAND)
|
||||
| (wxCENTER | wxFIXED_MINSIZE)
|
||||
| (wxCENTER | wxEXPAND)
|
||||
| (wxFIXED_MINSIZE | wxEXPAND)
|
||||
;
|
||||
|
||||
wxUnusedVar(n);
|
||||
}
|
||||
254
libs/wxWidgets-3.3.1/tests/sizers/gridsizer.cpp
Normal file
254
libs/wxWidgets-3.3.1/tests/sizers/gridsizer.cpp
Normal file
@@ -0,0 +1,254 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Name: tests/sizers/gridsizer.cpp
|
||||
// Purpose: Unit tests for wxGridSizer and wxFlexGridSizer.
|
||||
// Author: Vadim Zeitlin
|
||||
// Created: 2015-04-03
|
||||
// Copyright: (c) 2015 Vadim Zeitlin <vadim@wxwidgets.org>
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// headers
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#include "testprec.h"
|
||||
|
||||
|
||||
#ifndef WX_PRECOMP
|
||||
#include "wx/app.h"
|
||||
#include "wx/sizer.h"
|
||||
#include "wx/vector.h"
|
||||
#endif // WX_PRECOMP
|
||||
|
||||
#include "asserthelper.h"
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// test fixtures
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// Base class for the two fixtures below.
|
||||
class GridSizerTestCaseBase
|
||||
{
|
||||
protected:
|
||||
explicit GridSizerTestCaseBase(wxGridSizer* sizer);
|
||||
~GridSizerTestCaseBase();
|
||||
// Clear the current sizer contents and add the specified windows to it,
|
||||
// using the same flags for all of them.
|
||||
void SetChildren(const wxVector<wxWindow*>& children,
|
||||
const wxSizerFlags& flags);
|
||||
|
||||
wxWindow *m_win;
|
||||
wxGridSizer* const m_sizerBase;
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(GridSizerTestCaseBase);
|
||||
};
|
||||
|
||||
class GridSizerTestCase : public GridSizerTestCaseBase
|
||||
{
|
||||
protected:
|
||||
GridSizerTestCase()
|
||||
: GridSizerTestCaseBase(m_sizer = new wxGridSizer(2))
|
||||
{
|
||||
}
|
||||
|
||||
wxGridSizer *m_sizer;
|
||||
};
|
||||
|
||||
class FlexGridSizerTestCase : public GridSizerTestCaseBase
|
||||
{
|
||||
protected:
|
||||
FlexGridSizerTestCase()
|
||||
: GridSizerTestCaseBase(m_sizer = new wxFlexGridSizer(2))
|
||||
{
|
||||
}
|
||||
|
||||
wxFlexGridSizer *m_sizer;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// test initialization
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
GridSizerTestCaseBase::GridSizerTestCaseBase(wxGridSizer* sizer)
|
||||
: m_sizerBase(sizer)
|
||||
{
|
||||
m_win = new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY);
|
||||
m_win->SetClientSize(127, 35);
|
||||
|
||||
m_win->SetSizer(m_sizerBase);
|
||||
}
|
||||
|
||||
GridSizerTestCaseBase::~GridSizerTestCaseBase()
|
||||
{
|
||||
delete m_win;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// helpers
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
void GridSizerTestCaseBase::SetChildren(const wxVector<wxWindow*>& children,
|
||||
const wxSizerFlags& flags)
|
||||
{
|
||||
m_sizerBase->Clear();
|
||||
for ( wxVector<wxWindow*>::const_iterator i = children.begin();
|
||||
i != children.end();
|
||||
++i )
|
||||
{
|
||||
m_sizerBase->Add(*i, flags);
|
||||
}
|
||||
|
||||
m_win->Layout();
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// tests themselves
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE_METHOD(GridSizerTestCase,
|
||||
"wxGridSizer::Layout",
|
||||
"[grid-sizer][sizer]")
|
||||
{
|
||||
const wxSize sizeTotal = m_win->GetClientSize();
|
||||
const wxSize sizeChild(sizeTotal.x / 2, sizeTotal.y / 2);
|
||||
|
||||
wxVector<wxWindow*> children;
|
||||
for ( int n = 0; n < 3; n++ )
|
||||
{
|
||||
children.push_back(new wxWindow(m_win, wxID_ANY));
|
||||
}
|
||||
|
||||
SetChildren(children, wxSizerFlags().Expand());
|
||||
CHECK( children[0]->GetRect() == wxRect(wxPoint(0, 0), sizeChild) );
|
||||
CHECK( children[1]->GetRect() == wxRect(wxPoint(sizeChild.x, 0), sizeChild) );
|
||||
CHECK( children[2]->GetRect() == wxRect(wxPoint(0, sizeChild.y), sizeChild) );
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(FlexGridSizerTestCase,
|
||||
"wxFlexGridSizer::Layout",
|
||||
"[grid-sizer][sizer]")
|
||||
{
|
||||
const wxSize sizeTotal = m_win->GetClientSize();
|
||||
const wxSize sizeChild(sizeTotal.x / 4, sizeTotal.y / 4);
|
||||
const wxSize sizeRest(sizeTotal.x - sizeTotal.x / 4,
|
||||
sizeTotal.y - sizeTotal.y / 4);
|
||||
|
||||
wxVector<wxWindow*> children;
|
||||
for ( int n = 0; n < 4; n++ )
|
||||
{
|
||||
children.push_back(new wxWindow(m_win, wxID_ANY, wxDefaultPosition,
|
||||
sizeChild));
|
||||
}
|
||||
|
||||
m_sizer->AddGrowableRow(1);
|
||||
m_sizer->AddGrowableCol(1);
|
||||
|
||||
// Without Expand() windows have their initial size.
|
||||
SECTION("No flags")
|
||||
{
|
||||
SetChildren(children, wxSizerFlags());
|
||||
CHECK( children[0]->GetSize() == sizeChild );
|
||||
CHECK( children[1]->GetSize() == sizeChild );
|
||||
CHECK( children[2]->GetSize() == sizeChild );
|
||||
CHECK( children[3]->GetSize() == sizeChild );
|
||||
}
|
||||
|
||||
// With just expand, they expand to fill the entire column and the row
|
||||
// containing them (which may or not expand on its own).
|
||||
SECTION("Expand")
|
||||
{
|
||||
SetChildren(children, wxSizerFlags().Expand());
|
||||
CHECK( children[0]->GetSize() == sizeChild );
|
||||
CHECK( children[1]->GetSize() == wxSize(sizeRest.x, sizeChild.y) );
|
||||
CHECK( children[2]->GetSize() == wxSize(sizeChild.x, sizeRest.y) );
|
||||
CHECK( children[3]->GetSize() == sizeRest );
|
||||
}
|
||||
|
||||
// With expand and another alignment flag, they should expand only in the
|
||||
// direction in which the alignment is not given.
|
||||
SECTION("Expand and center vertically")
|
||||
{
|
||||
SetChildren(children, wxSizerFlags().Expand().CentreVertical());
|
||||
CHECK( children[0]->GetSize() == sizeChild );
|
||||
CHECK( children[1]->GetSize() == wxSize(sizeRest.x, sizeChild.y) );
|
||||
CHECK( children[2]->GetSize() == sizeChild );
|
||||
CHECK( children[3]->GetSize() == wxSize(sizeRest.x, sizeChild.y) );
|
||||
}
|
||||
|
||||
// Same as above but mirrored.
|
||||
SECTION("Expand and center horizontally")
|
||||
{
|
||||
SetChildren(children, wxSizerFlags().Expand().CentreHorizontal());
|
||||
CHECK( children[0]->GetSize() == sizeChild );
|
||||
CHECK( children[1]->GetSize() == sizeChild );
|
||||
CHECK( children[2]->GetSize() == wxSize(sizeChild.x, sizeRest.y) );
|
||||
CHECK( children[3]->GetSize() == wxSize(sizeChild.x, sizeRest.y) );
|
||||
}
|
||||
|
||||
// Test alignment flag too.
|
||||
SECTION("Right align")
|
||||
{
|
||||
SetChildren(children, wxSizerFlags().Right());
|
||||
CHECK( children[0]->GetPosition() == wxPoint( 0, 0) );
|
||||
CHECK( children[1]->GetPosition() == wxPoint(sizeRest.x, 0) );
|
||||
CHECK( children[2]->GetPosition() == wxPoint( 0, sizeChild.y) );
|
||||
CHECK( children[3]->GetPosition() == wxPoint(sizeRest.x, sizeChild.y) );
|
||||
}
|
||||
|
||||
// Also test combining centering in one direction and aligning in another.
|
||||
SECTION("Right align and center vertically")
|
||||
{
|
||||
SetChildren(children, wxSizerFlags().Right().CentreVertical());
|
||||
|
||||
const int yMid = sizeChild.y + (sizeRest.y - sizeChild.y) / 2;
|
||||
|
||||
CHECK( children[0]->GetPosition() == wxPoint( 0, 0) );
|
||||
CHECK( children[1]->GetPosition() == wxPoint(sizeRest.x, 0) );
|
||||
CHECK( children[2]->GetPosition() == wxPoint( 0, yMid) );
|
||||
CHECK( children[3]->GetPosition() == wxPoint(sizeRest.x, yMid) );
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(GridSizerTestCase,
|
||||
"wxGridSizer::IncompatibleFlags",
|
||||
"[grid-sizer][sizer]")
|
||||
{
|
||||
WX_ASSERT_FAILS_WITH_ASSERT_MESSAGE
|
||||
(
|
||||
"Combining wxEXPAND and wxCENTRE should assert",
|
||||
m_sizer->Add(10, 10, wxSizerFlags().Expand().Centre())
|
||||
);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(FlexGridSizerTestCase,
|
||||
"wxFlexGridSizer::GrowMode",
|
||||
"[grid-sizer][sizer]")
|
||||
{
|
||||
wxVector<wxWindow*> children;
|
||||
for ( int n = 0; n < 4; n++ )
|
||||
{
|
||||
children.push_back(new wxWindow(m_win, wxID_ANY));
|
||||
}
|
||||
|
||||
// Proportions of growable columns should be respected.
|
||||
m_sizer->AddGrowableCol(0, 1);
|
||||
m_sizer->AddGrowableCol(1, 4);
|
||||
|
||||
// However proportions of growable rows should not because we use
|
||||
// wxFLEX_GROWMODE_ALL which tells the sizer to grow all rows uniformly.
|
||||
m_sizer->AddGrowableRow(0, 1);
|
||||
m_sizer->AddGrowableRow(1, 4);
|
||||
|
||||
m_sizer->SetFlexibleDirection(wxHORIZONTAL);
|
||||
m_sizer->SetNonFlexibleGrowMode(wxFLEX_GROWMODE_ALL);
|
||||
|
||||
// Make both dimensions divisible by 5 to avoid dealing with extra pixels.
|
||||
m_win->SetClientSize(100, 100);
|
||||
|
||||
SetChildren(children, wxSizerFlags().Expand());
|
||||
|
||||
// Check that we have different widths but same heights for all children.
|
||||
CHECK( children[0]->GetSize() == wxSize(20, 50) );
|
||||
CHECK( children[1]->GetSize() == wxSize(80, 50) );
|
||||
CHECK( children[2]->GetSize() == wxSize(20, 50) );
|
||||
CHECK( children[3]->GetSize() == wxSize(80, 50) );
|
||||
}
|
||||
114
libs/wxWidgets-3.3.1/tests/sizers/wrapsizer.cpp
Normal file
114
libs/wxWidgets-3.3.1/tests/sizers/wrapsizer.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Name: tests/sizers/wrapsizer.cpp
|
||||
// Purpose: Unit tests for wxWrapSizer
|
||||
// Author: Catalin Raceanu
|
||||
// Created: 2010-10-23
|
||||
// Copyright: (c) 2010 wxWidgets development team
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// headers
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#include "testprec.h"
|
||||
|
||||
|
||||
#ifndef WX_PRECOMP
|
||||
#include "wx/app.h"
|
||||
#endif // WX_PRECOMP
|
||||
|
||||
#include "wx/wrapsizer.h"
|
||||
|
||||
#include "asserthelper.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
TEST_CASE("wxWrapSizer::CalcMin", "[wxWrapSizer]")
|
||||
{
|
||||
std::unique_ptr<wxWindow> win(new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY));
|
||||
win->SetClientSize(180, 240);
|
||||
|
||||
wxSizer *sizer = new wxWrapSizer(wxHORIZONTAL);
|
||||
win->SetSizer(sizer);
|
||||
|
||||
wxSize sizeMinExpected;
|
||||
|
||||
// With a single child the min size must be the same as child size.
|
||||
const wxSize sizeChild1 = wxSize(80, 60);
|
||||
sizeMinExpected = sizeChild1;
|
||||
|
||||
wxWindow * const
|
||||
child1 = new wxWindow(win.get(), wxID_ANY, wxDefaultPosition, sizeChild1);
|
||||
child1->SetBackgroundColour(*wxRED);
|
||||
sizer->Add(child1);
|
||||
win->Layout();
|
||||
|
||||
CHECK( sizeMinExpected == sizer->CalcMin() );
|
||||
|
||||
// If both children can fit in the same row, the minimal size of the sizer
|
||||
// is determined by the sum of their minimal horizontal dimensions and
|
||||
// the maximum of their minimal vertical dimensions.
|
||||
const wxSize sizeChild2 = wxSize(100, 80);
|
||||
sizeMinExpected.x += sizeChild2.x;
|
||||
sizeMinExpected.y = wxMax(sizeChild1.y, sizeChild2.y);
|
||||
|
||||
wxWindow * const
|
||||
child2 = new wxWindow(win.get(), wxID_ANY, wxDefaultPosition, sizeChild2);
|
||||
child2->SetBackgroundColour(*wxYELLOW);
|
||||
sizer->Add(child2);
|
||||
win->Layout();
|
||||
|
||||
CHECK( sizeMinExpected == sizer->CalcMin() );
|
||||
|
||||
// Three children will take at least two rows so the minimal size in
|
||||
// vertical direction must increase.
|
||||
const wxSize sizeChild3 = wxSize(90, 40);
|
||||
sizeMinExpected.y += sizeChild3.y;
|
||||
|
||||
wxWindow * const
|
||||
child3 = new wxWindow(win.get(), wxID_ANY, wxDefaultPosition, sizeChild3);
|
||||
child3->SetBackgroundColour(*wxGREEN);
|
||||
sizer->Add(child3);
|
||||
win->Layout();
|
||||
|
||||
CHECK( sizeMinExpected == sizer->CalcMin() );
|
||||
}
|
||||
|
||||
TEST_CASE("wxWrapSizer::CalcMinFromMinor", "[wxWrapSizer]")
|
||||
{
|
||||
std::unique_ptr<wxWindow> win(new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY));
|
||||
win->SetClientSize(180, 240);
|
||||
|
||||
wxSizer* boxSizer = new wxBoxSizer(wxHORIZONTAL);
|
||||
win->SetSizer(boxSizer);
|
||||
|
||||
// To test CalcMinFromMinor function the wrap sizer with the
|
||||
// horizonral align added to the box sizer with horizontal align.
|
||||
wxSizer* wrapSizer = new wxWrapSizer(wxHORIZONTAL);
|
||||
boxSizer->Add(wrapSizer);
|
||||
|
||||
// Add three child windows. Sum of the first and the second windows widths should
|
||||
// be less than the width of the third window.
|
||||
const wxSize sizeChild1 = wxSize(40, 60);
|
||||
wxWindow * const
|
||||
child1 = new wxWindow(win.get(), wxID_ANY, wxDefaultPosition, sizeChild1);
|
||||
child1->SetBackgroundColour(*wxRED);
|
||||
wrapSizer->Add(child1);
|
||||
|
||||
const wxSize sizeChild2 = wxSize(50, 80);
|
||||
wxWindow * const
|
||||
child2 = new wxWindow(win.get(), wxID_ANY, wxDefaultPosition, sizeChild2);
|
||||
child2->SetBackgroundColour(*wxGREEN);
|
||||
wrapSizer->Add(child2);
|
||||
|
||||
const wxSize sizeChild3 = wxSize(100, 120);
|
||||
wxWindow * const
|
||||
child3 = new wxWindow(win.get(), wxID_ANY, wxDefaultPosition, sizeChild3);
|
||||
child3->SetBackgroundColour(*wxBLUE);
|
||||
wrapSizer->Add(child3);
|
||||
|
||||
// First two windows should be in a first row and the third in a second row.
|
||||
const wxSize sizeMinExpected = wxSize(sizeChild3.x, sizeChild2.y + sizeChild3.y);
|
||||
win->Layout();
|
||||
CHECK(sizeMinExpected == wrapSizer->CalcMin());
|
||||
}
|
||||
Reference in New Issue
Block a user