Add unit test for wxFlexGridSizer layout logic.

Currently just test that wxEXPAND flag works as expected.
This commit is contained in:
Vadim Zeitlin
2015-04-04 18:19:49 +02:00
parent 78b98bf00c
commit 7e865daa26
9 changed files with 167 additions and 0 deletions

View File

@@ -251,6 +251,7 @@ TEST_GUI_OBJECTS = \
test_gui_settings.o \ test_gui_settings.o \
test_gui_socket.o \ test_gui_socket.o \
test_gui_boxsizer.o \ test_gui_boxsizer.o \
test_gui_gridsizer.o \
test_gui_wrapsizer.o \ test_gui_wrapsizer.o \
test_gui_toplevel.o \ test_gui_toplevel.o \
test_gui_valnum.o \ test_gui_valnum.o \
@@ -1014,6 +1015,9 @@ test_gui_socket.o: $(srcdir)/net/socket.cpp $(TEST_GUI_ODEP)
test_gui_boxsizer.o: $(srcdir)/sizers/boxsizer.cpp $(TEST_GUI_ODEP) test_gui_boxsizer.o: $(srcdir)/sizers/boxsizer.cpp $(TEST_GUI_ODEP)
$(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/sizers/boxsizer.cpp $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/sizers/boxsizer.cpp
test_gui_gridsizer.o: $(srcdir)/sizers/gridsizer.cpp $(TEST_GUI_ODEP)
$(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/sizers/gridsizer.cpp
test_gui_wrapsizer.o: $(srcdir)/sizers/wrapsizer.cpp $(TEST_GUI_ODEP) test_gui_wrapsizer.o: $(srcdir)/sizers/wrapsizer.cpp $(TEST_GUI_ODEP)
$(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/sizers/wrapsizer.cpp $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/sizers/wrapsizer.cpp

View File

@@ -235,6 +235,7 @@ TEST_GUI_OBJECTS = \
$(OBJS)\test_gui_settings.obj \ $(OBJS)\test_gui_settings.obj \
$(OBJS)\test_gui_socket.obj \ $(OBJS)\test_gui_socket.obj \
$(OBJS)\test_gui_boxsizer.obj \ $(OBJS)\test_gui_boxsizer.obj \
$(OBJS)\test_gui_gridsizer.obj \
$(OBJS)\test_gui_wrapsizer.obj \ $(OBJS)\test_gui_wrapsizer.obj \
$(OBJS)\test_gui_toplevel.obj \ $(OBJS)\test_gui_toplevel.obj \
$(OBJS)\test_gui_valnum.obj \ $(OBJS)\test_gui_valnum.obj \
@@ -1058,6 +1059,9 @@ $(OBJS)\test_gui_socket.obj: .\net\socket.cpp
$(OBJS)\test_gui_boxsizer.obj: .\sizers\boxsizer.cpp $(OBJS)\test_gui_boxsizer.obj: .\sizers\boxsizer.cpp
$(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\sizers\boxsizer.cpp $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\sizers\boxsizer.cpp
$(OBJS)\test_gui_gridsizer.obj: .\sizers\gridsizer.cpp
$(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\sizers\gridsizer.cpp
$(OBJS)\test_gui_wrapsizer.obj: .\sizers\wrapsizer.cpp $(OBJS)\test_gui_wrapsizer.obj: .\sizers\wrapsizer.cpp
$(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\sizers\wrapsizer.cpp $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\sizers\wrapsizer.cpp

View File

@@ -229,6 +229,7 @@ TEST_GUI_OBJECTS = \
$(OBJS)\test_gui_settings.o \ $(OBJS)\test_gui_settings.o \
$(OBJS)\test_gui_socket.o \ $(OBJS)\test_gui_socket.o \
$(OBJS)\test_gui_boxsizer.o \ $(OBJS)\test_gui_boxsizer.o \
$(OBJS)\test_gui_gridsizer.o \
$(OBJS)\test_gui_wrapsizer.o \ $(OBJS)\test_gui_wrapsizer.o \
$(OBJS)\test_gui_toplevel.o \ $(OBJS)\test_gui_toplevel.o \
$(OBJS)\test_gui_valnum.o \ $(OBJS)\test_gui_valnum.o \
@@ -1034,6 +1035,9 @@ $(OBJS)\test_gui_socket.o: ./net/socket.cpp
$(OBJS)\test_gui_boxsizer.o: ./sizers/boxsizer.cpp $(OBJS)\test_gui_boxsizer.o: ./sizers/boxsizer.cpp
$(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $<
$(OBJS)\test_gui_gridsizer.o: ./sizers/gridsizer.cpp
$(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $<
$(OBJS)\test_gui_wrapsizer.o: ./sizers/wrapsizer.cpp $(OBJS)\test_gui_wrapsizer.o: ./sizers/wrapsizer.cpp
$(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $<

View File

@@ -239,6 +239,7 @@ TEST_GUI_OBJECTS = \
$(OBJS)\test_gui_settings.obj \ $(OBJS)\test_gui_settings.obj \
$(OBJS)\test_gui_socket.obj \ $(OBJS)\test_gui_socket.obj \
$(OBJS)\test_gui_boxsizer.obj \ $(OBJS)\test_gui_boxsizer.obj \
$(OBJS)\test_gui_gridsizer.obj \
$(OBJS)\test_gui_wrapsizer.obj \ $(OBJS)\test_gui_wrapsizer.obj \
$(OBJS)\test_gui_toplevel.obj \ $(OBJS)\test_gui_toplevel.obj \
$(OBJS)\test_gui_valnum.obj \ $(OBJS)\test_gui_valnum.obj \
@@ -1235,6 +1236,9 @@ $(OBJS)\test_gui_socket.obj: .\net\socket.cpp
$(OBJS)\test_gui_boxsizer.obj: .\sizers\boxsizer.cpp $(OBJS)\test_gui_boxsizer.obj: .\sizers\boxsizer.cpp
$(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\sizers\boxsizer.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\sizers\boxsizer.cpp
$(OBJS)\test_gui_gridsizer.obj: .\sizers\gridsizer.cpp
$(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\sizers\gridsizer.cpp
$(OBJS)\test_gui_wrapsizer.obj: .\sizers\wrapsizer.cpp $(OBJS)\test_gui_wrapsizer.obj: .\sizers\wrapsizer.cpp
$(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\sizers\wrapsizer.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\sizers\wrapsizer.cpp

139
tests/sizers/gridsizer.cpp Normal file
View File

@@ -0,0 +1,139 @@
///////////////////////////////////////////////////////////////////////////////
// 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"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/app.h"
#include "wx/sizer.h"
#include "wx/vector.h"
#endif // WX_PRECOMP
#include "asserthelper.h"
// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------
class GridSizerTestCase : public CppUnit::TestCase
{
public:
GridSizerTestCase() { }
virtual void setUp();
virtual void tearDown();
private:
CPPUNIT_TEST_SUITE( GridSizerTestCase );
CPPUNIT_TEST( Expand );
CPPUNIT_TEST_SUITE_END();
void Expand();
// 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;
wxFlexGridSizer *m_sizer;
DECLARE_NO_COPY_CLASS(GridSizerTestCase)
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( GridSizerTestCase );
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GridSizerTestCase, "GridSizerTestCase" );
// ----------------------------------------------------------------------------
// test initialization
// ----------------------------------------------------------------------------
void GridSizerTestCase::setUp()
{
m_win = new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY);
m_win->SetClientSize(127, 35);
m_sizer = new wxFlexGridSizer(2);
m_win->SetSizer(m_sizer);
}
void GridSizerTestCase::tearDown()
{
delete m_win;
m_win = NULL;
m_sizer = NULL;
}
// ----------------------------------------------------------------------------
// helpers
// ----------------------------------------------------------------------------
void GridSizerTestCase::SetChildren(const wxVector<wxWindow*>& children,
const wxSizerFlags& flags)
{
m_sizer->Clear();
for ( wxVector<wxWindow*>::const_iterator i = children.begin();
i != children.end();
++i )
{
m_sizer->Add(*i, flags);
}
m_win->Layout();
}
// ----------------------------------------------------------------------------
// tests themselves
// ----------------------------------------------------------------------------
void GridSizerTestCase::Expand()
{
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.
SetChildren(children, wxSizerFlags());
CPPUNIT_ASSERT_EQUAL( sizeChild, children[0]->GetSize() );
CPPUNIT_ASSERT_EQUAL( sizeChild, children[1]->GetSize() );
CPPUNIT_ASSERT_EQUAL( sizeChild, children[2]->GetSize() );
CPPUNIT_ASSERT_EQUAL( sizeChild, children[3]->GetSize() );
// With just expand, they expand to fill the entire column and the row
// containing them (which may or not expand on its own).
SetChildren(children, wxSizerFlags().Expand());
CPPUNIT_ASSERT_EQUAL( sizeChild, children[0]->GetSize() );
CPPUNIT_ASSERT_EQUAL( wxSize(sizeRest.x, sizeChild.y),
children[1]->GetSize() );
CPPUNIT_ASSERT_EQUAL( wxSize(sizeChild.x, sizeRest.y),
children[2]->GetSize() );
CPPUNIT_ASSERT_EQUAL( sizeRest, children[3]->GetSize() );
}

View File

@@ -261,6 +261,7 @@
--> -->
net/socket.cpp net/socket.cpp
sizers/boxsizer.cpp sizers/boxsizer.cpp
sizers/gridsizer.cpp
sizers/wrapsizer.cpp sizers/wrapsizer.cpp
toplevel/toplevel.cpp toplevel/toplevel.cpp
validators/valnum.cpp validators/valnum.cpp

View File

@@ -409,6 +409,9 @@
<File <File
RelativePath=".\controls\gaugetest.cpp"> RelativePath=".\controls\gaugetest.cpp">
</File> </File>
<File
RelativePath=".\sizers\gridsizer.cpp">
</File>
<File <File
RelativePath=".\controls\gridtest.cpp"> RelativePath=".\controls\gridtest.cpp">
</File> </File>

View File

@@ -1014,6 +1014,10 @@
RelativePath=".\controls\gaugetest.cpp" RelativePath=".\controls\gaugetest.cpp"
> >
</File> </File>
<File
RelativePath=".\sizers\gridsizer.cpp"
>
</File>
<File <File
RelativePath=".\controls\gridtest.cpp" RelativePath=".\controls\gridtest.cpp"
> >

View File

@@ -986,6 +986,10 @@
RelativePath=".\controls\gaugetest.cpp" RelativePath=".\controls\gaugetest.cpp"
> >
</File> </File>
<File
RelativePath=".\sizers\gridsizer.cpp"
>
</File>
<File <File
RelativePath=".\controls\gridtest.cpp" RelativePath=".\controls\gridtest.cpp"
> >