From 7e865daa267197d887edfe0bebf728acb355c848 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 4 Apr 2015 18:19:49 +0200 Subject: [PATCH] Add unit test for wxFlexGridSizer layout logic. Currently just test that wxEXPAND flag works as expected. --- tests/Makefile.in | 4 + tests/makefile.bcc | 4 + tests/makefile.gcc | 4 + tests/makefile.vc | 4 + tests/sizers/gridsizer.cpp | 139 +++++++++++++++++++++++++++++++++ tests/test.bkl | 1 + tests/test_vc7_test_gui.vcproj | 3 + tests/test_vc8_test_gui.vcproj | 4 + tests/test_vc9_test_gui.vcproj | 4 + 9 files changed, 167 insertions(+) create mode 100644 tests/sizers/gridsizer.cpp diff --git a/tests/Makefile.in b/tests/Makefile.in index 08d8a5a62b..600ea18003 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -251,6 +251,7 @@ TEST_GUI_OBJECTS = \ test_gui_settings.o \ test_gui_socket.o \ test_gui_boxsizer.o \ + test_gui_gridsizer.o \ test_gui_wrapsizer.o \ test_gui_toplevel.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) $(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) $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/sizers/wrapsizer.cpp diff --git a/tests/makefile.bcc b/tests/makefile.bcc index edf78a3199..18075e7ae0 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -235,6 +235,7 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_settings.obj \ $(OBJS)\test_gui_socket.obj \ $(OBJS)\test_gui_boxsizer.obj \ + $(OBJS)\test_gui_gridsizer.obj \ $(OBJS)\test_gui_wrapsizer.obj \ $(OBJS)\test_gui_toplevel.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 $(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 $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\sizers\wrapsizer.cpp diff --git a/tests/makefile.gcc b/tests/makefile.gcc index 9167512348..cf4a00db2c 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -229,6 +229,7 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_settings.o \ $(OBJS)\test_gui_socket.o \ $(OBJS)\test_gui_boxsizer.o \ + $(OBJS)\test_gui_gridsizer.o \ $(OBJS)\test_gui_wrapsizer.o \ $(OBJS)\test_gui_toplevel.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 $(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 $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< diff --git a/tests/makefile.vc b/tests/makefile.vc index 9aef18cb8b..c640e26b7d 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -239,6 +239,7 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_settings.obj \ $(OBJS)\test_gui_socket.obj \ $(OBJS)\test_gui_boxsizer.obj \ + $(OBJS)\test_gui_gridsizer.obj \ $(OBJS)\test_gui_wrapsizer.obj \ $(OBJS)\test_gui_toplevel.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 $(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 $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\sizers\wrapsizer.cpp diff --git a/tests/sizers/gridsizer.cpp b/tests/sizers/gridsizer.cpp new file mode 100644 index 0000000000..ba7e1262b2 --- /dev/null +++ b/tests/sizers/gridsizer.cpp @@ -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 +/////////////////////////////////////////////////////////////////////////////// + +// ---------------------------------------------------------------------------- +// 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& 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& children, + const wxSizerFlags& flags) +{ + m_sizer->Clear(); + for ( wxVector::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 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() ); +} diff --git a/tests/test.bkl b/tests/test.bkl index d2b71a4b14..3a12c9aa32 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -261,6 +261,7 @@ --> net/socket.cpp sizers/boxsizer.cpp + sizers/gridsizer.cpp sizers/wrapsizer.cpp toplevel/toplevel.cpp validators/valnum.cpp diff --git a/tests/test_vc7_test_gui.vcproj b/tests/test_vc7_test_gui.vcproj index d3129454f4..e8c42fa6a8 100644 --- a/tests/test_vc7_test_gui.vcproj +++ b/tests/test_vc7_test_gui.vcproj @@ -409,6 +409,9 @@ + + diff --git a/tests/test_vc8_test_gui.vcproj b/tests/test_vc8_test_gui.vcproj index 07ef45774b..74627c17f6 100644 --- a/tests/test_vc8_test_gui.vcproj +++ b/tests/test_vc8_test_gui.vcproj @@ -1014,6 +1014,10 @@ RelativePath=".\controls\gaugetest.cpp" > + + diff --git a/tests/test_vc9_test_gui.vcproj b/tests/test_vc9_test_gui.vcproj index f4b5ad2263..6c71a3bea6 100644 --- a/tests/test_vc9_test_gui.vcproj +++ b/tests/test_vc9_test_gui.vcproj @@ -986,6 +986,10 @@ RelativePath=".\controls\gaugetest.cpp" > + +