wrapsizer.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/sizers/wrapsizer.cpp
  3. // Purpose: Unit tests for wxWrapSizer
  4. // Author: Catalin Raceanu
  5. // Created: 2010-10-23
  6. // Copyright: (c) 2010 wxWidgets development team
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #ifndef WX_PRECOMP
  16. #include "wx/app.h"
  17. #endif // WX_PRECOMP
  18. #include "wx/wrapsizer.h"
  19. #include "asserthelper.h"
  20. // ----------------------------------------------------------------------------
  21. // test class
  22. // ----------------------------------------------------------------------------
  23. class WrapSizerTestCase : public CppUnit::TestCase
  24. {
  25. public:
  26. WrapSizerTestCase() { }
  27. virtual void setUp();
  28. virtual void tearDown();
  29. private:
  30. CPPUNIT_TEST_SUITE( WrapSizerTestCase );
  31. CPPUNIT_TEST( CalcMin );
  32. CPPUNIT_TEST_SUITE_END();
  33. void CalcMin();
  34. wxWindow *m_win;
  35. wxSizer *m_sizer;
  36. DECLARE_NO_COPY_CLASS(WrapSizerTestCase)
  37. };
  38. // register in the unnamed registry so that these tests are run by default
  39. CPPUNIT_TEST_SUITE_REGISTRATION( WrapSizerTestCase );
  40. // also include in its own registry so that these tests can be run alone
  41. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WrapSizerTestCase, "WrapSizerTestCase" );
  42. // ----------------------------------------------------------------------------
  43. // test initialization
  44. // ----------------------------------------------------------------------------
  45. void WrapSizerTestCase::setUp()
  46. {
  47. m_win = new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY);
  48. m_win->SetClientSize(180, 240);
  49. m_sizer = new wxWrapSizer(wxHORIZONTAL);
  50. m_win->SetSizer(m_sizer);
  51. }
  52. void WrapSizerTestCase::tearDown()
  53. {
  54. delete m_win;
  55. m_win = NULL;
  56. m_sizer = NULL;
  57. }
  58. // ----------------------------------------------------------------------------
  59. // tests themselves
  60. // ----------------------------------------------------------------------------
  61. void WrapSizerTestCase::CalcMin()
  62. {
  63. const wxSize sizeTotal = m_win->GetClientSize();
  64. wxSize sizeMinExpected;
  65. // With a single child the min size must be the same as child size.
  66. const wxSize sizeChild1 = wxSize(sizeTotal.x/2 - 10, sizeTotal.y/4);
  67. sizeMinExpected = sizeChild1;
  68. wxWindow * const
  69. child1 = new wxWindow(m_win, wxID_ANY, wxDefaultPosition, sizeChild1);
  70. child1->SetBackgroundColour(*wxRED);
  71. m_sizer->Add(child1);
  72. m_win->Layout();
  73. CPPUNIT_ASSERT_EQUAL( sizeMinExpected, m_sizer->CalcMin() );
  74. // If both children can fit in the same row, the minimal size of the sizer
  75. // is determined by the sum of their minimal horizontal dimensions and
  76. // the maximum of their minimal vertical dimensions.
  77. const wxSize sizeChild2 = wxSize(sizeTotal.x/2 + 10, sizeTotal.y/3);
  78. sizeMinExpected.x += sizeChild2.x;
  79. sizeMinExpected.y = wxMax(sizeChild1.y, sizeChild2.y);
  80. wxWindow * const
  81. child2 = new wxWindow(m_win, wxID_ANY, wxDefaultPosition, sizeChild2);
  82. child2->SetBackgroundColour(*wxYELLOW);
  83. m_sizer->Add(child2);
  84. m_win->Layout();
  85. CPPUNIT_ASSERT_EQUAL( sizeMinExpected, m_sizer->CalcMin() );
  86. // Three children will take at least two rows so the minimal size in
  87. // vertical direction must increase.
  88. const wxSize sizeChild3 = wxSize(sizeTotal.x/2, sizeTotal.y/5);
  89. sizeMinExpected.y += sizeChild3.y;
  90. wxWindow * const
  91. child3 = new wxWindow(m_win, wxID_ANY, wxDefaultPosition, sizeChild3);
  92. child3->SetBackgroundColour(*wxGREEN);
  93. m_sizer->Add(child3);
  94. m_win->Layout();
  95. CPPUNIT_ASSERT_EQUAL( sizeMinExpected, m_sizer->CalcMin() );
  96. }