X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/906c935a80b10d53cecf57f71ab5f3f4f1d529ec..1ec4e9c2b72a01a7ba7da569e0f8d04a81aaba10:/tests/menu/menu.cpp?ds=sidebyside diff --git a/tests/menu/menu.cpp b/tests/menu/menu.cpp index 3424af6b50..abc4db0b0d 100644 --- a/tests/menu/menu.cpp +++ b/tests/menu/menu.cpp @@ -22,6 +22,8 @@ #endif // WX_PRECOMP #include "wx/menu.h" +#include "wx/uiaction.h" + #include // ---------------------------------------------------------------------------- @@ -82,18 +84,24 @@ private: CPPUNIT_TEST_SUITE( MenuTestCase ); CPPUNIT_TEST( FindInMenubar ); CPPUNIT_TEST( FindInMenu ); + CPPUNIT_TEST( EnableTop ); CPPUNIT_TEST( Count ); CPPUNIT_TEST( Labels ); CPPUNIT_TEST( RadioItems ); + CPPUNIT_TEST( RemoveAdd ); + WXUISIM_TEST( Events ); CPPUNIT_TEST_SUITE_END(); void CreateFrame(); void FindInMenubar(); void FindInMenu(); + void EnableTop(); void Count(); void Labels(); void RadioItems(); + void RemoveAdd(); + void Events(); wxFrame* m_frame; @@ -108,6 +116,9 @@ private: wxArrayString m_menuLabels; + // The menu containing the item with MenuTestCase_Bar id. + wxMenu* m_menuWithBar; + DECLARE_NO_COPY_CLASS(MenuTestCase) }; @@ -146,7 +157,8 @@ void MenuTestCase::CreateFrame() PopulateMenu(helpMenu, "Helpmenu item ", itemcount); - helpMenu->Append(MenuTestCase_Bar, "Bar"); + helpMenu->Append(MenuTestCase_Bar, "Bar\tF1"); + m_menuWithBar = helpMenu; helpMenu->AppendSubMenu(subMenu, "Sub&menu", "Test a submenu"); // +2 for "Foo" and "Bar", +2 for the 2 submenus @@ -256,6 +268,16 @@ void MenuTestCase::FindInMenu() } } +void MenuTestCase::EnableTop() +{ + wxMenuBar* const bar = m_frame->GetMenuBar(); + CPPUNIT_ASSERT( bar->IsEnabledTop(0) ); + bar->EnableTop( 0, false ); + CPPUNIT_ASSERT( !bar->IsEnabledTop(0) ); + bar->EnableTop( 0, true ); + CPPUNIT_ASSERT( bar->IsEnabledTop(0) ); +} + void MenuTestCase::Count() { wxMenuBar* bar = m_frame->GetMenuBar(); @@ -359,3 +381,100 @@ void MenuTestCase::RadioItems() menu->Check(MenuTestCase_First + 4, true); CPPUNIT_ASSERT( !menu->IsChecked(MenuTestCase_First + 5) ); } + +void MenuTestCase::RemoveAdd() +{ + wxMenuBar* bar = m_frame->GetMenuBar(); + + wxMenu* menu0 = bar->GetMenu(0); + wxMenu* menu1 = bar->GetMenu(1); + wxMenuItem* item = new wxMenuItem(menu0, MenuTestCase_Foo + 100, "t&ext\tCtrl-E"); + menu0->Insert(0, item); + CPPUNIT_ASSERT( menu0->FindItemByPosition(0) == item ); + menu0->Remove(item); + CPPUNIT_ASSERT( menu0->FindItemByPosition(0) != item ); + menu1->Insert(0, item); + CPPUNIT_ASSERT( menu1->FindItemByPosition(0) == item ); + menu1->Remove(item); + CPPUNIT_ASSERT( menu1->FindItemByPosition(0) != item ); + menu0->Insert(0, item); + CPPUNIT_ASSERT( menu0->FindItemByPosition(0) == item ); + menu0->Delete(item); +} + +void MenuTestCase::Events() +{ +#if wxUSE_UIACTIONSIMULATOR + class MenuEventHandler : public wxEvtHandler + { + public: + MenuEventHandler(wxWindow* win) + : m_win(win) + { + m_win->Connect(wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(MenuEventHandler::OnMenu), + NULL, + this); + + m_gotEvent = false; + m_event = NULL; + } + + virtual ~MenuEventHandler() + { + m_win->Disconnect(wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(MenuEventHandler::OnMenu), + NULL, + this); + + delete m_event; + } + + const wxCommandEvent& GetEvent() + { + CPPUNIT_ASSERT( m_gotEvent ); + + m_gotEvent = false; + + return *m_event; + } + + private: + void OnMenu(wxCommandEvent& event) + { + CPPUNIT_ASSERT( !m_gotEvent ); + + delete m_event; + m_event = static_cast(event.Clone()); + m_gotEvent = true; + } + + wxWindow* const m_win; + wxCommandEvent* m_event; + bool m_gotEvent; + }; + + MenuEventHandler handler(m_frame); + + // Invoke the accelerator. + m_frame->Show(); + m_frame->SetFocus(); + wxYield(); + + wxUIActionSimulator sim; + sim.KeyDown(WXK_F1); + sim.KeyUp(WXK_F1); + wxYield(); + + const wxCommandEvent& ev = handler.GetEvent(); + CPPUNIT_ASSERT_EQUAL( static_cast(MenuTestCase_Bar), ev.GetId() ); + + wxObject* const src = ev.GetEventObject(); + CPPUNIT_ASSERT( src ); + + CPPUNIT_ASSERT_EQUAL( "wxMenu", + wxString(src->GetClassInfo()->GetClassName()) ); + CPPUNIT_ASSERT_EQUAL( static_cast(m_menuWithBar), + src ); +#endif // wxUSE_UIACTIONSIMULATOR +}