1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/comboboxtest.cpp
3 // Purpose: wxComboBox unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 #include "wx/combobox.h"
27 #include "textentrytest.h"
28 #include "itemcontainertest.h"
29 #include "testableframe.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 class ComboBoxTestCase
: public TextEntryTestCase
, public ItemContainerTestCase
,
36 public CppUnit::TestCase
39 ComboBoxTestCase() { }
42 virtual void tearDown();
45 virtual wxTextEntry
*GetTestEntry() const { return m_combo
; }
46 virtual wxWindow
*GetTestWindow() const { return m_combo
; }
48 virtual wxItemContainer
*GetContainer() const { return m_combo
; }
49 virtual wxWindow
*GetContainerWindow() const { return m_combo
; }
51 virtual void CheckStringSelection(const char * WXUNUSED(sel
))
53 // do nothing here, as explained in TextEntryTestCase comment, our
54 // GetStringSelection() is the wxChoice, not wxTextEntry, one and there
55 // is no way to return the selection contents directly
58 CPPUNIT_TEST_SUITE( ComboBoxTestCase
);
60 wxITEM_CONTAINER_TESTS();
62 CPPUNIT_TEST( PopDismiss
);
64 CPPUNIT_TEST( ReadOnly
);
65 CPPUNIT_TEST_SUITE_END();
74 DECLARE_NO_COPY_CLASS(ComboBoxTestCase
)
77 // register in the unnamed registry so that these tests are run by default
78 CPPUNIT_TEST_SUITE_REGISTRATION( ComboBoxTestCase
);
80 // also include in it's own registry so that these tests can be run alone
81 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ComboBoxTestCase
, "ComboBoxTestCase" );
83 // ----------------------------------------------------------------------------
84 // test initialization
85 // ----------------------------------------------------------------------------
87 void ComboBoxTestCase::setUp()
89 m_combo
= new wxComboBox(wxTheApp
->GetTopWindow(), wxID_ANY
);
92 void ComboBoxTestCase::tearDown()
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 void ComboBoxTestCase::Size()
104 // under MSW changing combobox size is a non-trivial operation because of
105 // confusion between the size of the control with and without dropdown, so
106 // check that it does work as expected
108 const int heightOrig
= m_combo
->GetSize().y
;
110 // check that the height doesn't change if we don't touch it
111 m_combo
->SetSize(100, -1);
112 CPPUNIT_ASSERT_EQUAL( heightOrig
, m_combo
->GetSize().y
);
114 // check that setting both big and small (but not too small, there is a
115 // limit on how small the control can become under MSW) heights works
116 m_combo
->SetSize(-1, 50);
117 CPPUNIT_ASSERT_EQUAL( 50, m_combo
->GetSize().y
);
119 m_combo
->SetSize(-1, 10);
120 CPPUNIT_ASSERT_EQUAL( 10, m_combo
->GetSize().y
);
122 // and also that restoring it works (this used to be broken before 2.9.1)
123 m_combo
->SetSize(-1, heightOrig
);
124 CPPUNIT_ASSERT_EQUAL( heightOrig
, m_combo
->GetSize().y
);
127 void ComboBoxTestCase::PopDismiss()
129 #if defined(__WXMSW__) || defined(__WXGTK210__)
130 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
133 EventCounter
count(m_combo
, wxEVT_COMMAND_COMBOBOX_DROPDOWN
);
134 EventCounter
count1(m_combo
, wxEVT_COMMAND_COMBOBOX_CLOSEUP
);
139 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_COMMAND_COMBOBOX_DROPDOWN
));
140 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_COMMAND_COMBOBOX_CLOSEUP
));
144 void ComboBoxTestCase::Sort()
146 #if !defined(__WXOSX__)
147 m_combo
= new wxComboBox(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
148 wxDefaultPosition
, wxDefaultSize
, 0, NULL
,
151 m_combo
->Append("aaa");
152 m_combo
->Append("Aaa");
153 m_combo
->Append("aba");
154 m_combo
->Append("aaab");
155 m_combo
->Append("aab");
156 m_combo
->Append("AAA");
158 CPPUNIT_ASSERT_EQUAL("AAA", m_combo
->GetString(0));
159 CPPUNIT_ASSERT_EQUAL("Aaa", m_combo
->GetString(1));
160 CPPUNIT_ASSERT_EQUAL("aaa", m_combo
->GetString(2));
161 CPPUNIT_ASSERT_EQUAL("aaab", m_combo
->GetString(3));
162 CPPUNIT_ASSERT_EQUAL("aab", m_combo
->GetString(4));
163 CPPUNIT_ASSERT_EQUAL("aba", m_combo
->GetString(5));
165 m_combo
->Append("a");
167 CPPUNIT_ASSERT_EQUAL("a", m_combo
->GetString(0));
171 void ComboBoxTestCase::ReadOnly()
174 wxArrayString testitems
;
175 testitems
.Add("item 1");
176 testitems
.Add("item 2");
178 m_combo
= new wxComboBox(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
179 wxDefaultPosition
, wxDefaultSize
, testitems
,
182 m_combo
->SetValue("item 1");
184 CPPUNIT_ASSERT_EQUAL("item 1", m_combo
->GetValue());
186 m_combo
->SetValue("not an item");
188 CPPUNIT_ASSERT_EQUAL("item 1", m_combo
->GetValue());
190 // Since this uses FindString it is case insensitive
191 m_combo
->SetValue("ITEM 2");
193 CPPUNIT_ASSERT_EQUAL("item 2", m_combo
->GetValue());
197 #endif //wxUSE_COMBOBOX