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 // ----------------------------------------------------------------------------
25 #include "wx/uiaction.h"
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
38 MenuTestCase_Foo
= 10000,
43 void PopulateMenu(wxMenu
* menu
, const wxString
& name
, size_t& itemcount
)
45 // Start at item 1 to make it human-readable ;)
46 for (int n
=1; n
<6; ++n
, ++itemcount
)
48 wxString label
= name
; label
<< n
;
49 menu
->Append(MenuTestCase_First
+ itemcount
, label
, label
+ " help string");
53 void RecursivelyCountMenuItems(const wxMenu
* menu
, size_t& count
)
55 CPPUNIT_ASSERT( menu
);
57 count
+= menu
->GetMenuItemCount();
58 for (size_t n
=0; n
< menu
->GetMenuItemCount(); ++n
)
60 wxMenuItem
* item
= menu
->FindItemByPosition(n
);
61 if (item
->IsSubMenu())
63 RecursivelyCountMenuItems(item
->GetSubMenu(), count
);
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 class MenuTestCase
: public CppUnit::TestCase
80 virtual void setUp() { CreateFrame(); }
81 virtual void tearDown() { m_frame
->Destroy(); }
84 CPPUNIT_TEST_SUITE( MenuTestCase
);
85 CPPUNIT_TEST( FindInMenubar
);
86 CPPUNIT_TEST( FindInMenu
);
87 CPPUNIT_TEST( EnableTop
);
88 CPPUNIT_TEST( Count
);
89 CPPUNIT_TEST( Labels
);
90 CPPUNIT_TEST( RadioItems
);
91 CPPUNIT_TEST( RemoveAdd
);
92 WXUISIM_TEST( Events
);
93 CPPUNIT_TEST_SUITE_END();
108 // Holds the number of menuitems contained in all the menus
111 // Store here the id of a known submenu item, to be searched for later
114 // and a sub-submenu item
115 int m_subsubmenuItemId
;
117 wxArrayString m_menuLabels
;
119 // The menu containing the item with MenuTestCase_Bar id.
120 wxMenu
* m_menuWithBar
;
122 DECLARE_NO_COPY_CLASS(MenuTestCase
)
125 // register in the unnamed registry so that these tests are run by default
126 CPPUNIT_TEST_SUITE_REGISTRATION( MenuTestCase
);
128 // also include in its own registry so that these tests can be run alone
129 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MenuTestCase
, "MenuTestCase" );
131 void MenuTestCase::CreateFrame()
133 m_frame
= new wxFrame(NULL
, wxID_ANY
, "test frame");
135 wxMenu
*fileMenu
= new wxMenu
;
136 wxMenu
*helpMenu
= new wxMenu
;
137 wxMenu
*subMenu
= new wxMenu
;
138 wxMenu
*subsubMenu
= new wxMenu
;
140 size_t itemcount
= 0;
142 PopulateMenu(subsubMenu
, "Subsubmenu item ", itemcount
);
144 // Store one of its IDs for later
145 m_subsubmenuItemId
= MenuTestCase_First
+ itemcount
- 2;
147 PopulateMenu(subMenu
, "Submenu item ", itemcount
);
149 // Store one of its IDs for later
150 m_submenuItemId
= MenuTestCase_First
+ itemcount
- 2;
152 subMenu
->AppendSubMenu(subsubMenu
, "Subsubmen&u", "Test a subsubmenu");
154 PopulateMenu(fileMenu
, "Filemenu item ", itemcount
);
156 fileMenu
->Append(MenuTestCase_Foo
, "&Foo\tCtrl-F", "Test item to be found");
159 PopulateMenu(helpMenu
, "Helpmenu item ", itemcount
);
160 helpMenu
->Append(MenuTestCase_Bar
, "Bar\tF1");
161 m_menuWithBar
= helpMenu
;
162 helpMenu
->AppendSubMenu(subMenu
, "Sub&menu", "Test a submenu");
164 // +2 for "Foo" and "Bar", +2 for the 2 submenus
165 m_itemCount
= itemcount
+ 4;
167 // Use an arraystring here, to help with future tests
168 m_menuLabels
.Add("&File");
169 m_menuLabels
.Add("&Help");
171 wxMenuBar
*menuBar
= new wxMenuBar();
172 menuBar
->Append(fileMenu
, m_menuLabels
[0]);
173 menuBar
->Append(helpMenu
, m_menuLabels
[1]);
174 m_frame
->SetMenuBar(menuBar
);
177 void MenuTestCase::FindInMenubar()
179 wxMenuBar
* bar
= m_frame
->GetMenuBar();
182 CPPUNIT_ASSERT( bar
->FindMenu("File") != wxNOT_FOUND
);
183 CPPUNIT_ASSERT( bar
->FindMenu("&File") != wxNOT_FOUND
);
184 CPPUNIT_ASSERT( bar
->FindMenu("&Fail") == wxNOT_FOUND
);
186 // Find by menu name plus item name:
187 CPPUNIT_ASSERT( bar
->FindMenuItem("File", "Foo") != wxNOT_FOUND
);
188 CPPUNIT_ASSERT( bar
->FindMenuItem("&File", "&Foo") != wxNOT_FOUND
);
189 // and using the menu label
190 int index
= bar
->FindMenu("&File");
191 CPPUNIT_ASSERT( index
!= wxNOT_FOUND
);
192 wxString menulabel
= bar
->GetMenuLabel(index
);
193 CPPUNIT_ASSERT( bar
->FindMenuItem(menulabel
, "&Foo") != wxNOT_FOUND
);
195 wxString menutitle
= bar
->GetMenu(index
)->GetTitle();
196 CPPUNIT_ASSERT( bar
->FindMenuItem(menutitle
, "&Foo") != wxNOT_FOUND
);
199 for (size_t n
=0; n
< bar
->GetMenuCount(); ++n
)
201 CPPUNIT_ASSERT( bar
->GetMenu(n
) );
206 wxMenuItem
* item
= NULL
;
207 item
= bar
->FindItem(MenuTestCase_Foo
, &menu
);
208 CPPUNIT_ASSERT( item
);
209 CPPUNIT_ASSERT( menu
);
210 // Check that the correct menu was found
211 CPPUNIT_ASSERT( menu
->FindChildItem(MenuTestCase_Foo
) );
213 // Find submenu item:
214 item
= bar
->FindItem(m_submenuItemId
, &menu
);
215 CPPUNIT_ASSERT( item
);
216 CPPUNIT_ASSERT( menu
);
217 // and, for completeness, a subsubmenu one:
218 item
= bar
->FindItem(m_subsubmenuItemId
, &menu
);
219 CPPUNIT_ASSERT( item
);
220 CPPUNIT_ASSERT( menu
);
223 void MenuTestCase::FindInMenu()
225 wxMenuBar
* bar
= m_frame
->GetMenuBar();
228 wxMenu
* menuFind
= bar
->GetMenu(0);
229 CPPUNIT_ASSERT( menuFind
->FindItem("Foo") != wxNOT_FOUND
);
230 CPPUNIT_ASSERT( menuFind
->FindItem("&Foo") != wxNOT_FOUND
);
232 wxMenu
* menuHelp
= bar
->GetMenu(1);
233 CPPUNIT_ASSERT( menuHelp
->FindItem("Submenu") != wxNOT_FOUND
);
234 CPPUNIT_ASSERT( menuHelp
->FindItem("Sub&menu") != wxNOT_FOUND
);
238 for (n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
240 CPPUNIT_ASSERT( menuHelp
->FindItemByPosition(n
) );
244 CPPUNIT_ASSERT( menuHelp
->FindItem(MenuTestCase_Bar
) );
245 CPPUNIT_ASSERT( menuHelp
->FindItem(MenuTestCase_Foo
) == NULL
);
247 for (n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
250 wxMenuItem
* itemByPos
= menuHelp
->FindItemByPosition(n
);
251 CPPUNIT_ASSERT( itemByPos
);
252 wxMenuItem
* itemById
= menuHelp
->FindChildItem(itemByPos
->GetId(), &locatedAt
);
253 CPPUNIT_ASSERT_EQUAL( itemByPos
, itemById
);
254 CPPUNIT_ASSERT_EQUAL( locatedAt
, n
);
257 // Find submenu item:
258 for (n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
260 wxMenuItem
* item
= menuHelp
->FindItemByPosition(n
);
261 if (item
->IsSubMenu())
264 wxMenuItem
* submenuItem
= menuHelp
->FindItem(m_submenuItemId
, &submenu
);
265 CPPUNIT_ASSERT( submenuItem
);
266 CPPUNIT_ASSERT( item
->GetSubMenu() == submenu
);
271 void MenuTestCase::EnableTop()
273 wxMenuBar
* const bar
= m_frame
->GetMenuBar();
274 CPPUNIT_ASSERT( bar
->IsEnabledTop(0) );
275 bar
->EnableTop( 0, false );
276 CPPUNIT_ASSERT( !bar
->IsEnabledTop(0) );
277 bar
->EnableTop( 0, true );
278 CPPUNIT_ASSERT( bar
->IsEnabledTop(0) );
281 void MenuTestCase::Count()
283 wxMenuBar
* bar
= m_frame
->GetMenuBar();
284 // I suppose you could call this "counting menubars" :)
285 CPPUNIT_ASSERT( bar
);
287 CPPUNIT_ASSERT_EQUAL( bar
->GetMenuCount(), 2 );
290 for (size_t n
=0; n
< bar
->GetMenuCount(); ++n
)
292 RecursivelyCountMenuItems(bar
->GetMenu(n
), count
);
294 CPPUNIT_ASSERT_EQUAL( count
, m_itemCount
);
297 void MenuTestCase::Labels()
299 wxMenuBar
* bar
= m_frame
->GetMenuBar();
300 CPPUNIT_ASSERT( bar
);
302 wxMenuItem
* itemFoo
= bar
->FindItem(MenuTestCase_Foo
, &filemenu
);
303 CPPUNIT_ASSERT( itemFoo
);
304 CPPUNIT_ASSERT( filemenu
);
306 // These return labels including mnemonics/accelerators:
309 CPPUNIT_ASSERT_EQUAL( "&File", bar
->GetMenuLabel(0) );
310 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", bar
->GetLabel(MenuTestCase_Foo
) );
313 CPPUNIT_ASSERT_EQUAL( "&File", filemenu
->GetTitle() );
314 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", filemenu
->GetLabel(MenuTestCase_Foo
) );
317 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", itemFoo
->GetItemLabel() );
319 // These return labels stripped of mnemonics/accelerators:
322 CPPUNIT_ASSERT_EQUAL( "File", bar
->GetMenuLabelText(0) );
325 CPPUNIT_ASSERT_EQUAL( "Foo", filemenu
->GetLabelText(MenuTestCase_Foo
) );
328 CPPUNIT_ASSERT_EQUAL( "Foo", itemFoo
->GetItemLabelText() );
329 CPPUNIT_ASSERT_EQUAL( "Foo", wxMenuItem::GetLabelText("&Foo\tCtrl-F") );
332 void MenuTestCase::RadioItems()
334 wxMenuBar
* const bar
= m_frame
->GetMenuBar();
335 wxMenu
* const menu
= new wxMenu
;
336 bar
->Append(menu
, "&Radio");
338 // Adding consecutive radio items creates a radio group.
339 menu
->AppendRadioItem(MenuTestCase_First
, "Radio 0");
340 menu
->AppendRadioItem(MenuTestCase_First
+ 1, "Radio 1");
342 // First item of a radio group is checked by default.
343 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
) );
345 // Checking the second one make the first one unchecked however.
346 menu
->Check(MenuTestCase_First
+ 1, true);
347 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
) );
348 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 1) );
350 // Adding more radio items after a separator creates another radio group...
351 menu
->AppendSeparator();
352 menu
->AppendRadioItem(MenuTestCase_First
+ 2, "Radio 2");
353 menu
->AppendRadioItem(MenuTestCase_First
+ 3, "Radio 3");
354 menu
->AppendRadioItem(MenuTestCase_First
+ 4, "Radio 4");
356 // ... which is independent from the first one.
357 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 2) );
359 menu
->Check(MenuTestCase_First
+ 3, true);
360 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 3) );
361 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
+ 2) );
362 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 1) );
365 // Insert an item in the middle of an existing radio group.
366 menu
->InsertRadioItem(4, MenuTestCase_First
+ 5, "Radio 5");
367 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 3) );
369 menu
->Check( MenuTestCase_First
+ 5, true );
370 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
+ 3) );
373 // Prepend a couple of items before the first group.
374 menu
->PrependRadioItem(MenuTestCase_First
+ 6, "Radio 6");
375 menu
->PrependRadioItem(MenuTestCase_First
+ 7, "Radio 7");
376 menu
->Check(MenuTestCase_First
+ 7, true);
377 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
+ 1) );
380 // Check that the last radio group still works as expected.
381 menu
->Check(MenuTestCase_First
+ 4, true);
382 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
+ 5) );
385 void MenuTestCase::RemoveAdd()
387 wxMenuBar
* bar
= m_frame
->GetMenuBar();
389 wxMenu
* menu0
= bar
->GetMenu(0);
390 wxMenu
* menu1
= bar
->GetMenu(1);
391 wxMenuItem
* item
= new wxMenuItem(menu0
, MenuTestCase_Foo
+ 100, "t&ext\tCtrl-E");
392 menu0
->Insert(0, item
);
393 CPPUNIT_ASSERT( menu0
->FindItemByPosition(0) == item
);
395 CPPUNIT_ASSERT( menu0
->FindItemByPosition(0) != item
);
396 menu1
->Insert(0, item
);
397 CPPUNIT_ASSERT( menu1
->FindItemByPosition(0) == item
);
399 CPPUNIT_ASSERT( menu1
->FindItemByPosition(0) != item
);
400 menu0
->Insert(0, item
);
401 CPPUNIT_ASSERT( menu0
->FindItemByPosition(0) == item
);
405 void MenuTestCase::Events()
408 // FIXME: For some reason, we sporadically fail to get the event in
409 // buildbot slave builds even though the test always passes locally.
410 // There is undoubtedly something wrong here but without being able
411 // to debug it, I have no idea what is it, so let's just disable
412 // this test when running under buildbot to let the entire test
414 if ( IsAutomaticTest() )
418 #if wxUSE_UIACTIONSIMULATOR
419 class MenuEventHandler
: public wxEvtHandler
422 MenuEventHandler(wxWindow
* win
)
425 m_win
->Connect(wxEVT_MENU
,
426 wxCommandEventHandler(MenuEventHandler::OnMenu
),
434 virtual ~MenuEventHandler()
436 m_win
->Disconnect(wxEVT_MENU
,
437 wxCommandEventHandler(MenuEventHandler::OnMenu
),
444 const wxCommandEvent
& GetEvent()
446 CPPUNIT_ASSERT( m_gotEvent
);
454 void OnMenu(wxCommandEvent
& event
)
456 CPPUNIT_ASSERT( !m_gotEvent
);
459 m_event
= static_cast<wxCommandEvent
*>(event
.Clone());
463 wxWindow
* const m_win
;
464 wxCommandEvent
* m_event
;
468 MenuEventHandler
handler(m_frame
);
470 // Invoke the accelerator.
475 wxUIActionSimulator sim
;
480 const wxCommandEvent
& ev
= handler
.GetEvent();
481 CPPUNIT_ASSERT_EQUAL( static_cast<int>(MenuTestCase_Bar
), ev
.GetId() );
483 wxObject
* const src
= ev
.GetEventObject();
484 CPPUNIT_ASSERT( src
);
486 CPPUNIT_ASSERT_EQUAL( "wxMenu",
487 wxString(src
->GetClassInfo()->GetClassName()) );
488 CPPUNIT_ASSERT_EQUAL( static_cast<wxObject
*>(m_menuWithBar
),
490 #endif // wxUSE_UIACTIONSIMULATOR