]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/comboboxtest.cpp
Add new wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED event. Implement for all backends, exten...
[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_SUITE_END();
66
67 void Size();
68 void PopDismiss();
69 void Sort();
70 void ReadOnly();
71
72 wxComboBox *m_combo;
73
74 DECLARE_NO_COPY_CLASS(ComboBoxTestCase)
75 };
76
77 // register in the unnamed registry so that these tests are run by default
78 CPPUNIT_TEST_SUITE_REGISTRATION( ComboBoxTestCase );
79
80 // also include in its own registry so that these tests can be run alone
81 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ComboBoxTestCase, "ComboBoxTestCase" );
82
83 // ----------------------------------------------------------------------------
84 // test initialization
85 // ----------------------------------------------------------------------------
86
87 void ComboBoxTestCase::setUp()
88 {
89 m_combo = new wxComboBox(wxTheApp->GetTopWindow(), wxID_ANY);
90 }
91
92 void ComboBoxTestCase::tearDown()
93 {
94 delete m_combo;
95 m_combo = NULL;
96 }
97
98 // ----------------------------------------------------------------------------
99 // tests themselves
100 // ----------------------------------------------------------------------------
101
102 void ComboBoxTestCase::Size()
103 {
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
107
108 const int heightOrig = m_combo->GetSize().y;
109
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 );
113
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 );
118
119 m_combo->SetSize(-1, 10);
120 CPPUNIT_ASSERT_EQUAL( 10, m_combo->GetSize().y );
121
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 );
125 }
126
127 void ComboBoxTestCase::PopDismiss()
128 {
129 #if defined(__WXMSW__) || defined(__WXGTK210__)
130 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
131 wxTestableFrame);
132
133 EventCounter count(m_combo, wxEVT_COMMAND_COMBOBOX_DROPDOWN);
134 EventCounter count1(m_combo, wxEVT_COMMAND_COMBOBOX_CLOSEUP);
135
136 m_combo->Popup();
137 m_combo->Dismiss();
138
139 CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_COMBOBOX_DROPDOWN));
140 CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_COMBOBOX_CLOSEUP));
141 #endif
142 }
143
144 void ComboBoxTestCase::Sort()
145 {
146 #if !defined(__WXOSX__)
147 m_combo = new wxComboBox(wxTheApp->GetTopWindow(), wxID_ANY, "",
148 wxDefaultPosition, wxDefaultSize, 0, NULL,
149 wxCB_SORT);
150
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");
157
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));
164
165 m_combo->Append("a");
166
167 CPPUNIT_ASSERT_EQUAL("a", m_combo->GetString(0));
168 #endif
169 }
170
171 void ComboBoxTestCase::ReadOnly()
172 {
173 #ifndef __WXOSX__
174 wxArrayString testitems;
175 testitems.Add("item 1");
176 testitems.Add("item 2");
177
178 m_combo = new wxComboBox(wxTheApp->GetTopWindow(), wxID_ANY, "",
179 wxDefaultPosition, wxDefaultSize, testitems,
180 wxCB_READONLY);
181
182 m_combo->SetValue("item 1");
183
184 CPPUNIT_ASSERT_EQUAL("item 1", m_combo->GetValue());
185
186 m_combo->SetValue("not an item");
187
188 CPPUNIT_ASSERT_EQUAL("item 1", m_combo->GetValue());
189
190 // Since this uses FindString it is case insensitive
191 m_combo->SetValue("ITEM 2");
192
193 CPPUNIT_ASSERT_EQUAL("item 2", m_combo->GetValue());
194 #endif
195 }
196
197 #endif //wxUSE_COMBOBOX