From 758bd6fa6ea7400c6fc472ff456cfb16b3e1c958 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 29 Sep 2019 23:41:20 +0200 Subject: [PATCH 1/4] Create test frame in menu test in more predetermined position Create this frame as child of the main application window to make it appear near it, instead of in a more or less random location. This facilitates debugging, but doesn't really change anything otherwise. --- tests/menu/menu.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/menu/menu.cpp b/tests/menu/menu.cpp index 2a76f94c38..eb52235b22 100644 --- a/tests/menu/menu.cpp +++ b/tests/menu/menu.cpp @@ -138,7 +138,7 @@ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MenuTestCase, "MenuTestCase" ); void MenuTestCase::CreateFrame() { - m_frame = new wxFrame(NULL, wxID_ANY, "test frame"); + m_frame = new wxFrame(wxTheApp->GetTopWindow(), wxID_ANY, "test frame"); wxMenu *fileMenu = new wxMenu; wxMenu *helpMenu = new wxMenu; From bd5b3725b97f98efa34ee7143329f1adad348eef Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 29 Sep 2019 23:42:56 +0200 Subject: [PATCH 2/4] Simplify menu items counting code in the unit test Get rid of an extra variable and just update m_itemCount on the go. No real changes, but this'll make adding more test menu items simpler. --- tests/menu/menu.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tests/menu/menu.cpp b/tests/menu/menu.cpp index eb52235b22..ce33963e3a 100644 --- a/tests/menu/menu.cpp +++ b/tests/menu/menu.cpp @@ -145,36 +145,37 @@ void MenuTestCase::CreateFrame() wxMenu *subMenu = new wxMenu; wxMenu *subsubMenu = new wxMenu; - size_t itemcount = 0; + m_itemCount = 0; - PopulateMenu(subsubMenu, "Subsubmenu item ", itemcount); + PopulateMenu(subsubMenu, "Subsubmenu item ", m_itemCount); // Store one of its IDs for later - m_subsubmenuItemId = MenuTestCase_First + itemcount - 2; + m_subsubmenuItemId = MenuTestCase_First + m_itemCount - 2; - PopulateMenu(subMenu, "Submenu item ", itemcount); + PopulateMenu(subMenu, "Submenu item ", m_itemCount); // Store one of its IDs for later - m_submenuItemId = MenuTestCase_First + itemcount - 2; + m_submenuItemId = MenuTestCase_First + m_itemCount - 2; subMenu->AppendSubMenu(subsubMenu, "Subsubmen&u", "Test a subsubmenu"); + m_itemCount++; // Check GetTitle() returns the correct string _before_ appending to the bar fileMenu->SetTitle("&Foo\tCtrl-F"); CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", fileMenu->GetTitle() ); - PopulateMenu(fileMenu, "Filemenu item ", itemcount); + PopulateMenu(fileMenu, "Filemenu item ", m_itemCount); fileMenu->Append(MenuTestCase_Foo, "&Foo\tCtrl-F", "Test item to be found"); + m_itemCount++; - PopulateMenu(helpMenu, "Helpmenu item ", itemcount); + PopulateMenu(helpMenu, "Helpmenu item ", m_itemCount); helpMenu->Append(MenuTestCase_Bar, "Bar\tF1"); + m_itemCount++; m_menuWithBar = helpMenu; helpMenu->AppendSubMenu(subMenu, "Sub&menu", "Test a submenu"); - - // +2 for "Foo" and "Bar", +2 for the 2 submenus - m_itemCount = itemcount + 4; + m_itemCount++; // Use an arraystring here, to help with future tests m_menuLabels.Add("&File"); From 0aaa05ae3adcf50f27049190423827ff1103aaaf Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 29 Sep 2019 23:45:55 +0200 Subject: [PATCH 3/4] Override Ctrl-A accelerator when wxTextCtrl has focus in wxMSW This key combination is used for selecting all text, while it's also relatively common to use it as an accelerator for some menu item. Resolve the conflict in favour of wxTextCtrl, i.e. let it have this key when it has focus, while still allowing to use it as an accelerator otherwise. --- src/msw/textctrl.cpp | 1 + tests/menu/menu.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/msw/textctrl.cpp b/src/msw/textctrl.cpp index 9118db73bd..2196918b93 100644 --- a/src/msw/textctrl.cpp +++ b/src/msw/textctrl.cpp @@ -2048,6 +2048,7 @@ bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG* msg) { switch ( vkey ) { + case 'A': case 'C': case 'V': case 'X': diff --git a/tests/menu/menu.cpp b/tests/menu/menu.cpp index ce33963e3a..99531c5622 100644 --- a/tests/menu/menu.cpp +++ b/tests/menu/menu.cpp @@ -36,6 +36,7 @@ namespace enum { MenuTestCase_Foo = 10000, + MenuTestCase_SelectAll, MenuTestCase_Bar, MenuTestCase_First }; @@ -168,6 +169,9 @@ void MenuTestCase::CreateFrame() fileMenu->Append(MenuTestCase_Foo, "&Foo\tCtrl-F", "Test item to be found"); m_itemCount++; + fileMenu->Append(MenuTestCase_SelectAll, "Select &all\tCtrl-A", + "Accelerator conflicting with wxTextCtrl"); + m_itemCount++; PopulateMenu(helpMenu, "Helpmenu item ", m_itemCount); @@ -548,6 +552,11 @@ public: return *m_event; } + bool GotEvent() const + { + return m_gotEvent; + } + private: void OnMenu(wxCommandEvent& event) { @@ -607,6 +616,24 @@ void MenuTestCase::Events() wxString(src->GetClassInfo()->GetClassName()) ); CPPUNIT_ASSERT_EQUAL( static_cast(m_menuWithBar), src ); + + // Invoke another accelerator, it should also work. + sim.Char('A', wxMOD_CONTROL); + wxYield(); + + const wxCommandEvent& ev2 = handler.GetEvent(); + CHECK( ev2.GetId() == MenuTestCase_SelectAll ); + + // Now create a text control which uses the same accelerator for itself and + // check that when the text control has focus, the accelerator does _not_ + // work. + wxTextCtrl* const text = new wxTextCtrl(m_frame, wxID_ANY, "Testing"); + text->SetFocus(); + + sim.Char('A', wxMOD_CONTROL); + wxYield(); + + CHECK( !handler.GotEvent() ); #endif // wxUSE_UIACTIONSIMULATOR } From 8db55e965395aeb40017b39b17167d50f78af12d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 30 Sep 2019 11:27:42 +0200 Subject: [PATCH 4/4] Explicitly cast enum to int inside CHECK() Somehow using enum results in wrong comparison being done when using gcc 5.3 (the test run when using this compiler fails because the RHS value is "true" and not the numeric value of the enum element), so add a cast to make this work. --- tests/menu/menu.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/menu/menu.cpp b/tests/menu/menu.cpp index 99531c5622..b2723baea3 100644 --- a/tests/menu/menu.cpp +++ b/tests/menu/menu.cpp @@ -622,7 +622,7 @@ void MenuTestCase::Events() wxYield(); const wxCommandEvent& ev2 = handler.GetEvent(); - CHECK( ev2.GetId() == MenuTestCase_SelectAll ); + CHECK( ev2.GetId() == static_cast(MenuTestCase_SelectAll) ); // Now create a text control which uses the same accelerator for itself and // check that when the text control has focus, the accelerator does _not_