Provide more information for failures involving FindFocus()

It is useful to know where the focus actually is when a test checking
that it's set to a particular window fails.
This commit is contained in:
Vadim Zeitlin
2019-07-17 15:09:34 +02:00
parent ee0f21388f
commit e3caafabc3
5 changed files with 77 additions and 9 deletions

View File

@@ -25,6 +25,7 @@
#include "wx/uiaction.h"
#include "testableframe.h"
#include "testwindow.h"
class RadioButtonTestCase : public CppUnit::TestCase
{
@@ -229,7 +230,7 @@ TEST_CASE("wxRadioButton::Focus", "[radiobutton][focus]")
// Initially the first radio button should be checked.
radio1->SetFocus();
CHECK(radio1->GetValue());
CHECK(wxWindow::FindFocus() == radio1);
CHECK_FOCUS_IS(radio1);
// Switching focus from it shouldn't change this.
dummyButton->SetFocus();
@@ -242,12 +243,12 @@ TEST_CASE("wxRadioButton::Focus", "[radiobutton][focus]")
CHECK(radio2->GetValue());
// While not changing focus.
CHECK(wxWindow::FindFocus() == dummyButton);
CHECK_FOCUS_IS(dummyButton);
// And giving the focus to the panel shouldn't change radio button
// selection.
radioPanel->SetFocus();
CHECK(wxWindow::FindFocus() == radio2);
CHECK_FOCUS_IS(radio2);
CHECK(!radio1->GetValue());
CHECK(radio2->GetValue());
}

View File

@@ -20,6 +20,8 @@
#include "wx/srchctrl.h"
#include "testwindow.h"
class SearchCtrlTestCase
{
public:
@@ -45,7 +47,7 @@ protected:
SEARCH_CTRL_TEST_CASE("wxSearchCtrl::Focus", "[wxSearchCtrl][focus]")
{
m_search->SetFocus();
CHECK( m_search->HasFocus() );
CHECK_FOCUS_IS( m_search );
}
#endif // !__WXOSX__

View File

@@ -24,6 +24,8 @@
#include "wx/stc/stc.h"
#include "wx/uiaction.h"
#include "testwindow.h"
#if defined(__WXOSX_COCOA__) || defined(__WXMSW__) || defined(__WXGTK__)
class StcPopupWindowsTestCase
@@ -99,7 +101,7 @@ TEST_CASE_METHOD(StcPopupWindowsTestCase,
if ( m_stc->AutoCompActive() )
m_stc->AutoCompCancel();
CHECK( m_stc->HasFocus() );
CHECK_FOCUS_IS( m_stc );
CHECK( m_focusAlwaysRetained );
}
@@ -143,7 +145,7 @@ TEST_CASE_METHOD(StcPopupWindowsTestCase,
m_stc->CallTipCancel();
// Verify that clicking the call tip did not take focus from the STC.
CHECK( m_stc->HasFocus() );
CHECK_FOCUS_IS( m_stc );
CHECK( m_focusAlwaysRetained );
}

View File

@@ -21,6 +21,8 @@
#include "asserthelper.h"
#include "testableframe.h"
#include "testwindow.h"
#include "wx/uiaction.h"
#include "wx/caret.h"
#include "wx/cshelp.h"
@@ -153,7 +155,7 @@ void WindowTestCase::FocusEvent()
m_window->SetFocus();
CPPUNIT_ASSERT(setfocus.WaitEvent(500));
CPPUNIT_ASSERT(m_window->HasFocus());
CHECK_FOCUS_IS( m_window );
wxButton* button = new wxButton(wxTheApp->GetTopWindow(), wxID_ANY);
@@ -298,7 +300,7 @@ void WindowTestCase::Focus()
if ( m_window->AcceptsFocus() )
{
m_window->SetFocus();
CPPUNIT_ASSERT(m_window->HasFocus());
CHECK_FOCUS_IS(m_window);
}
//Set the focus back to the main window
@@ -307,7 +309,7 @@ void WindowTestCase::Focus()
if ( m_window->AcceptsFocusFromKeyboard() )
{
m_window->SetFocusFromKbd();
CPPUNIT_ASSERT(m_window->HasFocus());
CHECK_FOCUS_IS(m_window);
}
#endif
}

61
tests/testwindow.h Normal file
View File

@@ -0,0 +1,61 @@
///////////////////////////////////////////////////////////////////////////////
// Name: tests/testwindow.h
// Purpose: Unit test helper for comparing wxWindow pointers.
// Author: Vadim Zeitlin
// Copyright: (c) 2019 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_TESTS_TESTWINDOW_H_
#define _WX_TESTS_TESTWINDOW_H_
#include "wx/window.h"
// We need to wrap wxWindow* in a class as specializing StringMaker for
// wxWindow* doesn't seem to work.
class wxWindowPtr
{
public:
explicit wxWindowPtr(wxWindow* win) : m_win(win) {}
wxString Dump() const
{
if ( !m_win )
return "(no window)";
wxString s = m_win->GetClassInfo()->GetClassName();
const wxString& label = m_win->GetLabel();
if ( !label.empty() )
{
s += wxString::Format(" (label=\"%s\")", label);
}
return s;
}
private:
friend bool operator==(wxWindowPtr wp1, wxWindowPtr wp2)
{
return wp1.m_win == wp2.m_win;
}
wxWindow* const m_win;
};
// Macro providing more information about the current focus if comparison
// fails.
#define CHECK_FOCUS_IS(w) CHECK(wxWindowPtr(wxWindow::FindFocus()) == wxWindowPtr(w))
namespace Catch
{
template <>
struct StringMaker<wxWindowPtr>
{
static std::string convert(const wxWindowPtr window)
{
return window.Dump().ToStdString();
}
};
}
#endif // _WX_TESTS_TESTWINDOW_H_