+// Create a menu bar with a single menu containing wxID_APPLY menu item and
+// attach it to the specified frame.
+wxMenu* CreateTestMenu(wxFrame* frame)
+{
+ wxMenu* const menu = new wxMenu;
+ menu->Append(wxID_APPLY);
+ wxMenuBar* const mb = new wxMenuBar;
+ mb->Append(menu, "&Menu");
+ frame->SetMenuBar(mb);
+
+ return menu;
+}
+
+// Helper for checking that the menu event processing resulted in the expected
+// output from the handlers.
+//
+// Notice that this is supposed to be used with ASSERT_MENU_EVENT_RESULT()
+// macro to make the file name and line number of the caller appear in the
+// failure messages.
+void
+CheckMenuEvent(wxMenu* menu, const char* result, CppUnit::SourceLine sourceLine)
+{
+ g_str.clear();
+
+ // Trigger the menu event: this is more reliable than using
+ // wxUIActionSimulator and currently works in all ports as they all call
+ // wxMenuBase::SendEvent() from their respective menu event handlers.
+ menu->SendEvent(wxID_APPLY);
+
+ CPPUNIT_NS::assertEquals( result, g_str, sourceLine, "" );
+}
+
+#define ASSERT_MENU_EVENT_RESULT(menu, result) \
+ CheckMenuEvent((menu), (result), CPPUNIT_SOURCELINE())
+
+void EventPropagationTestCase::MenuEvent()
+{
+ wxFrame* const frame = static_cast<wxFrame*>(wxTheApp->GetTopWindow());
+
+ // Create a minimal menu bar.
+ wxMenu* const menu = CreateTestMenu(frame);
+ wxMenuBar* const mb = menu->GetMenuBar();
+ wxScopedPtr<wxMenuBar> ensureMenuBarDestruction(mb);
+ wxON_BLOCK_EXIT_OBJ1( *frame, wxFrame::SetMenuBar, (wxMenuBar*)NULL );
+
+ // Check that wxApp gets the event exactly once.
+ ASSERT_MENU_EVENT_RESULT( menu, "aA" );
+
+
+ // Check that the menu event handler is called.
+ TestMenuEvtHandler hm('m'); // 'm' for "menu"
+ menu->SetNextHandler(&hm);
+ wxON_BLOCK_EXIT_OBJ1( *menu,
+ wxEvtHandler::SetNextHandler, (wxEvtHandler*)NULL );
+ ASSERT_MENU_EVENT_RESULT( menu, "aomA" );
+
+
+ // Test that the event handler associated with the menu bar gets the event.
+ TestMenuEvtHandler hb('b'); // 'b' for "menu Bar"
+ mb->PushEventHandler(&hb);
+ wxON_BLOCK_EXIT_OBJ1( *mb, wxWindow::PopEventHandler, false );
+
+ ASSERT_MENU_EVENT_RESULT( menu, "aomobA" );
+
+
+ // Also test that the window to which the menu belongs gets the event.
+ TestMenuEvtHandler hw('w'); // 'w' for "Window"
+ frame->PushEventHandler(&hw);
+ wxON_BLOCK_EXIT_OBJ1( *frame, wxWindow::PopEventHandler, false );
+
+ ASSERT_MENU_EVENT_RESULT( menu, "aomobowA" );
+}
+
+// Minimal viable implementations of wxDocument and wxView.
+class EventTestDocument : public wxDocument
+{
+public:
+ EventTestDocument() { }
+
+ wxDECLARE_DYNAMIC_CLASS(EventTestDocument);
+};
+
+class EventTestView : public wxView
+{
+public:
+ EventTestView() { }
+
+ virtual void OnDraw(wxDC*) { }
+
+ wxDECLARE_DYNAMIC_CLASS(EventTestView);
+};
+
+wxIMPLEMENT_DYNAMIC_CLASS(EventTestDocument, wxDocument);
+wxIMPLEMENT_DYNAMIC_CLASS(EventTestView, wxView);
+
+void EventPropagationTestCase::DocView()
+{
+ // Set up the parent frame and its menu bar.
+ wxDocManager docManager;
+
+ wxScopedPtr<wxDocMDIParentFrame>
+ parent(new wxDocMDIParentFrame(&docManager, NULL, wxID_ANY, "Parent"));
+
+ wxMenu* const menu = CreateTestMenu(parent.get());
+
+
+ // Set up the event handlers.
+ TestEvtSink sinkDM('m');
+ docManager.Connect(wxEVT_MENU,
+ wxEventHandler(TestEvtSink::Handle), NULL, &sinkDM);
+
+ TestEvtSink sinkParent('p');
+ parent->Connect(wxEVT_MENU,
+ wxEventHandler(TestEvtSink::Handle), NULL, &sinkParent);
+
+
+ // Check that wxDocManager and wxFrame get the event in order.
+ ASSERT_MENU_EVENT_RESULT( menu, "ampA" );
+
+
+ // Now check what happens if we have an active document.
+ wxDocTemplate docTemplate(&docManager, "Test", "", "", "",
+ "Test Document", "Test View",
+ wxCLASSINFO(EventTestDocument),
+ wxCLASSINFO(EventTestView));
+ wxDocument* const doc = docTemplate.CreateDocument("");
+ wxView* const view = doc->GetFirstView();
+
+ wxScopedPtr<wxFrame>
+ child(new wxDocMDIChildFrame(doc, view, parent.get(), wxID_ANY, "Child"));
+
+ wxMenu* const menuChild = CreateTestMenu(child.get());
+
+#ifdef __WXGTK__
+ // There are a lot of hacks related to child frame menu bar handling in
+ // wxGTK and, in particular, the code in src/gtk/mdi.cpp relies on getting
+ // idle events to really put everything in place. Moreover, as wxGTK uses
+ // GtkNotebook as its MDI pages container, the frame must be shown for all
+ // this to work as gtk_notebook_set_current_page() doesn't do anything if
+ // called for a hidden window (this incredible fact cost me quite some time
+ // to find empirically -- only to notice its confirmation in GTK+
+ // documentation immediately afterwards). So just do whatever it takes to
+ // make things work "as usual".
+ child->Show();
+ parent->Show();
+ wxYield();
+#endif // __WXGTK__
+
+ TestEvtSink sinkDoc('d');
+ doc->Connect(wxEVT_MENU,
+ wxEventHandler(TestEvtSink::Handle), NULL, &sinkDoc);
+
+ TestEvtSink sinkView('v');
+ view->Connect(wxEVT_MENU,
+ wxEventHandler(TestEvtSink::Handle), NULL, &sinkView);
+
+ TestEvtSink sinkChild('c');
+ child->Connect(wxEVT_MENU,
+ wxEventHandler(TestEvtSink::Handle), NULL, &sinkChild);
+
+ // Check that wxDocument, wxView, wxDocManager, child frame and the parent
+ // get the event in order.
+ ASSERT_MENU_EVENT_RESULT( menuChild, "advmcpA" );
+}