1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/menu/menu.cpp
3 // Purpose: wxMenu unit test
4 // Author: wxWidgets team
6 // Copyright: (c) 2010 wxWidgets team
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
24 #include "wx/uiaction.h"
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
37 MenuTestCase_Foo
= 10000,
42 void PopulateMenu(wxMenu
* menu
, const wxString
& name
, size_t& itemcount
)
44 // Start at item 1 to make it human-readable ;)
45 for (int n
=1; n
<6; ++n
, ++itemcount
)
47 wxString label
= name
; label
<< n
;
48 menu
->Append(MenuTestCase_First
+ itemcount
, label
, label
+ " help string");
52 void RecursivelyCountMenuItems(const wxMenu
* menu
, size_t& count
)
54 CPPUNIT_ASSERT( menu
);
56 count
+= menu
->GetMenuItemCount();
57 for (size_t n
=0; n
< menu
->GetMenuItemCount(); ++n
)
59 wxMenuItem
* item
= menu
->FindItemByPosition(n
);
60 if (item
->IsSubMenu())
62 RecursivelyCountMenuItems(item
->GetSubMenu(), count
);
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 class MenuTestCase
: public CppUnit::TestCase
79 virtual void setUp() { CreateFrame(); }
80 virtual void tearDown() { m_frame
->Destroy(); }
83 CPPUNIT_TEST_SUITE( MenuTestCase
);
84 CPPUNIT_TEST( FindInMenubar
);
85 CPPUNIT_TEST( FindInMenu
);
86 CPPUNIT_TEST( EnableTop
);
87 CPPUNIT_TEST( Count
);
88 CPPUNIT_TEST( Labels
);
89 CPPUNIT_TEST( RadioItems
);
90 CPPUNIT_TEST( RemoveAdd
);
91 WXUISIM_TEST( Events
);
92 CPPUNIT_TEST_SUITE_END();
107 // Holds the number of menuitems contained in all the menus
110 // Store here the id of a known submenu item, to be searched for later
113 // and a sub-submenu item
114 int m_subsubmenuItemId
;
116 wxArrayString m_menuLabels
;
118 // The menu containing the item with MenuTestCase_Bar id.
119 wxMenu
* m_menuWithBar
;
121 DECLARE_NO_COPY_CLASS(MenuTestCase
)
124 // register in the unnamed registry so that these tests are run by default
125 CPPUNIT_TEST_SUITE_REGISTRATION( MenuTestCase
);
127 // also include in its own registry so that these tests can be run alone
128 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MenuTestCase
, "MenuTestCase" );
130 void MenuTestCase::CreateFrame()
132 m_frame
= new wxFrame(NULL
, wxID_ANY
, "test frame");
134 wxMenu
*fileMenu
= new wxMenu
;
135 wxMenu
*helpMenu
= new wxMenu
;
136 wxMenu
*subMenu
= new wxMenu
;
137 wxMenu
*subsubMenu
= new wxMenu
;
139 size_t itemcount
= 0;
141 PopulateMenu(subsubMenu
, "Subsubmenu item ", itemcount
);
143 // Store one of its IDs for later
144 m_subsubmenuItemId
= MenuTestCase_First
+ itemcount
- 2;
146 PopulateMenu(subMenu
, "Submenu item ", itemcount
);
148 // Store one of its IDs for later
149 m_submenuItemId
= MenuTestCase_First
+ itemcount
- 2;
151 subMenu
->AppendSubMenu(subsubMenu
, "Subsubmen&u", "Test a subsubmenu");
153 PopulateMenu(fileMenu
, "Filemenu item ", itemcount
);
155 fileMenu
->Append(MenuTestCase_Foo
, "&Foo\tCtrl-F", "Test item to be found");
158 PopulateMenu(helpMenu
, "Helpmenu item ", itemcount
);
159 helpMenu
->Append(MenuTestCase_Bar
, "Bar\tF1");
160 m_menuWithBar
= helpMenu
;
161 helpMenu
->AppendSubMenu(subMenu
, "Sub&menu", "Test a submenu");
163 // +2 for "Foo" and "Bar", +2 for the 2 submenus
164 m_itemCount
= itemcount
+ 4;
166 // Use an arraystring here, to help with future tests
167 m_menuLabels
.Add("&File");
168 m_menuLabels
.Add("&Help");
170 wxMenuBar
*menuBar
= new wxMenuBar();
171 menuBar
->Append(fileMenu
, m_menuLabels
[0]);
172 menuBar
->Append(helpMenu
, m_menuLabels
[1]);
173 m_frame
->SetMenuBar(menuBar
);
176 void MenuTestCase::FindInMenubar()
178 wxMenuBar
* bar
= m_frame
->GetMenuBar();
181 CPPUNIT_ASSERT( bar
->FindMenu("File") != wxNOT_FOUND
);
182 CPPUNIT_ASSERT( bar
->FindMenu("&File") != wxNOT_FOUND
);
183 CPPUNIT_ASSERT( bar
->FindMenu("&Fail") == wxNOT_FOUND
);
185 // Find by menu name plus item name:
186 CPPUNIT_ASSERT( bar
->FindMenuItem("File", "Foo") != wxNOT_FOUND
);
187 CPPUNIT_ASSERT( bar
->FindMenuItem("&File", "&Foo") != wxNOT_FOUND
);
188 // and using the menu label
189 int index
= bar
->FindMenu("&File");
190 CPPUNIT_ASSERT( index
!= wxNOT_FOUND
);
191 wxString menulabel
= bar
->GetMenuLabel(index
);
192 CPPUNIT_ASSERT( bar
->FindMenuItem(menulabel
, "&Foo") != wxNOT_FOUND
);
194 wxString menutitle
= bar
->GetMenu(index
)->GetTitle();
195 CPPUNIT_ASSERT( bar
->FindMenuItem(menutitle
, "&Foo") != wxNOT_FOUND
);
198 for (size_t n
=0; n
< bar
->GetMenuCount(); ++n
)
200 CPPUNIT_ASSERT( bar
->GetMenu(n
) );
205 wxMenuItem
* item
= NULL
;
206 item
= bar
->FindItem(MenuTestCase_Foo
, &menu
);
207 CPPUNIT_ASSERT( item
);
208 CPPUNIT_ASSERT( menu
);
209 // Check that the correct menu was found
210 CPPUNIT_ASSERT( menu
->FindChildItem(MenuTestCase_Foo
) );
212 // Find submenu item:
213 item
= bar
->FindItem(m_submenuItemId
, &menu
);
214 CPPUNIT_ASSERT( item
);
215 CPPUNIT_ASSERT( menu
);
216 // and, for completeness, a subsubmenu one:
217 item
= bar
->FindItem(m_subsubmenuItemId
, &menu
);
218 CPPUNIT_ASSERT( item
);
219 CPPUNIT_ASSERT( menu
);
222 void MenuTestCase::FindInMenu()
224 wxMenuBar
* bar
= m_frame
->GetMenuBar();
227 wxMenu
* menuFind
= bar
->GetMenu(0);
228 CPPUNIT_ASSERT( menuFind
->FindItem("Foo") != wxNOT_FOUND
);
229 CPPUNIT_ASSERT( menuFind
->FindItem("&Foo") != wxNOT_FOUND
);
231 wxMenu
* menuHelp
= bar
->GetMenu(1);
232 CPPUNIT_ASSERT( menuHelp
->FindItem("Submenu") != wxNOT_FOUND
);
233 CPPUNIT_ASSERT( menuHelp
->FindItem("Sub&menu") != wxNOT_FOUND
);
237 for (n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
239 CPPUNIT_ASSERT( menuHelp
->FindItemByPosition(n
) );
243 CPPUNIT_ASSERT( menuHelp
->FindItem(MenuTestCase_Bar
) );
244 CPPUNIT_ASSERT( menuHelp
->FindItem(MenuTestCase_Foo
) == NULL
);
246 for (n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
249 wxMenuItem
* itemByPos
= menuHelp
->FindItemByPosition(n
);
250 CPPUNIT_ASSERT( itemByPos
);
251 wxMenuItem
* itemById
= menuHelp
->FindChildItem(itemByPos
->GetId(), &locatedAt
);
252 CPPUNIT_ASSERT_EQUAL( itemByPos
, itemById
);
253 CPPUNIT_ASSERT_EQUAL( locatedAt
, n
);
256 // Find submenu item:
257 for (n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
259 wxMenuItem
* item
= menuHelp
->FindItemByPosition(n
);
260 if (item
->IsSubMenu())
263 wxMenuItem
* submenuItem
= menuHelp
->FindItem(m_submenuItemId
, &submenu
);
264 CPPUNIT_ASSERT( submenuItem
);
265 CPPUNIT_ASSERT( item
->GetSubMenu() == submenu
);
270 void MenuTestCase::EnableTop()
272 wxMenuBar
* const bar
= m_frame
->GetMenuBar();
273 CPPUNIT_ASSERT( bar
->IsEnabledTop(0) );
274 bar
->EnableTop( 0, false );
275 CPPUNIT_ASSERT( !bar
->IsEnabledTop(0) );
276 bar
->EnableTop( 0, true );
277 CPPUNIT_ASSERT( bar
->IsEnabledTop(0) );
280 void MenuTestCase::Count()
282 wxMenuBar
* bar
= m_frame
->GetMenuBar();
283 // I suppose you could call this "counting menubars" :)
284 CPPUNIT_ASSERT( bar
);
286 CPPUNIT_ASSERT_EQUAL( bar
->GetMenuCount(), 2 );
289 for (size_t n
=0; n
< bar
->GetMenuCount(); ++n
)
291 RecursivelyCountMenuItems(bar
->GetMenu(n
), count
);
293 CPPUNIT_ASSERT_EQUAL( count
, m_itemCount
);
296 void MenuTestCase::Labels()
298 wxMenuBar
* bar
= m_frame
->GetMenuBar();
299 CPPUNIT_ASSERT( bar
);
301 wxMenuItem
* itemFoo
= bar
->FindItem(MenuTestCase_Foo
, &filemenu
);
302 CPPUNIT_ASSERT( itemFoo
);
303 CPPUNIT_ASSERT( filemenu
);
305 // These return labels including mnemonics/accelerators:
308 CPPUNIT_ASSERT_EQUAL( "&File", bar
->GetMenuLabel(0) );
309 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", bar
->GetLabel(MenuTestCase_Foo
) );
312 CPPUNIT_ASSERT_EQUAL( "&File", filemenu
->GetTitle() );
313 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", filemenu
->GetLabel(MenuTestCase_Foo
) );
316 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", itemFoo
->GetItemLabel() );
318 // These return labels stripped of mnemonics/accelerators:
321 CPPUNIT_ASSERT_EQUAL( "File", bar
->GetMenuLabelText(0) );
324 CPPUNIT_ASSERT_EQUAL( "Foo", filemenu
->GetLabelText(MenuTestCase_Foo
) );
327 CPPUNIT_ASSERT_EQUAL( "Foo", itemFoo
->GetItemLabelText() );
328 CPPUNIT_ASSERT_EQUAL( "Foo", wxMenuItem::GetLabelText("&Foo\tCtrl-F") );
331 void MenuTestCase::RadioItems()
333 wxMenuBar
* const bar
= m_frame
->GetMenuBar();
334 wxMenu
* const menu
= new wxMenu
;
335 bar
->Append(menu
, "&Radio");
337 // Adding consecutive radio items creates a radio group.
338 menu
->AppendRadioItem(MenuTestCase_First
, "Radio 0");
339 menu
->AppendRadioItem(MenuTestCase_First
+ 1, "Radio 1");
341 // First item of a radio group is checked by default.
342 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
) );
344 // Checking the second one make the first one unchecked however.
345 menu
->Check(MenuTestCase_First
+ 1, true);
346 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
) );
347 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 1) );
349 // Adding more radio items after a separator creates another radio group...
350 menu
->AppendSeparator();
351 menu
->AppendRadioItem(MenuTestCase_First
+ 2, "Radio 2");
352 menu
->AppendRadioItem(MenuTestCase_First
+ 3, "Radio 3");
353 menu
->AppendRadioItem(MenuTestCase_First
+ 4, "Radio 4");
355 // ... which is independent from the first one.
356 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 2) );
358 menu
->Check(MenuTestCase_First
+ 3, true);
359 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 3) );
360 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
+ 2) );
361 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 1) );
364 // Insert an item in the middle of an existing radio group.
365 menu
->InsertRadioItem(4, MenuTestCase_First
+ 5, "Radio 5");
366 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 3) );
368 menu
->Check( MenuTestCase_First
+ 5, true );
369 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
+ 3) );
372 // Prepend a couple of items before the first group.
373 menu
->PrependRadioItem(MenuTestCase_First
+ 6, "Radio 6");
374 menu
->PrependRadioItem(MenuTestCase_First
+ 7, "Radio 7");
375 menu
->Check(MenuTestCase_First
+ 7, true);
376 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
+ 1) );
379 // Check that the last radio group still works as expected.
380 menu
->Check(MenuTestCase_First
+ 4, true);
381 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
+ 5) );
384 void MenuTestCase::RemoveAdd()
386 wxMenuBar
* bar
= m_frame
->GetMenuBar();
388 wxMenu
* menu0
= bar
->GetMenu(0);
389 wxMenu
* menu1
= bar
->GetMenu(1);
390 wxMenuItem
* item
= new wxMenuItem(menu0
, MenuTestCase_Foo
+ 100, "t&ext\tCtrl-E");
391 menu0
->Insert(0, item
);
392 CPPUNIT_ASSERT( menu0
->FindItemByPosition(0) == item
);
394 CPPUNIT_ASSERT( menu0
->FindItemByPosition(0) != item
);
395 menu1
->Insert(0, item
);
396 CPPUNIT_ASSERT( menu1
->FindItemByPosition(0) == item
);
398 CPPUNIT_ASSERT( menu1
->FindItemByPosition(0) != item
);
399 menu0
->Insert(0, item
);
400 CPPUNIT_ASSERT( menu0
->FindItemByPosition(0) == item
);
404 void MenuTestCase::Events()
407 // FIXME: For some reason, we sporadically fail to get the event in
408 // buildbot slave builds even though the test always passes locally.
409 // There is undoubtedly something wrong here but without being able
410 // to debug it, I have no idea what is it, so let's just disable
411 // this test when running under buildbot to let the entire test
413 if ( IsAutomaticTest() )
417 #if wxUSE_UIACTIONSIMULATOR
418 class MenuEventHandler
: public wxEvtHandler
421 MenuEventHandler(wxWindow
* win
)
424 m_win
->Connect(wxEVT_MENU
,
425 wxCommandEventHandler(MenuEventHandler::OnMenu
),
433 virtual ~MenuEventHandler()
435 m_win
->Disconnect(wxEVT_MENU
,
436 wxCommandEventHandler(MenuEventHandler::OnMenu
),
443 const wxCommandEvent
& GetEvent()
445 CPPUNIT_ASSERT( m_gotEvent
);
453 void OnMenu(wxCommandEvent
& event
)
455 CPPUNIT_ASSERT( !m_gotEvent
);
458 m_event
= static_cast<wxCommandEvent
*>(event
.Clone());
462 wxWindow
* const m_win
;
463 wxCommandEvent
* m_event
;
467 MenuEventHandler
handler(m_frame
);
469 // Invoke the accelerator.
474 wxUIActionSimulator sim
;
479 const wxCommandEvent
& ev
= handler
.GetEvent();
480 CPPUNIT_ASSERT_EQUAL( static_cast<int>(MenuTestCase_Bar
), ev
.GetId() );
482 wxObject
* const src
= ev
.GetEventObject();
483 CPPUNIT_ASSERT( src
);
485 CPPUNIT_ASSERT_EQUAL( "wxMenu",
486 wxString(src
->GetClassInfo()->GetClassName()) );
487 CPPUNIT_ASSERT_EQUAL( static_cast<wxObject
*>(m_menuWithBar
),
489 #endif // wxUSE_UIACTIONSIMULATOR