Correct form of mnemonics returned by wxGTK wxMenu::GetTitle().
[wxWidgets.git] / tests / menu / menu.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/menu/menu.cpp
3 // Purpose: wxMenu unit test
4 // Author: wxWidgets team
5 // Created: 2010-11-10
6 // RCS-ID: $Id$
7 // Copyright: (c) 2010 wxWidgets team
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/wx.h"
22 #endif // WX_PRECOMP
23
24 #include "wx/menu.h"
25 #include <stdarg.h>
26
27 // ----------------------------------------------------------------------------
28 // helper
29 // ----------------------------------------------------------------------------
30
31 namespace
32 {
33
34 enum
35 {
36 MenuTestCase_Foo = 10000,
37 MenuTestCase_Bar,
38 MenuTestCase_First
39 };
40
41 void PopulateMenu(wxMenu* menu, const wxString& name, size_t& itemcount)
42 {
43 // Start at item 1 to make it human-readable ;)
44 for (int n=1; n<6; ++n, ++itemcount)
45 {
46 wxString label = name; label << n;
47 menu->Append(MenuTestCase_First + itemcount, label, label + " help string");
48 }
49 }
50
51 void RecursivelyCountMenuItems(const wxMenu* menu, size_t& count)
52 {
53 CPPUNIT_ASSERT( menu );
54
55 count += menu->GetMenuItemCount();
56 for (size_t n=0; n < menu->GetMenuItemCount(); ++n)
57 {
58 wxMenuItem* item = menu->FindItemByPosition(n);
59 if (item->IsSubMenu())
60 {
61 RecursivelyCountMenuItems(item->GetSubMenu(), count);
62 }
63 }
64 }
65
66 } // anon namespace
67
68
69 // ----------------------------------------------------------------------------
70 // test class
71 // ----------------------------------------------------------------------------
72
73 class MenuTestCase : public CppUnit::TestCase
74 {
75 public:
76 MenuTestCase() {}
77
78 virtual void setUp() { CreateFrame(); }
79 virtual void tearDown() { m_frame->Destroy(); }
80
81 private:
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();
88
89 void CreateFrame();
90
91 void FindInMenubar();
92 void FindInMenu();
93 void Count();
94 void Labels();
95
96 wxFrame* m_frame;
97
98 // Holds the number of menuitems contained in all the menus
99 size_t m_itemCount;
100
101 // Store here the id of a known submenu item, to be searched for later
102 int m_submenuItemId;
103
104 // and a sub-submenu item
105 int m_subsubmenuItemId;
106
107 wxArrayString m_menuLabels;
108
109 DECLARE_NO_COPY_CLASS(MenuTestCase)
110 };
111
112 // register in the unnamed registry so that these tests are run by default
113 CPPUNIT_TEST_SUITE_REGISTRATION( MenuTestCase );
114
115 // also include in it's own registry so that these tests can be run alone
116 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MenuTestCase, "MenuTestCase" );
117
118 void MenuTestCase::CreateFrame()
119 {
120 m_frame = new wxFrame(NULL, wxID_ANY, "test frame");
121
122 wxMenu *fileMenu = new wxMenu;
123 wxMenu *helpMenu = new wxMenu;
124 wxMenu *subMenu = new wxMenu;
125 wxMenu *subsubMenu = new wxMenu;
126
127 size_t itemcount = 0;
128
129 PopulateMenu(subsubMenu, "Subsubmenu item ", itemcount);
130
131 // Store one of its IDs for later
132 m_subsubmenuItemId = MenuTestCase_First + itemcount - 2;
133
134 PopulateMenu(subMenu, "Submenu item ", itemcount);
135
136 // Store one of its IDs for later
137 m_submenuItemId = MenuTestCase_First + itemcount - 2;
138
139 subMenu->AppendSubMenu(subsubMenu, "Subsubmen&u", "Test a subsubmenu");
140
141 PopulateMenu(fileMenu, "Filemenu item ", itemcount);
142
143 fileMenu->Append(MenuTestCase_Foo, "&Foo\tCtrl-F", "Test item to be found");
144
145
146 PopulateMenu(helpMenu, "Helpmenu item ", itemcount);
147 helpMenu->Append(MenuTestCase_Bar, "Bar");
148 helpMenu->AppendSubMenu(subMenu, "Sub&menu", "Test a submenu");
149
150 // +2 for "Foo" and "Bar", +2 for the 2 submenus
151 m_itemCount = itemcount + 4;
152
153 // Use an arraystring here, to help with future tests
154 m_menuLabels.Add("&File");
155 m_menuLabels.Add("&Help");
156
157 wxMenuBar *menuBar = new wxMenuBar();
158 menuBar->Append(fileMenu, m_menuLabels[0]);
159 menuBar->Append(helpMenu, m_menuLabels[1]);
160 m_frame->SetMenuBar(menuBar);
161 }
162
163 void MenuTestCase::FindInMenubar()
164 {
165 wxMenuBar* bar = m_frame->GetMenuBar();
166
167 // Find by name:
168 CPPUNIT_ASSERT( bar->FindMenu("File") != wxNOT_FOUND );
169 CPPUNIT_ASSERT( bar->FindMenu("&File") != wxNOT_FOUND );
170 CPPUNIT_ASSERT( bar->FindMenu("&Fail") == wxNOT_FOUND );
171
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 );
180 // and title
181 wxString menutitle = bar->GetMenu(index)->GetTitle();
182 CPPUNIT_ASSERT( bar->FindMenuItem(menutitle, "&Foo") != wxNOT_FOUND );
183
184 // Find by position:
185 for (size_t n=0; n < bar->GetMenuCount(); ++n)
186 {
187 CPPUNIT_ASSERT( bar->GetMenu(n) );
188 }
189
190 // Find by id:
191 wxMenu* menu = NULL;
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) );
198
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 );
207 }
208
209 void MenuTestCase::FindInMenu()
210 {
211 wxMenuBar* bar = m_frame->GetMenuBar();
212
213 // Find by name:
214 wxMenu* menuFind = bar->GetMenu(0);
215 CPPUNIT_ASSERT( menuFind->FindItem("Foo") != wxNOT_FOUND );
216 CPPUNIT_ASSERT( menuFind->FindItem("&Foo") != wxNOT_FOUND );
217 // and for submenus
218 wxMenu* menuHelp = bar->GetMenu(1);
219 CPPUNIT_ASSERT( menuHelp->FindItem("Submenu") != wxNOT_FOUND );
220 CPPUNIT_ASSERT( menuHelp->FindItem("Sub&menu") != wxNOT_FOUND );
221
222 // Find by position:
223 size_t n;
224 for (n=0; n < menuHelp->GetMenuItemCount(); ++n)
225 {
226 CPPUNIT_ASSERT( menuHelp->FindItemByPosition(n) );
227 }
228
229 // Find by id:
230 CPPUNIT_ASSERT( menuHelp->FindItem(MenuTestCase_Bar) );
231 CPPUNIT_ASSERT( menuHelp->FindItem(MenuTestCase_Foo) == NULL );
232
233 for (n=0; n < menuHelp->GetMenuItemCount(); ++n)
234 {
235 size_t locatedAt;
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 );
241 }
242
243 // Find submenu item:
244 for (n=0; n < menuHelp->GetMenuItemCount(); ++n)
245 {
246 wxMenuItem* item = menuHelp->FindItemByPosition(n);
247 if (item->IsSubMenu())
248 {
249 wxMenu* submenu;
250 wxMenuItem* submenuItem = menuHelp->FindItem(m_submenuItemId, &submenu);
251 CPPUNIT_ASSERT( submenuItem );
252 CPPUNIT_ASSERT( item->GetSubMenu() == submenu );
253 }
254 }
255 }
256
257 void MenuTestCase::Count()
258 {
259 wxMenuBar* bar = m_frame->GetMenuBar();
260 // I suppose you could call this "counting menubars" :)
261 CPPUNIT_ASSERT( bar );
262
263 CPPUNIT_ASSERT_EQUAL( bar->GetMenuCount(), 2 );
264
265 size_t count = 0;
266 for (size_t n=0; n < bar->GetMenuCount(); ++n)
267 {
268 RecursivelyCountMenuItems(bar->GetMenu(n), count);
269 }
270 CPPUNIT_ASSERT_EQUAL( count, m_itemCount );
271 }
272
273 void MenuTestCase::Labels()
274 {
275 wxMenuBar* bar = m_frame->GetMenuBar();
276 CPPUNIT_ASSERT( bar );
277 wxMenu* filemenu;
278 wxMenuItem* itemFoo = bar->FindItem(MenuTestCase_Foo, &filemenu);
279 CPPUNIT_ASSERT( itemFoo );
280 CPPUNIT_ASSERT( filemenu );
281
282 // These return labels including mnemonics/accelerators:
283
284 // wxMenuBar
285 CPPUNIT_ASSERT_EQUAL( "&File", bar->GetMenuLabel(0) );
286 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", bar->GetLabel(MenuTestCase_Foo) );
287
288 // wxMenu
289 CPPUNIT_ASSERT_EQUAL( "&File", filemenu->GetTitle() );
290 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", filemenu->GetLabel(MenuTestCase_Foo) );
291
292 // wxMenuItem
293 CPPUNIT_ASSERT_EQUAL( "&Foo\tCtrl-F", itemFoo->GetItemLabel() );
294
295 // These return labels stripped of mnemonics/accelerators:
296
297 // wxMenuBar
298 CPPUNIT_ASSERT_EQUAL( "File", bar->GetMenuLabelText(0) );
299
300 // wxMenu
301 CPPUNIT_ASSERT_EQUAL( "Foo", filemenu->GetLabelText(MenuTestCase_Foo) );
302
303 // wxMenuItem
304 CPPUNIT_ASSERT_EQUAL( "Foo", itemFoo->GetItemLabelText() );
305 CPPUNIT_ASSERT_EQUAL( "Foo", wxMenuItem::GetLabelText("&Foo\tCtrl-F") );
306 }