dataviewctrltest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/controls/dataviewctrltest.cpp
  3. // Purpose: wxDataViewCtrl unit test
  4. // Author: Vaclav Slavik
  5. // Created: 2011-08-08
  6. // Copyright: (c) 2011 Vaclav Slavik <vslavik@gmail.com>
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #if wxUSE_DATAVIEWCTRL
  13. #ifdef __BORLANDC__
  14. #pragma hdrstop
  15. #endif
  16. #include "wx/app.h"
  17. #include "wx/dataview.h"
  18. #include "testableframe.h"
  19. // ----------------------------------------------------------------------------
  20. // test class
  21. // ----------------------------------------------------------------------------
  22. class DataViewCtrlTestCase : public CppUnit::TestCase
  23. {
  24. public:
  25. DataViewCtrlTestCase() { }
  26. virtual void setUp();
  27. virtual void tearDown();
  28. private:
  29. CPPUNIT_TEST_SUITE( DataViewCtrlTestCase );
  30. CPPUNIT_TEST( DeleteSelected );
  31. CPPUNIT_TEST( DeleteNotSelected );
  32. CPPUNIT_TEST( GetSelectionForMulti );
  33. CPPUNIT_TEST( GetSelectionForSingle );
  34. CPPUNIT_TEST_SUITE_END();
  35. // Create wxDataViewTreeCtrl with the given style.
  36. void Create(long style);
  37. void DeleteSelected();
  38. void DeleteNotSelected();
  39. void GetSelectionForMulti();
  40. void GetSelectionForSingle();
  41. void TestSelectionFor0and1();
  42. // the dataview control itself
  43. wxDataViewTreeCtrl *m_dvc;
  44. // and some of its items
  45. wxDataViewItem m_root,
  46. m_child1,
  47. m_child2,
  48. m_grandchild;
  49. DECLARE_NO_COPY_CLASS(DataViewCtrlTestCase)
  50. };
  51. // register in the unnamed registry so that these tests are run by default
  52. CPPUNIT_TEST_SUITE_REGISTRATION( DataViewCtrlTestCase );
  53. // also include in its own registry so that these tests can be run alone
  54. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DataViewCtrlTestCase, "DataViewCtrlTestCase" );
  55. // ----------------------------------------------------------------------------
  56. // test initialization
  57. // ----------------------------------------------------------------------------
  58. void DataViewCtrlTestCase::Create(long style)
  59. {
  60. m_dvc = new wxDataViewTreeCtrl(wxTheApp->GetTopWindow(),
  61. wxID_ANY,
  62. wxDefaultPosition,
  63. wxSize(400, 200),
  64. style);
  65. m_root = m_dvc->AppendContainer(wxDataViewItem(), "The root");
  66. m_child1 = m_dvc->AppendContainer(m_root, "child1");
  67. m_grandchild = m_dvc->AppendItem(m_child1, "grandchild");
  68. m_child2 = m_dvc->AppendItem(m_root, "child2");
  69. m_dvc->SetSize(400, 200);
  70. m_dvc->ExpandAncestors(m_root);
  71. m_dvc->Refresh();
  72. m_dvc->Update();
  73. }
  74. void DataViewCtrlTestCase::setUp()
  75. {
  76. Create(wxDV_MULTIPLE);
  77. }
  78. void DataViewCtrlTestCase::tearDown()
  79. {
  80. delete m_dvc;
  81. m_dvc = NULL;
  82. m_root =
  83. m_child1 =
  84. m_child2 =
  85. m_grandchild = wxDataViewItem();
  86. }
  87. // ----------------------------------------------------------------------------
  88. // the tests themselves
  89. // ----------------------------------------------------------------------------
  90. void DataViewCtrlTestCase::DeleteSelected()
  91. {
  92. wxDataViewItemArray sel;
  93. sel.push_back(m_child1);
  94. sel.push_back(m_grandchild);
  95. sel.push_back(m_child2);
  96. m_dvc->SetSelections(sel);
  97. // delete a selected item
  98. m_dvc->DeleteItem(m_child1);
  99. m_dvc->GetSelections(sel);
  100. // m_child1 and its children should be removed from the selection now
  101. CPPUNIT_ASSERT_EQUAL( 1, sel.size() );
  102. CPPUNIT_ASSERT( sel[0] == m_child2 );
  103. }
  104. void DataViewCtrlTestCase::DeleteNotSelected()
  105. {
  106. // TODO not working on OS X as expected
  107. #ifndef __WXOSX__
  108. wxDataViewItemArray sel;
  109. sel.push_back(m_child1);
  110. sel.push_back(m_grandchild);
  111. m_dvc->SetSelections(sel);
  112. // delete unselected item
  113. m_dvc->DeleteItem(m_child2);
  114. m_dvc->GetSelections(sel);
  115. // m_child1 and its children should be unaffected
  116. CPPUNIT_ASSERT_EQUAL( 2, sel.size() );
  117. CPPUNIT_ASSERT( sel[0] == m_child1 );
  118. CPPUNIT_ASSERT( sel[1] == m_grandchild );
  119. #endif
  120. }
  121. void DataViewCtrlTestCase::TestSelectionFor0and1()
  122. {
  123. wxDataViewItemArray selections;
  124. // Initially there is no selection.
  125. CPPUNIT_ASSERT_EQUAL( 0, m_dvc->GetSelectedItemsCount() );
  126. CPPUNIT_ASSERT( !m_dvc->HasSelection() );
  127. CPPUNIT_ASSERT( !m_dvc->GetSelection().IsOk() );
  128. CPPUNIT_ASSERT( !m_dvc->GetSelections(selections) );
  129. CPPUNIT_ASSERT( selections.empty() );
  130. // Select one item.
  131. m_dvc->Select(m_child1);
  132. CPPUNIT_ASSERT_EQUAL( 1, m_dvc->GetSelectedItemsCount() );
  133. CPPUNIT_ASSERT( m_dvc->HasSelection() );
  134. CPPUNIT_ASSERT( m_dvc->GetSelection().IsOk() );
  135. CPPUNIT_ASSERT_EQUAL( 1, m_dvc->GetSelections(selections) );
  136. CPPUNIT_ASSERT( selections[0] == m_child1 );
  137. }
  138. void DataViewCtrlTestCase::GetSelectionForMulti()
  139. {
  140. wxDataViewItemArray selections;
  141. TestSelectionFor0and1();
  142. // Also test with more than one selected item.
  143. m_dvc->Select(m_child2);
  144. CPPUNIT_ASSERT_EQUAL( 2, m_dvc->GetSelectedItemsCount() );
  145. CPPUNIT_ASSERT( m_dvc->HasSelection() );
  146. CPPUNIT_ASSERT( !m_dvc->GetSelection().IsOk() );
  147. CPPUNIT_ASSERT_EQUAL( 2, m_dvc->GetSelections(selections) );
  148. CPPUNIT_ASSERT( selections[1] == m_child2 );
  149. }
  150. void DataViewCtrlTestCase::GetSelectionForSingle()
  151. {
  152. delete m_dvc;
  153. Create(0);
  154. TestSelectionFor0and1();
  155. }
  156. #endif //wxUSE_DATAVIEWCTRL