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