Add a beginning of wxMenu unit test.
[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_Quit = wxID_EXIT,
37 MenuTestCase_About = wxID_ABOUT,
38 MenuTestCase_First = 10000
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 // Add a 'real' item too, for future tests
141 fileMenu->Append(MenuTestCase_Quit, "E&xit\tAlt-X", "Quit this program");
142
143
144 PopulateMenu(helpMenu, "Helpmenu item ", itemcount);
145 helpMenu->Append(MenuTestCase_About, "&About...\tF1",
146 "(Would normally) Show about dialog");
147 helpMenu->AppendSubMenu(subMenu, "Sub&menu", "Test a submenu");
148
149 // +2 for E&xit and &About, +2 for the 2 submenus
150 m_itemCount = itemcount + 4;
151
152 // Use an arraystring here, to help with future tests
153 m_menuLabels.Add("&File");
154 m_menuLabels.Add("&Help");
155
156 wxMenuBar *menuBar = new wxMenuBar();
157 menuBar->Append(fileMenu, m_menuLabels[0]);
158 menuBar->Append(helpMenu, m_menuLabels[1]);
159 m_frame->SetMenuBar(menuBar);
160 }
161
162 void MenuTestCase::FindInMenubar()
163 {
164 wxMenuBar* bar = m_frame->GetMenuBar();
165
166 // Find by name:
167 CPPUNIT_ASSERT( bar->FindMenu("File") != wxNOT_FOUND );
168 CPPUNIT_ASSERT( bar->FindMenu("&File") != wxNOT_FOUND );
169 CPPUNIT_ASSERT( bar->FindMenu("&Fail") == wxNOT_FOUND );
170
171 // Find by menu name plus item name:
172 CPPUNIT_ASSERT( bar->FindMenuItem("File", "Exit") != wxNOT_FOUND );
173 CPPUNIT_ASSERT( bar->FindMenuItem("&File", "E&xit") != wxNOT_FOUND );
174 // and using the menu title
175 int index = bar->FindMenu("&File");
176 CPPUNIT_ASSERT( index != wxNOT_FOUND );
177 wxString menutitle = bar->GetMenuLabel(index);
178 CPPUNIT_ASSERT( bar->FindMenuItem(menutitle, "E&xit") != wxNOT_FOUND );
179
180 // Find by position:
181 for (size_t n=0; n < bar->GetMenuCount(); ++n)
182 {
183 CPPUNIT_ASSERT( bar->GetMenu(n) );
184 }
185
186 // Find by id:
187 wxMenu* menu = NULL;
188 wxMenuItem* item = NULL;
189 item = bar->FindItem(MenuTestCase_Quit, &menu);
190 CPPUNIT_ASSERT( item );
191 CPPUNIT_ASSERT( menu );
192 // Check that the correct menu was found
193 CPPUNIT_ASSERT( menu->FindChildItem(MenuTestCase_Quit) );
194
195 // Find submenu item:
196 item = bar->FindItem(m_submenuItemId, &menu);
197 CPPUNIT_ASSERT( item );
198 CPPUNIT_ASSERT( menu );
199 // and, for completeness, a submenu one:
200 item = bar->FindItem(m_subsubmenuItemId, &menu);
201 CPPUNIT_ASSERT( item );
202 CPPUNIT_ASSERT( menu );
203 }
204
205 void MenuTestCase::FindInMenu()
206 {
207 wxMenuBar* bar = m_frame->GetMenuBar();
208
209 // Find by name:
210 wxMenu* menuFind = bar->GetMenu(0);
211 CPPUNIT_ASSERT( menuFind->FindItem("Exit") != wxNOT_FOUND );
212 CPPUNIT_ASSERT( menuFind->FindItem("E&xit") != wxNOT_FOUND );
213 // and for submenus
214 wxMenu* menuHelp = bar->GetMenu(1);
215 CPPUNIT_ASSERT( menuHelp->FindItem("Submenu") != wxNOT_FOUND );
216 CPPUNIT_ASSERT( menuHelp->FindItem("Sub&menu") != wxNOT_FOUND );
217
218 // Find by position:
219 for (size_t 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_About) );
226 CPPUNIT_ASSERT( menuHelp->FindItem(MenuTestCase_Quit) == NULL );
227
228 for (size_t 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 (size_t 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 }