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