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