gridtest.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/controls/gridtest.cpp
  3. // Purpose: wxGrid unit test
  4. // Author: Steven Lamerton
  5. // Created: 2010-06-25
  6. // Copyright: (c) 2010 Steven Lamerton
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #include "testprec.h"
  9. #if wxUSE_GRID
  10. #ifdef __BORLANDC__
  11. #pragma hdrstop
  12. #endif
  13. #ifndef WX_PRECOMP
  14. #include "wx/app.h"
  15. #endif // WX_PRECOMP
  16. #include "wx/grid.h"
  17. #include "testableframe.h"
  18. #include "asserthelper.h"
  19. #include "wx/uiaction.h"
  20. // FIXME: A lot of mouse-related tests sporadically fail in wxGTK. This happens
  21. // almost all the time but sometimes the tests do pass and the failure
  22. // doesn't happen when debugging so this looks like some kind of event
  23. // dispatching/simulating problem rather than a real problem in wxGrid.
  24. //
  25. // Just disable these tests for now but it would be really great to
  26. // really fix the problem.
  27. #ifdef __WXGTK__
  28. #define NONGTK_TEST(test)
  29. #else
  30. #define NONGTK_TEST(test) WXUISIM_TEST(test)
  31. #endif
  32. class GridTestCase : public CppUnit::TestCase
  33. {
  34. public:
  35. GridTestCase() { }
  36. virtual void setUp();
  37. virtual void tearDown();
  38. private:
  39. CPPUNIT_TEST_SUITE( GridTestCase );
  40. WXUISIM_TEST( CellEdit );
  41. NONGTK_TEST( CellClick );
  42. NONGTK_TEST( CellSelect );
  43. NONGTK_TEST( LabelClick );
  44. NONGTK_TEST( SortClick );
  45. WXUISIM_TEST( Size );
  46. NONGTK_TEST( RangeSelect );
  47. CPPUNIT_TEST( Cursor );
  48. CPPUNIT_TEST( Selection );
  49. CPPUNIT_TEST( AddRowCol );
  50. CPPUNIT_TEST( ColumnOrder );
  51. CPPUNIT_TEST( ColumnVisibility );
  52. CPPUNIT_TEST( LineFormatting );
  53. CPPUNIT_TEST( SortSupport );
  54. CPPUNIT_TEST( Labels );
  55. CPPUNIT_TEST( SelectionMode );
  56. CPPUNIT_TEST( CellFormatting );
  57. WXUISIM_TEST( Editable );
  58. WXUISIM_TEST( ReadOnly );
  59. CPPUNIT_TEST( PseudoTest_NativeHeader );
  60. NONGTK_TEST( LabelClick );
  61. NONGTK_TEST( SortClick );
  62. CPPUNIT_TEST( ColumnOrder );
  63. CPPUNIT_TEST( PseudoTest_NativeLabels );
  64. NONGTK_TEST( LabelClick );
  65. NONGTK_TEST( SortClick );
  66. CPPUNIT_TEST( ColumnOrder );
  67. CPPUNIT_TEST_SUITE_END();
  68. void CellEdit();
  69. void CellClick();
  70. void CellSelect();
  71. void LabelClick();
  72. void SortClick();
  73. void Size();
  74. void RangeSelect();
  75. void Cursor();
  76. void Selection();
  77. void AddRowCol();
  78. void ColumnOrder();
  79. void ColumnVisibility();
  80. void LineFormatting();
  81. void SortSupport();
  82. void Labels();
  83. void SelectionMode();
  84. void CellFormatting();
  85. void Editable();
  86. void ReadOnly();
  87. void PseudoTest_NativeHeader() { ms_nativeheader = true; }
  88. void PseudoTest_NativeLabels() { ms_nativeheader = false;
  89. ms_nativelabels = true; }
  90. static bool ms_nativeheader;
  91. static bool ms_nativelabels;
  92. wxGrid *m_grid;
  93. DECLARE_NO_COPY_CLASS(GridTestCase)
  94. };
  95. // register in the unnamed registry so that these tests are run by default
  96. CPPUNIT_TEST_SUITE_REGISTRATION( GridTestCase );
  97. // also include in its own registry so that these tests can be run alone
  98. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GridTestCase, "GridTestCase" );
  99. //initialise the static variable
  100. bool GridTestCase::ms_nativeheader = false;
  101. bool GridTestCase::ms_nativelabels = false;
  102. void GridTestCase::setUp()
  103. {
  104. m_grid = new wxGrid(wxTheApp->GetTopWindow(), wxID_ANY);
  105. m_grid->CreateGrid(10, 2);
  106. m_grid->SetSize(400, 200);
  107. if( ms_nativeheader )
  108. m_grid->UseNativeColHeader();
  109. if( ms_nativelabels )
  110. m_grid->SetUseNativeColLabels();
  111. m_grid->Refresh();
  112. m_grid->Update();
  113. }
  114. void GridTestCase::tearDown()
  115. {
  116. // This is just a hack to continue the rest of the tests to run: if we
  117. // destroy the header control while it has capture, this results in an
  118. // assert failure and while handling an exception from it more bad things
  119. // happen (as it's thrown from a dtor), resulting in simply aborting
  120. // everything. So ensure that it doesn't have capture in any case.
  121. //
  122. // Of course, the right thing to do would be to understand why does it
  123. // still have capture when the grid is destroyed sometimes.
  124. wxWindow* const win = wxWindow::GetCapture();
  125. if ( win )
  126. win->ReleaseMouse();
  127. wxDELETE(m_grid);
  128. }
  129. void GridTestCase::CellEdit()
  130. {
  131. // TODO on OSX when running the grid test suite solo this works
  132. // but not when running it together with other tests
  133. #if wxUSE_UIACTIONSIMULATOR && !defined(__WXOSX__)
  134. EventCounter changing(m_grid, wxEVT_GRID_CELL_CHANGING);
  135. EventCounter changed(m_grid, wxEVT_GRID_CELL_CHANGED);
  136. EventCounter created(m_grid, wxEVT_GRID_EDITOR_CREATED);
  137. wxUIActionSimulator sim;
  138. m_grid->SetFocus();
  139. m_grid->SetGridCursor(1, 1);
  140. m_grid->ShowCellEditControl();
  141. sim.Text("abab");
  142. sim.Char(WXK_RETURN);
  143. wxYield();
  144. CPPUNIT_ASSERT_EQUAL(1, created.GetCount());
  145. CPPUNIT_ASSERT_EQUAL(1, changing.GetCount());
  146. CPPUNIT_ASSERT_EQUAL(1, changed.GetCount());
  147. #endif
  148. }
  149. void GridTestCase::CellClick()
  150. {
  151. #if wxUSE_UIACTIONSIMULATOR
  152. EventCounter lclick(m_grid, wxEVT_GRID_CELL_LEFT_CLICK);
  153. EventCounter ldclick(m_grid, wxEVT_GRID_CELL_LEFT_DCLICK);
  154. EventCounter rclick(m_grid, wxEVT_GRID_CELL_RIGHT_CLICK);
  155. EventCounter rdclick(m_grid, wxEVT_GRID_CELL_RIGHT_DCLICK);
  156. wxUIActionSimulator sim;
  157. wxRect rect = m_grid->CellToRect(0, 0);
  158. wxPoint point = m_grid->CalcScrolledPosition(rect.GetPosition());
  159. point = m_grid->ClientToScreen(point + wxPoint(m_grid->GetRowLabelSize(),
  160. m_grid->GetColLabelSize())
  161. + wxPoint(2, 2));
  162. sim.MouseMove(point);
  163. wxYield();
  164. sim.MouseClick();
  165. wxYield();
  166. CPPUNIT_ASSERT_EQUAL(1, lclick.GetCount());
  167. lclick.Clear();
  168. sim.MouseDblClick();
  169. wxYield();
  170. //A double click event sends a single click event first
  171. //test to ensure this still happens in the future
  172. CPPUNIT_ASSERT_EQUAL(1, lclick.GetCount());
  173. CPPUNIT_ASSERT_EQUAL(1, ldclick.GetCount());
  174. sim.MouseClick(wxMOUSE_BTN_RIGHT);
  175. wxYield();
  176. CPPUNIT_ASSERT_EQUAL(1, rclick.GetCount());
  177. rclick.Clear();
  178. sim.MouseDblClick(wxMOUSE_BTN_RIGHT);
  179. wxYield();
  180. CPPUNIT_ASSERT_EQUAL(1, rclick.GetCount());
  181. CPPUNIT_ASSERT_EQUAL(1, rdclick.GetCount());
  182. #endif
  183. }
  184. void GridTestCase::CellSelect()
  185. {
  186. #if wxUSE_UIACTIONSIMULATOR
  187. EventCounter cell(m_grid, wxEVT_GRID_SELECT_CELL);
  188. wxUIActionSimulator sim;
  189. wxRect rect = m_grid->CellToRect(0, 0);
  190. wxPoint point = m_grid->CalcScrolledPosition(rect.GetPosition());
  191. point = m_grid->ClientToScreen(point + wxPoint(m_grid->GetRowLabelSize(),
  192. m_grid->GetColLabelSize())
  193. + wxPoint(4, 4));
  194. sim.MouseMove(point);
  195. wxYield();
  196. sim.MouseClick();
  197. wxYield();
  198. CPPUNIT_ASSERT_EQUAL(1, cell.GetCount());
  199. cell.Clear();
  200. m_grid->SetGridCursor(1, 1);
  201. m_grid->GoToCell(1, 0);
  202. sim.MouseMove(point);
  203. wxYield();
  204. sim.MouseDblClick();
  205. wxYield();
  206. CPPUNIT_ASSERT_EQUAL(3, cell.GetCount());
  207. #endif
  208. }
  209. void GridTestCase::LabelClick()
  210. {
  211. #if wxUSE_UIACTIONSIMULATOR
  212. EventCounter lclick(m_grid, wxEVT_GRID_LABEL_LEFT_CLICK);
  213. EventCounter ldclick(m_grid, wxEVT_GRID_LABEL_LEFT_DCLICK);
  214. EventCounter rclick(m_grid, wxEVT_GRID_LABEL_RIGHT_CLICK);
  215. EventCounter rdclick(m_grid, wxEVT_GRID_LABEL_RIGHT_DCLICK);
  216. wxUIActionSimulator sim;
  217. wxPoint pos(m_grid->GetRowLabelSize() + 2, 2);
  218. pos = m_grid->ClientToScreen(pos);
  219. sim.MouseMove(pos);
  220. wxYield();
  221. sim.MouseClick();
  222. wxYield();
  223. CPPUNIT_ASSERT_EQUAL(1, lclick.GetCount());
  224. sim.MouseDblClick();
  225. wxYield();
  226. CPPUNIT_ASSERT_EQUAL(1, ldclick.GetCount());
  227. sim.MouseClick(wxMOUSE_BTN_RIGHT);
  228. wxYield();
  229. CPPUNIT_ASSERT_EQUAL(1, rclick.GetCount());
  230. rclick.Clear();
  231. sim.MouseDblClick(wxMOUSE_BTN_RIGHT);
  232. wxYield();
  233. if( ms_nativeheader )
  234. {
  235. //Right double click not supported with native headers so we get two
  236. //right click events
  237. CPPUNIT_ASSERT_EQUAL(2, rclick.GetCount());
  238. }
  239. else
  240. {
  241. CPPUNIT_ASSERT_EQUAL(1, rclick.GetCount());
  242. CPPUNIT_ASSERT_EQUAL(1, rdclick.GetCount());
  243. }
  244. #endif
  245. }
  246. void GridTestCase::SortClick()
  247. {
  248. #if wxUSE_UIACTIONSIMULATOR
  249. m_grid->SetSortingColumn(0);
  250. EventCounter sort(m_grid, wxEVT_GRID_COL_SORT);
  251. wxUIActionSimulator sim;
  252. wxPoint pos(m_grid->GetRowLabelSize() + 4, 4);
  253. pos = m_grid->ClientToScreen(pos);
  254. sim.MouseMove(pos);
  255. wxYield();
  256. sim.MouseClick();
  257. wxYield();
  258. CPPUNIT_ASSERT_EQUAL(1, sort.GetCount());
  259. #endif
  260. }
  261. void GridTestCase::Size()
  262. {
  263. // TODO on OSX resizing interactively works, but not automated
  264. #if wxUSE_UIACTIONSIMULATOR && !defined(__WXGTK__) && !defined(__WXOSX__)
  265. EventCounter colsize(m_grid, wxEVT_GRID_COL_SIZE);
  266. EventCounter rowsize(m_grid, wxEVT_GRID_ROW_SIZE);
  267. wxUIActionSimulator sim;
  268. wxPoint pt = m_grid->ClientToScreen(wxPoint(m_grid->GetRowLabelSize() +
  269. m_grid->GetColSize(0), 5));
  270. sim.MouseMove(pt);
  271. wxYield();
  272. sim.MouseDown();
  273. wxYield();
  274. sim.MouseMove(pt.x + 50, pt.y);
  275. wxYield();
  276. sim.MouseUp();
  277. wxYield();
  278. CPPUNIT_ASSERT_EQUAL(1, colsize.GetCount());
  279. pt = m_grid->ClientToScreen(wxPoint(5, m_grid->GetColLabelSize() +
  280. m_grid->GetRowSize(0)));
  281. sim.MouseDragDrop(pt.x, pt.y, pt.x, pt.y + 50);
  282. wxYield();
  283. CPPUNIT_ASSERT_EQUAL(1, rowsize.GetCount());
  284. #endif
  285. }
  286. void GridTestCase::RangeSelect()
  287. {
  288. #if wxUSE_UIACTIONSIMULATOR
  289. EventCounter select(m_grid, wxEVT_GRID_RANGE_SELECT);
  290. wxUIActionSimulator sim;
  291. //We add the extra 10 to ensure that we are inside the cell
  292. wxPoint pt = m_grid->ClientToScreen(wxPoint(m_grid->GetRowLabelSize() + 10,
  293. m_grid->GetColLabelSize() + 10)
  294. );
  295. sim.MouseMove(pt);
  296. wxYield();
  297. sim.MouseDown();
  298. wxYield();
  299. sim.MouseMove(pt.x + 50, pt.y + 50);
  300. wxYield();
  301. sim.MouseUp();
  302. wxYield();
  303. CPPUNIT_ASSERT_EQUAL(1, select.GetCount());
  304. #endif
  305. }
  306. void GridTestCase::Cursor()
  307. {
  308. m_grid->SetGridCursor(1, 1);
  309. CPPUNIT_ASSERT_EQUAL(1, m_grid->GetGridCursorCol());
  310. CPPUNIT_ASSERT_EQUAL(1, m_grid->GetGridCursorRow());
  311. m_grid->MoveCursorDown(false);
  312. m_grid->MoveCursorLeft(false);
  313. m_grid->MoveCursorUp(false);
  314. m_grid->MoveCursorUp(false);
  315. m_grid->MoveCursorRight(false);
  316. CPPUNIT_ASSERT_EQUAL(1, m_grid->GetGridCursorCol());
  317. CPPUNIT_ASSERT_EQUAL(0, m_grid->GetGridCursorRow());
  318. m_grid->SetCellValue(0, 0, "some text");
  319. m_grid->SetCellValue(3, 0, "other text");
  320. m_grid->SetCellValue(0, 1, "more text");
  321. m_grid->SetCellValue(3, 1, "extra text");
  322. m_grid->Update();
  323. m_grid->Refresh();
  324. m_grid->MoveCursorLeftBlock(false);
  325. CPPUNIT_ASSERT_EQUAL(0, m_grid->GetGridCursorCol());
  326. CPPUNIT_ASSERT_EQUAL(0, m_grid->GetGridCursorRow());
  327. m_grid->MoveCursorDownBlock(false);
  328. CPPUNIT_ASSERT_EQUAL(0, m_grid->GetGridCursorCol());
  329. CPPUNIT_ASSERT_EQUAL(3, m_grid->GetGridCursorRow());
  330. m_grid->MoveCursorRightBlock(false);
  331. CPPUNIT_ASSERT_EQUAL(1, m_grid->GetGridCursorCol());
  332. CPPUNIT_ASSERT_EQUAL(3, m_grid->GetGridCursorRow());
  333. m_grid->MoveCursorUpBlock(false);
  334. CPPUNIT_ASSERT_EQUAL(1, m_grid->GetGridCursorCol());
  335. CPPUNIT_ASSERT_EQUAL(0, m_grid->GetGridCursorRow());
  336. }
  337. void GridTestCase::Selection()
  338. {
  339. m_grid->SelectAll();
  340. CPPUNIT_ASSERT(m_grid->IsSelection());
  341. CPPUNIT_ASSERT(m_grid->IsInSelection(0, 0));
  342. CPPUNIT_ASSERT(m_grid->IsInSelection(9, 1));
  343. m_grid->SelectBlock(1, 0, 3, 1);
  344. wxGridCellCoordsArray topleft = m_grid->GetSelectionBlockTopLeft();
  345. wxGridCellCoordsArray bottomright = m_grid->GetSelectionBlockBottomRight();
  346. CPPUNIT_ASSERT_EQUAL(1, topleft.Count());
  347. CPPUNIT_ASSERT_EQUAL(1, bottomright.Count());
  348. CPPUNIT_ASSERT_EQUAL(0, topleft.Item(0).GetCol());
  349. CPPUNIT_ASSERT_EQUAL(1, topleft.Item(0).GetRow());
  350. CPPUNIT_ASSERT_EQUAL(1, bottomright.Item(0).GetCol());
  351. CPPUNIT_ASSERT_EQUAL(3, bottomright.Item(0).GetRow());
  352. m_grid->SelectCol(1);
  353. CPPUNIT_ASSERT(m_grid->IsInSelection(0, 1));
  354. CPPUNIT_ASSERT(m_grid->IsInSelection(9, 1));
  355. CPPUNIT_ASSERT(!m_grid->IsInSelection(3, 0));
  356. m_grid->SelectRow(4);
  357. CPPUNIT_ASSERT(m_grid->IsInSelection(4, 0));
  358. CPPUNIT_ASSERT(m_grid->IsInSelection(4, 1));
  359. CPPUNIT_ASSERT(!m_grid->IsInSelection(3, 0));
  360. }
  361. void GridTestCase::AddRowCol()
  362. {
  363. CPPUNIT_ASSERT_EQUAL(10, m_grid->GetNumberRows());
  364. CPPUNIT_ASSERT_EQUAL(2, m_grid->GetNumberCols());
  365. m_grid->AppendCols();
  366. m_grid->AppendRows();
  367. CPPUNIT_ASSERT_EQUAL(11, m_grid->GetNumberRows());
  368. CPPUNIT_ASSERT_EQUAL(3, m_grid->GetNumberCols());
  369. m_grid->AppendCols(2);
  370. m_grid->AppendRows(2);
  371. CPPUNIT_ASSERT_EQUAL(13, m_grid->GetNumberRows());
  372. CPPUNIT_ASSERT_EQUAL(5, m_grid->GetNumberCols());
  373. m_grid->InsertCols(1, 2);
  374. m_grid->InsertRows(2, 3);
  375. CPPUNIT_ASSERT_EQUAL(16, m_grid->GetNumberRows());
  376. CPPUNIT_ASSERT_EQUAL(7, m_grid->GetNumberCols());
  377. }
  378. void GridTestCase::ColumnOrder()
  379. {
  380. m_grid->AppendCols(2);
  381. CPPUNIT_ASSERT_EQUAL(4, m_grid->GetNumberCols());
  382. wxArrayInt neworder;
  383. neworder.push_back(1);
  384. neworder.push_back(3);
  385. neworder.push_back(2);
  386. neworder.push_back(0);
  387. m_grid->SetColumnsOrder(neworder);
  388. CPPUNIT_ASSERT_EQUAL(0, m_grid->GetColPos(1));
  389. CPPUNIT_ASSERT_EQUAL(1, m_grid->GetColPos(3));
  390. CPPUNIT_ASSERT_EQUAL(2, m_grid->GetColPos(2));
  391. CPPUNIT_ASSERT_EQUAL(3, m_grid->GetColPos(0));
  392. CPPUNIT_ASSERT_EQUAL(1, m_grid->GetColAt(0));
  393. CPPUNIT_ASSERT_EQUAL(3, m_grid->GetColAt(1));
  394. CPPUNIT_ASSERT_EQUAL(2, m_grid->GetColAt(2));
  395. CPPUNIT_ASSERT_EQUAL(0, m_grid->GetColAt(3));
  396. m_grid->ResetColPos();
  397. CPPUNIT_ASSERT_EQUAL(0, m_grid->GetColPos(0));
  398. CPPUNIT_ASSERT_EQUAL(1, m_grid->GetColPos(1));
  399. CPPUNIT_ASSERT_EQUAL(2, m_grid->GetColPos(2));
  400. CPPUNIT_ASSERT_EQUAL(3, m_grid->GetColPos(3));
  401. }
  402. void GridTestCase::ColumnVisibility()
  403. {
  404. m_grid->AppendCols(3);
  405. CPPUNIT_ASSERT( m_grid->IsColShown(1) );
  406. m_grid->HideCol(1);
  407. CPPUNIT_ASSERT( !m_grid->IsColShown(1) );
  408. CPPUNIT_ASSERT( m_grid->IsColShown(2) );
  409. m_grid->ShowCol(1);
  410. CPPUNIT_ASSERT( m_grid->IsColShown(1) );
  411. }
  412. void GridTestCase::LineFormatting()
  413. {
  414. CPPUNIT_ASSERT(m_grid->GridLinesEnabled());
  415. m_grid->EnableGridLines(false);
  416. CPPUNIT_ASSERT(!m_grid->GridLinesEnabled());
  417. m_grid->EnableGridLines();
  418. m_grid->SetGridLineColour(*wxRED);
  419. CPPUNIT_ASSERT_EQUAL(m_grid->GetGridLineColour(), *wxRED);
  420. }
  421. void GridTestCase::SortSupport()
  422. {
  423. CPPUNIT_ASSERT_EQUAL(wxNOT_FOUND, m_grid->GetSortingColumn());
  424. m_grid->SetSortingColumn(1);
  425. CPPUNIT_ASSERT(!m_grid->IsSortingBy(0));
  426. CPPUNIT_ASSERT(m_grid->IsSortingBy(1));
  427. CPPUNIT_ASSERT(m_grid->IsSortOrderAscending());
  428. m_grid->SetSortingColumn(0, false);
  429. CPPUNIT_ASSERT(m_grid->IsSortingBy(0));
  430. CPPUNIT_ASSERT(!m_grid->IsSortingBy(1));
  431. CPPUNIT_ASSERT(!m_grid->IsSortOrderAscending());
  432. m_grid->UnsetSortingColumn();
  433. CPPUNIT_ASSERT(!m_grid->IsSortingBy(0));
  434. CPPUNIT_ASSERT(!m_grid->IsSortingBy(1));
  435. }
  436. void GridTestCase::Labels()
  437. {
  438. CPPUNIT_ASSERT_EQUAL("A", m_grid->GetColLabelValue(0));
  439. CPPUNIT_ASSERT_EQUAL("1", m_grid->GetRowLabelValue(0));
  440. m_grid->SetColLabelValue(0, "Column 1");
  441. m_grid->SetRowLabelValue(0, "Row 1");
  442. CPPUNIT_ASSERT_EQUAL("Column 1", m_grid->GetColLabelValue(0));
  443. CPPUNIT_ASSERT_EQUAL("Row 1", m_grid->GetRowLabelValue(0));
  444. m_grid->SetLabelTextColour(*wxGREEN);
  445. m_grid->SetLabelBackgroundColour(*wxRED);
  446. CPPUNIT_ASSERT_EQUAL(*wxGREEN, m_grid->GetLabelTextColour());
  447. CPPUNIT_ASSERT_EQUAL(*wxRED, m_grid->GetLabelBackgroundColour());
  448. m_grid->SetColLabelTextOrientation(wxVERTICAL);
  449. CPPUNIT_ASSERT_EQUAL(static_cast<int>(wxVERTICAL),
  450. static_cast<int>(m_grid->GetColLabelTextOrientation()));
  451. }
  452. void GridTestCase::SelectionMode()
  453. {
  454. //We already test this mode in Select
  455. CPPUNIT_ASSERT_EQUAL(wxGrid::wxGridSelectCells,
  456. m_grid->GetSelectionMode());
  457. //Test row selection be selecting a single cell and checking the whole
  458. //row is selected
  459. m_grid->SetSelectionMode(wxGrid::wxGridSelectRows);
  460. m_grid->SelectBlock(3, 1, 3, 1);
  461. wxArrayInt selectedRows = m_grid->GetSelectedRows();
  462. CPPUNIT_ASSERT_EQUAL(1, selectedRows.Count());
  463. CPPUNIT_ASSERT_EQUAL(3, selectedRows[0]);
  464. CPPUNIT_ASSERT_EQUAL(wxGrid::wxGridSelectRows,
  465. m_grid->GetSelectionMode());
  466. //Test column selection be selecting a single cell and checking the whole
  467. //column is selected
  468. m_grid->SetSelectionMode(wxGrid::wxGridSelectColumns);
  469. m_grid->SelectBlock(3, 1, 3, 1);
  470. wxArrayInt selectedCols = m_grid->GetSelectedCols();
  471. CPPUNIT_ASSERT_EQUAL(1, selectedCols.Count());
  472. CPPUNIT_ASSERT_EQUAL(1, selectedCols[0]);
  473. CPPUNIT_ASSERT_EQUAL(wxGrid::wxGridSelectColumns,
  474. m_grid->GetSelectionMode());
  475. }
  476. void GridTestCase::CellFormatting()
  477. {
  478. //Check that initial alignment is default
  479. int horiz, cellhoriz, vert, cellvert;
  480. m_grid->GetDefaultCellAlignment(&horiz, &vert);
  481. m_grid->GetCellAlignment(0, 0, &cellhoriz, &cellvert);
  482. CPPUNIT_ASSERT_EQUAL(cellhoriz, horiz);
  483. CPPUNIT_ASSERT_EQUAL(cellvert, vert);
  484. //Check initial text colour and background colour are default
  485. wxColour text, back;
  486. back = m_grid->GetDefaultCellBackgroundColour();
  487. CPPUNIT_ASSERT_EQUAL(back, m_grid->GetCellBackgroundColour(0, 0));
  488. back = m_grid->GetDefaultCellTextColour();
  489. CPPUNIT_ASSERT_EQUAL(back, m_grid->GetCellTextColour(0, 0));
  490. #if WXWIN_COMPATIBILITY_2_8
  491. m_grid->SetCellAlignment(wxALIGN_CENTRE, 0, 0);
  492. m_grid->GetCellAlignment(0, 0, &cellhoriz, &cellvert);
  493. CPPUNIT_ASSERT_EQUAL(static_cast<int>(wxALIGN_CENTRE), cellhoriz);
  494. CPPUNIT_ASSERT_EQUAL(static_cast<int>(wxALIGN_CENTRE), cellvert);
  495. #endif // WXWIN_COMPATIBILITY_2_8
  496. m_grid->SetCellAlignment(0, 0, wxALIGN_LEFT, wxALIGN_BOTTOM);
  497. m_grid->GetCellAlignment(0, 0, &cellhoriz, &cellvert);
  498. CPPUNIT_ASSERT_EQUAL(static_cast<int>(wxALIGN_LEFT), cellhoriz);
  499. CPPUNIT_ASSERT_EQUAL(static_cast<int>(wxALIGN_BOTTOM), cellvert);
  500. #if WXWIN_COMPATIBILITY_2_8
  501. m_grid->SetCellTextColour(*wxRED, 0, 0);
  502. CPPUNIT_ASSERT_EQUAL(*wxRED, m_grid->GetCellTextColour(0, 0));
  503. #endif // WXWIN_COMPATIBILITY_2_8
  504. m_grid->SetCellTextColour(0, 0, *wxGREEN);
  505. CPPUNIT_ASSERT_EQUAL(*wxGREEN, m_grid->GetCellTextColour(0, 0));
  506. }
  507. void GridTestCase::Editable()
  508. {
  509. #if wxUSE_UIACTIONSIMULATOR
  510. //As the grid is not editable we shouldn't create an editor
  511. EventCounter created(m_grid, wxEVT_GRID_EDITOR_CREATED);
  512. wxUIActionSimulator sim;
  513. CPPUNIT_ASSERT(m_grid->IsEditable());
  514. m_grid->EnableEditing(false);
  515. CPPUNIT_ASSERT(!m_grid->IsEditable());
  516. m_grid->SetFocus();
  517. m_grid->SetGridCursor(1, 1);
  518. m_grid->ShowCellEditControl();
  519. sim.Text("abab");
  520. wxYield();
  521. sim.Char(WXK_RETURN);
  522. wxYield();
  523. CPPUNIT_ASSERT_EQUAL(0, created.GetCount());
  524. #endif
  525. }
  526. void GridTestCase::ReadOnly()
  527. {
  528. #if wxUSE_UIACTIONSIMULATOR
  529. //As the cell is readonly we shouldn't create an editor
  530. EventCounter created(m_grid, wxEVT_GRID_EDITOR_CREATED);
  531. wxUIActionSimulator sim;
  532. CPPUNIT_ASSERT(!m_grid->IsReadOnly(1, 1));
  533. m_grid->SetReadOnly(1, 1);
  534. CPPUNIT_ASSERT(m_grid->IsReadOnly(1, 1));
  535. m_grid->SetFocus();
  536. m_grid->SetGridCursor(1, 1);
  537. CPPUNIT_ASSERT(m_grid->IsCurrentCellReadOnly());
  538. m_grid->ShowCellEditControl();
  539. sim.Text("abab");
  540. wxYield();
  541. sim.Char(WXK_RETURN);
  542. wxYield();
  543. CPPUNIT_ASSERT_EQUAL(0, created.GetCount());
  544. #endif
  545. }
  546. #endif //wxUSE_GRID