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_SUITE_END();
100 // Holds the number of menuitems contained in all the menus
103 // Store here the id of a known submenu item, to be searched for later
106 // and a sub-submenu item
107 int m_subsubmenuItemId
;
109 wxArrayString m_menuLabels
;
111 DECLARE_NO_COPY_CLASS(MenuTestCase
)
114 // register in the unnamed registry so that these tests are run by default
115 CPPUNIT_TEST_SUITE_REGISTRATION( MenuTestCase
);
117 // also include in its own registry so that these tests can be run alone
118 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MenuTestCase
, "MenuTestCase" );
120 void MenuTestCase::CreateFrame()
122 m_frame
= new wxFrame(NULL
, wxID_ANY
, "test frame");
124 wxMenu
*fileMenu
= new wxMenu
;
125 wxMenu
*helpMenu
= new wxMenu
;
126 wxMenu
*subMenu
= new wxMenu
;
127 wxMenu
*subsubMenu
= new wxMenu
;
129 size_t itemcount
= 0;
131 PopulateMenu(subsubMenu
, "Subsubmenu item ", itemcount
);
133 // Store one of its IDs for later
134 m_subsubmenuItemId
= MenuTestCase_First
+ itemcount
- 2;
136 PopulateMenu(subMenu
, "Submenu item ", itemcount
);
138 // Store one of its IDs for later
139 m_submenuItemId
= MenuTestCase_First
+ itemcount
- 2;
141 subMenu
->AppendSubMenu(subsubMenu
, "Subsubmen&u", "Test a subsubmenu");
143 PopulateMenu(fileMenu
, "Filemenu item ", itemcount
);
145 fileMenu
->Append(MenuTestCase_Foo
, "&Foo\tCtrl-F", "Test item to be found");
148 PopulateMenu(helpMenu
, "Helpmenu item ", itemcount
);
149 helpMenu
->Append(MenuTestCase_Bar
, "Bar");
150 helpMenu
->AppendSubMenu(subMenu
, "Sub&menu", "Test a submenu");
152 // +2 for "Foo" and "Bar", +2 for the 2 submenus
153 m_itemCount
= itemcount
+ 4;
155 // Use an arraystring here, to help with future tests
156 m_menuLabels
.Add("&File");
157 m_menuLabels
.Add("&Help");
159 wxMenuBar
*menuBar
= new wxMenuBar();
160 menuBar
->Append(fileMenu
, m_menuLabels
[0]);
161 menuBar
->Append(helpMenu
, m_menuLabels
[1]);
162 m_frame
->SetMenuBar(menuBar
);
165 void MenuTestCase::FindInMenubar()
167 wxMenuBar
* bar
= m_frame
->GetMenuBar();
170 CPPUNIT_ASSERT( bar
->FindMenu("File") != wxNOT_FOUND
);
171 CPPUNIT_ASSERT( bar
->FindMenu("&File") != wxNOT_FOUND
);
172 CPPUNIT_ASSERT( bar
->FindMenu("&Fail") == wxNOT_FOUND
);
174 // Find by menu name plus item name:
175 CPPUNIT_ASSERT( bar
->FindMenuItem("File", "Foo") != wxNOT_FOUND
);
176 CPPUNIT_ASSERT( bar
->FindMenuItem("&File", "&Foo") != wxNOT_FOUND
);
177 // and using the menu label
178 int index
= bar
->FindMenu("&File");
179 CPPUNIT_ASSERT( index
!= wxNOT_FOUND
);
180 wxString menulabel
= bar
->GetMenuLabel(index
);
181 CPPUNIT_ASSERT( bar
->FindMenuItem(menulabel
, "&Foo") != wxNOT_FOUND
);
183 wxString menutitle
= bar
->GetMenu(index
)->GetTitle();
184 CPPUNIT_ASSERT( bar
->FindMenuItem(menutitle
, "&Foo") != wxNOT_FOUND
);
187 for (size_t n
=0; n
< bar
->GetMenuCount(); ++n
)
189 CPPUNIT_ASSERT( bar
->GetMenu(n
) );
194 wxMenuItem
* item
= NULL
;
195 item
= bar
->FindItem(MenuTestCase_Foo
, &menu
);
196 CPPUNIT_ASSERT( item
);
197 CPPUNIT_ASSERT( menu
);
198 // Check that the correct menu was found
199 CPPUNIT_ASSERT( menu
->FindChildItem(MenuTestCase_Foo
) );
201 // Find submenu item:
202 item
= bar
->FindItem(m_submenuItemId
, &menu
);
203 CPPUNIT_ASSERT( item
);
204 CPPUNIT_ASSERT( menu
);
205 // and, for completeness, a subsubmenu one:
206 item
= bar
->FindItem(m_subsubmenuItemId
, &menu
);
207 CPPUNIT_ASSERT( item
);
208 CPPUNIT_ASSERT( menu
);
211 void MenuTestCase::FindInMenu()
213 wxMenuBar
* bar
= m_frame
->GetMenuBar();
216 wxMenu
* menuFind
= bar
->GetMenu(0);
217 CPPUNIT_ASSERT( menuFind
->FindItem("Foo") != wxNOT_FOUND
);
218 CPPUNIT_ASSERT( menuFind
->FindItem("&Foo") != wxNOT_FOUND
);
220 wxMenu
* menuHelp
= bar
->GetMenu(1);
221 CPPUNIT_ASSERT( menuHelp
->FindItem("Submenu") != wxNOT_FOUND
);
222 CPPUNIT_ASSERT( menuHelp
->FindItem("Sub&menu") != wxNOT_FOUND
);
226 for (n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
228 CPPUNIT_ASSERT( menuHelp
->FindItemByPosition(n
) );
232 CPPUNIT_ASSERT( menuHelp
->FindItem(MenuTestCase_Bar
) );
233 CPPUNIT_ASSERT( menuHelp
->FindItem(MenuTestCase_Foo
) == NULL
);
235 for (n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
238 wxMenuItem
* itemByPos
= menuHelp
->FindItemByPosition(n
);
239 CPPUNIT_ASSERT( itemByPos
);
240 wxMenuItem
* itemById
= menuHelp
->FindChildItem(itemByPos
->GetId(), &locatedAt
);
241 CPPUNIT_ASSERT_EQUAL( itemByPos
, itemById
);
242 CPPUNIT_ASSERT_EQUAL( locatedAt
, n
);
245 // Find submenu item:
246 for (n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
248 wxMenuItem
* item
= menuHelp
->FindItemByPosition(n
);
249 if (item
->IsSubMenu())
252 wxMenuItem
* submenuItem
= menuHelp
->FindItem(m_submenuItemId
, &submenu
);
253 CPPUNIT_ASSERT( submenuItem
);
254 CPPUNIT_ASSERT( item
->GetSubMenu() == submenu
);
259 void MenuTestCase::Count()
261 wxMenuBar
* bar
= m_frame
->GetMenuBar();
262 // I suppose you could call this "counting menubars" :)
263 CPPUNIT_ASSERT( bar
);
265 CPPUNIT_ASSERT_EQUAL( bar
->GetMenuCount(), 2 );
268 for (size_t n
=0; n
< bar
->GetMenuCount(); ++n
)
270 RecursivelyCountMenuItems(bar
->GetMenu(n
), count
);
272 CPPUNIT_ASSERT_EQUAL( count
, m_itemCount
);
275 void MenuTestCase::Labels()
277 wxMenuBar
* bar
= m_frame
->GetMenuBar();
278 CPPUNIT_ASSERT( bar
);
280 wxMenuItem
* itemFoo
= bar
->FindItem(MenuTestCase_Foo
, &filemenu
);
281 CPPUNIT_ASSERT( itemFoo
);
282 CPPUNIT_ASSERT( filemenu
);
284 // These return labels including mnemonics/accelerators:
287 CPPUNIT_ASSERT_EQUAL( "&File", bar
->GetMenuLabel(0) );
288 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", bar
->GetLabel(MenuTestCase_Foo
) );
291 CPPUNIT_ASSERT_EQUAL( "&File", filemenu
->GetTitle() );
292 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", filemenu
->GetLabel(MenuTestCase_Foo
) );
295 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", itemFoo
->GetItemLabel() );
297 // These return labels stripped of mnemonics/accelerators:
300 CPPUNIT_ASSERT_EQUAL( "File", bar
->GetMenuLabelText(0) );
303 CPPUNIT_ASSERT_EQUAL( "Foo", filemenu
->GetLabelText(MenuTestCase_Foo
) );
306 CPPUNIT_ASSERT_EQUAL( "Foo", itemFoo
->GetItemLabelText() );
307 CPPUNIT_ASSERT_EQUAL( "Foo", wxMenuItem::GetLabelText("&Foo\tCtrl-F") );
310 void MenuTestCase::RadioItems()
312 wxMenuBar
* const bar
= m_frame
->GetMenuBar();
313 wxMenu
* const menu
= new wxMenu
;
314 bar
->Append(menu
, "&Radio");
316 // Adding consecutive radio items creates a radio group.
317 menu
->AppendRadioItem(MenuTestCase_First
, "Radio 0");
318 menu
->AppendRadioItem(MenuTestCase_First
+ 1, "Radio 1");
320 // First item of a radio group is checked by default.
321 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
) );
323 // Checking the second one make the first one unchecked however.
324 menu
->Check(MenuTestCase_First
+ 1, true);
325 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
) );
326 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 1) );
328 // Adding more radio items after a separator creates another radio group...
329 menu
->AppendSeparator();
330 menu
->AppendRadioItem(MenuTestCase_First
+ 2, "Radio 2");
331 menu
->AppendRadioItem(MenuTestCase_First
+ 3, "Radio 3");
332 menu
->AppendRadioItem(MenuTestCase_First
+ 4, "Radio 4");
334 // ... which is independent from the first one.
335 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 2) );
337 menu
->Check(MenuTestCase_First
+ 3, true);
338 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 3) );
339 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
+ 2) );
340 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 1) );
343 // Insert an item in the middle of an existing radio group.
344 menu
->InsertRadioItem(4, MenuTestCase_First
+ 5, "Radio 5");
345 CPPUNIT_ASSERT( menu
->IsChecked(MenuTestCase_First
+ 3) );
347 menu
->Check( MenuTestCase_First
+ 5, true );
348 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
+ 3) );
351 // Prepend a couple of items before the first group.
352 menu
->PrependRadioItem(MenuTestCase_First
+ 6, "Radio 6");
353 menu
->PrependRadioItem(MenuTestCase_First
+ 7, "Radio 7");
354 menu
->Check(MenuTestCase_First
+ 7, true);
355 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
+ 1) );
358 // Check that the last radio group still works as expected.
359 menu
->Check(MenuTestCase_First
+ 4, true);
360 CPPUNIT_ASSERT( !menu
->IsChecked(MenuTestCase_First
+ 5) );