X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f8314f3c20b1c426825481acc1ec3a42106455bf..32753ae949d861ddaf7c66f4c483bc57493c6c6c:/tests/menu/menu.cpp?ds=inline diff --git a/tests/menu/menu.cpp b/tests/menu/menu.cpp index 961651c518..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,14 +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; @@ -104,13 +116,16 @@ private: wxArrayString m_menuLabels; + // The menu containing the item with MenuTestCase_Bar id. + wxMenu* m_menuWithBar; + DECLARE_NO_COPY_CLASS(MenuTestCase) }; // register in the unnamed registry so that these tests are run by default CPPUNIT_TEST_SUITE_REGISTRATION( MenuTestCase ); -// also include in it's own registry so that these tests can be run alone +// also include in its own registry so that these tests can be run alone CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MenuTestCase, "MenuTestCase" ); void MenuTestCase::CreateFrame() @@ -142,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 @@ -170,10 +186,13 @@ void MenuTestCase::FindInMenubar() // Find by menu name plus item name: CPPUNIT_ASSERT( bar->FindMenuItem("File", "Foo") != wxNOT_FOUND ); CPPUNIT_ASSERT( bar->FindMenuItem("&File", "&Foo") != wxNOT_FOUND ); - // and using the menu title + // and using the menu label int index = bar->FindMenu("&File"); CPPUNIT_ASSERT( index != wxNOT_FOUND ); - wxString menutitle = bar->GetMenuLabel(index); + wxString menulabel = bar->GetMenuLabel(index); + CPPUNIT_ASSERT( bar->FindMenuItem(menulabel, "&Foo") != wxNOT_FOUND ); + // and title + wxString menutitle = bar->GetMenu(index)->GetTitle(); CPPUNIT_ASSERT( bar->FindMenuItem(menutitle, "&Foo") != wxNOT_FOUND ); // Find by position: @@ -195,7 +214,7 @@ void MenuTestCase::FindInMenubar() item = bar->FindItem(m_submenuItemId, &menu); CPPUNIT_ASSERT( item ); CPPUNIT_ASSERT( menu ); - // and, for completeness, a submenu one: + // and, for completeness, a subsubmenu one: item = bar->FindItem(m_subsubmenuItemId, &menu); CPPUNIT_ASSERT( item ); CPPUNIT_ASSERT( menu ); @@ -215,7 +234,8 @@ void MenuTestCase::FindInMenu() CPPUNIT_ASSERT( menuHelp->FindItem("Sub&menu") != wxNOT_FOUND ); // Find by position: - for (size_t n=0; n < menuHelp->GetMenuItemCount(); ++n) + size_t n; + for (n=0; n < menuHelp->GetMenuItemCount(); ++n) { CPPUNIT_ASSERT( menuHelp->FindItemByPosition(n) ); } @@ -224,7 +244,7 @@ void MenuTestCase::FindInMenu() CPPUNIT_ASSERT( menuHelp->FindItem(MenuTestCase_Bar) ); CPPUNIT_ASSERT( menuHelp->FindItem(MenuTestCase_Foo) == NULL ); - for (size_t n=0; n < menuHelp->GetMenuItemCount(); ++n) + for (n=0; n < menuHelp->GetMenuItemCount(); ++n) { size_t locatedAt; wxMenuItem* itemByPos = menuHelp->FindItemByPosition(n); @@ -235,7 +255,7 @@ void MenuTestCase::FindInMenu() } // Find submenu item: - for (size_t n=0; n < menuHelp->GetMenuItemCount(); ++n) + for (n=0; n < menuHelp->GetMenuItemCount(); ++n) { wxMenuItem* item = menuHelp->FindItemByPosition(n); if (item->IsSubMenu()) @@ -248,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(); @@ -263,3 +293,188 @@ void MenuTestCase::Count() } CPPUNIT_ASSERT_EQUAL( count, m_itemCount ); } + +void MenuTestCase::Labels() +{ + wxMenuBar* bar = m_frame->GetMenuBar(); + CPPUNIT_ASSERT( bar ); + wxMenu* filemenu; + wxMenuItem* itemFoo = bar->FindItem(MenuTestCase_Foo, &filemenu); + CPPUNIT_ASSERT( itemFoo ); + CPPUNIT_ASSERT( filemenu ); + + // These return labels including mnemonics/accelerators: + + // wxMenuBar + CPPUNIT_ASSERT_EQUAL( "&File", bar->GetMenuLabel(0) ); + CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", bar->GetLabel(MenuTestCase_Foo) ); + + // wxMenu + CPPUNIT_ASSERT_EQUAL( "&File", filemenu->GetTitle() ); + CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", filemenu->GetLabel(MenuTestCase_Foo) ); + + // wxMenuItem + CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", itemFoo->GetItemLabel() ); + + // These return labels stripped of mnemonics/accelerators: + + // wxMenuBar + CPPUNIT_ASSERT_EQUAL( "File", bar->GetMenuLabelText(0) ); + + // wxMenu + CPPUNIT_ASSERT_EQUAL( "Foo", filemenu->GetLabelText(MenuTestCase_Foo) ); + + // wxMenuItem + CPPUNIT_ASSERT_EQUAL( "Foo", itemFoo->GetItemLabelText() ); + CPPUNIT_ASSERT_EQUAL( "Foo", wxMenuItem::GetLabelText("&Foo\tCtrl-F") ); +} + +void MenuTestCase::RadioItems() +{ + wxMenuBar * const bar = m_frame->GetMenuBar(); + wxMenu * const menu = new wxMenu; + bar->Append(menu, "&Radio"); + + // Adding consecutive radio items creates a radio group. + menu->AppendRadioItem(MenuTestCase_First, "Radio 0"); + menu->AppendRadioItem(MenuTestCase_First + 1, "Radio 1"); + + // First item of a radio group is checked by default. + CPPUNIT_ASSERT( menu->IsChecked(MenuTestCase_First) ); + + // Checking the second one make the first one unchecked however. + menu->Check(MenuTestCase_First + 1, true); + CPPUNIT_ASSERT( !menu->IsChecked(MenuTestCase_First) ); + CPPUNIT_ASSERT( menu->IsChecked(MenuTestCase_First + 1) ); + + // Adding more radio items after a separator creates another radio group... + menu->AppendSeparator(); + menu->AppendRadioItem(MenuTestCase_First + 2, "Radio 2"); + menu->AppendRadioItem(MenuTestCase_First + 3, "Radio 3"); + menu->AppendRadioItem(MenuTestCase_First + 4, "Radio 4"); + + // ... which is independent from the first one. + CPPUNIT_ASSERT( menu->IsChecked(MenuTestCase_First + 2) ); + + menu->Check(MenuTestCase_First + 3, true); + CPPUNIT_ASSERT( menu->IsChecked(MenuTestCase_First + 3) ); + CPPUNIT_ASSERT( !menu->IsChecked(MenuTestCase_First + 2) ); + CPPUNIT_ASSERT( menu->IsChecked(MenuTestCase_First + 1) ); + + + // Insert an item in the middle of an existing radio group. + menu->InsertRadioItem(4, MenuTestCase_First + 5, "Radio 5"); + CPPUNIT_ASSERT( menu->IsChecked(MenuTestCase_First + 3) ); + + menu->Check( MenuTestCase_First + 5, true ); + CPPUNIT_ASSERT( !menu->IsChecked(MenuTestCase_First + 3) ); + + + // Prepend a couple of items before the first group. + menu->PrependRadioItem(MenuTestCase_First + 6, "Radio 6"); + menu->PrependRadioItem(MenuTestCase_First + 7, "Radio 7"); + menu->Check(MenuTestCase_First + 7, true); + CPPUNIT_ASSERT( !menu->IsChecked(MenuTestCase_First + 1) ); + + + // Check that the last radio group still works as expected. + 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 +}