wxWidgets/tests/menu/menu.cpp
Vadim Zeitlin f8314f3c20 Don't use standard menu ids in the unit test to avoid Mac problems.
wxOSX rearranges the standard menu items such as wxID_EXIT and wxID_ABOUT and,
for the former, changes its text to "Quit", so don't use them in the menu unit
test which expects to find the items in the menus to which they were added and
exactly with the labels used when adding them.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66144 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2010-11-13 17:13:29 +00:00

266 lines
7.5 KiB
C++

///////////////////////////////////////////////////////////////////////////////
// Name: tests/menu/menu.cpp
// Purpose: wxMenu unit test
// Author: wxWidgets team
// Created: 2010-11-10
// RCS-ID: $Id$
// Copyright: (c) 2010 wxWidgets team
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif // WX_PRECOMP
#include "wx/menu.h"
#include <stdarg.h>
// ----------------------------------------------------------------------------
// helper
// ----------------------------------------------------------------------------
namespace
{
enum
{
MenuTestCase_Foo = 10000,
MenuTestCase_Bar,
MenuTestCase_First
};
void PopulateMenu(wxMenu* menu, const wxString& name, size_t& itemcount)
{
// Start at item 1 to make it human-readable ;)
for (int n=1; n<6; ++n, ++itemcount)
{
wxString label = name; label << n;
menu->Append(MenuTestCase_First + itemcount, label, label + " help string");
}
}
void RecursivelyCountMenuItems(const wxMenu* menu, size_t& count)
{
CPPUNIT_ASSERT( menu );
count += menu->GetMenuItemCount();
for (size_t n=0; n < menu->GetMenuItemCount(); ++n)
{
wxMenuItem* item = menu->FindItemByPosition(n);
if (item->IsSubMenu())
{
RecursivelyCountMenuItems(item->GetSubMenu(), count);
}
}
}
} // anon namespace
// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------
class MenuTestCase : public CppUnit::TestCase
{
public:
MenuTestCase() {}
virtual void setUp() { CreateFrame(); }
virtual void tearDown() { m_frame->Destroy(); }
private:
CPPUNIT_TEST_SUITE( MenuTestCase );
CPPUNIT_TEST( FindInMenubar );
CPPUNIT_TEST( FindInMenu );
CPPUNIT_TEST( Count );
CPPUNIT_TEST_SUITE_END();
void CreateFrame();
void FindInMenubar();
void FindInMenu();
void Count();
wxFrame* m_frame;
// Holds the number of menuitems contained in all the menus
size_t m_itemCount;
// Store here the id of a known submenu item, to be searched for later
int m_submenuItemId;
// and a sub-submenu item
int m_subsubmenuItemId;
wxArrayString m_menuLabels;
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
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MenuTestCase, "MenuTestCase" );
void MenuTestCase::CreateFrame()
{
m_frame = new wxFrame(NULL, wxID_ANY, "test frame");
wxMenu *fileMenu = new wxMenu;
wxMenu *helpMenu = new wxMenu;
wxMenu *subMenu = new wxMenu;
wxMenu *subsubMenu = new wxMenu;
size_t itemcount = 0;
PopulateMenu(subsubMenu, "Subsubmenu item ", itemcount);
// Store one of its IDs for later
m_subsubmenuItemId = MenuTestCase_First + itemcount - 2;
PopulateMenu(subMenu, "Submenu item ", itemcount);
// Store one of its IDs for later
m_submenuItemId = MenuTestCase_First + itemcount - 2;
subMenu->AppendSubMenu(subsubMenu, "Subsubmen&u", "Test a subsubmenu");
PopulateMenu(fileMenu, "Filemenu item ", itemcount);
fileMenu->Append(MenuTestCase_Foo, "&Foo\tCtrl-F", "Test item to be found");
PopulateMenu(helpMenu, "Helpmenu item ", itemcount);
helpMenu->Append(MenuTestCase_Bar, "Bar");
helpMenu->AppendSubMenu(subMenu, "Sub&menu", "Test a submenu");
// +2 for "Foo" and "Bar", +2 for the 2 submenus
m_itemCount = itemcount + 4;
// Use an arraystring here, to help with future tests
m_menuLabels.Add("&File");
m_menuLabels.Add("&Help");
wxMenuBar *menuBar = new wxMenuBar();
menuBar->Append(fileMenu, m_menuLabels[0]);
menuBar->Append(helpMenu, m_menuLabels[1]);
m_frame->SetMenuBar(menuBar);
}
void MenuTestCase::FindInMenubar()
{
wxMenuBar* bar = m_frame->GetMenuBar();
// Find by name:
CPPUNIT_ASSERT( bar->FindMenu("File") != wxNOT_FOUND );
CPPUNIT_ASSERT( bar->FindMenu("&File") != wxNOT_FOUND );
CPPUNIT_ASSERT( bar->FindMenu("&Fail") == wxNOT_FOUND );
// 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
int index = bar->FindMenu("&File");
CPPUNIT_ASSERT( index != wxNOT_FOUND );
wxString menutitle = bar->GetMenuLabel(index);
CPPUNIT_ASSERT( bar->FindMenuItem(menutitle, "&Foo") != wxNOT_FOUND );
// Find by position:
for (size_t n=0; n < bar->GetMenuCount(); ++n)
{
CPPUNIT_ASSERT( bar->GetMenu(n) );
}
// Find by id:
wxMenu* menu = NULL;
wxMenuItem* item = NULL;
item = bar->FindItem(MenuTestCase_Foo, &menu);
CPPUNIT_ASSERT( item );
CPPUNIT_ASSERT( menu );
// Check that the correct menu was found
CPPUNIT_ASSERT( menu->FindChildItem(MenuTestCase_Foo) );
// Find submenu item:
item = bar->FindItem(m_submenuItemId, &menu);
CPPUNIT_ASSERT( item );
CPPUNIT_ASSERT( menu );
// and, for completeness, a submenu one:
item = bar->FindItem(m_subsubmenuItemId, &menu);
CPPUNIT_ASSERT( item );
CPPUNIT_ASSERT( menu );
}
void MenuTestCase::FindInMenu()
{
wxMenuBar* bar = m_frame->GetMenuBar();
// Find by name:
wxMenu* menuFind = bar->GetMenu(0);
CPPUNIT_ASSERT( menuFind->FindItem("Foo") != wxNOT_FOUND );
CPPUNIT_ASSERT( menuFind->FindItem("&Foo") != wxNOT_FOUND );
// and for submenus
wxMenu* menuHelp = bar->GetMenu(1);
CPPUNIT_ASSERT( menuHelp->FindItem("Submenu") != wxNOT_FOUND );
CPPUNIT_ASSERT( menuHelp->FindItem("Sub&menu") != wxNOT_FOUND );
// Find by position:
for (size_t n=0; n < menuHelp->GetMenuItemCount(); ++n)
{
CPPUNIT_ASSERT( menuHelp->FindItemByPosition(n) );
}
// Find by id:
CPPUNIT_ASSERT( menuHelp->FindItem(MenuTestCase_Bar) );
CPPUNIT_ASSERT( menuHelp->FindItem(MenuTestCase_Foo) == NULL );
for (size_t n=0; n < menuHelp->GetMenuItemCount(); ++n)
{
size_t locatedAt;
wxMenuItem* itemByPos = menuHelp->FindItemByPosition(n);
CPPUNIT_ASSERT( itemByPos );
wxMenuItem* itemById = menuHelp->FindChildItem(itemByPos->GetId(), &locatedAt);
CPPUNIT_ASSERT_EQUAL( itemByPos, itemById );
CPPUNIT_ASSERT_EQUAL( locatedAt, n );
}
// Find submenu item:
for (size_t n=0; n < menuHelp->GetMenuItemCount(); ++n)
{
wxMenuItem* item = menuHelp->FindItemByPosition(n);
if (item->IsSubMenu())
{
wxMenu* submenu;
wxMenuItem* submenuItem = menuHelp->FindItem(m_submenuItemId, &submenu);
CPPUNIT_ASSERT( submenuItem );
CPPUNIT_ASSERT( item->GetSubMenu() == submenu );
}
}
}
void MenuTestCase::Count()
{
wxMenuBar* bar = m_frame->GetMenuBar();
// I suppose you could call this "counting menubars" :)
CPPUNIT_ASSERT( bar );
CPPUNIT_ASSERT_EQUAL( bar->GetMenuCount(), 2 );
size_t count = 0;
for (size_t n=0; n < bar->GetMenuCount(); ++n)
{
RecursivelyCountMenuItems(bar->GetMenu(n), count);
}
CPPUNIT_ASSERT_EQUAL( count, m_itemCount );
}