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 // ---------------------------------------------------------------------------- 10 // headers 11 // ---------------------------------------------------------------------------- 12 13 #include "testprec.h" 14 15 16 #ifndef WX_PRECOMP 17 #include "wx/app.h" 18 #endif // WX_PRECOMP 19 20 #include "wx/wrapsizer.h" 21 22 #include "asserthelper.h" 23 24 TEST_CASE("wxWrapSizer::CalcMin", "[wxWrapSizer]") 25 { 26 wxScopedPtr<wxWindow> win(new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY)); 27 win->SetClientSize(180, 240); 28 29 wxSizer *sizer = new wxWrapSizer(wxHORIZONTAL); 30 win->SetSizer(sizer); 31 32 wxSize sizeMinExpected; 33 34 // With a single child the min size must be the same as child size. 35 const wxSize sizeChild1 = wxSize(80, 60); 36 sizeMinExpected = sizeChild1; 37 38 wxWindow * const 39 child1 = new wxWindow(win.get(), wxID_ANY, wxDefaultPosition, sizeChild1); 40 child1->SetBackgroundColour(*wxRED); 41 sizer->Add(child1); 42 win->Layout(); 43 44 CHECK( sizeMinExpected == sizer->CalcMin() ); 45 46 // If both children can fit in the same row, the minimal size of the sizer 47 // is determined by the sum of their minimal horizontal dimensions and 48 // the maximum of their minimal vertical dimensions. 49 const wxSize sizeChild2 = wxSize(100, 80); 50 sizeMinExpected.x += sizeChild2.x; 51 sizeMinExpected.y = wxMax(sizeChild1.y, sizeChild2.y); 52 53 wxWindow * const 54 child2 = new wxWindow(win.get(), wxID_ANY, wxDefaultPosition, sizeChild2); 55 child2->SetBackgroundColour(*wxYELLOW); 56 sizer->Add(child2); 57 win->Layout(); 58 59 CHECK( sizeMinExpected == sizer->CalcMin() ); 60 61 // Three children will take at least two rows so the minimal size in 62 // vertical direction must increase. 63 const wxSize sizeChild3 = wxSize(90, 40); 64 sizeMinExpected.y += sizeChild3.y; 65 66 wxWindow * const 67 child3 = new wxWindow(win.get(), wxID_ANY, wxDefaultPosition, sizeChild3); 68 child3->SetBackgroundColour(*wxGREEN); 69 sizer->Add(child3); 70 win->Layout(); 71 72 CHECK( sizeMinExpected == sizer->CalcMin() ); 73 } 74 75 TEST_CASE("wxWrapSizer::CalcMinFromMinor", "[wxWrapSizer]") 76 { 77 wxScopedPtr<wxWindow> win(new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY)); 78 win->SetClientSize(180, 240); 79 80 wxSizer* boxSizer = new wxBoxSizer(wxHORIZONTAL); 81 win->SetSizer(boxSizer); 82 83 // To test CalcMinFromMinor function the wrap sizer with the 84 // horizonral align added to the box sizer with horizontal align. 85 wxSizer* wrapSizer = new wxWrapSizer(wxHORIZONTAL); 86 boxSizer->Add(wrapSizer); 87 88 // Add three child windows. Sum of the first and the second windows widths should 89 // be less than the width of the third window. 90 const wxSize sizeChild1 = wxSize(40, 60); 91 wxWindow * const 92 child1 = new wxWindow(win.get(), wxID_ANY, wxDefaultPosition, sizeChild1); 93 child1->SetBackgroundColour(*wxRED); 94 wrapSizer->Add(child1); 95 96 const wxSize sizeChild2 = wxSize(50, 80); 97 wxWindow * const 98 child2 = new wxWindow(win.get(), wxID_ANY, wxDefaultPosition, sizeChild2); 99 child2->SetBackgroundColour(*wxGREEN); 100 wrapSizer->Add(child2); 101 102 const wxSize sizeChild3 = wxSize(100, 120); 103 wxWindow * const 104 child3 = new wxWindow(win.get(), wxID_ANY, wxDefaultPosition, sizeChild3); 105 child3->SetBackgroundColour(*wxBLUE); 106 wrapSizer->Add(child3); 107 108 // First two windows should be in a first row and the third in a second row. 109 const wxSize sizeMinExpected = wxSize(sizeChild3.x, sizeChild2.y + sizeChild3.y); 110 win->Layout(); 111 CHECK(sizeMinExpected == wrapSizer->CalcMin()); 112 } 113