File: radiobuttontest.cpp

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (274 lines) | stat: -rw-r--r-- 8,759 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
///////////////////////////////////////////////////////////////////////////////
// Name:        tests/controls/radiobuttontest.cpp
// Purpose:     wxRadioButton unit test
// Author:      Steven Lamerton
// Created:     2010-07-30
// Copyright:   (c) 2010 Steven Lamerton
///////////////////////////////////////////////////////////////////////////////

#include "testprec.h"

#if wxUSE_RADIOBTN


#ifndef WX_PRECOMP
    #include "wx/app.h"
    #include "wx/button.h"
    #include "wx/panel.h"
    #include "wx/radiobut.h"
    #include "wx/sizer.h"
    #include "wx/stattext.h"
#endif // WX_PRECOMP

#include "wx/uiaction.h"
#include "testableframe.h"
#include "testwindow.h"

class RadioButtonTestCase
{
public:
    RadioButtonTestCase();
    ~RadioButtonTestCase();

protected:
    wxRadioButton* m_radio;

    wxDECLARE_NO_COPY_CLASS(RadioButtonTestCase);
};

RadioButtonTestCase::RadioButtonTestCase()
{
    m_radio = new wxRadioButton(wxTheApp->GetTopWindow(), wxID_ANY,
                                "wxRadioButton");
    m_radio->Update();
    m_radio->Refresh();
}

RadioButtonTestCase::~RadioButtonTestCase()
{
    delete m_radio;
}

TEST_CASE_METHOD(RadioButtonTestCase, "RadioButton::Click", "[radiobutton]")
{
    // OS X doesn't support selecting a single radio button
#if wxUSE_UIACTIONSIMULATOR && !defined(__WXOSX__)
    EventCounter selected(m_radio, wxEVT_RADIOBUTTON);

    wxUIActionSimulator sim;
    wxYield();

    sim.MouseMove(m_radio->GetScreenPosition() + wxPoint(10, 10));
    sim.MouseClick();

    wxYield();

    CHECK(selected.GetCount() == 1);
#endif
}

TEST_CASE_METHOD(RadioButtonTestCase, "RadioButton::Value", "[radiobutton]")
{
#ifndef __WXGTK__
    EventCounter selected(m_radio, wxEVT_RADIOBUTTON);

    m_radio->SetValue(true);

    CHECK(m_radio->GetValue());

    m_radio->SetValue(false);

    CHECK(!m_radio->GetValue());

    CHECK(selected.GetCount() == 0);
#endif
}

TEST_CASE_METHOD(RadioButtonTestCase, "RadioButton::Group", "[radiobutton]")
{
    wxWindow* const parent = wxTheApp->GetTopWindow();

    // Create two different radio groups.
    wxScopedPtr<wxRadioButton> g1radio0(new wxRadioButton(parent, wxID_ANY, "radio 1.0",
                                                wxDefaultPosition, wxDefaultSize,
                                                wxRB_GROUP));

    wxScopedPtr<wxRadioButton> g1radio1(new wxRadioButton(parent, wxID_ANY, "radio 1.1"));

    wxScopedPtr<wxRadioButton> g2radio0(new wxRadioButton(parent, wxID_ANY, "radio 2.0",
                                                wxDefaultPosition, wxDefaultSize,
                                                wxRB_GROUP));

    wxScopedPtr<wxRadioButton> g2radio1(new wxRadioButton(parent, wxID_ANY, "radio 2.1"));

    // Check that having another control between radio buttons doesn't break
    // grouping.
    wxScopedPtr<wxStaticText> text(new wxStaticText(parent, wxID_ANY, "Label"));
    wxScopedPtr<wxRadioButton> g2radio2(new wxRadioButton(parent, wxID_ANY, "radio 2.2"));

    g1radio0->SetValue(true);
    g2radio0->SetValue(true);

    CHECK(g1radio0->GetValue());
    CHECK(!g1radio1->GetValue());
    CHECK(g2radio0->GetValue());
    CHECK(!g2radio1->GetValue());

    g1radio1->SetValue(true);
    g2radio1->SetValue(true);

    CHECK(!g1radio0->GetValue());
    CHECK(g1radio1->GetValue());
    CHECK(!g2radio0->GetValue());
    CHECK(g2radio1->GetValue());

    g2radio2->SetValue(true);
    CHECK(!g2radio0->GetValue());
    CHECK(!g2radio1->GetValue());
    CHECK(g2radio2->GetValue());

    g1radio0->SetValue(true);
    g2radio0->SetValue(true);

    CHECK(g1radio0->GetValue());
    CHECK(!g1radio1->GetValue());
    CHECK(g2radio0->GetValue());
    CHECK(!g2radio1->GetValue());


    // Check that group navigation functions behave as expected.

    // GetFirstInGroup()
    CHECK_SAME_WINDOW(g1radio0->GetFirstInGroup(), g1radio0);
    CHECK_SAME_WINDOW(g1radio1->GetFirstInGroup(), g1radio0);

    CHECK_SAME_WINDOW(g2radio0->GetFirstInGroup(), g2radio0);
    CHECK_SAME_WINDOW(g2radio1->GetFirstInGroup(), g2radio0);
    CHECK_SAME_WINDOW(g2radio2->GetFirstInGroup(), g2radio0);

    // GetLastInGroup()
    CHECK_SAME_WINDOW(g1radio0->GetLastInGroup(), g1radio1);
    CHECK_SAME_WINDOW(g1radio1->GetLastInGroup(), g1radio1);

    CHECK_SAME_WINDOW(g2radio0->GetLastInGroup(), g2radio2);
    CHECK_SAME_WINDOW(g2radio1->GetLastInGroup(), g2radio2);
    CHECK_SAME_WINDOW(g2radio2->GetLastInGroup(), g2radio2);

    // GetNextInGroup()
    CHECK_SAME_WINDOW(g1radio0->GetNextInGroup(), g1radio1);
    CHECK_SAME_WINDOW(g1radio1->GetNextInGroup(), NULL);

    CHECK_SAME_WINDOW(g2radio0->GetNextInGroup(), g2radio1);
    CHECK_SAME_WINDOW(g2radio1->GetNextInGroup(), g2radio2);
    CHECK_SAME_WINDOW(g2radio2->GetNextInGroup(), NULL);

    // GetPreviousInGroup()
    CHECK_SAME_WINDOW(g1radio0->GetPreviousInGroup(), NULL);
    CHECK_SAME_WINDOW(g1radio1->GetPreviousInGroup(), g1radio0);

    CHECK_SAME_WINDOW(g2radio0->GetPreviousInGroup(), NULL);
    CHECK_SAME_WINDOW(g2radio1->GetPreviousInGroup(), g2radio0);
    CHECK_SAME_WINDOW(g2radio2->GetPreviousInGroup(), g2radio1);
}

TEST_CASE_METHOD(RadioButtonTestCase, "RadioButton::Single", "[radiobutton]")
{
    //Create a group of 2 buttons, having second button selected
    wxScopedPtr<wxRadioButton> gradio0(new wxRadioButton(wxTheApp->GetTopWindow(),
        wxID_ANY, "wxRadioButton",
        wxDefaultPosition,
        wxDefaultSize, wxRB_GROUP));

    wxScopedPtr<wxRadioButton> gradio1(new wxRadioButton(wxTheApp->GetTopWindow(),
        wxID_ANY, "wxRadioButton"));

    gradio1->SetValue(true);

    //Create a "single" button (by default it will not be selected)
    wxScopedPtr<wxRadioButton> sradio(new wxRadioButton(wxTheApp->GetTopWindow(),
        wxID_ANY, "wxRadioButton",
        wxDefaultPosition,
        wxDefaultSize, wxRB_SINGLE));

    //Create a non-grouped button and select it
    wxScopedPtr<wxRadioButton> ngradio(new wxRadioButton(wxTheApp->GetTopWindow(),
        wxID_ANY, "wxRadioButton"));

    ngradio->SetValue(true);

    //Select the "single" button
    sradio->SetValue(true);

    CHECK(gradio1->GetValue());
    CHECK(ngradio->GetValue());

    // Also check that navigation works as expected with "single" buttons.
    CHECK_SAME_WINDOW(sradio->GetFirstInGroup(), sradio);
    CHECK_SAME_WINDOW(sradio->GetLastInGroup(), sradio);
    CHECK_SAME_WINDOW(sradio->GetPreviousInGroup(), NULL);
    CHECK_SAME_WINDOW(sradio->GetNextInGroup(), NULL);
}

TEST_CASE("RadioButton::Focus", "[radiobutton][focus]")
{
    // Create a container panel just to be able to destroy all the windows
    // created here at once by simply destroying it.
    wxWindow* const tlw = wxTheApp->GetTopWindow();
    wxScopedPtr<wxPanel> parentPanel(new wxPanel(tlw));

    // Create a panel containing 2 radio buttons and another control outside
    // this panel, so that we could give focus to something different and then
    // return it back to the panel.
    wxPanel* const radioPanel = new wxPanel(parentPanel.get());
    wxRadioButton* const radio1 = new wxRadioButton(radioPanel, wxID_ANY, "1");
    wxRadioButton* const radio2 = new wxRadioButton(radioPanel, wxID_ANY, "2");
    wxSizer* const radioSizer = new wxBoxSizer(wxHORIZONTAL);
    radioSizer->Add(radio1);
    radioSizer->Add(radio2);
    radioPanel->SetSizer(radioSizer);

    wxButton* const dummyButton = new wxButton(parentPanel.get(), wxID_OK);

    wxSizer* const sizer = new wxBoxSizer(wxVERTICAL);
    sizer->Add(radioPanel, wxSizerFlags(1).Expand());
    sizer->Add(dummyButton, wxSizerFlags().Expand());
    parentPanel->SetSizer(sizer);

    parentPanel->SetSize(tlw->GetClientSize());
    parentPanel->Layout();

    // Initially the first radio button should be checked.
    radio1->SetFocus();
    CHECK(radio1->GetValue());
    CHECK_FOCUS_IS(radio1);

    // Switching focus from it shouldn't change this.
    dummyButton->SetFocus();
    CHECK(radio1->GetValue());

    // Checking another radio button should make it checked and uncheck the
    // first one.
    radio2->SetValue(true);
    CHECK(!radio1->GetValue());
    CHECK(radio2->GetValue());

    // While not changing focus.
    CHECK_FOCUS_IS(dummyButton);

    // And giving the focus to the panel shouldn't change radio button
    // selection.
    radioPanel->SetFocus();

    // Under MSW, focus is always on the selected button, but in the other
    // ports this is not necessarily the case, i.e. under wxGTK this check
    // would fail because focus gets set to the first button -- even though the
    // second one remains checked.
#ifdef __WXMSW__
    CHECK_FOCUS_IS(radio2);
#endif

    CHECK(!radio1->GetValue());
    CHECK(radio2->GetValue());
}

#endif //wxUSE_RADIOBTN