1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/menu/menu.cpp
3 // Purpose: wxMenu unit test
4 // Author: wxWidgets team
7 // Copyright: (c) 2010 wxWidgets team
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
36 MenuTestCase_Foo
= 10000,
41 void PopulateMenu(wxMenu
* menu
, const wxString
& name
, size_t& itemcount
)
43 // Start at item 1 to make it human-readable ;)
44 for (int n
=1; n
<6; ++n
, ++itemcount
)
46 wxString label
= name
; label
<< n
;
47 menu
->Append(MenuTestCase_First
+ itemcount
, label
, label
+ " help string");
51 void RecursivelyCountMenuItems(const wxMenu
* menu
, size_t& count
)
53 CPPUNIT_ASSERT( menu
);
55 count
+= menu
->GetMenuItemCount();
56 for (size_t n
=0; n
< menu
->GetMenuItemCount(); ++n
)
58 wxMenuItem
* item
= menu
->FindItemByPosition(n
);
59 if (item
->IsSubMenu())
61 RecursivelyCountMenuItems(item
->GetSubMenu(), count
);
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 class MenuTestCase
: public CppUnit::TestCase
78 virtual void setUp() { CreateFrame(); }
79 virtual void tearDown() { m_frame
->Destroy(); }
82 CPPUNIT_TEST_SUITE( MenuTestCase
);
83 CPPUNIT_TEST( FindInMenubar
);
84 CPPUNIT_TEST( FindInMenu
);
85 CPPUNIT_TEST( Count
);
86 CPPUNIT_TEST( Labels
);
87 CPPUNIT_TEST_SUITE_END();
98 // Holds the number of menuitems contained in all the menus
101 // Store here the id of a known submenu item, to be searched for later
104 // and a sub-submenu item
105 int m_subsubmenuItemId
;
107 wxArrayString m_menuLabels
;
109 DECLARE_NO_COPY_CLASS(MenuTestCase
)
112 // register in the unnamed registry so that these tests are run by default
113 CPPUNIT_TEST_SUITE_REGISTRATION( MenuTestCase
);
115 // also include in its own registry so that these tests can be run alone
116 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MenuTestCase
, "MenuTestCase" );
118 void MenuTestCase::CreateFrame()
120 m_frame
= new wxFrame(NULL
, wxID_ANY
, "test frame");
122 wxMenu
*fileMenu
= new wxMenu
;
123 wxMenu
*helpMenu
= new wxMenu
;
124 wxMenu
*subMenu
= new wxMenu
;
125 wxMenu
*subsubMenu
= new wxMenu
;
127 size_t itemcount
= 0;
129 PopulateMenu(subsubMenu
, "Subsubmenu item ", itemcount
);
131 // Store one of its IDs for later
132 m_subsubmenuItemId
= MenuTestCase_First
+ itemcount
- 2;
134 PopulateMenu(subMenu
, "Submenu item ", itemcount
);
136 // Store one of its IDs for later
137 m_submenuItemId
= MenuTestCase_First
+ itemcount
- 2;
139 subMenu
->AppendSubMenu(subsubMenu
, "Subsubmen&u", "Test a subsubmenu");
141 PopulateMenu(fileMenu
, "Filemenu item ", itemcount
);
143 fileMenu
->Append(MenuTestCase_Foo
, "&Foo\tCtrl-F", "Test item to be found");
146 PopulateMenu(helpMenu
, "Helpmenu item ", itemcount
);
147 helpMenu
->Append(MenuTestCase_Bar
, "Bar");
148 helpMenu
->AppendSubMenu(subMenu
, "Sub&menu", "Test a submenu");
150 // +2 for "Foo" and "Bar", +2 for the 2 submenus
151 m_itemCount
= itemcount
+ 4;
153 // Use an arraystring here, to help with future tests
154 m_menuLabels
.Add("&File");
155 m_menuLabels
.Add("&Help");
157 wxMenuBar
*menuBar
= new wxMenuBar();
158 menuBar
->Append(fileMenu
, m_menuLabels
[0]);
159 menuBar
->Append(helpMenu
, m_menuLabels
[1]);
160 m_frame
->SetMenuBar(menuBar
);
163 void MenuTestCase::FindInMenubar()
165 wxMenuBar
* bar
= m_frame
->GetMenuBar();
168 CPPUNIT_ASSERT( bar
->FindMenu("File") != wxNOT_FOUND
);
169 CPPUNIT_ASSERT( bar
->FindMenu("&File") != wxNOT_FOUND
);
170 CPPUNIT_ASSERT( bar
->FindMenu("&Fail") == wxNOT_FOUND
);
172 // Find by menu name plus item name:
173 CPPUNIT_ASSERT( bar
->FindMenuItem("File", "Foo") != wxNOT_FOUND
);
174 CPPUNIT_ASSERT( bar
->FindMenuItem("&File", "&Foo") != wxNOT_FOUND
);
175 // and using the menu label
176 int index
= bar
->FindMenu("&File");
177 CPPUNIT_ASSERT( index
!= wxNOT_FOUND
);
178 wxString menulabel
= bar
->GetMenuLabel(index
);
179 CPPUNIT_ASSERT( bar
->FindMenuItem(menulabel
, "&Foo") != wxNOT_FOUND
);
181 wxString menutitle
= bar
->GetMenu(index
)->GetTitle();
182 CPPUNIT_ASSERT( bar
->FindMenuItem(menutitle
, "&Foo") != wxNOT_FOUND
);
185 for (size_t n
=0; n
< bar
->GetMenuCount(); ++n
)
187 CPPUNIT_ASSERT( bar
->GetMenu(n
) );
192 wxMenuItem
* item
= NULL
;
193 item
= bar
->FindItem(MenuTestCase_Foo
, &menu
);
194 CPPUNIT_ASSERT( item
);
195 CPPUNIT_ASSERT( menu
);
196 // Check that the correct menu was found
197 CPPUNIT_ASSERT( menu
->FindChildItem(MenuTestCase_Foo
) );
199 // Find submenu item:
200 item
= bar
->FindItem(m_submenuItemId
, &menu
);
201 CPPUNIT_ASSERT( item
);
202 CPPUNIT_ASSERT( menu
);
203 // and, for completeness, a subsubmenu one:
204 item
= bar
->FindItem(m_subsubmenuItemId
, &menu
);
205 CPPUNIT_ASSERT( item
);
206 CPPUNIT_ASSERT( menu
);
209 void MenuTestCase::FindInMenu()
211 wxMenuBar
* bar
= m_frame
->GetMenuBar();
214 wxMenu
* menuFind
= bar
->GetMenu(0);
215 CPPUNIT_ASSERT( menuFind
->FindItem("Foo") != wxNOT_FOUND
);
216 CPPUNIT_ASSERT( menuFind
->FindItem("&Foo") != wxNOT_FOUND
);
218 wxMenu
* menuHelp
= bar
->GetMenu(1);
219 CPPUNIT_ASSERT( menuHelp
->FindItem("Submenu") != wxNOT_FOUND
);
220 CPPUNIT_ASSERT( menuHelp
->FindItem("Sub&menu") != wxNOT_FOUND
);
224 for (n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
226 CPPUNIT_ASSERT( menuHelp
->FindItemByPosition(n
) );
230 CPPUNIT_ASSERT( menuHelp
->FindItem(MenuTestCase_Bar
) );
231 CPPUNIT_ASSERT( menuHelp
->FindItem(MenuTestCase_Foo
) == NULL
);
233 for (n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
236 wxMenuItem
* itemByPos
= menuHelp
->FindItemByPosition(n
);
237 CPPUNIT_ASSERT( itemByPos
);
238 wxMenuItem
* itemById
= menuHelp
->FindChildItem(itemByPos
->GetId(), &locatedAt
);
239 CPPUNIT_ASSERT_EQUAL( itemByPos
, itemById
);
240 CPPUNIT_ASSERT_EQUAL( locatedAt
, n
);
243 // Find submenu item:
244 for (n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
246 wxMenuItem
* item
= menuHelp
->FindItemByPosition(n
);
247 if (item
->IsSubMenu())
250 wxMenuItem
* submenuItem
= menuHelp
->FindItem(m_submenuItemId
, &submenu
);
251 CPPUNIT_ASSERT( submenuItem
);
252 CPPUNIT_ASSERT( item
->GetSubMenu() == submenu
);
257 void MenuTestCase::Count()
259 wxMenuBar
* bar
= m_frame
->GetMenuBar();
260 // I suppose you could call this "counting menubars" :)
261 CPPUNIT_ASSERT( bar
);
263 CPPUNIT_ASSERT_EQUAL( bar
->GetMenuCount(), 2 );
266 for (size_t n
=0; n
< bar
->GetMenuCount(); ++n
)
268 RecursivelyCountMenuItems(bar
->GetMenu(n
), count
);
270 CPPUNIT_ASSERT_EQUAL( count
, m_itemCount
);
273 void MenuTestCase::Labels()
275 wxMenuBar
* bar
= m_frame
->GetMenuBar();
276 CPPUNIT_ASSERT( bar
);
278 wxMenuItem
* itemFoo
= bar
->FindItem(MenuTestCase_Foo
, &filemenu
);
279 CPPUNIT_ASSERT( itemFoo
);
280 CPPUNIT_ASSERT( filemenu
);
282 // These return labels including mnemonics/accelerators:
285 CPPUNIT_ASSERT_EQUAL( "&File", bar
->GetMenuLabel(0) );
286 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", bar
->GetLabel(MenuTestCase_Foo
) );
289 CPPUNIT_ASSERT_EQUAL( "&File", filemenu
->GetTitle() );
290 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", filemenu
->GetLabel(MenuTestCase_Foo
) );
293 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", itemFoo
->GetItemLabel() );
295 // These return labels stripped of mnemonics/accelerators:
298 CPPUNIT_ASSERT_EQUAL( "File", bar
->GetMenuLabelText(0) );
301 CPPUNIT_ASSERT_EQUAL( "Foo", filemenu
->GetLabelText(MenuTestCase_Foo
) );
304 CPPUNIT_ASSERT_EQUAL( "Foo", itemFoo
->GetItemLabelText() );
305 CPPUNIT_ASSERT_EQUAL( "Foo", wxMenuItem::GetLabelText("&Foo\tCtrl-F") );