Merge in from trunk r68684 - r69046
[wxWidgets.git] / tests / controls / comboboxtest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/comboboxtest.cpp
3 // Purpose: wxComboBox unit test
4 // Author: Vadim Zeitlin
5 // Created: 2007-09-25
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #if wxUSE_COMBOBOX
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #include "wx/app.h"
24 #include "wx/combobox.h"
25 #endif // WX_PRECOMP
26
27 #include "textentrytest.h"
28 #include "itemcontainertest.h"
29 #include "testableframe.h"
30
31 // ----------------------------------------------------------------------------
32 // test class
33 // ----------------------------------------------------------------------------
34
35 class ComboBoxTestCase : public TextEntryTestCase, public ItemContainerTestCase,
36 public CppUnit::TestCase
37 {
38 public:
39 ComboBoxTestCase() { }
40
41 virtual void setUp();
42 virtual void tearDown();
43
44 private:
45 virtual wxTextEntry *GetTestEntry() const { return m_combo; }
46 virtual wxWindow *GetTestWindow() const { return m_combo; }
47
48 virtual wxItemContainer *GetContainer() const { return m_combo; }
49 virtual wxWindow *GetContainerWindow() const { return m_combo; }
50
51 virtual void CheckStringSelection(const char * WXUNUSED(sel))
52 {
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
56 }
57
58 CPPUNIT_TEST_SUITE( ComboBoxTestCase );
59 wxTEXT_ENTRY_TESTS();
60 wxITEM_CONTAINER_TESTS();
61 CPPUNIT_TEST( Size );
62 CPPUNIT_TEST( PopDismiss );
63 CPPUNIT_TEST( Sort );
64 CPPUNIT_TEST( ReadOnly );
65 CPPUNIT_TEST( IsEmpty );
66 CPPUNIT_TEST_SUITE_END();
67
68 void Size();
69 void PopDismiss();
70 void Sort();
71 void ReadOnly();
72 void IsEmpty();
73
74 wxComboBox *m_combo;
75
76 DECLARE_NO_COPY_CLASS(ComboBoxTestCase)
77 };
78
79 // register in the unnamed registry so that these tests are run by default
80 CPPUNIT_TEST_SUITE_REGISTRATION( ComboBoxTestCase );
81
82 // also include in its own registry so that these tests can be run alone
83 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ComboBoxTestCase, "ComboBoxTestCase" );
84
85 // ----------------------------------------------------------------------------
86 // test initialization
87 // ----------------------------------------------------------------------------
88
89 void ComboBoxTestCase::setUp()
90 {
91 m_combo = new wxComboBox(wxTheApp->GetTopWindow(), wxID_ANY);
92 }
93
94 void ComboBoxTestCase::tearDown()
95 {
96 delete m_combo;
97 m_combo = NULL;
98 }
99
100 // ----------------------------------------------------------------------------
101 // tests themselves
102 // ----------------------------------------------------------------------------
103
104 void ComboBoxTestCase::Size()
105 {
106 // under MSW changing combobox size is a non-trivial operation because of
107 // confusion between the size of the control with and without dropdown, so
108 // check that it does work as expected
109
110 const int heightOrig = m_combo->GetSize().y;
111
112 // check that the height doesn't change if we don't touch it
113 m_combo->SetSize(100, -1);
114 CPPUNIT_ASSERT_EQUAL( heightOrig, m_combo->GetSize().y );
115
116 // check that setting both big and small (but not too small, there is a
117 // limit on how small the control can become under MSW) heights works
118 m_combo->SetSize(-1, 50);
119 CPPUNIT_ASSERT_EQUAL( 50, m_combo->GetSize().y );
120
121 m_combo->SetSize(-1, 10);
122 CPPUNIT_ASSERT_EQUAL( 10, m_combo->GetSize().y );
123
124 // and also that restoring it works (this used to be broken before 2.9.1)
125 m_combo->SetSize(-1, heightOrig);
126 CPPUNIT_ASSERT_EQUAL( heightOrig, m_combo->GetSize().y );
127 }
128
129 void ComboBoxTestCase::PopDismiss()
130 {
131 #if defined(__WXMSW__) || defined(__WXGTK210__)
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 #endif
144 }
145
146 void ComboBoxTestCase::Sort()
147 {
148 #if !defined(__WXOSX__)
149 m_combo = new wxComboBox(wxTheApp->GetTopWindow(), wxID_ANY, "",
150 wxDefaultPosition, wxDefaultSize, 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 #endif
171 }
172
173 void ComboBoxTestCase::ReadOnly()
174 {
175 #ifndef __WXOSX__
176 wxArrayString testitems;
177 testitems.Add("item 1");
178 testitems.Add("item 2");
179
180 m_combo = new wxComboBox(wxTheApp->GetTopWindow(), wxID_ANY, "",
181 wxDefaultPosition, wxDefaultSize, testitems,
182 wxCB_READONLY);
183
184 m_combo->SetValue("item 1");
185
186 CPPUNIT_ASSERT_EQUAL("item 1", m_combo->GetValue());
187
188 m_combo->SetValue("not an item");
189
190 CPPUNIT_ASSERT_EQUAL("item 1", m_combo->GetValue());
191
192 // Since this uses FindString it is case insensitive
193 m_combo->SetValue("ITEM 2");
194
195 CPPUNIT_ASSERT_EQUAL("item 2", m_combo->GetValue());
196 #endif
197 }
198
199 void ComboBoxTestCase::IsEmpty()
200 {
201 CPPUNIT_ASSERT( m_combo->IsListEmpty() );
202 CPPUNIT_ASSERT( m_combo->IsTextEmpty() );
203
204 m_combo->Append("foo");
205 CPPUNIT_ASSERT( !m_combo->IsListEmpty() );
206 CPPUNIT_ASSERT( m_combo->IsTextEmpty() );
207
208 m_combo->SetValue("bar");
209 CPPUNIT_ASSERT( !m_combo->IsListEmpty() );
210 CPPUNIT_ASSERT( !m_combo->IsTextEmpty() );
211
212 m_combo->Clear();
213 CPPUNIT_ASSERT( m_combo->IsListEmpty() );
214 CPPUNIT_ASSERT( m_combo->IsTextEmpty() );
215
216 #ifdef TEST_INVALID_COMBOBOX_ISEMPTY
217 // Compiling this should fail, see failtest target definition in test.bkl.
218 m_combo->IsEmpty();
219 #endif
220 }
221
222 #endif //wxUSE_COMBOBOX