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( RadioItems
);
88 CPPUNIT_TEST( RemoveAdd
);
89 CPPUNIT_TEST_SUITE_END();
102 // Holds the number of menuitems contained in all the menus
105 // Store here the id of a known submenu item, to be searched for later
108 // and a sub-submenu item
109 int m_subsubmenuItemId
;
111 wxArrayString m_menuLabels
;
113 DECLARE_NO_COPY_CLASS(MenuTestCase
)
116 // register in the unnamed registry so that these tests are run by default
117 CPPUNIT_TEST_SUITE_REGISTRATION( MenuTestCase
);
119 // also include in its own registry so that these tests can be run alone
120 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MenuTestCase
, "MenuTestCase" );
122 void MenuTestCase::CreateFrame()
124 m_frame
= new wxFrame(NULL
, wxID_ANY
, "test frame");
126 wxMenu
*fileMenu
= new wxMenu
;
127 wxMenu
*helpMenu
= new wxMenu
;
128 wxMenu
*subMenu
= new wxMenu
;
129 wxMenu
*subsubMenu
= new wxMenu
;
131 size_t itemcount
= 0;
133 PopulateMenu(subsubMenu
, "Subsubmenu item ", itemcount
);
135 // Store one of its IDs for later
136 m_subsubmenuItemId
= MenuTestCase_First
+ itemcount
- 2;
138 PopulateMenu(subMenu
, "Submenu item ", itemcount
);
140 // Store one of its IDs for later
141 m_submenuItemId
= MenuTestCase_First
+ itemcount
- 2;
143 subMenu
->AppendSubMenu(subsubMenu
, "Subsubmen&u", "Test a subsubmenu");
145 PopulateMenu(fileMenu
, "Filemenu item ", itemcount
);
147 fileMenu
->Append(MenuTestCase_Foo
, "&Foo\tCtrl-F", "Test item to be found");
150 PopulateMenu(helpMenu
, "Helpmenu item ", itemcount
);
151 helpMenu
->Append(MenuTestCase_Bar
, "Bar");
152 helpMenu
->AppendSubMenu(subMenu
, "Sub&menu", "Test a submenu");
154 // +2 for "Foo" and "Bar", +2 for the 2 submenus
155 m_itemCount
= itemcount
+ 4;
157 // Use an arraystring here, to help with future tests
158 m_menuLabels
.Add("&File");
159 m_menuLabels
.Add("&Help");
161 wxMenuBar
*menuBar
= new wxMenuBar();
162 menuBar
->Append(fileMenu
, m_menuLabels
[0]);
163 menuBar
->Append(helpMenu
, m_menuLabels
[1]);
164 m_frame
->SetMenuBar(menuBar
);
167 void MenuTestCase::FindInMenubar()
169 wxMenuBar
* bar
= m_frame
->GetMenuBar();
172 CPPUNIT_ASSERT( bar
->FindMenu("File") != wxNOT_FOUND
);
173 CPPUNIT_ASSERT( bar
->FindMenu("&File") != wxNOT_FOUND
);
174 CPPUNIT_ASSERT( bar
->FindMenu("&Fail") == wxNOT_FOUND
);
176 // Find by menu name plus item name:
177 CPPUNIT_ASSERT( bar
->FindMenuItem("File", "Foo") != wxNOT_FOUND
);
178 CPPUNIT_ASSERT( bar
->FindMenuItem("&File", "&Foo") != wxNOT_FOUND
);
179 // and using the menu label
180 int index
= bar
->FindMenu("&File");
181 CPPUNIT_ASSERT( index
!= wxNOT_FOUND
);
182 wxString menulabel
= bar
->GetMenuLabel(index
);
183 CPPUNIT_ASSERT( bar
->FindMenuItem(menulabel
, "&Foo") != wxNOT_FOUND
);
185 wxString menutitle
= bar
->GetMenu(index
)->GetTitle();
186 CPPUNIT_ASSERT( bar
->FindMenuItem(menutitle
, "&Foo") != wxNOT_FOUND
);
189 for (size_t n
=0; n
< bar
->GetMenuCount(); ++n
)
191 CPPUNIT_ASSERT( bar
->GetMenu(n
) );
196 wxMenuItem
* item
= NULL
;
197 item
= bar
->FindItem(MenuTestCase_Foo
, &menu
);
198 CPPUNIT_ASSERT( item
);
199 CPPUNIT_ASSERT( menu
);
200 // Check that the correct menu was found
201 CPPUNIT_ASSERT( menu
->FindChildItem(MenuTestCase_Foo
) );
203 // Find submenu item:
204 item
= bar
->FindItem(m_submenuItemId
, &menu
);
205 CPPUNIT_ASSERT( item
);
206 CPPUNIT_ASSERT( menu
);
207 // and, for completeness, a subsubmenu one:
208 item
= bar
->FindItem(m_subsubmenuItemId
, &menu
);
209 CPPUNIT_ASSERT( item
);
210 CPPUNIT_ASSERT( menu
);
213 void MenuTestCase::FindInMenu()
215 wxMenuBar
* bar
= m_frame
->GetMenuBar();
218 wxMenu
* menuFind
= bar
->GetMenu(0);
219 CPPUNIT_ASSERT( menuFind
->FindItem("Foo") != wxNOT_FOUND
);
220 CPPUNIT_ASSERT( menuFind
->FindItem("&Foo") != wxNOT_FOUND
);
222 wxMenu
* menuHelp
= bar
->GetMenu(1);
223 CPPUNIT_ASSERT( menuHelp
->FindItem("Submenu") != wxNOT_FOUND
);
224 CPPUNIT_ASSERT( menuHelp
->FindItem("Sub&menu") != wxNOT_FOUND
);
228 for (n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
230 CPPUNIT_ASSERT( menuHelp
->FindItemByPosition(n
) );
234 CPPUNIT_ASSERT( menuHelp
->FindItem(MenuTestCase_Bar
) );
235 CPPUNIT_ASSERT( menuHelp
->FindItem(MenuTestCase_Foo
) == NULL
);
237 for (n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
240 wxMenuItem
* itemByPos
= menuHelp
->FindItemByPosition(n
);
241 CPPUNIT_ASSERT( itemByPos
);
242 wxMenuItem
* itemById
= menuHelp
->FindChildItem(itemByPos
->GetId(), &locatedAt
);
243 CPPUNIT_ASSERT_EQUAL( itemByPos
, itemById
);
244 CPPUNIT_ASSERT_EQUAL( locatedAt
, n
);
247 // Find submenu item:
248 for (n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
250 wxMenuItem
* item
= menuHelp
->FindItemByPosition(n
);
251 if (item
->IsSubMenu())
254 wxMenuItem
* submenuItem
= menuHelp
->FindItem(m_submenuItemId
, &submenu
);
255 CPPUNIT_ASSERT( submenuItem
);
256 CPPUNIT_ASSERT( item
->GetSubMenu() == submenu
);
261 void MenuTestCase::Count()
263 wxMenuBar
* bar
= m_frame
->GetMenuBar();
264 // I suppose you could call this "counting menubars" :)
265 CPPUNIT_ASSERT( bar
);
267 CPPUNIT_ASSERT_EQUAL( bar
->GetMenuCount(), 2 );
270 for (size_t n
=0; n
< bar
->GetMenuCount(); ++n
)
272 RecursivelyCountMenuItems(bar
->GetMenu(n
), count
);
274 CPPUNIT_ASSERT_EQUAL( count
, m_itemCount
);
277 void MenuTestCase::Labels()
279 wxMenuBar
* bar
= m_frame
->GetMenuBar();
280 CPPUNIT_ASSERT( bar
);
282 wxMenuItem
* itemFoo
= bar
->FindItem(MenuTestCase_Foo
, &filemenu
);
283 CPPUNIT_ASSERT( itemFoo
);
284 CPPUNIT_ASSERT( filemenu
);
286 // These return labels including mnemonics/accelerators:
289 CPPUNIT_ASSERT_EQUAL( "&File", bar
->GetMenuLabel(0) );
290 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", bar
->GetLabel(MenuTestCase_Foo
) );
293 CPPUNIT_ASSERT_EQUAL( "&File", filemenu
->GetTitle() );
294 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", filemenu
->GetLabel(MenuTestCase_Foo
) );
297 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", itemFoo
->GetItemLabel() );
299 // These return labels stripped of mnemonics/accelerators:
302 CPPUNIT_ASSERT_EQUAL( "File", bar
->GetMenuLabelText(0) );
305 CPPUNIT_ASSERT_EQUAL( "Foo", filemenu
->GetLabelText(MenuTestCase_Foo
) );
308 CPPUNIT_ASSERT_EQUAL( "Foo", itemFoo
->GetItemLabelText() );
309 CPPUNIT_ASSERT_EQUAL( "Foo", wxMenuItem::GetLabelText("&Foo\tCtrl-F") );
312 void MenuTestCase::RadioItems()
314 wxMenuBar
* const bar
= m_frame
->GetMenuBar();
315 wxMenu
* const menu
= new wxMenu
;
316 bar
->Append(menu
, "&Radio");
318 // Adding consecutive radio items creates a radio group.
319 menu
->AppendRadioItem(MenuTestCase_First
, "Radio 0");
320 menu
->AppendRadioItem(MenuTestCase_First
+ 1, "Radio 1");
322 // First item of a radio group is checked by default.
323 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
) );
325 // Checking the second one make the first one unchecked however.
326 menu
->Check(MenuTestCase_First
+ 1, true);
327 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
) );
328 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 1) );
330 // Adding more radio items after a separator creates another radio group...
331 menu
->AppendSeparator();
332 menu
->AppendRadioItem(MenuTestCase_First
+ 2, "Radio 2");
333 menu
->AppendRadioItem(MenuTestCase_First
+ 3, "Radio 3");
334 menu
->AppendRadioItem(MenuTestCase_First
+ 4, "Radio 4");
336 // ... which is independent from the first one.
337 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 2) );
339 menu
->Check(MenuTestCase_First
+ 3, true);
340 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 3) );
341 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
+ 2) );
342 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 1) );
345 // Insert an item in the middle of an existing radio group.
346 menu
->InsertRadioItem(4, MenuTestCase_First
+ 5, "Radio 5");
347 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 3) );
349 menu
->Check( MenuTestCase_First
+ 5, true );
350 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
+ 3) );
353 // Prepend a couple of items before the first group.
354 menu
->PrependRadioItem(MenuTestCase_First
+ 6, "Radio 6");
355 menu
->PrependRadioItem(MenuTestCase_First
+ 7, "Radio 7");
356 menu
->Check(MenuTestCase_First
+ 7, true);
357 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
+ 1) );
360 // Check that the last radio group still works as expected.
361 menu
->Check(MenuTestCase_First
+ 4, true);
362 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
+ 5) );
365 void MenuTestCase::RemoveAdd()
367 wxMenuBar
* bar
= m_frame
->GetMenuBar();
369 wxMenu
* menu0
= bar
->GetMenu(0);
370 wxMenu
* menu1
= bar
->GetMenu(1);
371 wxMenuItem
* item
= new wxMenuItem(menu0
, MenuTestCase_Foo
+ 100, "t&ext\tCtrl-E");
372 menu0
->Insert(0, item
);
373 CPPUNIT_ASSERT( menu0
->FindItemByPosition(0) == item
);
375 CPPUNIT_ASSERT( menu0
->FindItemByPosition(0) != item
);
376 menu1
->Insert(0, item
);
377 CPPUNIT_ASSERT( menu1
->FindItemByPosition(0) == item
);
379 CPPUNIT_ASSERT( menu1
->FindItemByPosition(0) != item
);
380 menu0
->Insert(0, item
);
381 CPPUNIT_ASSERT( menu0
->FindItemByPosition(0) == item
);