1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        tests/controls/ownerdrawncomboboxtest.cpp
3 // Purpose:     OwnerDrawnComboBox unit test
4 // Author:      Jaakko Salli
5 // Created:     2010-12-17
6 // Copyright:   (c) 2010 Jaakko Salli
7 ///////////////////////////////////////////////////////////////////////////////
8 
9 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12 
13 #include "testprec.h"
14 
15 #if wxUSE_ODCOMBOBOX
16 
17 #ifdef __BORLANDC__
18     #pragma hdrstop
19 #endif
20 
21 #ifndef WX_PRECOMP
22     #include "wx/app.h"
23 #endif // WX_PRECOMP
24 
25 #include "wx/odcombo.h"
26 
27 #include "textentrytest.h"
28 #include "itemcontainertest.h"
29 #include "testableframe.h"
30 
31 // ----------------------------------------------------------------------------
32 // test class
33 // ----------------------------------------------------------------------------
34 
35 class OwnerDrawnComboBoxTestCase : public TextEntryTestCase,
36                                    public ItemContainerTestCase,
37                                    public CppUnit::TestCase
38 {
39 public:
OwnerDrawnComboBoxTestCase()40     OwnerDrawnComboBoxTestCase() { }
41 
42     virtual void setUp();
43     virtual void tearDown();
44 
45 private:
GetTestEntry() const46     virtual wxTextEntry *GetTestEntry() const { return m_combo; }
GetTestWindow() const47     virtual wxWindow *GetTestWindow() const { return m_combo; }
48 
GetContainer() const49     virtual wxItemContainer *GetContainer() const { return m_combo; }
GetContainerWindow() const50     virtual wxWindow *GetContainerWindow() const { return m_combo; }
51 
CheckStringSelection(const char * WXUNUSED (sel))52     virtual void CheckStringSelection(const char * WXUNUSED(sel))
53     {
54         // do nothing here, as explained in TextEntryTestCase comment, our
55         // GetStringSelection() is the wxChoice, not wxTextEntry, one and there
56         // is no way to return the selection contents directly
57     }
58 
59     CPPUNIT_TEST_SUITE( OwnerDrawnComboBoxTestCase );
60         wxTEXT_ENTRY_TESTS();
61         wxITEM_CONTAINER_TESTS();
62         CPPUNIT_TEST( Size );
63         CPPUNIT_TEST( PopDismiss );
64         CPPUNIT_TEST( Sort );
65         CPPUNIT_TEST( ReadOnly );
66     CPPUNIT_TEST_SUITE_END();
67 
68     void Size();
69     void PopDismiss();
70     void Sort();
71     void ReadOnly();
72 
73     wxOwnerDrawnComboBox *m_combo;
74 
75     DECLARE_NO_COPY_CLASS(OwnerDrawnComboBoxTestCase)
76 };
77 
78 // register in the unnamed registry so that these tests are run by default
79 CPPUNIT_TEST_SUITE_REGISTRATION( OwnerDrawnComboBoxTestCase );
80 
81 // also include in its own registry so that these tests can be run alone
82 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( OwnerDrawnComboBoxTestCase,
83                                        "OwnerDrawnComboBoxTestCase" );
84 
85 // ----------------------------------------------------------------------------
86 // test initialization
87 // ----------------------------------------------------------------------------
88 
setUp()89 void OwnerDrawnComboBoxTestCase::setUp()
90 {
91     m_combo = new wxOwnerDrawnComboBox(wxTheApp->GetTopWindow(), wxID_ANY);
92 }
93 
tearDown()94 void OwnerDrawnComboBoxTestCase::tearDown()
95 {
96     delete m_combo;
97     m_combo = NULL;
98 }
99 
100 // ----------------------------------------------------------------------------
101 // tests themselves
102 // ----------------------------------------------------------------------------
103 
Size()104 void OwnerDrawnComboBoxTestCase::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 
PopDismiss()129 void OwnerDrawnComboBoxTestCase::PopDismiss()
130 {
131     EventCounter drop(m_combo, wxEVT_COMBOBOX_DROPDOWN);
132     EventCounter close(m_combo, wxEVT_COMBOBOX_CLOSEUP);
133 
134     m_combo->Popup();
135     m_combo->Dismiss();
136 
137     CPPUNIT_ASSERT_EQUAL(1, drop.GetCount());
138     CPPUNIT_ASSERT_EQUAL(1, close.GetCount());
139 }
140 
Sort()141 void OwnerDrawnComboBoxTestCase::Sort()
142 {
143     delete m_combo;
144     m_combo = new wxOwnerDrawnComboBox(wxTheApp->GetTopWindow(),
145                                        wxID_ANY, "",
146                                        wxDefaultPosition, wxDefaultSize,
147                                        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 }
168 
ReadOnly()169 void OwnerDrawnComboBoxTestCase::ReadOnly()
170 {
171     wxArrayString testitems;
172     testitems.Add("item 1");
173     testitems.Add("item 2");
174 
175     delete m_combo;
176     m_combo = new wxOwnerDrawnComboBox(wxTheApp->GetTopWindow(), wxID_ANY, "",
177                                        wxDefaultPosition, wxDefaultSize,
178                                        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 }
194 
195 #endif // wxUSE_ODCOMBOBOX
196