Don't use some "recent" C++98 features not supported by VC6.
[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_SUITE_END();
87
88 void CreateFrame();
89
90 void FindInMenubar();
91 void FindInMenu();
92 void Count();
93
94 wxFrame* m_frame;
95
96 // Holds the number of menuitems contained in all the menus
97 size_t m_itemCount;
98
99 // Store here the id of a known submenu item, to be searched for later
100 int m_submenuItemId;
101
102 // and a sub-submenu item
103 int m_subsubmenuItemId;
104
105 wxArrayString m_menuLabels;
106
107 DECLARE_NO_COPY_CLASS(MenuTestCase)
108 };
109
110 // register in the unnamed registry so that these tests are run by default
111 CPPUNIT_TEST_SUITE_REGISTRATION( MenuTestCase );
112
113 // also include in it's own registry so that these tests can be run alone
114 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MenuTestCase, "MenuTestCase" );
115
116 void MenuTestCase::CreateFrame()
117 {
118 m_frame = new wxFrame(NULL, wxID_ANY, "test frame");
119
120 wxMenu *fileMenu = new wxMenu;
121 wxMenu *helpMenu = new wxMenu;
122 wxMenu *subMenu = new wxMenu;
123 wxMenu *subsubMenu = new wxMenu;
124
125 size_t itemcount = 0;
126
127 PopulateMenu(subsubMenu, "Subsubmenu item ", itemcount);
128
129 // Store one of its IDs for later
130 m_subsubmenuItemId = MenuTestCase_First + itemcount - 2;
131
132 PopulateMenu(subMenu, "Submenu item ", itemcount);
133
134 // Store one of its IDs for later
135 m_submenuItemId = MenuTestCase_First + itemcount - 2;
136
137 subMenu->AppendSubMenu(subsubMenu, "Subsubmen&u", "Test a subsubmenu");
138
139 PopulateMenu(fileMenu, "Filemenu item ", itemcount);
140
141 fileMenu->Append(MenuTestCase_Foo, "&Foo\tCtrl-F", "Test item to be found");
142
143
144 PopulateMenu(helpMenu, "Helpmenu item ", itemcount);
145 helpMenu->Append(MenuTestCase_Bar, "Bar");
146 helpMenu->AppendSubMenu(subMenu, "Sub&menu", "Test a submenu");
147
148 // +2 for "Foo" and "Bar", +2 for the 2 submenus
149 m_itemCount = itemcount + 4;
150
151 // Use an arraystring here, to help with future tests
152 m_menuLabels.Add("&File");
153 m_menuLabels.Add("&Help");
154
155 wxMenuBar *menuBar = new wxMenuBar();
156 menuBar->Append(fileMenu, m_menuLabels[0]);
157 menuBar->Append(helpMenu, m_menuLabels[1]);
158 m_frame->SetMenuBar(menuBar);
159 }
160
161 void MenuTestCase::FindInMenubar()
162 {
163 wxMenuBar* bar = m_frame->GetMenuBar();
164
165 // Find by name:
166 CPPUNIT_ASSERT( bar->FindMenu("File") != wxNOT_FOUND );
167 CPPUNIT_ASSERT( bar->FindMenu("&File") != wxNOT_FOUND );
168 CPPUNIT_ASSERT( bar->FindMenu("&Fail") == wxNOT_FOUND );
169
170 // Find by menu name plus item name:
171 CPPUNIT_ASSERT( bar->FindMenuItem("File", "Foo") != wxNOT_FOUND );
172 CPPUNIT_ASSERT( bar->FindMenuItem("&File", "&Foo") != wxNOT_FOUND );
173 // and using the menu title
174 int index = bar->FindMenu("&File");
175 CPPUNIT_ASSERT( index != wxNOT_FOUND );
176 wxString menutitle = bar->GetMenuLabel(index);
177 CPPUNIT_ASSERT( bar->FindMenuItem(menutitle, "&Foo") != wxNOT_FOUND );
178
179 // Find by position:
180 for (size_t n=0; n < bar->GetMenuCount(); ++n)
181 {
182 CPPUNIT_ASSERT( bar->GetMenu(n) );
183 }
184
185 // Find by id:
186 wxMenu* menu = NULL;
187 wxMenuItem* item = NULL;
188 item = bar->FindItem(MenuTestCase_Foo, &menu);
189 CPPUNIT_ASSERT( item );
190 CPPUNIT_ASSERT( menu );
191 // Check that the correct menu was found
192 CPPUNIT_ASSERT( menu->FindChildItem(MenuTestCase_Foo) );
193
194 // Find submenu item:
195 item = bar->FindItem(m_submenuItemId, &menu);
196 CPPUNIT_ASSERT( item );
197 CPPUNIT_ASSERT( menu );
198 // and, for completeness, a submenu one:
199 item = bar->FindItem(m_subsubmenuItemId, &menu);
200 CPPUNIT_ASSERT( item );
201 CPPUNIT_ASSERT( menu );
202 }
203
204 void MenuTestCase::FindInMenu()
205 {
206 wxMenuBar* bar = m_frame->GetMenuBar();
207
208 // Find by name:
209 wxMenu* menuFind = bar->GetMenu(0);
210 CPPUNIT_ASSERT( menuFind->FindItem("Foo") != wxNOT_FOUND );
211 CPPUNIT_ASSERT( menuFind->FindItem("&Foo") != wxNOT_FOUND );
212 // and for submenus
213 wxMenu* menuHelp = bar->GetMenu(1);
214 CPPUNIT_ASSERT( menuHelp->FindItem("Submenu") != wxNOT_FOUND );
215 CPPUNIT_ASSERT( menuHelp->FindItem("Sub&menu") != wxNOT_FOUND );
216
217 // Find by position:
218 size_t n;
219 for (n=0; n < menuHelp->GetMenuItemCount(); ++n)
220 {
221 CPPUNIT_ASSERT( menuHelp->FindItemByPosition(n) );
222 }
223
224 // Find by id:
225 CPPUNIT_ASSERT( menuHelp->FindItem(MenuTestCase_Bar) );
226 CPPUNIT_ASSERT( menuHelp->FindItem(MenuTestCase_Foo) == NULL );
227
228 for (n=0; n < menuHelp->GetMenuItemCount(); ++n)
229 {
230 size_t locatedAt;
231 wxMenuItem* itemByPos = menuHelp->FindItemByPosition(n);
232 CPPUNIT_ASSERT( itemByPos );
233 wxMenuItem* itemById = menuHelp->FindChildItem(itemByPos->GetId(), &locatedAt);
234 CPPUNIT_ASSERT_EQUAL( itemByPos, itemById );
235 CPPUNIT_ASSERT_EQUAL( locatedAt, n );
236 }
237
238 // Find submenu item:
239 for (n=0; n < menuHelp->GetMenuItemCount(); ++n)
240 {
241 wxMenuItem* item = menuHelp->FindItemByPosition(n);
242 if (item->IsSubMenu())
243 {
244 wxMenu* submenu;
245 wxMenuItem* submenuItem = menuHelp->FindItem(m_submenuItemId, &submenu);
246 CPPUNIT_ASSERT( submenuItem );
247 CPPUNIT_ASSERT( item->GetSubMenu() == submenu );
248 }
249 }
250 }
251
252 void MenuTestCase::Count()
253 {
254 wxMenuBar* bar = m_frame->GetMenuBar();
255 // I suppose you could call this "counting menubars" :)
256 CPPUNIT_ASSERT( bar );
257
258 CPPUNIT_ASSERT_EQUAL( bar->GetMenuCount(), 2 );
259
260 size_t count = 0;
261 for (size_t n=0; n < bar->GetMenuCount(); ++n)
262 {
263 RecursivelyCountMenuItems(bar->GetMenu(n), count);
264 }
265 CPPUNIT_ASSERT_EQUAL( count, m_itemCount );
266 }