1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/checkboxtest.cpp
3 // Purpose: wCheckBox unit test
4 // Author: Steven Lamerton
7 // Copyright: (c) 2010 Steven Lamerton
8 ///////////////////////////////////////////////////////////////////////////////
20 #include "wx/checkbox.h"
23 #include "testableframe.h"
25 class CheckBoxTestCase
: public CppUnit::TestCase
28 CheckBoxTestCase() { }
34 CPPUNIT_TEST_SUITE( CheckBoxTestCase
);
35 CPPUNIT_TEST( Check
);
36 #ifdef wxHAS_3STATE_CHECKBOX
37 CPPUNIT_TEST( ThirdState
);
38 CPPUNIT_TEST( ThirdStateUser
);
39 CPPUNIT_TEST( InvalidStyles
);
40 #endif // wxHAS_3STATE_CHECKBOX
41 CPPUNIT_TEST_SUITE_END();
44 #ifdef wxHAS_3STATE_CHECKBOX
46 void ThirdStateUser();
48 #endif // wxHAS_3STATE_CHECKBOX
50 // Initialize m_check with a new checkbox with the specified style
52 // This function always returns false just to make it more convenient to
53 // use inside WX_ASSERT_FAILS_WITH_ASSERT(), its return value doesn't have
54 // any meaning otherwise.
55 bool CreateCheckBox(long style
)
57 m_check
= new wxCheckBox(wxTheApp
->GetTopWindow(), wxID_ANY
, "Check box",
58 wxDefaultPosition
, wxDefaultSize
, style
);
65 DECLARE_NO_COPY_CLASS(CheckBoxTestCase
)
68 // register in the unnamed registry so that these tests are run by default
69 CPPUNIT_TEST_SUITE_REGISTRATION( CheckBoxTestCase
);
71 // also include in its own registry so that these tests can be run alone
72 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CheckBoxTestCase
, "CheckBoxTestCase" );
74 void CheckBoxTestCase::setUp()
76 m_check
= new wxCheckBox(wxTheApp
->GetTopWindow(), wxID_ANY
, "Check box");
79 void CheckBoxTestCase::tearDown()
84 void CheckBoxTestCase::Check()
86 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
89 EventCounter
count(m_check
, wxEVT_COMMAND_CHECKBOX_CLICKED
);
91 //We should be unchecked by default
92 CPPUNIT_ASSERT(!m_check
->IsChecked());
94 m_check
->SetValue(true);
96 CPPUNIT_ASSERT(m_check
->IsChecked());
98 m_check
->SetValue(false);
100 CPPUNIT_ASSERT(!m_check
->IsChecked());
102 m_check
->Set3StateValue(wxCHK_CHECKED
);
104 CPPUNIT_ASSERT(m_check
->IsChecked());
106 m_check
->Set3StateValue(wxCHK_UNCHECKED
);
108 CPPUNIT_ASSERT(!m_check
->IsChecked());
110 //None of these should send events
111 CPPUNIT_ASSERT_EQUAL(0, frame
->GetEventCount());
114 #ifdef wxHAS_3STATE_CHECKBOX
115 void CheckBoxTestCase::ThirdState()
118 CreateCheckBox(wxCHK_3STATE
);
120 CPPUNIT_ASSERT_EQUAL(wxCHK_UNCHECKED
, m_check
->Get3StateValue());
121 CPPUNIT_ASSERT(m_check
->Is3State());
122 CPPUNIT_ASSERT(!m_check
->Is3rdStateAllowedForUser());
124 m_check
->SetValue(true);
126 CPPUNIT_ASSERT_EQUAL(wxCHK_CHECKED
, m_check
->Get3StateValue());
128 m_check
->Set3StateValue(wxCHK_UNDETERMINED
);
130 CPPUNIT_ASSERT_EQUAL(wxCHK_UNDETERMINED
, m_check
->Get3StateValue());
133 void CheckBoxTestCase::ThirdStateUser()
136 CreateCheckBox(wxCHK_3STATE
| wxCHK_ALLOW_3RD_STATE_FOR_USER
);
138 CPPUNIT_ASSERT_EQUAL(wxCHK_UNCHECKED
, m_check
->Get3StateValue());
139 CPPUNIT_ASSERT(m_check
->Is3State());
140 CPPUNIT_ASSERT(m_check
->Is3rdStateAllowedForUser());
142 m_check
->SetValue(true);
144 CPPUNIT_ASSERT_EQUAL(wxCHK_CHECKED
, m_check
->Get3StateValue());
146 m_check
->Set3StateValue(wxCHK_UNDETERMINED
);
148 CPPUNIT_ASSERT_EQUAL(wxCHK_UNDETERMINED
, m_check
->Get3StateValue());
151 void CheckBoxTestCase::InvalidStyles()
153 // Check that using incompatible styles doesn't work.
155 WX_ASSERT_FAILS_WITH_ASSERT( CreateCheckBox(wxCHK_2STATE
| wxCHK_3STATE
) );
157 CPPUNIT_ASSERT( !m_check
->Is3State() );
158 CPPUNIT_ASSERT( !m_check
->Is3rdStateAllowedForUser() );
162 WX_ASSERT_FAILS_WITH_ASSERT(
163 CreateCheckBox(wxCHK_2STATE
| wxCHK_ALLOW_3RD_STATE_FOR_USER
) );
165 CPPUNIT_ASSERT( !m_check
->Is3State() );
166 CPPUNIT_ASSERT( !m_check
->Is3rdStateAllowedForUser() );
169 // wxCHK_ALLOW_3RD_STATE_FOR_USER without wxCHK_3STATE doesn't work.
171 WX_ASSERT_FAILS_WITH_ASSERT( CreateCheckBox(wxCHK_ALLOW_3RD_STATE_FOR_USER
) );
174 #endif // wxHAS_3STATE_CHECKBOX
176 #endif // wxUSE_CHECKBOX