]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/radiobuttontest.cpp
Don't crash in wxOSX wxTextEntry if the window wasn't created yet.
[wxWidgets.git] / tests / controls / radiobuttontest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/radiobuttontest.cpp
3 // Purpose: wxRadioButton unit test
4 // Author: Steven Lamerton
5 // Created: 2010-07-30
6 // RCS-ID: $Id$
7 // Copyright: (c) 2010 Steven Lamerton
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #include "testprec.h"
11
12 #if wxUSE_RADIOBTN
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #ifndef WX_PRECOMP
19 #include "wx/app.h"
20 #include "wx/radiobut.h"
21 #endif // WX_PRECOMP
22
23 #include "wx/uiaction.h"
24 #include "testableframe.h"
25
26 class RadioButtonTestCase : public CppUnit::TestCase
27 {
28 public:
29 RadioButtonTestCase() { }
30
31 void setUp();
32 void tearDown();
33
34 private:
35 CPPUNIT_TEST_SUITE( RadioButtonTestCase );
36 WXUISIM_TEST( Click );
37 CPPUNIT_TEST( Value );
38 CPPUNIT_TEST( Group );
39 CPPUNIT_TEST_SUITE_END();
40
41 void Click();
42 void Value();
43 void Group();
44
45 wxRadioButton* m_radio;
46
47 DECLARE_NO_COPY_CLASS(RadioButtonTestCase)
48 };
49
50 // register in the unnamed registry so that these tests are run by default
51 CPPUNIT_TEST_SUITE_REGISTRATION( RadioButtonTestCase );
52
53 // also include in its own registry so that these tests can be run alone
54 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RadioButtonTestCase,
55 "RadioButtonTestCase" );
56
57 void RadioButtonTestCase::setUp()
58 {
59 m_radio = new wxRadioButton(wxTheApp->GetTopWindow(), wxID_ANY,
60 "wxRadioButton");
61 m_radio->Update();
62 m_radio->Refresh();
63 }
64
65 void RadioButtonTestCase::tearDown()
66 {
67 wxDELETE(m_radio);
68 }
69
70 void RadioButtonTestCase::Click()
71 {
72 // GTK does not support selecting a single radio button
73 #if wxUSE_UIACTIONSIMULATOR && !defined(__WXGTK__)
74 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
75 wxTestableFrame);
76
77 EventCounter count(m_radio, wxEVT_COMMAND_RADIOBUTTON_SELECTED);
78
79 wxUIActionSimulator sim;
80
81 sim.MouseMove(m_radio->GetScreenPosition() + wxPoint(10, 10));
82 sim.MouseClick();
83
84 wxYield();
85
86 CPPUNIT_ASSERT_EQUAL( 1, frame->GetEventCount() );
87 #endif
88 }
89
90 void RadioButtonTestCase::Value()
91 {
92 #ifndef __WXGTK__
93 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
94 wxTestableFrame);
95
96 EventCounter count(m_radio, wxEVT_COMMAND_RADIOBUTTON_SELECTED);
97
98 m_radio->SetValue(true);
99
100 CPPUNIT_ASSERT(m_radio->GetValue());
101
102 m_radio->SetValue(false);
103
104 CPPUNIT_ASSERT(!m_radio->GetValue());
105
106 CPPUNIT_ASSERT_EQUAL(0, frame->GetEventCount());
107 #endif
108 }
109
110 void RadioButtonTestCase::Group()
111 {
112 //Add another button to the first group and create another of two buttons
113 wxRadioButton* g1radio0 = new wxRadioButton(wxTheApp->GetTopWindow(),
114 wxID_ANY, "wxRadioButton",
115 wxDefaultPosition,
116 wxDefaultSize, wxRB_GROUP);
117
118 wxRadioButton* g1radio1 = new wxRadioButton(wxTheApp->GetTopWindow(),
119 wxID_ANY, "wxRadioButton");
120
121 wxRadioButton* g2radio0 = new wxRadioButton(wxTheApp->GetTopWindow(),
122 wxID_ANY, "wxRadioButton",
123 wxDefaultPosition,
124 wxDefaultSize, wxRB_GROUP);
125
126 wxRadioButton* g2radio1 = new wxRadioButton(wxTheApp->GetTopWindow(),
127 wxID_ANY, "wxRadioButton");
128
129 g1radio0->SetValue(true);
130 g2radio0->SetValue(true);
131
132 CPPUNIT_ASSERT(g1radio0->GetValue());
133 CPPUNIT_ASSERT(!g1radio1->GetValue());
134 CPPUNIT_ASSERT(g2radio0->GetValue());
135 CPPUNIT_ASSERT(!g2radio1->GetValue());
136
137 g1radio1->SetValue(true);
138 g2radio1->SetValue(true);
139
140 CPPUNIT_ASSERT(!g1radio0->GetValue());
141 CPPUNIT_ASSERT(g1radio1->GetValue());
142 CPPUNIT_ASSERT(!g2radio0->GetValue());
143 CPPUNIT_ASSERT(g2radio1->GetValue());
144
145 g1radio0->SetValue(true);
146 g2radio0->SetValue(true);
147
148 CPPUNIT_ASSERT(g1radio0->GetValue());
149 CPPUNIT_ASSERT(!g1radio1->GetValue());
150 CPPUNIT_ASSERT(g2radio0->GetValue());
151 CPPUNIT_ASSERT(!g2radio1->GetValue());
152
153 wxDELETE(g1radio0);
154 wxDELETE(g1radio1);
155 wxDELETE(g2radio0);
156 wxDELETE(g2radio1);
157 }
158
159 #endif //wxUSE_RADIOBTN