2010-08-22 18:16:05 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/controls/buttontest.cpp
|
|
|
|
// Purpose: wxButton unit test
|
|
|
|
// Author: Steven Lamerton
|
|
|
|
// Created: 2010-06-21
|
|
|
|
// Copyright: (c) 2010 Steven Lamerton
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
#if wxUSE_BUTTON
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/app.h"
|
|
|
|
#include "wx/button.h"
|
|
|
|
#endif // WX_PRECOMP
|
|
|
|
|
|
|
|
#include "testableframe.h"
|
|
|
|
#include "wx/uiaction.h"
|
|
|
|
#include "wx/artprov.h"
|
2021-07-22 13:15:07 -04:00
|
|
|
|
|
|
|
// Get operator<<(wxSize) so that wxSize values are shown correctly in case of
|
|
|
|
// a failure of a CHECK() involving them.
|
2010-08-22 18:16:05 -04:00
|
|
|
#include "asserthelper.h"
|
|
|
|
|
2021-07-22 13:15:07 -04:00
|
|
|
class ButtonTestCase
|
2010-08-22 18:16:05 -04:00
|
|
|
{
|
|
|
|
public:
|
2021-07-22 13:15:07 -04:00
|
|
|
ButtonTestCase();
|
|
|
|
~ButtonTestCase();
|
2010-08-22 18:16:05 -04:00
|
|
|
|
2021-07-22 13:15:07 -04:00
|
|
|
protected:
|
2010-08-22 18:16:05 -04:00
|
|
|
wxButton* m_button;
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxDECLARE_NO_COPY_CLASS(ButtonTestCase);
|
2010-08-22 18:16:05 -04:00
|
|
|
};
|
|
|
|
|
2021-07-22 13:15:07 -04:00
|
|
|
ButtonTestCase::ButtonTestCase()
|
2010-08-22 18:16:05 -04:00
|
|
|
{
|
|
|
|
//We use wxTheApp->GetTopWindow() as there is only a single testable frame
|
|
|
|
//so it will always be returned
|
|
|
|
m_button = new wxButton(wxTheApp->GetTopWindow(), wxID_ANY, "wxButton");
|
|
|
|
}
|
|
|
|
|
2021-07-22 13:15:07 -04:00
|
|
|
ButtonTestCase::~ButtonTestCase()
|
2010-08-22 18:16:05 -04:00
|
|
|
{
|
2021-07-22 13:15:07 -04:00
|
|
|
delete m_button;
|
2010-08-22 18:16:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#if wxUSE_UIACTIONSIMULATOR
|
|
|
|
|
2021-07-22 13:15:07 -04:00
|
|
|
TEST_CASE_METHOD(ButtonTestCase, "Button::Click", "[button]")
|
2010-08-22 18:16:05 -04:00
|
|
|
{
|
|
|
|
//We use the internal class EventCounter which handles connecting and
|
|
|
|
//disconnecting the control to the wxTestableFrame
|
2013-04-25 06:11:03 -04:00
|
|
|
EventCounter clicked(m_button, wxEVT_BUTTON);
|
2010-08-22 18:16:05 -04:00
|
|
|
|
|
|
|
wxUIActionSimulator sim;
|
|
|
|
|
|
|
|
//We move in slightly to account for window decorations, we need to yield
|
|
|
|
//after every wxUIActionSimulator action to keep everything working in GTK
|
|
|
|
sim.MouseMove(m_button->GetScreenPosition() + wxPoint(10, 10));
|
|
|
|
wxYield();
|
|
|
|
|
|
|
|
sim.MouseClick();
|
|
|
|
wxYield();
|
|
|
|
|
2021-07-22 13:15:07 -04:00
|
|
|
CHECK( clicked.GetCount() == 1 );
|
2010-08-22 18:16:05 -04:00
|
|
|
}
|
|
|
|
|
2021-07-22 13:15:07 -04:00
|
|
|
TEST_CASE_METHOD(ButtonTestCase, "Button::Disabled", "[button]")
|
2010-08-22 18:16:05 -04:00
|
|
|
{
|
|
|
|
wxUIActionSimulator sim;
|
|
|
|
|
2018-12-08 19:01:49 -05:00
|
|
|
// In this test we disable the button and check events are not sent and we
|
|
|
|
// do it once by disabling the previously enabled button and once by
|
|
|
|
// creating the button in the disabled state.
|
|
|
|
SECTION("Disable after creation")
|
|
|
|
{
|
|
|
|
m_button->Disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Create disabled")
|
|
|
|
{
|
|
|
|
delete m_button;
|
|
|
|
m_button = new wxButton();
|
|
|
|
m_button->Disable();
|
|
|
|
m_button->Create(wxTheApp->GetTopWindow(), wxID_ANY, "wxButton");
|
|
|
|
}
|
2010-08-22 18:16:05 -04:00
|
|
|
|
2018-12-12 18:39:25 -05:00
|
|
|
EventCounter clicked(m_button, wxEVT_BUTTON);
|
|
|
|
|
2010-08-22 18:16:05 -04:00
|
|
|
sim.MouseMove(m_button->GetScreenPosition() + wxPoint(10, 10));
|
|
|
|
wxYield();
|
|
|
|
|
|
|
|
sim.MouseClick();
|
|
|
|
wxYield();
|
|
|
|
|
2021-07-22 13:15:07 -04:00
|
|
|
CHECK( clicked.GetCount() == 0 );
|
2010-08-22 18:16:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // wxUSE_UIACTIONSIMULATOR
|
|
|
|
|
2021-07-22 13:15:07 -04:00
|
|
|
TEST_CASE_METHOD(ButtonTestCase, "Button::Auth", "[button]")
|
2010-08-22 18:16:05 -04:00
|
|
|
{
|
|
|
|
//Some functions only work on specific operating system versions, for
|
|
|
|
//this we need a runtime check
|
|
|
|
int major = 0;
|
|
|
|
|
|
|
|
if(wxGetOsVersion(&major) != wxOS_WINDOWS_NT || major < 6)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//We are running Windows Vista or newer
|
2021-07-22 13:15:07 -04:00
|
|
|
CHECK(!m_button->GetAuthNeeded());
|
2010-08-22 18:16:05 -04:00
|
|
|
|
|
|
|
m_button->SetAuthNeeded();
|
|
|
|
|
2021-07-22 13:15:07 -04:00
|
|
|
CHECK(m_button->GetAuthNeeded());
|
2010-08-22 18:16:05 -04:00
|
|
|
|
|
|
|
//We test both states
|
|
|
|
m_button->SetAuthNeeded(false);
|
|
|
|
|
2021-07-22 13:15:07 -04:00
|
|
|
CHECK(!m_button->GetAuthNeeded());
|
2010-08-22 18:16:05 -04:00
|
|
|
}
|
|
|
|
|
2021-07-22 13:15:07 -04:00
|
|
|
TEST_CASE_METHOD(ButtonTestCase, "Button::BitmapMargins", "[button]")
|
2010-08-22 18:16:05 -04:00
|
|
|
{
|
2011-04-03 16:31:32 -04:00
|
|
|
//Some functions only work on specific platforms in which case we can use
|
2010-08-22 18:16:05 -04:00
|
|
|
//a preprocessor check
|
|
|
|
#ifdef __WXMSW__
|
|
|
|
//We must set a bitmap before we can set its margins, when writing unit
|
|
|
|
//tests it is easiest to use an image from wxArtProvider
|
|
|
|
m_button->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION, wxART_OTHER,
|
|
|
|
wxSize(32, 32)));
|
|
|
|
|
|
|
|
m_button->SetBitmapMargins(15, 15);
|
|
|
|
|
2021-07-22 13:15:07 -04:00
|
|
|
CHECK( m_button->GetBitmapMargins() == wxSize(15, 15) );
|
2010-08-22 18:16:05 -04:00
|
|
|
|
|
|
|
m_button->SetBitmapMargins(wxSize(20, 20));
|
|
|
|
|
2021-07-22 13:15:07 -04:00
|
|
|
CHECK( m_button->GetBitmapMargins() == wxSize(20, 20) );
|
2010-08-22 18:16:05 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-07-22 13:15:07 -04:00
|
|
|
TEST_CASE_METHOD(ButtonTestCase, "Button::Bitmap", "[button]")
|
2010-08-22 18:16:05 -04:00
|
|
|
{
|
|
|
|
//We start with no bitmaps
|
2021-07-22 13:15:07 -04:00
|
|
|
CHECK(!m_button->GetBitmap().IsOk());
|
2010-08-22 18:16:05 -04:00
|
|
|
|
2020-08-21 12:05:56 -04:00
|
|
|
// Some bitmap, doesn't really matter which.
|
|
|
|
const wxBitmap bmp = wxArtProvider::GetBitmap(wxART_INFORMATION);
|
2010-09-05 09:31:13 -04:00
|
|
|
|
2020-08-21 12:05:56 -04:00
|
|
|
m_button->SetBitmap(bmp);
|
2010-08-22 18:16:05 -04:00
|
|
|
|
2021-07-22 13:15:07 -04:00
|
|
|
CHECK(m_button->GetBitmap().IsOk());
|
2020-08-21 12:05:56 -04:00
|
|
|
|
2021-09-03 15:12:19 -04:00
|
|
|
// The call above shouldn't affect any other bitmaps as returned by the API
|
|
|
|
// even though the same (normal) bitmap does appear for all the states.
|
|
|
|
CHECK( !m_button->GetBitmapCurrent().IsOk() );
|
|
|
|
CHECK( !m_button->GetBitmapDisabled().IsOk() );
|
|
|
|
CHECK( !m_button->GetBitmapFocus().IsOk() );
|
|
|
|
CHECK( !m_button->GetBitmapPressed().IsOk() );
|
|
|
|
|
|
|
|
// Do set one of the bitmaps now.
|
|
|
|
m_button->SetBitmapPressed(wxArtProvider::GetBitmap(wxART_ERROR));
|
|
|
|
CHECK( m_button->GetBitmapPressed().IsOk() );
|
|
|
|
|
2020-08-21 12:05:56 -04:00
|
|
|
// Check that resetting the button label doesn't result in problems when
|
|
|
|
// updating the bitmap later, as it used to be the case in wxGTK (#18898).
|
|
|
|
m_button->SetLabel(wxString());
|
|
|
|
CHECK_NOTHROW( m_button->Disable() );
|
2021-09-02 18:28:58 -04:00
|
|
|
|
|
|
|
// Also check that setting an invalid bitmap doesn't do anything untoward,
|
|
|
|
// such as crashing, as it used to do in wxOSX (#19257).
|
|
|
|
CHECK_NOTHROW( m_button->SetBitmapPressed(wxNullBitmap) );
|
|
|
|
CHECK( !m_button->GetBitmapPressed().IsOk() );
|
2010-08-22 18:16:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //wxUSE_BUTTON
|