Files
wxWidgets/tests/controls/label.cpp
T
Vadim Zeitlin 79f06970a8 Make wxTextCtrl::SetLabel() consistently do nothing everywhere
Previously it did the same thing as SetValue() in wxMSW but didn't do
anything in wxGTK (and probably other ports), which made it easy to
write code that only worked under Windows but mysteriously didn't show
any text under Linux.

Change it to not do anything anywhere and add an assert to help
detecting wrong uses of this function.

Also add a unit test checking this behaviour.

See #24982.
2024-12-02 01:16:37 +01:00

150 lines
4.8 KiB
C++

///////////////////////////////////////////////////////////////////////////////
// Name: tests/controls/label.cpp
// Purpose: wxControl and wxStaticText label tests
// Author: Francesco Montorsi
// Created: 2010-3-21
// Copyright: (c) 2010 Francesco Montorsi
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#ifndef WX_PRECOMP
#include "wx/app.h"
#endif // WX_PRECOMP
#include "wx/checkbox.h"
#include "wx/control.h"
#include "wx/stattext.h"
#include "wx/textctrl.h"
#include "wx/generic/stattextg.h"
#include <memory>
namespace
{
const char* const ORIGINAL_LABEL = "origin label";
// The actual testing function. It will change the label of the provided
// control, which is assumed to be ORIGINAL_LABEL initially.
void DoTestLabel(wxControl* c)
{
CHECK( c->GetLabel() == ORIGINAL_LABEL );
const wxString testLabelArray[] = {
"label without mnemonics and markup",
"label with &mnemonic",
"label with <span foreground='blue'>some</span> <b>markup</b>",
"label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic",
"label with an && (ampersand)",
"label with an && (&ampersand)",
"", // empty label should work too
};
for ( unsigned int s = 0; s < WXSIZEOF(testLabelArray); s++ )
{
const wxString& l = testLabelArray[s];
// GetLabel() should always return the string passed to SetLabel()
c->SetLabel(l);
CHECK( c->GetLabel() == l );
// GetLabelText() should always return unescaped version of the label
CHECK( c->GetLabelText() == wxControl::RemoveMnemonics(l) );
// GetLabelText() should always return the string passed to SetLabelText()
c->SetLabelText(l);
CHECK( c->GetLabelText() == l );
// And GetLabel() should be the escaped version of the text
CHECK( l == wxControl::RemoveMnemonics(c->GetLabel()) );
}
// Check that both "&" and "&amp;" work in markup.
#if wxUSE_MARKUP
c->SetLabelMarkup("mnemonic in &amp;markup");
CHECK( c->GetLabel() == "mnemonic in &markup" );
CHECK( c->GetLabelText() == "mnemonic in markup" );
c->SetLabelMarkup("mnemonic in &markup");
CHECK( c->GetLabel() == "mnemonic in &markup" );
CHECK( c->GetLabelText() == "mnemonic in markup" );
c->SetLabelMarkup("&amp;&amp; finally");
CHECK( c->GetLabel() == "&& finally" );
CHECK( c->GetLabelText() == "& finally" );
c->SetLabelMarkup("&& finally");
CHECK( c->GetLabel() == "&& finally" );
CHECK( c->GetLabelText() == "& finally" );
#endif // wxUSE_MARKUP
}
} // anonymous namespace
TEST_CASE("wxControl::Label", "[wxControl][label]")
{
SECTION("wxStaticText")
{
const std::unique_ptr<wxStaticText>
st(new wxStaticText(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL));
DoTestLabel(st.get());
}
SECTION("wxStaticText/ellipsized")
{
const std::unique_ptr<wxStaticText>
st(new wxStaticText(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL,
wxDefaultPosition, wxDefaultSize,
wxST_ELLIPSIZE_START));
DoTestLabel(st.get());
}
SECTION("wxGenericStaticText")
{
const std::unique_ptr<wxGenericStaticText>
gst(new wxGenericStaticText(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL));
DoTestLabel(gst.get());
}
SECTION("wxCheckBox")
{
const std::unique_ptr<wxCheckBox>
cb(new wxCheckBox(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL));
DoTestLabel(cb.get());
}
SECTION("wxTextCtrl")
{
const std::unique_ptr<wxTextCtrl>
tc(new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL));
// Setting the label of a wxTextCtrl should _not_ work, it has value
// and not a label.
WX_ASSERT_FAILS_WITH_ASSERT( tc->SetLabel("something else") );
CHECK( tc->GetValue() == ORIGINAL_LABEL );
}
}
TEST_CASE("wxControl::RemoveMnemonics", "[wxControl][label][mnemonics]")
{
CHECK( "mnemonic" == wxControl::RemoveMnemonics("&mnemonic") );
CHECK( "&mnemonic" == wxControl::RemoveMnemonics("&&mnemonic") );
CHECK( "&mnemonic" == wxControl::RemoveMnemonics("&&&mnemonic") );
}
TEST_CASE("wxControl::FindAccelIndex", "[wxControl][label][mnemonics]")
{
CHECK( wxControl::FindAccelIndex("foo") == wxNOT_FOUND );
CHECK( wxControl::FindAccelIndex("&foo") == 0 );
CHECK( wxControl::FindAccelIndex("f&oo") == 1 );
CHECK( wxControl::FindAccelIndex("foo && bar") == wxNOT_FOUND );
CHECK( wxControl::FindAccelIndex("foo && &bar") == 6 );
}