comboboxtest.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/controls/comboboxtest.cpp
  3. // Purpose: wxComboBox unit test
  4. // Author: Vadim Zeitlin
  5. // Created: 2007-09-25
  6. // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org>
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #if wxUSE_COMBOBOX
  13. #ifdef __BORLANDC__
  14. #pragma hdrstop
  15. #endif
  16. #ifndef WX_PRECOMP
  17. #include "wx/app.h"
  18. #include "wx/combobox.h"
  19. #endif // WX_PRECOMP
  20. #include "textentrytest.h"
  21. #include "itemcontainertest.h"
  22. #include "testableframe.h"
  23. // ----------------------------------------------------------------------------
  24. // test class
  25. // ----------------------------------------------------------------------------
  26. class ComboBoxTestCase : public TextEntryTestCase, public ItemContainerTestCase,
  27. public CppUnit::TestCase
  28. {
  29. public:
  30. ComboBoxTestCase() { }
  31. virtual void setUp();
  32. virtual void tearDown();
  33. private:
  34. virtual wxTextEntry *GetTestEntry() const { return m_combo; }
  35. virtual wxWindow *GetTestWindow() const { return m_combo; }
  36. virtual wxItemContainer *GetContainer() const { return m_combo; }
  37. virtual wxWindow *GetContainerWindow() const { return m_combo; }
  38. virtual void CheckStringSelection(const char * WXUNUSED(sel))
  39. {
  40. // do nothing here, as explained in TextEntryTestCase comment, our
  41. // GetStringSelection() is the wxChoice, not wxTextEntry, one and there
  42. // is no way to return the selection contents directly
  43. }
  44. CPPUNIT_TEST_SUITE( ComboBoxTestCase );
  45. #ifdef __WXOSX__
  46. CPPUNIT_TEST( SetValue );
  47. CPPUNIT_TEST( TextChangeEvents );
  48. CPPUNIT_TEST( Selection );
  49. CPPUNIT_TEST( InsertionPoint );
  50. CPPUNIT_TEST( Replace );
  51. // TODO on OS X only works interactively
  52. // WXUISIM_TEST( Editable );
  53. CPPUNIT_TEST( Hint );
  54. CPPUNIT_TEST( CopyPaste );
  55. CPPUNIT_TEST( UndoRedo );
  56. #else
  57. wxTEXT_ENTRY_TESTS();
  58. #endif
  59. wxITEM_CONTAINER_TESTS();
  60. CPPUNIT_TEST( Size );
  61. CPPUNIT_TEST( PopDismiss );
  62. CPPUNIT_TEST( Sort );
  63. CPPUNIT_TEST( ReadOnly );
  64. CPPUNIT_TEST( IsEmpty );
  65. CPPUNIT_TEST_SUITE_END();
  66. void Size();
  67. void PopDismiss();
  68. void Sort();
  69. void ReadOnly();
  70. void IsEmpty();
  71. wxComboBox *m_combo;
  72. DECLARE_NO_COPY_CLASS(ComboBoxTestCase)
  73. };
  74. // register in the unnamed registry so that these tests are run by default
  75. CPPUNIT_TEST_SUITE_REGISTRATION( ComboBoxTestCase );
  76. // also include in its own registry so that these tests can be run alone
  77. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ComboBoxTestCase, "ComboBoxTestCase" );
  78. // ----------------------------------------------------------------------------
  79. // test initialization
  80. // ----------------------------------------------------------------------------
  81. void ComboBoxTestCase::setUp()
  82. {
  83. m_combo = new wxComboBox(wxTheApp->GetTopWindow(), wxID_ANY);
  84. }
  85. void ComboBoxTestCase::tearDown()
  86. {
  87. delete m_combo;
  88. m_combo = NULL;
  89. }
  90. // ----------------------------------------------------------------------------
  91. // tests themselves
  92. // ----------------------------------------------------------------------------
  93. void ComboBoxTestCase::Size()
  94. {
  95. // under MSW changing combobox size is a non-trivial operation because of
  96. // confusion between the size of the control with and without dropdown, so
  97. // check that it does work as expected
  98. const int heightOrig = m_combo->GetSize().y;
  99. // check that the height doesn't change if we don't touch it
  100. m_combo->SetSize(100, -1);
  101. CPPUNIT_ASSERT_EQUAL( heightOrig, m_combo->GetSize().y );
  102. // check that setting both big and small (but not too small, there is a
  103. // limit on how small the control can become under MSW) heights works
  104. m_combo->SetSize(-1, 50);
  105. CPPUNIT_ASSERT_EQUAL( 50, m_combo->GetSize().y );
  106. m_combo->SetSize(-1, 10);
  107. CPPUNIT_ASSERT_EQUAL( 10, m_combo->GetSize().y );
  108. // and also that restoring it works (this used to be broken before 2.9.1)
  109. m_combo->SetSize(-1, heightOrig);
  110. CPPUNIT_ASSERT_EQUAL( heightOrig, m_combo->GetSize().y );
  111. }
  112. void ComboBoxTestCase::PopDismiss()
  113. {
  114. #if defined(__WXMSW__) || defined(__WXGTK210__)
  115. EventCounter drop(m_combo, wxEVT_COMBOBOX_DROPDOWN);
  116. EventCounter close(m_combo, wxEVT_COMBOBOX_CLOSEUP);
  117. m_combo->Popup();
  118. m_combo->Dismiss();
  119. CPPUNIT_ASSERT_EQUAL(1, drop.GetCount());
  120. CPPUNIT_ASSERT_EQUAL(1, close.GetCount());
  121. #endif
  122. }
  123. void ComboBoxTestCase::Sort()
  124. {
  125. #if !defined(__WXOSX__)
  126. delete m_combo;
  127. m_combo = new wxComboBox(wxTheApp->GetTopWindow(), wxID_ANY, "",
  128. wxDefaultPosition, wxDefaultSize, 0, NULL,
  129. wxCB_SORT);
  130. m_combo->Append("aaa");
  131. m_combo->Append("Aaa");
  132. m_combo->Append("aba");
  133. m_combo->Append("aaab");
  134. m_combo->Append("aab");
  135. m_combo->Append("AAA");
  136. CPPUNIT_ASSERT_EQUAL("AAA", m_combo->GetString(0));
  137. CPPUNIT_ASSERT_EQUAL("Aaa", m_combo->GetString(1));
  138. CPPUNIT_ASSERT_EQUAL("aaa", m_combo->GetString(2));
  139. CPPUNIT_ASSERT_EQUAL("aaab", m_combo->GetString(3));
  140. CPPUNIT_ASSERT_EQUAL("aab", m_combo->GetString(4));
  141. CPPUNIT_ASSERT_EQUAL("aba", m_combo->GetString(5));
  142. m_combo->Append("a");
  143. CPPUNIT_ASSERT_EQUAL("a", m_combo->GetString(0));
  144. #endif
  145. }
  146. void ComboBoxTestCase::ReadOnly()
  147. {
  148. wxArrayString testitems;
  149. testitems.Add("item 1");
  150. testitems.Add("item 2");
  151. delete m_combo;
  152. m_combo = new wxComboBox(wxTheApp->GetTopWindow(), wxID_ANY, "",
  153. wxDefaultPosition, wxDefaultSize, testitems,
  154. wxCB_READONLY);
  155. m_combo->SetValue("item 1");
  156. CPPUNIT_ASSERT_EQUAL("item 1", m_combo->GetValue());
  157. m_combo->SetValue("not an item");
  158. CPPUNIT_ASSERT_EQUAL("item 1", m_combo->GetValue());
  159. // Since this uses FindString it is case insensitive
  160. m_combo->SetValue("ITEM 2");
  161. CPPUNIT_ASSERT_EQUAL("item 2", m_combo->GetValue());
  162. }
  163. void ComboBoxTestCase::IsEmpty()
  164. {
  165. CPPUNIT_ASSERT( m_combo->IsListEmpty() );
  166. CPPUNIT_ASSERT( m_combo->IsTextEmpty() );
  167. m_combo->Append("foo");
  168. CPPUNIT_ASSERT( !m_combo->IsListEmpty() );
  169. CPPUNIT_ASSERT( m_combo->IsTextEmpty() );
  170. m_combo->SetValue("bar");
  171. CPPUNIT_ASSERT( !m_combo->IsListEmpty() );
  172. CPPUNIT_ASSERT( !m_combo->IsTextEmpty() );
  173. m_combo->Clear();
  174. CPPUNIT_ASSERT( m_combo->IsListEmpty() );
  175. CPPUNIT_ASSERT( m_combo->IsTextEmpty() );
  176. #ifdef TEST_INVALID_COMBOBOX_ISEMPTY
  177. // Compiling this should fail, see failtest target definition in test.bkl.
  178. m_combo->IsEmpty();
  179. #endif
  180. }
  181. #endif //wxUSE_COMBOBOX