Files
wxWidgets/tests/controls/spinctrldbltest.cpp
T
Paul Cornett 8e005d1c31
Unix builds / Ubuntu 18.04 wxGTK 3 compatible 3.0 (push) Has been cancelled
Unix builds / Ubuntu 24.04 wxGTK ASAN not compatible (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxGTK UTF-8 (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxQt (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxX11 (push) Has been cancelled
Unix builds / Ubuntu 20.04 wxGTK 3 with clang (push) Has been cancelled
Unix builds / Ubuntu 22.04 wxGTK with wx containers (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxDFB (push) Has been cancelled
Unix builds / Ubuntu 24.04 wxGTK UBSAN (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxGTK 3 static with gcc 4.8 (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxGTK 2 (push) Has been cancelled
CMake builds / Ubuntu 22.04 wxGTK 3 (push) Has been cancelled
CMake builds / macOS latest wxGTK 3 Unix Makefiles (push) Has been cancelled
CMake builds / MSW/MSVC wxMSW (push) Has been cancelled
CMake builds / MSW/Clang wxMSW (push) Has been cancelled
CMake builds / macOS latest wxOSX Ninja (push) Has been cancelled
CMake builds / macOS 14 wxOSX Xcode (push) Has been cancelled
CMake builds / macOS 14 wxIOS (push) Has been cancelled
CMake builds / MSW/MSVC wxQt 5.15 (push) Has been cancelled
CMake builds / MSW/MSVC wxQt 6.10 (push) Has been cancelled
Mac builds / wxMac ARM ASAN not compatible (push) Has been cancelled
Mac builds / wxMac Universal C++14 (push) Has been cancelled
Mac builds / wxiOS Simulator on Silicon Mac (push) Has been cancelled
Mac builds / wxiOS (push) Has been cancelled
Mac builds / wxMac Intel C++17 (push) Has been cancelled
Mac Xcode builds / iOS Simulator static (push) Has been cancelled
Mac Xcode builds / macOS dynamic Release (push) Has been cancelled
Mac Xcode builds / iOS static Debug (push) Has been cancelled
MSW builds / wxMSW vs2022 DLL Debug x64 (push) Has been cancelled
MSW builds / wxMSW vs2022 DLL Release x64 (push) Has been cancelled
MSW builds / wxMSW vs2022 Debug Win32 (push) Has been cancelled
MSW builds / wxMSW vs2022 Release arm64 (push) Has been cancelled
MSW builds / wxMSW vs2026 DLL Release x64 (push) Has been cancelled
MSW cross-builds / wxMSW 64 bits not compatible (push) Has been cancelled
MSW cross-builds / wxMSW/Univ (push) Has been cancelled
MSW cross-builds / wxMSW 32 bits (push) Has been cancelled
Code Checks / Check Spelling (push) Has been cancelled
Code Checks / Check Whitespace (push) Has been cancelled
Code Checks / Check Mixed EOL (push) Has been cancelled
Code Checks / Check C++ Style (push) Has been cancelled
Code Checks / Check All Headers In allheaders.h (push) Has been cancelled
Update Documentation / Update Online Documentation (push) Has been cancelled
Remove m_ prefix from local variable
2026-05-21 16:57:51 -07:00

296 lines
7.7 KiB
C++

///////////////////////////////////////////////////////////////////////////////
// Name: tests/controls/spinctrldbltest.cpp
// Purpose: wxSpinCtrlDouble unit test
// Author: Steven Lamerton
// Created: 2010-07-22
// 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 <memory>
class SpinCtrlDoubleTestCase
{
public:
SpinCtrlDoubleTestCase(int style = wxSP_ARROW_KEYS)
: m_spin(new wxSpinCtrlDouble(wxTheApp->GetTopWindow(), wxID_ANY, "",
wxDefaultPosition, wxDefaultSize,
style))
{
}
~SpinCtrlDoubleTestCase()
{
delete m_spin;
}
protected:
wxSpinCtrlDouble* const m_spin;
wxDECLARE_NO_COPY_CLASS(SpinCtrlDoubleTestCase);
};
class SpinCtrlDoubleTestCaseWrap : public SpinCtrlDoubleTestCase
{
public:
SpinCtrlDoubleTestCaseWrap()
: SpinCtrlDoubleTestCase(wxSP_ARROW_KEYS | wxSP_WRAP)
{
}
};
TEST_CASE("SpinCtrlDouble::NoEventsInCtor", "[spinctrl][spinctrldouble]")
{
// Verify that creating the control does not generate any events. This is
// unexpected and shouldn't happen.
std::unique_ptr<wxSpinCtrlDouble> spin(new wxSpinCtrlDouble);
EventCounter updatedSpin(spin.get(), wxEVT_SPINCTRLDOUBLE);
EventCounter updatedText(spin.get(), wxEVT_TEXT);
spin->Create(wxTheApp->GetTopWindow(), wxID_ANY, "",
wxDefaultPosition, wxDefaultSize, 0,
0., 100., 17.);
CHECK( updatedSpin.GetCount() == 0 );
CHECK( updatedText.GetCount() == 0 );
}
#if wxUSE_UIACTIONSIMULATOR
TEST_CASE_METHOD(SpinCtrlDoubleTestCase,
"SpinCtrlDouble::Arrows", "[spinctrl][spinctrldouble]")
{
EventCounter updated(m_spin, wxEVT_SPINCTRLDOUBLE);
wxUIActionSimulator sim;
m_spin->SetFocus();
wxYield();
sim.Char(WXK_UP);
wxYield();
CHECK( updated.GetCount() == 1 );
CHECK( m_spin->GetValue() == 1.0 );
updated.Clear();
sim.Char(WXK_DOWN);
wxYield();
CHECK( updated.GetCount() == 1 );
CHECK( m_spin->GetValue() == 0.0 );
}
TEST_CASE_METHOD(SpinCtrlDoubleTestCaseWrap,
"SpinCtrlDouble::Wrap", "[spinctrl][spinctrldouble]")
{
wxUIActionSimulator sim;
m_spin->SetFocus();
wxYield();
sim.Char(WXK_DOWN);
wxYield();
CHECK( m_spin->GetValue() == 100.0 );
sim.Char(WXK_UP);
wxYield();
CHECK( m_spin->GetValue() == 0.0 );
}
#endif // wxUSE_UIACTIONSIMULATOR
TEST_CASE_METHOD(SpinCtrlDoubleTestCase,
"SpinCtrlDouble::Range", "[spinctrl][spinctrldouble]")
{
CHECK( m_spin->GetMin() == 0.0 );
CHECK( m_spin->GetMax() == 100.0 );
// 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_SPINCTRLDOUBLE);
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.0, 10.0);
CHECK( m_spin->GetMin() == -10.0 );
CHECK( m_spin->GetMax() == 10.0 );
#ifndef __WXQT__
//Test backwards ranges
m_spin->SetRange(75.0, 50.0);
CHECK( m_spin->GetMin() == 75.0 );
CHECK( m_spin->GetMax() == 50.0 );
#endif
}
TEST_CASE_METHOD(SpinCtrlDoubleTestCase,
"SpinCtrlDouble::Value", "[spinctrl][spinctrldouble]")
{
EventCounter updatedSpin(m_spin, wxEVT_SPINCTRLDOUBLE);
EventCounter updatedText(m_spin, wxEVT_TEXT);
m_spin->SetDigits(2);
m_spin->SetIncrement(0.1);
CHECK( m_spin->GetValue() == 0.0 );
m_spin->SetValue(50.0);
CHECK( m_spin->GetValue() == 50.0 );
m_spin->SetValue(49.1);
CHECK( m_spin->GetValue() == 49.1 );
// 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() == "49.10" );
m_spin->SetValue("57.30");
CHECK( m_spin->GetTextValue() == "57.30" );
CHECK( m_spin->GetValue() == 57.3 );
CHECK( updatedSpin.GetCount() == 0 );
CHECK( updatedText.GetCount() == 0 );
m_spin->SetValue("");
#ifndef __WXQT__
CHECK( m_spin->GetTextValue() == "" );
#else
CHECK( m_spin->GetTextValue() == "0.00" ); // the control automatically displays minVal
#endif
CHECK( m_spin->GetValue() == 0 );
CHECK( updatedSpin.GetCount() == 0 );
CHECK( updatedText.GetCount() == 0 );
}
#if wxUSE_UIACTIONSIMULATOR
TEST_CASE_METHOD(SpinCtrlDoubleTestCase,
"SpinCtrlDouble::Increment", "[spinctrl][spinctrldouble]")
{
CHECK( m_spin->GetIncrement() == 1.0 );
m_spin->SetDigits(1);
m_spin->SetIncrement(0.1);
CHECK( m_spin->GetIncrement() == 0.1 );
wxUIActionSimulator sim;
m_spin->SetFocus();
wxYield();
sim.Char(WXK_UP);
wxYield();
CHECK( m_spin->GetValue() == 0.1 );
}
#endif // wxUSE_UIACTIONSIMULATOR
TEST_CASE_METHOD(SpinCtrlDoubleTestCase,
"SpinCtrlDouble::Digits", "[spinctrl][spinctrldouble]")
{
// Setting increment should adjust the number of digits shown to be big
// enough to show numbers with the corresponding granularity.
m_spin->SetIncrement(0.1);
m_spin->SetValue(1.23456789);
CHECK( m_spin->GetTextValue() == "1.2" );
m_spin->SetIncrement(0.01);
m_spin->SetValue(1.23456789);
CHECK( m_spin->GetTextValue() == "1.23" );
m_spin->SetDigits(5);
CHECK( m_spin->GetDigits() == 5 );
m_spin->SetValue(1.23456789);
CHECK( m_spin->GetTextValue() == "1.23457" );
// The number of digits shouldn't (implicitly) decrease however.
m_spin->SetIncrement(0.001);
m_spin->SetValue(1.23456789);
CHECK( m_spin->GetTextValue() == "1.23457" );
// Check that using increment greater than 1 also works.
m_spin->SetDigits(0);
m_spin->SetIncrement(2.5);
m_spin->SetValue(7.5);
CHECK( m_spin->GetTextValue() == "7.5" );
}
static inline unsigned int GetInitialDigits(double inc)
{
std::unique_ptr<wxSpinCtrlDouble> sc(new wxSpinCtrlDouble
(
wxTheApp->GetTopWindow(),
wxID_ANY,
wxEmptyString,
wxDefaultPosition,
wxDefaultSize,
wxSP_ARROW_KEYS,
0, 50, 0,
inc
));
return sc->GetDigits();
}
TEST_CASE("SpinCtrlDouble::InitialDigits", "[spinctrldouble][initialdigits]")
{
REQUIRE(GetInitialDigits(15) == 0);
REQUIRE(GetInitialDigits(10) == 0);
REQUIRE(GetInitialDigits(1) == 0);
REQUIRE(GetInitialDigits(0.999) == 1);
REQUIRE(GetInitialDigits(0.15) == 1);
REQUIRE(GetInitialDigits(0.11) == 1);
REQUIRE(GetInitialDigits(0.1) == 1);
REQUIRE(GetInitialDigits(0.0999) == 2);
REQUIRE(GetInitialDigits(0.015) == 2);
REQUIRE(GetInitialDigits(0.011) == 2);
REQUIRE(GetInitialDigits(0.01) == 2);
REQUIRE(GetInitialDigits(9.99e-5) == 5);
REQUIRE(GetInitialDigits(1e-5) == 5);
REQUIRE(GetInitialDigits(9.9999e-10) == 10);
REQUIRE(GetInitialDigits(1e-10) == 10);
REQUIRE(GetInitialDigits(9.9999e-20) == 20);
REQUIRE(GetInitialDigits(1e-20) == 20);
REQUIRE(GetInitialDigits(9.9999e-21) == 20);
REQUIRE(GetInitialDigits(1e-21) == 20);
REQUIRE(GetInitialDigits(9.9999e-22) == 20);
REQUIRE(GetInitialDigits(1e-22) == 20);
}
#endif