]> git.saurik.com Git - wxWidgets.git/blame - tests/controls/comboboxtest.cpp
Correct the initial value setting in wxMSW wxSpinCtrl.
[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 );
59 wxTEXT_ENTRY_TESTS();
232fdc63 60 wxITEM_CONTAINER_TESTS();
0772a898 61 CPPUNIT_TEST( Size );
232fdc63
VZ
62 CPPUNIT_TEST( PopDismiss );
63 CPPUNIT_TEST( Sort );
64 CPPUNIT_TEST( ReadOnly );
36a96421 65 CPPUNIT_TEST( IsEmpty );
dc7f9c9c
VZ
66 CPPUNIT_TEST_SUITE_END();
67
0772a898 68 void Size();
232fdc63
VZ
69 void PopDismiss();
70 void Sort();
71 void ReadOnly();
36a96421 72 void IsEmpty();
0772a898 73
dc7f9c9c
VZ
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
80CPPUNIT_TEST_SUITE_REGISTRATION( ComboBoxTestCase );
81
e3778b4d 82// also include in its own registry so that these tests can be run alone
dc7f9c9c
VZ
83CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ComboBoxTestCase, "ComboBoxTestCase" );
84
85// ----------------------------------------------------------------------------
86// test initialization
87// ----------------------------------------------------------------------------
88
89void ComboBoxTestCase::setUp()
90{
91 m_combo = new wxComboBox(wxTheApp->GetTopWindow(), wxID_ANY);
92}
93
94void ComboBoxTestCase::tearDown()
95{
96 delete m_combo;
97 m_combo = NULL;
98}
99
100// ----------------------------------------------------------------------------
101// tests themselves
102// ----------------------------------------------------------------------------
103
0772a898
VZ
104void 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
232fdc63
VZ
129void ComboBoxTestCase::PopDismiss()
130{
131#if defined(__WXMSW__) || defined(__WXGTK210__)
744d91d4
SL
132 EventCounter drop(m_combo, wxEVT_COMMAND_COMBOBOX_DROPDOWN);
133 EventCounter close(m_combo, wxEVT_COMMAND_COMBOBOX_CLOSEUP);
232fdc63
VZ
134
135 m_combo->Popup();
136 m_combo->Dismiss();
137
744d91d4
SL
138 CPPUNIT_ASSERT_EQUAL(1, drop.GetCount());
139 CPPUNIT_ASSERT_EQUAL(1, close.GetCount());
232fdc63
VZ
140#endif
141}
142
143void ComboBoxTestCase::Sort()
144{
a6856aa8 145#if !defined(__WXOSX__)
232fdc63
VZ
146 m_combo = new wxComboBox(wxTheApp->GetTopWindow(), wxID_ANY, "",
147 wxDefaultPosition, wxDefaultSize, 0, NULL,
148 wxCB_SORT);
149
150 m_combo->Append("aaa");
151 m_combo->Append("Aaa");
152 m_combo->Append("aba");
153 m_combo->Append("aaab");
154 m_combo->Append("aab");
155 m_combo->Append("AAA");
156
157 CPPUNIT_ASSERT_EQUAL("AAA", m_combo->GetString(0));
158 CPPUNIT_ASSERT_EQUAL("Aaa", m_combo->GetString(1));
159 CPPUNIT_ASSERT_EQUAL("aaa", m_combo->GetString(2));
160 CPPUNIT_ASSERT_EQUAL("aaab", m_combo->GetString(3));
161 CPPUNIT_ASSERT_EQUAL("aab", m_combo->GetString(4));
162 CPPUNIT_ASSERT_EQUAL("aba", m_combo->GetString(5));
163
164 m_combo->Append("a");
165
166 CPPUNIT_ASSERT_EQUAL("a", m_combo->GetString(0));
167#endif
168}
169
170void ComboBoxTestCase::ReadOnly()
171{
172#ifndef __WXOSX__
173 wxArrayString testitems;
174 testitems.Add("item 1");
175 testitems.Add("item 2");
176
177 m_combo = new wxComboBox(wxTheApp->GetTopWindow(), wxID_ANY, "",
178 wxDefaultPosition, wxDefaultSize, testitems,
179 wxCB_READONLY);
180
181 m_combo->SetValue("item 1");
182
183 CPPUNIT_ASSERT_EQUAL("item 1", m_combo->GetValue());
184
185 m_combo->SetValue("not an item");
186
187 CPPUNIT_ASSERT_EQUAL("item 1", m_combo->GetValue());
188
189 // Since this uses FindString it is case insensitive
190 m_combo->SetValue("ITEM 2");
191
192 CPPUNIT_ASSERT_EQUAL("item 2", m_combo->GetValue());
193#endif
194}
195
36a96421
VZ
196void ComboBoxTestCase::IsEmpty()
197{
198 CPPUNIT_ASSERT( m_combo->IsListEmpty() );
199 CPPUNIT_ASSERT( m_combo->IsTextEmpty() );
200
201 m_combo->Append("foo");
202 CPPUNIT_ASSERT( !m_combo->IsListEmpty() );
203 CPPUNIT_ASSERT( m_combo->IsTextEmpty() );
204
205 m_combo->SetValue("bar");
206 CPPUNIT_ASSERT( !m_combo->IsListEmpty() );
207 CPPUNIT_ASSERT( !m_combo->IsTextEmpty() );
208
209 m_combo->Clear();
210 CPPUNIT_ASSERT( m_combo->IsListEmpty() );
211 CPPUNIT_ASSERT( m_combo->IsTextEmpty() );
212
213#ifdef TEST_INVALID_COMBOBOX_ISEMPTY
214 // Compiling this should fail, see failtest target definition in test.bkl.
215 m_combo->IsEmpty();
216#endif
217}
218
232fdc63 219#endif //wxUSE_COMBOBOX