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