1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/checkboxtest.cpp
3 // Purpose: wCheckBox unit test
4 // Author: Steven Lamerton
6 // Copyright: (c) 2010 Steven Lamerton
7 ///////////////////////////////////////////////////////////////////////////////
19 #include "wx/checkbox.h"
22 #include "testableframe.h"
24 class CheckBoxTestCase
: public CppUnit::TestCase
27 CheckBoxTestCase() { }
33 CPPUNIT_TEST_SUITE( CheckBoxTestCase
);
34 CPPUNIT_TEST( Check
);
35 #ifdef wxHAS_3STATE_CHECKBOX
36 CPPUNIT_TEST( ThirdState
);
37 CPPUNIT_TEST( ThirdStateUser
);
38 CPPUNIT_TEST( InvalidStyles
);
39 #endif // wxHAS_3STATE_CHECKBOX
40 CPPUNIT_TEST_SUITE_END();
43 #ifdef wxHAS_3STATE_CHECKBOX
45 void ThirdStateUser();
47 #endif // wxHAS_3STATE_CHECKBOX
49 // Initialize m_check with a new checkbox with the specified style
51 // This function always returns false just to make it more convenient to
52 // use inside WX_ASSERT_FAILS_WITH_ASSERT(), its return value doesn't have
53 // any meaning otherwise.
54 bool CreateCheckBox(long style
)
56 m_check
= new wxCheckBox(wxTheApp
->GetTopWindow(), wxID_ANY
, "Check box",
57 wxDefaultPosition
, wxDefaultSize
, style
);
64 DECLARE_NO_COPY_CLASS(CheckBoxTestCase
)
67 // register in the unnamed registry so that these tests are run by default
68 CPPUNIT_TEST_SUITE_REGISTRATION( CheckBoxTestCase
);
70 // also include in its own registry so that these tests can be run alone
71 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CheckBoxTestCase
, "CheckBoxTestCase" );
73 void CheckBoxTestCase::setUp()
75 m_check
= new wxCheckBox(wxTheApp
->GetTopWindow(), wxID_ANY
, "Check box");
78 void CheckBoxTestCase::tearDown()
83 void CheckBoxTestCase::Check()
85 EventCounter
clicked(m_check
, wxEVT_CHECKBOX
);
87 //We should be unchecked by default
88 CPPUNIT_ASSERT(!m_check
->IsChecked());
90 m_check
->SetValue(true);
92 CPPUNIT_ASSERT(m_check
->IsChecked());
94 m_check
->SetValue(false);
96 CPPUNIT_ASSERT(!m_check
->IsChecked());
98 m_check
->Set3StateValue(wxCHK_CHECKED
);
100 CPPUNIT_ASSERT(m_check
->IsChecked());
102 m_check
->Set3StateValue(wxCHK_UNCHECKED
);
104 CPPUNIT_ASSERT(!m_check
->IsChecked());
106 //None of these should send events
107 CPPUNIT_ASSERT_EQUAL(0, clicked
.GetCount());
110 #ifdef wxHAS_3STATE_CHECKBOX
111 void CheckBoxTestCase::ThirdState()
114 CreateCheckBox(wxCHK_3STATE
);
116 CPPUNIT_ASSERT_EQUAL(wxCHK_UNCHECKED
, m_check
->Get3StateValue());
117 CPPUNIT_ASSERT(m_check
->Is3State());
118 CPPUNIT_ASSERT(!m_check
->Is3rdStateAllowedForUser());
120 m_check
->SetValue(true);
122 CPPUNIT_ASSERT_EQUAL(wxCHK_CHECKED
, m_check
->Get3StateValue());
124 m_check
->Set3StateValue(wxCHK_UNDETERMINED
);
126 CPPUNIT_ASSERT_EQUAL(wxCHK_UNDETERMINED
, m_check
->Get3StateValue());
129 void CheckBoxTestCase::ThirdStateUser()
132 CreateCheckBox(wxCHK_3STATE
| wxCHK_ALLOW_3RD_STATE_FOR_USER
);
134 CPPUNIT_ASSERT_EQUAL(wxCHK_UNCHECKED
, m_check
->Get3StateValue());
135 CPPUNIT_ASSERT(m_check
->Is3State());
136 CPPUNIT_ASSERT(m_check
->Is3rdStateAllowedForUser());
138 m_check
->SetValue(true);
140 CPPUNIT_ASSERT_EQUAL(wxCHK_CHECKED
, m_check
->Get3StateValue());
142 m_check
->Set3StateValue(wxCHK_UNDETERMINED
);
144 CPPUNIT_ASSERT_EQUAL(wxCHK_UNDETERMINED
, m_check
->Get3StateValue());
147 void CheckBoxTestCase::InvalidStyles()
149 // Check that using incompatible styles doesn't work.
151 WX_ASSERT_FAILS_WITH_ASSERT( CreateCheckBox(wxCHK_2STATE
| wxCHK_3STATE
) );
153 CPPUNIT_ASSERT( !m_check
->Is3State() );
154 CPPUNIT_ASSERT( !m_check
->Is3rdStateAllowedForUser() );
158 WX_ASSERT_FAILS_WITH_ASSERT(
159 CreateCheckBox(wxCHK_2STATE
| wxCHK_ALLOW_3RD_STATE_FOR_USER
) );
161 CPPUNIT_ASSERT( !m_check
->Is3State() );
162 CPPUNIT_ASSERT( !m_check
->Is3rdStateAllowedForUser() );
165 // wxCHK_ALLOW_3RD_STATE_FOR_USER without wxCHK_3STATE doesn't work.
167 WX_ASSERT_FAILS_WITH_ASSERT( CreateCheckBox(wxCHK_ALLOW_3RD_STATE_FOR_USER
) );
170 #endif // wxHAS_3STATE_CHECKBOX
172 #endif // wxUSE_CHECKBOX