1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        tests/controls/togglebuttontest.cpp
3 // Purpose:     wxToggleButton unit test
4 // Author:      Steven Lamerton
5 // Created:     2010-07-14
6 // Copyright:   (c) 2010 Steven Lamerton
7 ///////////////////////////////////////////////////////////////////////////////
8 
9 #include "testprec.h"
10 
11 #if wxUSE_TOGGLEBTN
12 
13 
14 #ifndef WX_PRECOMP
15     #include "wx/app.h"
16 #endif // WX_PRECOMP
17 
18 #include "testableframe.h"
19 #include "wx/uiaction.h"
20 #include "wx/tglbtn.h"
21 
22 class ToggleButtonTestCase : public CppUnit::TestCase
23 {
24 public:
ToggleButtonTestCase()25     ToggleButtonTestCase() { }
26 
27     void setUp() wxOVERRIDE;
28     void tearDown() wxOVERRIDE;
29 
30 private:
31     CPPUNIT_TEST_SUITE( ToggleButtonTestCase );
32         WXUISIM_TEST( Click );
33         CPPUNIT_TEST( Value );
34     CPPUNIT_TEST_SUITE_END();
35 
36     void Click();
37     void Value();
38 
39     wxToggleButton* m_button;
40 
41     wxDECLARE_NO_COPY_CLASS(ToggleButtonTestCase);
42 };
43 
44 // register in the unnamed registry so that these tests are run by default
45 CPPUNIT_TEST_SUITE_REGISTRATION( ToggleButtonTestCase );
46 
47 // also include in its own registry so that these tests can be run alone
48 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ToggleButtonTestCase, "ToggleButtonTestCase" );
49 
setUp()50 void ToggleButtonTestCase::setUp()
51 {
52     m_button = new wxToggleButton(wxTheApp->GetTopWindow(), wxID_ANY, "wxToggleButton");
53 }
54 
tearDown()55 void ToggleButtonTestCase::tearDown()
56 {
57     wxDELETE(m_button);
58 }
59 
Click()60 void ToggleButtonTestCase::Click()
61 {
62 #if wxUSE_UIACTIONSIMULATOR
63     EventCounter clicked(m_button, wxEVT_TOGGLEBUTTON);
64 
65     wxUIActionSimulator sim;
66 
67     //We move in slightly to account for window decorations
68     sim.MouseMove(m_button->GetScreenPosition() + wxPoint(10, 10));
69     wxYield();
70 
71     sim.MouseClick();
72     wxYield();
73 
74     CPPUNIT_ASSERT_EQUAL(1, clicked.GetCount());
75     CPPUNIT_ASSERT(m_button->GetValue());
76     clicked.Clear();
77 
78     sim.MouseClick();
79     wxYield();
80 
81     CPPUNIT_ASSERT_EQUAL(1, clicked.GetCount());
82     CPPUNIT_ASSERT(!m_button->GetValue());
83 #endif
84 }
85 
Value()86 void ToggleButtonTestCase::Value()
87 {
88     EventCounter clicked(m_button, wxEVT_BUTTON);
89 
90     m_button->SetValue(true);
91 
92     CPPUNIT_ASSERT(m_button->GetValue());
93 
94     m_button->SetValue(false);
95 
96     CPPUNIT_ASSERT(!m_button->GetValue());
97 
98     CPPUNIT_ASSERT_EQUAL( 0, clicked.GetCount() );
99 }
100 
101 #endif //wxUSE_TOGGLEBTN
102