diff --git a/tests/controls/radiobuttontest.cpp b/tests/controls/radiobuttontest.cpp index 535cc17e25..88a53d9438 100644 --- a/tests/controls/radiobuttontest.cpp +++ b/tests/controls/radiobuttontest.cpp @@ -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()); } diff --git a/tests/controls/searchctrltest.cpp b/tests/controls/searchctrltest.cpp index 31dc748322..5620363172 100644 --- a/tests/controls/searchctrltest.cpp +++ b/tests/controls/searchctrltest.cpp @@ -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__ diff --git a/tests/controls/styledtextctrltest.cpp b/tests/controls/styledtextctrltest.cpp index 63c8610cb6..057b120fc5 100644 --- a/tests/controls/styledtextctrltest.cpp +++ b/tests/controls/styledtextctrltest.cpp @@ -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 ); } diff --git a/tests/controls/windowtest.cpp b/tests/controls/windowtest.cpp index 02541ef08f..1ee147b377 100644 --- a/tests/controls/windowtest.cpp +++ b/tests/controls/windowtest.cpp @@ -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 } diff --git a/tests/testwindow.h b/tests/testwindow.h new file mode 100644 index 0000000000..64b455b261 --- /dev/null +++ b/tests/testwindow.h @@ -0,0 +1,61 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/testwindow.h +// Purpose: Unit test helper for comparing wxWindow pointers. +// Author: Vadim Zeitlin +// Copyright: (c) 2019 Vadim Zeitlin +// 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 + { + static std::string convert(const wxWindowPtr window) + { + return window.Dump().ToStdString(); + } + }; +} + +#endif // _WX_TESTS_TESTWINDOW_H_