ownerdrawncomboboxtest.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/controls/ownerdrawncomboboxtest.cpp
  3. // Purpose: OwnerDrawnComboBox unit test
  4. // Author: Jaakko Salli
  5. // Created: 2010-12-17
  6. // Copyright: (c) 2010 Jaakko Salli
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #if wxUSE_ODCOMBOBOX
  13. #ifdef __BORLANDC__
  14. #pragma hdrstop
  15. #endif
  16. #ifndef WX_PRECOMP
  17. #include "wx/app.h"
  18. #endif // WX_PRECOMP
  19. #include "wx/odcombo.h"
  20. #include "textentrytest.h"
  21. #include "itemcontainertest.h"
  22. #include "testableframe.h"
  23. // ----------------------------------------------------------------------------
  24. // test class
  25. // ----------------------------------------------------------------------------
  26. class OwnerDrawnComboBoxTestCase : public TextEntryTestCase,
  27. public ItemContainerTestCase,
  28. public CppUnit::TestCase
  29. {
  30. public:
  31. OwnerDrawnComboBoxTestCase() { }
  32. virtual void setUp();
  33. virtual void tearDown();
  34. private:
  35. virtual wxTextEntry *GetTestEntry() const { return m_combo; }
  36. virtual wxWindow *GetTestWindow() const { return m_combo; }
  37. virtual wxItemContainer *GetContainer() const { return m_combo; }
  38. virtual wxWindow *GetContainerWindow() const { return m_combo; }
  39. virtual void CheckStringSelection(const char * WXUNUSED(sel))
  40. {
  41. // do nothing here, as explained in TextEntryTestCase comment, our
  42. // GetStringSelection() is the wxChoice, not wxTextEntry, one and there
  43. // is no way to return the selection contents directly
  44. }
  45. CPPUNIT_TEST_SUITE( OwnerDrawnComboBoxTestCase );
  46. wxTEXT_ENTRY_TESTS();
  47. wxITEM_CONTAINER_TESTS();
  48. CPPUNIT_TEST( Size );
  49. CPPUNIT_TEST( PopDismiss );
  50. CPPUNIT_TEST( Sort );
  51. CPPUNIT_TEST( ReadOnly );
  52. CPPUNIT_TEST_SUITE_END();
  53. void Size();
  54. void PopDismiss();
  55. void Sort();
  56. void ReadOnly();
  57. wxOwnerDrawnComboBox *m_combo;
  58. DECLARE_NO_COPY_CLASS(OwnerDrawnComboBoxTestCase)
  59. };
  60. // register in the unnamed registry so that these tests are run by default
  61. CPPUNIT_TEST_SUITE_REGISTRATION( OwnerDrawnComboBoxTestCase );
  62. // also include in its own registry so that these tests can be run alone
  63. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( OwnerDrawnComboBoxTestCase,
  64. "OwnerDrawnComboBoxTestCase" );
  65. // ----------------------------------------------------------------------------
  66. // test initialization
  67. // ----------------------------------------------------------------------------
  68. void OwnerDrawnComboBoxTestCase::setUp()
  69. {
  70. m_combo = new wxOwnerDrawnComboBox(wxTheApp->GetTopWindow(), wxID_ANY);
  71. }
  72. void OwnerDrawnComboBoxTestCase::tearDown()
  73. {
  74. delete m_combo;
  75. m_combo = NULL;
  76. }
  77. // ----------------------------------------------------------------------------
  78. // tests themselves
  79. // ----------------------------------------------------------------------------
  80. void OwnerDrawnComboBoxTestCase::Size()
  81. {
  82. // under MSW changing combobox size is a non-trivial operation because of
  83. // confusion between the size of the control with and without dropdown, so
  84. // check that it does work as expected
  85. const int heightOrig = m_combo->GetSize().y;
  86. // check that the height doesn't change if we don't touch it
  87. m_combo->SetSize(100, -1);
  88. CPPUNIT_ASSERT_EQUAL( heightOrig, m_combo->GetSize().y );
  89. // check that setting both big and small (but not too small, there is a
  90. // limit on how small the control can become under MSW) heights works
  91. m_combo->SetSize(-1, 50);
  92. CPPUNIT_ASSERT_EQUAL( 50, m_combo->GetSize().y );
  93. m_combo->SetSize(-1, 10);
  94. CPPUNIT_ASSERT_EQUAL( 10, m_combo->GetSize().y );
  95. // and also that restoring it works (this used to be broken before 2.9.1)
  96. m_combo->SetSize(-1, heightOrig);
  97. CPPUNIT_ASSERT_EQUAL( heightOrig, m_combo->GetSize().y );
  98. }
  99. void OwnerDrawnComboBoxTestCase::PopDismiss()
  100. {
  101. EventCounter drop(m_combo, wxEVT_COMBOBOX_DROPDOWN);
  102. EventCounter close(m_combo, wxEVT_COMBOBOX_CLOSEUP);
  103. m_combo->Popup();
  104. m_combo->Dismiss();
  105. CPPUNIT_ASSERT_EQUAL(1, drop.GetCount());
  106. CPPUNIT_ASSERT_EQUAL(1, close.GetCount());
  107. }
  108. void OwnerDrawnComboBoxTestCase::Sort()
  109. {
  110. delete m_combo;
  111. m_combo = new wxOwnerDrawnComboBox(wxTheApp->GetTopWindow(),
  112. wxID_ANY, "",
  113. wxDefaultPosition, wxDefaultSize,
  114. 0, NULL,
  115. wxCB_SORT);
  116. m_combo->Append("aaa");
  117. m_combo->Append("Aaa");
  118. m_combo->Append("aba");
  119. m_combo->Append("aaab");
  120. m_combo->Append("aab");
  121. m_combo->Append("AAA");
  122. CPPUNIT_ASSERT_EQUAL("AAA", m_combo->GetString(0));
  123. CPPUNIT_ASSERT_EQUAL("Aaa", m_combo->GetString(1));
  124. CPPUNIT_ASSERT_EQUAL("aaa", m_combo->GetString(2));
  125. CPPUNIT_ASSERT_EQUAL("aaab", m_combo->GetString(3));
  126. CPPUNIT_ASSERT_EQUAL("aab", m_combo->GetString(4));
  127. CPPUNIT_ASSERT_EQUAL("aba", m_combo->GetString(5));
  128. m_combo->Append("a");
  129. CPPUNIT_ASSERT_EQUAL("a", m_combo->GetString(0));
  130. }
  131. void OwnerDrawnComboBoxTestCase::ReadOnly()
  132. {
  133. wxArrayString testitems;
  134. testitems.Add("item 1");
  135. testitems.Add("item 2");
  136. delete m_combo;
  137. m_combo = new wxOwnerDrawnComboBox(wxTheApp->GetTopWindow(), wxID_ANY, "",
  138. wxDefaultPosition, wxDefaultSize,
  139. testitems,
  140. wxCB_READONLY);
  141. m_combo->SetValue("item 1");
  142. CPPUNIT_ASSERT_EQUAL("item 1", m_combo->GetValue());
  143. m_combo->SetValue("not an item");
  144. CPPUNIT_ASSERT_EQUAL("item 1", m_combo->GetValue());
  145. // Since this uses FindString it is case insensitive
  146. m_combo->SetValue("ITEM 2");
  147. CPPUNIT_ASSERT_EQUAL("item 2", m_combo->GetValue());
  148. }
  149. #endif // wxUSE_ODCOMBOBOX