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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
///////////////////////////////////////////////////////////////////////////////
// Name: tests/controls/spinctrltest.cpp
// Purpose: wxSpinCtrl unit test
// Author: Steven Lamerton
// Created: 2010-07-21
// Copyright: (c) 2010 Steven Lamerton
///////////////////////////////////////////////////////////////////////////////
#include "testprec.h"
#if wxUSE_SPINCTRL
#ifndef WX_PRECOMP
#include "wx/app.h"
#endif // WX_PRECOMP
#include "testableframe.h"
#include "wx/uiaction.h"
#include "wx/spinctrl.h"
#include "wx/textctrl.h"
class SpinCtrlTestCase1
{
public:
SpinCtrlTestCase1()
: m_spin(new wxSpinCtrl())
{
}
~SpinCtrlTestCase1()
{
delete m_spin;
}
protected:
wxSpinCtrl* m_spin;
};
class SpinCtrlTestCase2
{
public:
SpinCtrlTestCase2()
: m_spin(new wxSpinCtrl(wxTheApp->GetTopWindow()))
{
}
~SpinCtrlTestCase2()
{
delete m_spin;
}
protected:
wxSpinCtrl* m_spin;
};
class SpinCtrlTestCase3
{
public:
SpinCtrlTestCase3()
: m_spin(new wxSpinCtrl(wxTheApp->GetTopWindow()))
{
m_spin->Bind(wxEVT_SPINCTRL, &SpinCtrlTestCase3::OnSpinSetValue, this);
}
~SpinCtrlTestCase3()
{
delete m_spin;
}
private:
void OnSpinSetValue(wxSpinEvent &e)
{
// Constrain the value to be in the 1..16 range or 32.
int newVal = e.GetValue();
if ( newVal == 31 )
m_spin->SetValue(16);
else if ( newVal > 16 )
m_spin->SetValue(32);
}
protected:
wxSpinCtrl* m_spin;
};
TEST_CASE_METHOD(SpinCtrlTestCase2, "SpinCtrl::Init", "[spinctrl]")
{
// Initial value is defined by "initial" argument which is 0 by default.
CHECK(m_spin->GetValue() == 0);
}
TEST_CASE_METHOD(SpinCtrlTestCase1, "SpinCtrl::Init2", "[spinctrl]")
{
m_spin->Create(wxTheApp->GetTopWindow(), wxID_ANY, "",
wxDefaultPosition, wxDefaultSize, 0,
0, 100, 17);
// Recreate the control with another "initial" to check this.
CHECK(m_spin->GetValue() == 17);
}
TEST_CASE_METHOD(SpinCtrlTestCase1, "SpinCtrl::Init3", "[spinctrl]")
{
m_spin->Create(wxTheApp->GetTopWindow(), wxID_ANY, "",
wxDefaultPosition, wxDefaultSize, 0,
0, 200, 150);
// Recreate the control with another "initial" outside of standard spin
// ctrl range.
CHECK(m_spin->GetValue() == 150);
}
TEST_CASE_METHOD(SpinCtrlTestCase1, "SpinCtrl::Init4", "[spinctrl]")
{
m_spin->Create(wxTheApp->GetTopWindow(), wxID_ANY, "99",
wxDefaultPosition, wxDefaultSize, 0,
0, 100, 17);
// Recreate the control with another "initial" outside of standard spin
// ctrl range.
// But if the text string is specified, it takes precedence.
CHECK(m_spin->GetValue() == 99);
}
TEST_CASE_METHOD(SpinCtrlTestCase1, "SpinCtrl::InitOutOfRange", "[spinctrl]")
{
m_spin->Create(wxTheApp->GetTopWindow(), wxID_ANY, "",
wxDefaultPosition, wxDefaultSize, 0,
10, 20, 0);
// Recreate the control with another "initial" outside of the valid range:
// it shouldn't be taken into account.
CHECK(m_spin->GetValue() == 10);
}
TEST_CASE_METHOD(SpinCtrlTestCase1, "SpinCtrl::NoEventsInCtor", "[spinctrl]")
{
// Verify that creating the control does not generate any events. This is
// unexpected and shouldn't happen.
EventCounter updatedSpin(m_spin, wxEVT_SPINCTRL);
EventCounter updatedText(m_spin, wxEVT_TEXT);
m_spin->Create(wxTheApp->GetTopWindow(), wxID_ANY, "",
wxDefaultPosition, wxDefaultSize, 0,
0, 100, 17);
CHECK(updatedSpin.GetCount() == 0);
CHECK(updatedText.GetCount() == 0);
}
TEST_CASE_METHOD(SpinCtrlTestCase2, "SpinCtrl::Arrows", "[spinctrl]")
{
#if wxUSE_UIACTIONSIMULATOR
EventCounter updated(m_spin, wxEVT_SPINCTRL);
wxUIActionSimulator sim;
m_spin->SetFocus();
wxYield();
sim.Char(WXK_UP);
wxYield();
CHECK(updated.GetCount() == 1);
CHECK(m_spin->GetValue() == 1);
updated.Clear();
sim.Char(WXK_DOWN);
wxYield();
CHECK(updated.GetCount() == 1);
CHECK(m_spin->GetValue() == 0);
#endif
}
TEST_CASE_METHOD(SpinCtrlTestCase1, "SpinCtrl::Wrap", "[spinctrl]")
{
#if wxUSE_UIACTIONSIMULATOR
m_spin->Create(wxTheApp->GetTopWindow(), wxID_ANY, "",
wxDefaultPosition, wxDefaultSize,
wxSP_ARROW_KEYS | wxSP_WRAP);
wxUIActionSimulator sim;
m_spin->SetFocus();
wxYield();
sim.Char(WXK_DOWN);
wxYield();
CHECK(m_spin->GetValue() == 100);
sim.Char(WXK_UP);
wxYield();
CHECK(m_spin->GetValue() == 0);
#endif
}
TEST_CASE_METHOD(SpinCtrlTestCase2, "SpinCtrl::Range", "[spinctrl]")
{
CHECK(m_spin->GetMin() == 0);
CHECK(m_spin->GetMax() == 100);
CHECK(m_spin->GetBase() == 10);
// Test that the value is adjusted to be inside the new valid range but
// that this doesn't result in any events (as this is not something done by
// the user).
{
EventCounter updatedSpin(m_spin, wxEVT_SPINCTRL);
EventCounter updatedText(m_spin, wxEVT_TEXT);
m_spin->SetRange(1, 10);
CHECK(m_spin->GetValue() == 1);
CHECK(updatedSpin.GetCount() == 0);
CHECK(updatedText.GetCount() == 0);
}
// Test negative ranges
m_spin->SetRange(-10, 10);
CHECK(m_spin->GetMin() == -10);
CHECK(m_spin->GetMax() == 10);
// With base 16 only ranges including values >= 0 are allowed
m_spin->SetRange(0, 10);
int oldMinVal = m_spin->GetMin();
int oldMaxVal = m_spin->GetMax();
CHECK(oldMinVal == 0);
CHECK(oldMaxVal == 10);
CHECK(m_spin->SetBase(16) == true);
CHECK(m_spin->GetBase() == 16);
// New range should be silently ignored
m_spin->SetRange(-20, 20);
CHECK(m_spin->GetMin() == oldMinVal);
CHECK(m_spin->GetMax() == oldMaxVal);
// This range should be accepted
m_spin->SetRange(2, 8);
CHECK(m_spin->GetMin() == 2);
CHECK(m_spin->GetMax() == 8);
CHECK(m_spin->SetBase(10) == true);
CHECK(m_spin->GetBase() == 10);
//Test backwards ranges
m_spin->SetRange(75, 50);
CHECK(m_spin->GetMin() == 75);
CHECK(m_spin->GetMax() == 50);
}
TEST_CASE_METHOD(SpinCtrlTestCase2, "SpinCtrl::Value", "[spinctrl]")
{
EventCounter updatedSpin(m_spin, wxEVT_SPINCTRL);
EventCounter updatedText(m_spin, wxEVT_TEXT);
CHECK(m_spin->GetValue() == 0);
m_spin->SetValue(50);
CHECK(m_spin->GetValue() == 50);
m_spin->SetValue(-10);
CHECK(m_spin->GetValue() == 0);
m_spin->SetValue(110);
CHECK(m_spin->GetValue() == 100);
// Calling SetValue() shouldn't have generated any events.
CHECK(updatedSpin.GetCount() == 0);
CHECK(updatedText.GetCount() == 0);
// Also test that setting the text value works.
CHECK( m_spin->GetTextValue() == "100" );
m_spin->SetValue("57");
CHECK( m_spin->GetTextValue() == "57" );
CHECK( m_spin->GetValue() == 57 );
CHECK(updatedSpin.GetCount() == 0);
CHECK(updatedText.GetCount() == 0);
m_spin->SetValue("");
CHECK( m_spin->GetTextValue() == "" );
CHECK( m_spin->GetValue() == 0 );
CHECK(updatedSpin.GetCount() == 0);
CHECK(updatedText.GetCount() == 0);
}
TEST_CASE_METHOD(SpinCtrlTestCase2, "SpinCtrl::Base", "[spinctrl]")
{
CHECK(m_spin->GetMin() == 0);
CHECK(m_spin->GetMax() == 100);
CHECK(m_spin->GetBase() == 10);
// Only 10 and 16 bases are allowed
CHECK(m_spin->SetBase(10) == true);
CHECK(m_spin->GetBase() == 10);
CHECK_FALSE(m_spin->SetBase(8));
CHECK(m_spin->GetBase() == 10);
CHECK_FALSE(m_spin->SetBase(2));
CHECK(m_spin->GetBase() == 10);
CHECK(m_spin->SetBase(16) == true);
CHECK(m_spin->GetBase() == 16);
CHECK(m_spin->SetBase(10) == true);
CHECK(m_spin->GetBase() == 10);
// When range contains negative values only base 10 is allowed
m_spin->SetRange(-10, 10);
CHECK(m_spin->GetMin() == -10);
CHECK(m_spin->GetMax() == 10);
CHECK_FALSE(m_spin->SetBase(8));
CHECK(m_spin->GetBase() == 10);
CHECK_FALSE(m_spin->SetBase(2));
CHECK(m_spin->GetBase() == 10);
CHECK_FALSE(m_spin->SetBase(16));
CHECK(m_spin->GetBase() == 10);
CHECK(m_spin->SetBase(10) == true);
CHECK(m_spin->GetBase() == 10);
}
TEST_CASE_METHOD(SpinCtrlTestCase3, "SpinCtrl::SetValueInsideEventHandler", "[spinctrl]")
{
#if wxUSE_UIACTIONSIMULATOR
// A dummy control with which we change the focus.
wxTextCtrl* text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY);
text->Move(m_spin->GetSize().x, m_spin->GetSize().y * 3);
wxUIActionSimulator sim;
// run multiple times to make sure there are no issues with keeping old value
for ( size_t i = 0; i < 2; i++ )
{
m_spin->SetFocus();
wxYield();
sim.Char(WXK_DELETE);
sim.Char(WXK_DELETE);
sim.Text("20");
wxYield();
text->SetFocus();
wxYield();
CHECK(m_spin->GetValue() == 32);
}
delete text;
#endif // wxUSE_UIACTIONSIMULATOR
}
TEST_CASE_METHOD(SpinCtrlTestCase1, "SpinCtrl::Increment", "[spinctrl]")
{
#if wxUSE_UIACTIONSIMULATOR
m_spin->Create(wxTheApp->GetTopWindow(), wxID_ANY, "",
wxDefaultPosition, wxDefaultSize,
wxSP_ARROW_KEYS | wxSP_WRAP);
wxUIActionSimulator sim;
CHECK( m_spin->GetIncrement() == 1 );
m_spin->SetFocus();
wxYield();
m_spin->SetIncrement( 5 );
sim.Char(WXK_UP);
wxYield();
CHECK(m_spin->GetValue() == 5);
int increment = m_spin->GetIncrement();
CHECK( increment == 5 );
#endif
}
#endif
|