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 EventCounter
clicked(m_check
, wxEVT_CHECKBOX
);
88 //We should be unchecked by default
89 CPPUNIT_ASSERT(!m_check
->IsChecked());
91 m_check
->SetValue(true);
93 CPPUNIT_ASSERT(m_check
->IsChecked());
95 m_check
->SetValue(false);
97 CPPUNIT_ASSERT(!m_check
->IsChecked());
99 m_check
->Set3StateValue(wxCHK_CHECKED
);
101 CPPUNIT_ASSERT(m_check
->IsChecked());
103 m_check
->Set3StateValue(wxCHK_UNCHECKED
);
105 CPPUNIT_ASSERT(!m_check
->IsChecked());
107 //None of these should send events
108 CPPUNIT_ASSERT_EQUAL(0, clicked
.GetCount());
111 #ifdef wxHAS_3STATE_CHECKBOX
112 void CheckBoxTestCase::ThirdState()
115 CreateCheckBox(wxCHK_3STATE
);
117 CPPUNIT_ASSERT_EQUAL(wxCHK_UNCHECKED
, m_check
->Get3StateValue());
118 CPPUNIT_ASSERT(m_check
->Is3State());
119 CPPUNIT_ASSERT(!m_check
->Is3rdStateAllowedForUser());
121 m_check
->SetValue(true);
123 CPPUNIT_ASSERT_EQUAL(wxCHK_CHECKED
, m_check
->Get3StateValue());
125 m_check
->Set3StateValue(wxCHK_UNDETERMINED
);
127 CPPUNIT_ASSERT_EQUAL(wxCHK_UNDETERMINED
, m_check
->Get3StateValue());
130 void CheckBoxTestCase::ThirdStateUser()
133 CreateCheckBox(wxCHK_3STATE
| wxCHK_ALLOW_3RD_STATE_FOR_USER
);
135 CPPUNIT_ASSERT_EQUAL(wxCHK_UNCHECKED
, m_check
->Get3StateValue());
136 CPPUNIT_ASSERT(m_check
->Is3State());
137 CPPUNIT_ASSERT(m_check
->Is3rdStateAllowedForUser());
139 m_check
->SetValue(true);
141 CPPUNIT_ASSERT_EQUAL(wxCHK_CHECKED
, m_check
->Get3StateValue());
143 m_check
->Set3StateValue(wxCHK_UNDETERMINED
);
145 CPPUNIT_ASSERT_EQUAL(wxCHK_UNDETERMINED
, m_check
->Get3StateValue());
148 void CheckBoxTestCase::InvalidStyles()
150 // Check that using incompatible styles doesn't work.
152 WX_ASSERT_FAILS_WITH_ASSERT( CreateCheckBox(wxCHK_2STATE
| wxCHK_3STATE
) );
154 CPPUNIT_ASSERT( !m_check
->Is3State() );
155 CPPUNIT_ASSERT( !m_check
->Is3rdStateAllowedForUser() );
159 WX_ASSERT_FAILS_WITH_ASSERT(
160 CreateCheckBox(wxCHK_2STATE
| wxCHK_ALLOW_3RD_STATE_FOR_USER
) );
162 CPPUNIT_ASSERT( !m_check
->Is3State() );
163 CPPUNIT_ASSERT( !m_check
->Is3rdStateAllowedForUser() );
166 // wxCHK_ALLOW_3RD_STATE_FOR_USER without wxCHK_3STATE doesn't work.
168 WX_ASSERT_FAILS_WITH_ASSERT( CreateCheckBox(wxCHK_ALLOW_3RD_STATE_FOR_USER
) );
171 #endif // wxHAS_3STATE_CHECKBOX
173 #endif // wxUSE_CHECKBOX