1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/gridtest.cpp
3 // Purpose: wxGrid unit test
4 // Author: Steven Lamerton
7 // Copyright: (c) 2010 Steven Lamerton
8 ///////////////////////////////////////////////////////////////////////////////
23 #include "testableframe.h"
24 #include "asserthelper.h"
25 #include "wx/uiaction.h"
27 class GridTestCase
: public CppUnit::TestCase
33 virtual void tearDown();
36 CPPUNIT_TEST_SUITE( GridTestCase
);
37 WXUISIM_TEST( CellEdit
);
38 WXUISIM_TEST( CellClick
);
39 WXUISIM_TEST( CellSelect
);
40 WXUISIM_TEST( LabelClick
);
41 WXUISIM_TEST( SortClick
);
43 WXUISIM_TEST( RangeSelect
);
44 CPPUNIT_TEST( Cursor
);
45 CPPUNIT_TEST( Selection
);
46 CPPUNIT_TEST( AddRowCol
);
47 CPPUNIT_TEST( ColumnOrder
);
48 CPPUNIT_TEST( LineFormatting
);
49 CPPUNIT_TEST( SortSupport
);
50 CPPUNIT_TEST( Labels
);
51 CPPUNIT_TEST( SelectionMode
);
52 CPPUNIT_TEST( CellFormatting
);
53 WXUISIM_TEST( Editable
);
54 WXUISIM_TEST( ReadOnly
);
55 CPPUNIT_TEST( PseudoTest_NativeHeader
);
56 WXUISIM_TEST( LabelClick
);
57 WXUISIM_TEST( SortClick
);
58 CPPUNIT_TEST( ColumnOrder
);
59 CPPUNIT_TEST( PseudoTest_NativeLabels
);
60 WXUISIM_TEST( LabelClick
);
61 WXUISIM_TEST( SortClick
);
62 CPPUNIT_TEST( ColumnOrder
);
63 CPPUNIT_TEST_SUITE_END();
76 void LineFormatting();
80 void CellFormatting();
83 void PseudoTest_NativeHeader() { ms_nativeheader
= true; }
84 void PseudoTest_NativeLabels() { ms_nativeheader
= false;
85 ms_nativelabels
= true; }
87 static bool ms_nativeheader
;
88 static bool ms_nativelabels
;
92 DECLARE_NO_COPY_CLASS(GridTestCase
)
95 // register in the unnamed registry so that these tests are run by default
96 CPPUNIT_TEST_SUITE_REGISTRATION( GridTestCase
);
98 // also include in its own registry so that these tests can be run alone
99 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GridTestCase
, "GridTestCase" );
101 //initialise the static variable
102 bool GridTestCase::ms_nativeheader
= false;
103 bool GridTestCase::ms_nativelabels
= false;
105 void GridTestCase::setUp()
107 m_grid
= new wxGrid(wxTheApp
->GetTopWindow(), wxID_ANY
);
108 m_grid
->CreateGrid(10, 2);
109 m_grid
->SetSize(400, 200);
111 if( ms_nativeheader
)
112 m_grid
->UseNativeColHeader();
114 if( ms_nativelabels
)
115 m_grid
->SetUseNativeColLabels();
121 void GridTestCase::tearDown()
126 void GridTestCase::CellEdit()
128 #if wxUSE_UIACTIONSIMULATOR
129 EventCounter
changing(m_grid
, wxEVT_GRID_CELL_CHANGING
);
130 EventCounter
changed(m_grid
, wxEVT_GRID_CELL_CHANGED
);
131 EventCounter
created(m_grid
, wxEVT_GRID_EDITOR_CREATED
);
133 wxUIActionSimulator sim
;
136 m_grid
->SetGridCursor(1, 1);
137 m_grid
->ShowCellEditControl();
140 sim
.Char(WXK_RETURN
);
144 CPPUNIT_ASSERT_EQUAL(1, created
.GetCount());
145 CPPUNIT_ASSERT_EQUAL(1, changing
.GetCount());
146 CPPUNIT_ASSERT_EQUAL(1, changed
.GetCount());
150 void GridTestCase::CellClick()
152 #if wxUSE_UIACTIONSIMULATOR
153 EventCounter
lclick(m_grid
, wxEVT_GRID_CELL_LEFT_CLICK
);
154 EventCounter
ldclick(m_grid
, wxEVT_GRID_CELL_LEFT_DCLICK
);
155 EventCounter
rclick(m_grid
, wxEVT_GRID_CELL_RIGHT_CLICK
);
156 EventCounter
rdclick(m_grid
, wxEVT_GRID_CELL_RIGHT_DCLICK
);
159 wxUIActionSimulator sim
;
161 wxRect rect
= m_grid
->CellToRect(0, 0);
162 wxPoint point
= m_grid
->CalcScrolledPosition(rect
.GetPosition());
163 point
= m_grid
->ClientToScreen(point
+ wxPoint(m_grid
->GetRowLabelSize(),
164 m_grid
->GetColLabelSize())
167 sim
.MouseMove(point
);
173 CPPUNIT_ASSERT_EQUAL(1, lclick
.GetCount());
179 //A double click event sends a single click event first
180 //test to ensure this still happens in the future
181 CPPUNIT_ASSERT_EQUAL(1, lclick
.GetCount());
182 CPPUNIT_ASSERT_EQUAL(1, ldclick
.GetCount());
184 sim
.MouseClick(wxMOUSE_BTN_RIGHT
);
187 CPPUNIT_ASSERT_EQUAL(1, rclick
.GetCount());
190 sim
.MouseDblClick(wxMOUSE_BTN_RIGHT
);
193 CPPUNIT_ASSERT_EQUAL(1, rclick
.GetCount());
194 CPPUNIT_ASSERT_EQUAL(1, rdclick
.GetCount());
198 void GridTestCase::CellSelect()
200 #if wxUSE_UIACTIONSIMULATOR
201 EventCounter
cell(m_grid
, wxEVT_GRID_SELECT_CELL
);
203 wxUIActionSimulator sim
;
205 wxRect rect
= m_grid
->CellToRect(0, 0);
206 wxPoint point
= m_grid
->CalcScrolledPosition(rect
.GetPosition());
207 point
= m_grid
->ClientToScreen(point
+ wxPoint(m_grid
->GetRowLabelSize(),
208 m_grid
->GetColLabelSize())
211 sim
.MouseMove(point
);
217 CPPUNIT_ASSERT_EQUAL(1, cell
.GetCount());
221 m_grid
->SetGridCursor(1, 1);
222 m_grid
->GoToCell(1, 0);
224 sim
.MouseMove(point
);
230 CPPUNIT_ASSERT_EQUAL(3, cell
.GetCount());
234 void GridTestCase::LabelClick()
236 #if wxUSE_UIACTIONSIMULATOR
237 EventCounter
lclick(m_grid
, wxEVT_GRID_LABEL_LEFT_CLICK
);
238 EventCounter
ldclick(m_grid
, wxEVT_GRID_LABEL_LEFT_DCLICK
);
239 EventCounter
rclick(m_grid
, wxEVT_GRID_LABEL_RIGHT_CLICK
);
240 EventCounter
rdclick(m_grid
, wxEVT_GRID_LABEL_RIGHT_DCLICK
);
242 wxUIActionSimulator sim
;
244 wxPoint
pos(m_grid
->GetRowLabelSize() + 2, 2);
245 pos
= m_grid
->ClientToScreen(pos
);
253 CPPUNIT_ASSERT_EQUAL(1, lclick
.GetCount());
258 CPPUNIT_ASSERT_EQUAL(1, ldclick
.GetCount());
260 sim
.MouseClick(wxMOUSE_BTN_RIGHT
);
263 CPPUNIT_ASSERT_EQUAL(1, rclick
.GetCount());
266 sim
.MouseDblClick(wxMOUSE_BTN_RIGHT
);
269 if( ms_nativeheader
)
271 //Right double click not supported with native headers so we get two
273 CPPUNIT_ASSERT_EQUAL(2, rclick
.GetCount());
277 CPPUNIT_ASSERT_EQUAL(1, rclick
.GetCount());
278 CPPUNIT_ASSERT_EQUAL(1, rdclick
.GetCount());
283 void GridTestCase::SortClick()
285 #if wxUSE_UIACTIONSIMULATOR
286 m_grid
->SetSortingColumn(0);
288 EventCounter
sort(m_grid
, wxEVT_GRID_COL_SORT
);
290 wxUIActionSimulator sim
;
292 wxPoint
pos(m_grid
->GetRowLabelSize() + 4, 4);
293 pos
= m_grid
->ClientToScreen(pos
);
301 CPPUNIT_ASSERT_EQUAL(1, sort
.GetCount());
305 void GridTestCase::Size()
307 #if wxUSE_UIACTIONSIMULATOR && !defined(__WXGTK__)
308 EventCounter
colsize(m_grid
, wxEVT_GRID_COL_SIZE
);
309 EventCounter
rowsize(m_grid
, wxEVT_GRID_ROW_SIZE
);
311 wxUIActionSimulator sim
;
313 wxPoint pt
= m_grid
->ClientToScreen(wxPoint(m_grid
->GetRowLabelSize() +
314 m_grid
->GetColSize(0), 5));
322 sim
.MouseMove(pt
.x
+ 50, pt
.y
);
328 CPPUNIT_ASSERT_EQUAL(1, colsize
.GetCount());
330 pt
= m_grid
->ClientToScreen(wxPoint(5, m_grid
->GetColLabelSize() +
331 m_grid
->GetRowSize(0)));
333 sim
.MouseDragDrop(pt
.x
, pt
.y
, pt
.x
, pt
.y
+ 50);
336 CPPUNIT_ASSERT_EQUAL(1, rowsize
.GetCount());
340 void GridTestCase::RangeSelect()
342 #if wxUSE_UIACTIONSIMULATOR
343 EventCounter
select(m_grid
, wxEVT_GRID_RANGE_SELECT
);
345 wxUIActionSimulator sim
;
347 //We add the extra 10 to ensure that we are inside the cell
348 wxPoint pt
= m_grid
->ClientToScreen(wxPoint(m_grid
->GetRowLabelSize() + 10,
349 m_grid
->GetColLabelSize() + 10)
358 sim
.MouseMove(pt
.x
+ 50, pt
.y
+ 50);
364 CPPUNIT_ASSERT_EQUAL(1, select
.GetCount());
368 void GridTestCase::Cursor()
370 m_grid
->SetGridCursor(1, 1);
372 CPPUNIT_ASSERT_EQUAL(1, m_grid
->GetGridCursorCol());
373 CPPUNIT_ASSERT_EQUAL(1, m_grid
->GetGridCursorRow());
375 m_grid
->MoveCursorDown(false);
376 m_grid
->MoveCursorLeft(false);
377 m_grid
->MoveCursorUp(false);
378 m_grid
->MoveCursorUp(false);
379 m_grid
->MoveCursorRight(false);
381 CPPUNIT_ASSERT_EQUAL(1, m_grid
->GetGridCursorCol());
382 CPPUNIT_ASSERT_EQUAL(0, m_grid
->GetGridCursorRow());
384 m_grid
->SetCellValue(0, 0, "some text");
385 m_grid
->SetCellValue(3, 0, "other text");
386 m_grid
->SetCellValue(0, 1, "more text");
387 m_grid
->SetCellValue(3, 1, "extra text");
392 m_grid
->MoveCursorLeftBlock(false);
394 CPPUNIT_ASSERT_EQUAL(0, m_grid
->GetGridCursorCol());
395 CPPUNIT_ASSERT_EQUAL(0, m_grid
->GetGridCursorRow());
397 m_grid
->MoveCursorDownBlock(false);
399 CPPUNIT_ASSERT_EQUAL(0, m_grid
->GetGridCursorCol());
400 CPPUNIT_ASSERT_EQUAL(3, m_grid
->GetGridCursorRow());
402 m_grid
->MoveCursorRightBlock(false);
404 CPPUNIT_ASSERT_EQUAL(1, m_grid
->GetGridCursorCol());
405 CPPUNIT_ASSERT_EQUAL(3, m_grid
->GetGridCursorRow());
407 m_grid
->MoveCursorUpBlock(false);
409 CPPUNIT_ASSERT_EQUAL(1, m_grid
->GetGridCursorCol());
410 CPPUNIT_ASSERT_EQUAL(0, m_grid
->GetGridCursorRow());
413 void GridTestCase::Selection()
417 CPPUNIT_ASSERT(m_grid
->IsSelection());
418 CPPUNIT_ASSERT(m_grid
->IsInSelection(0, 0));
419 CPPUNIT_ASSERT(m_grid
->IsInSelection(9, 1));
421 m_grid
->SelectBlock(1, 0, 3, 1);
423 wxGridCellCoordsArray topleft
= m_grid
->GetSelectionBlockTopLeft();
424 wxGridCellCoordsArray bottomright
= m_grid
->GetSelectionBlockBottomRight();
426 CPPUNIT_ASSERT_EQUAL(1, topleft
.Count());
427 CPPUNIT_ASSERT_EQUAL(1, bottomright
.Count());
429 CPPUNIT_ASSERT_EQUAL(0, topleft
.Item(0).GetCol());
430 CPPUNIT_ASSERT_EQUAL(1, topleft
.Item(0).GetRow());
431 CPPUNIT_ASSERT_EQUAL(1, bottomright
.Item(0).GetCol());
432 CPPUNIT_ASSERT_EQUAL(3, bottomright
.Item(0).GetRow());
434 m_grid
->SelectCol(1);
436 CPPUNIT_ASSERT(m_grid
->IsInSelection(0, 1));
437 CPPUNIT_ASSERT(m_grid
->IsInSelection(9, 1));
438 CPPUNIT_ASSERT(!m_grid
->IsInSelection(3, 0));
440 m_grid
->SelectRow(4);
442 CPPUNIT_ASSERT(m_grid
->IsInSelection(4, 0));
443 CPPUNIT_ASSERT(m_grid
->IsInSelection(4, 1));
444 CPPUNIT_ASSERT(!m_grid
->IsInSelection(3, 0));
447 void GridTestCase::AddRowCol()
449 CPPUNIT_ASSERT_EQUAL(10, m_grid
->GetNumberRows());
450 CPPUNIT_ASSERT_EQUAL(2, m_grid
->GetNumberCols());
452 m_grid
->AppendCols();
453 m_grid
->AppendRows();
455 CPPUNIT_ASSERT_EQUAL(11, m_grid
->GetNumberRows());
456 CPPUNIT_ASSERT_EQUAL(3, m_grid
->GetNumberCols());
458 m_grid
->AppendCols(2);
459 m_grid
->AppendRows(2);
461 CPPUNIT_ASSERT_EQUAL(13, m_grid
->GetNumberRows());
462 CPPUNIT_ASSERT_EQUAL(5, m_grid
->GetNumberCols());
464 m_grid
->InsertCols(1, 2);
465 m_grid
->InsertRows(2, 3);
467 CPPUNIT_ASSERT_EQUAL(16, m_grid
->GetNumberRows());
468 CPPUNIT_ASSERT_EQUAL(7, m_grid
->GetNumberCols());
471 void GridTestCase::ColumnOrder()
473 m_grid
->AppendCols(2);
475 CPPUNIT_ASSERT_EQUAL(4, m_grid
->GetNumberCols());
478 neworder
.push_back(1);
479 neworder
.push_back(3);
480 neworder
.push_back(2);
481 neworder
.push_back(0);
483 m_grid
->SetColumnsOrder(neworder
);
485 CPPUNIT_ASSERT_EQUAL(0, m_grid
->GetColPos(1));
486 CPPUNIT_ASSERT_EQUAL(1, m_grid
->GetColPos(3));
487 CPPUNIT_ASSERT_EQUAL(2, m_grid
->GetColPos(2));
488 CPPUNIT_ASSERT_EQUAL(3, m_grid
->GetColPos(0));
490 CPPUNIT_ASSERT_EQUAL(1, m_grid
->GetColAt(0));
491 CPPUNIT_ASSERT_EQUAL(3, m_grid
->GetColAt(1));
492 CPPUNIT_ASSERT_EQUAL(2, m_grid
->GetColAt(2));
493 CPPUNIT_ASSERT_EQUAL(0, m_grid
->GetColAt(3));
495 m_grid
->ResetColPos();
497 CPPUNIT_ASSERT_EQUAL(0, m_grid
->GetColPos(0));
498 CPPUNIT_ASSERT_EQUAL(1, m_grid
->GetColPos(1));
499 CPPUNIT_ASSERT_EQUAL(2, m_grid
->GetColPos(2));
500 CPPUNIT_ASSERT_EQUAL(3, m_grid
->GetColPos(3));
503 void GridTestCase::LineFormatting()
505 CPPUNIT_ASSERT(m_grid
->GridLinesEnabled());
507 m_grid
->EnableGridLines(false);
509 CPPUNIT_ASSERT(!m_grid
->GridLinesEnabled());
511 m_grid
->EnableGridLines();
513 m_grid
->SetGridLineColour(*wxRED
);
515 CPPUNIT_ASSERT_EQUAL(m_grid
->GetGridLineColour(), *wxRED
);
518 void GridTestCase::SortSupport()
520 CPPUNIT_ASSERT_EQUAL(wxNOT_FOUND
, m_grid
->GetSortingColumn());
522 m_grid
->SetSortingColumn(1);
524 CPPUNIT_ASSERT(!m_grid
->IsSortingBy(0));
525 CPPUNIT_ASSERT(m_grid
->IsSortingBy(1));
526 CPPUNIT_ASSERT(m_grid
->IsSortOrderAscending());
528 m_grid
->SetSortingColumn(0, false);
530 CPPUNIT_ASSERT(m_grid
->IsSortingBy(0));
531 CPPUNIT_ASSERT(!m_grid
->IsSortingBy(1));
532 CPPUNIT_ASSERT(!m_grid
->IsSortOrderAscending());
534 m_grid
->UnsetSortingColumn();
536 CPPUNIT_ASSERT(!m_grid
->IsSortingBy(0));
537 CPPUNIT_ASSERT(!m_grid
->IsSortingBy(1));
540 void GridTestCase::Labels()
542 CPPUNIT_ASSERT_EQUAL("A", m_grid
->GetColLabelValue(0));
543 CPPUNIT_ASSERT_EQUAL("1", m_grid
->GetRowLabelValue(0));
545 m_grid
->SetColLabelValue(0, "Column 1");
546 m_grid
->SetRowLabelValue(0, "Row 1");
548 CPPUNIT_ASSERT_EQUAL("Column 1", m_grid
->GetColLabelValue(0));
549 CPPUNIT_ASSERT_EQUAL("Row 1", m_grid
->GetRowLabelValue(0));
551 m_grid
->SetLabelTextColour(*wxGREEN
);
552 m_grid
->SetLabelBackgroundColour(*wxRED
);
554 CPPUNIT_ASSERT_EQUAL(*wxGREEN
, m_grid
->GetLabelTextColour());
555 CPPUNIT_ASSERT_EQUAL(*wxRED
, m_grid
->GetLabelBackgroundColour());
557 m_grid
->SetColLabelTextOrientation(wxVERTICAL
);
559 CPPUNIT_ASSERT_EQUAL(static_cast<int>(wxVERTICAL
),
560 static_cast<int>(m_grid
->GetColLabelTextOrientation()));
563 void GridTestCase::SelectionMode()
565 //We already test this mode in Select
566 CPPUNIT_ASSERT_EQUAL(wxGrid::wxGridSelectCells
,
567 m_grid
->GetSelectionMode());
569 //Test row selection be selecting a single cell and checking the whole
571 m_grid
->SetSelectionMode(wxGrid::wxGridSelectRows
);
572 m_grid
->SelectBlock(3, 1, 3, 1);
574 wxArrayInt selectedRows
= m_grid
->GetSelectedRows();
575 CPPUNIT_ASSERT_EQUAL(1, selectedRows
.Count());
576 CPPUNIT_ASSERT_EQUAL(3, selectedRows
[0]);
578 CPPUNIT_ASSERT_EQUAL(wxGrid::wxGridSelectRows
,
579 m_grid
->GetSelectionMode());
582 //Test column selection be selecting a single cell and checking the whole
584 m_grid
->SetSelectionMode(wxGrid::wxGridSelectColumns
);
585 m_grid
->SelectBlock(3, 1, 3, 1);
587 wxArrayInt selectedCols
= m_grid
->GetSelectedCols();
588 CPPUNIT_ASSERT_EQUAL(1, selectedCols
.Count());
589 CPPUNIT_ASSERT_EQUAL(1, selectedCols
[0]);
591 CPPUNIT_ASSERT_EQUAL(wxGrid::wxGridSelectColumns
,
592 m_grid
->GetSelectionMode());
595 void GridTestCase::CellFormatting()
597 //Check that initial alignment is default
598 int horiz
, cellhoriz
, vert
, cellvert
;
600 m_grid
->GetDefaultCellAlignment(&horiz
, &vert
);
601 m_grid
->GetCellAlignment(0, 0, &cellhoriz
, &cellvert
);
603 CPPUNIT_ASSERT_EQUAL(cellhoriz
, horiz
);
604 CPPUNIT_ASSERT_EQUAL(cellvert
, vert
);
606 //Check initial text colour and background colour are default
609 back
= m_grid
->GetDefaultCellBackgroundColour();
611 CPPUNIT_ASSERT_EQUAL(back
, m_grid
->GetCellBackgroundColour(0, 0));
613 back
= m_grid
->GetDefaultCellTextColour();
615 CPPUNIT_ASSERT_EQUAL(back
, m_grid
->GetCellTextColour(0, 0));
617 #if WXWIN_COMPATIBILITY_2_8
618 m_grid
->SetCellAlignment(wxALIGN_CENTRE
, 0, 0);
619 m_grid
->GetCellAlignment(0, 0, &cellhoriz
, &cellvert
);
621 CPPUNIT_ASSERT_EQUAL(static_cast<int>(wxALIGN_CENTRE
), cellhoriz
);
622 CPPUNIT_ASSERT_EQUAL(static_cast<int>(wxALIGN_CENTRE
), cellvert
);
623 #endif // WXWIN_COMPATIBILITY_2_8
625 m_grid
->SetCellAlignment(0, 0, wxALIGN_LEFT
, wxALIGN_BOTTOM
);
626 m_grid
->GetCellAlignment(0, 0, &cellhoriz
, &cellvert
);
628 CPPUNIT_ASSERT_EQUAL(static_cast<int>(wxALIGN_LEFT
), cellhoriz
);
629 CPPUNIT_ASSERT_EQUAL(static_cast<int>(wxALIGN_BOTTOM
), cellvert
);
631 #if WXWIN_COMPATIBILITY_2_8
632 m_grid
->SetCellTextColour(*wxRED
, 0, 0);
633 CPPUNIT_ASSERT_EQUAL(*wxRED
, m_grid
->GetCellTextColour(0, 0));
634 #endif // WXWIN_COMPATIBILITY_2_8
636 m_grid
->SetCellTextColour(0, 0, *wxGREEN
);
637 CPPUNIT_ASSERT_EQUAL(*wxGREEN
, m_grid
->GetCellTextColour(0, 0));
640 void GridTestCase::Editable()
642 #if wxUSE_UIACTIONSIMULATOR
643 //As the grid is not editable we shouldn't create an editor
644 EventCounter
created(m_grid
, wxEVT_GRID_EDITOR_CREATED
);
646 wxUIActionSimulator sim
;
648 CPPUNIT_ASSERT(m_grid
->IsEditable());
650 m_grid
->EnableEditing(false);
652 CPPUNIT_ASSERT(!m_grid
->IsEditable());
655 m_grid
->SetGridCursor(1, 1);
656 m_grid
->ShowCellEditControl();
661 sim
.Char(WXK_RETURN
);
664 CPPUNIT_ASSERT_EQUAL(0, created
.GetCount());
668 void GridTestCase::ReadOnly()
670 #if wxUSE_UIACTIONSIMULATOR
671 //As the cell is readonly we shouldn't create an editor
672 EventCounter
created(m_grid
, wxEVT_GRID_EDITOR_CREATED
);
674 wxUIActionSimulator sim
;
676 CPPUNIT_ASSERT(!m_grid
->IsReadOnly(1, 1));
678 m_grid
->SetReadOnly(1, 1);
680 CPPUNIT_ASSERT(m_grid
->IsReadOnly(1, 1));
683 m_grid
->SetGridCursor(1, 1);
685 CPPUNIT_ASSERT(m_grid
->IsCurrentCellReadOnly());
687 m_grid
->ShowCellEditControl();
692 sim
.Char(WXK_RETURN
);
695 CPPUNIT_ASSERT_EQUAL(0, created
.GetCount());