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_Quit
= wxID_EXIT
,
37 MenuTestCase_About
= wxID_ABOUT
,
38 MenuTestCase_First
= 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_SUITE_END();
96 // Holds the number of menuitems contained in all the menus
99 // Store here the id of a known submenu item, to be searched for later
102 // and a sub-submenu item
103 int m_subsubmenuItemId
;
105 wxArrayString m_menuLabels
;
107 DECLARE_NO_COPY_CLASS(MenuTestCase
)
110 // register in the unnamed registry so that these tests are run by default
111 CPPUNIT_TEST_SUITE_REGISTRATION( MenuTestCase
);
113 // also include in it's own registry so that these tests can be run alone
114 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MenuTestCase
, "MenuTestCase" );
116 void MenuTestCase::CreateFrame()
118 m_frame
= new wxFrame(NULL
, wxID_ANY
, "test frame");
120 wxMenu
*fileMenu
= new wxMenu
;
121 wxMenu
*helpMenu
= new wxMenu
;
122 wxMenu
*subMenu
= new wxMenu
;
123 wxMenu
*subsubMenu
= new wxMenu
;
125 size_t itemcount
= 0;
127 PopulateMenu(subsubMenu
, "Subsubmenu item ", itemcount
);
129 // Store one of its IDs for later
130 m_subsubmenuItemId
= MenuTestCase_First
+ itemcount
- 2;
132 PopulateMenu(subMenu
, "Submenu item ", itemcount
);
134 // Store one of its IDs for later
135 m_submenuItemId
= MenuTestCase_First
+ itemcount
- 2;
137 subMenu
->AppendSubMenu(subsubMenu
, "Subsubmen&u", "Test a subsubmenu");
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");
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");
149 // +2 for E&xit and &About, +2 for the 2 submenus
150 m_itemCount
= itemcount
+ 4;
152 // Use an arraystring here, to help with future tests
153 m_menuLabels
.Add("&File");
154 m_menuLabels
.Add("&Help");
156 wxMenuBar
*menuBar
= new wxMenuBar();
157 menuBar
->Append(fileMenu
, m_menuLabels
[0]);
158 menuBar
->Append(helpMenu
, m_menuLabels
[1]);
159 m_frame
->SetMenuBar(menuBar
);
162 void MenuTestCase::FindInMenubar()
164 wxMenuBar
* bar
= m_frame
->GetMenuBar();
167 CPPUNIT_ASSERT( bar
->FindMenu("File") != wxNOT_FOUND
);
168 CPPUNIT_ASSERT( bar
->FindMenu("&File") != wxNOT_FOUND
);
169 CPPUNIT_ASSERT( bar
->FindMenu("&Fail") == wxNOT_FOUND
);
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
);
181 for (size_t n
=0; n
< bar
->GetMenuCount(); ++n
)
183 CPPUNIT_ASSERT( bar
->GetMenu(n
) );
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
) );
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
);
205 void MenuTestCase::FindInMenu()
207 wxMenuBar
* bar
= m_frame
->GetMenuBar();
210 wxMenu
* menuFind
= bar
->GetMenu(0);
211 CPPUNIT_ASSERT( menuFind
->FindItem("Exit") != wxNOT_FOUND
);
212 CPPUNIT_ASSERT( menuFind
->FindItem("E&xit") != wxNOT_FOUND
);
214 wxMenu
* menuHelp
= bar
->GetMenu(1);
215 CPPUNIT_ASSERT( menuHelp
->FindItem("Submenu") != wxNOT_FOUND
);
216 CPPUNIT_ASSERT( menuHelp
->FindItem("Sub&menu") != wxNOT_FOUND
);
219 for (size_t n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
221 CPPUNIT_ASSERT( menuHelp
->FindItemByPosition(n
) );
225 CPPUNIT_ASSERT( menuHelp
->FindItem(MenuTestCase_About
) );
226 CPPUNIT_ASSERT( menuHelp
->FindItem(MenuTestCase_Quit
) == NULL
);
228 for (size_t n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
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
);
238 // Find submenu item:
239 for (size_t n
=0; n
< menuHelp
->GetMenuItemCount(); ++n
)
241 wxMenuItem
* item
= menuHelp
->FindItemByPosition(n
);
242 if (item
->IsSubMenu())
245 wxMenuItem
* submenuItem
= menuHelp
->FindItem(m_submenuItemId
, &submenu
);
246 CPPUNIT_ASSERT( submenuItem
);
247 CPPUNIT_ASSERT( item
->GetSubMenu() == submenu
);
252 void MenuTestCase::Count()
254 wxMenuBar
* bar
= m_frame
->GetMenuBar();
255 // I suppose you could call this "counting menubars" :)
256 CPPUNIT_ASSERT( bar
);
258 CPPUNIT_ASSERT_EQUAL( bar
->GetMenuCount(), 2 );
261 for (size_t n
=0; n
< bar
->GetMenuCount(); ++n
)
263 RecursivelyCountMenuItems(bar
->GetMenu(n
), count
);
265 CPPUNIT_ASSERT_EQUAL( count
, m_itemCount
);