00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00012
00013 #ifdef __GNUG__
00014 #pragma implementation "EventSimulationHelper.h"
00015 #endif
00016
00017 #include <wxGuiTest/EventSimulationHelper.h>
00018
00019 #include <cppunit/TestAssert.h>
00020
00021 #include <wx/treectrl.h>
00022 #include <wx/notebook.h>
00023 #include <wx/spinctrl.h>
00024
00025 using namespace wxTst;
00026
00027
00028 bool EventSimulationHelper::s_isSettingSpinCtrlValue = false;
00029
00030
00031 EventSimulationHelper::EventSimulationHelper ()
00032 {
00033
00034 }
00035
00036
00037 EventSimulationHelper::~EventSimulationHelper ()
00038 {
00039
00040 }
00041
00042
00043 void EventSimulationHelper::SelectMenuItem (int id, wxWindow *frame)
00044 {
00045 wxCommandEvent evt (wxEVT_COMMAND_MENU_SELECTED, id);
00046 evt.SetEventObject (frame);
00047 ::wxPostEvent (frame->GetEventHandler (), evt);
00048
00049
00050 }
00051
00052
00053 void EventSimulationHelper::SelectMenuItem (int id,
00054 wxEvtHandler *evtHandler)
00055 {
00056 wxCommandEvent evt (wxEVT_COMMAND_MENU_SELECTED, id);
00057 evt.SetEventObject (evtHandler);
00058 ::wxPostEvent (evtHandler, evt);
00059 }
00060
00061
00062 void EventSimulationHelper::SelectAndCheckMenuItem (int id,
00063 wxWindow *window, wxMenu **menu)
00064 {
00065
00066 wxMenuItem *menuItem = NULL;
00067 if (menu == NULL) {
00068
00069 wxFrame *frame = wxDynamicCast (window, wxFrame);
00070 CPPUNIT_ASSERT_MESSAGE ("'window' is not a wxFrame, setting menu is required",
00071 frame != NULL);
00072 wxMenuBar *menuBar = frame->GetMenuBar ();
00073 CPPUNIT_ASSERT_MESSAGE ("'window' being a wxFrame does not contain a menu bar",
00074 menuBar != NULL);
00075 menuItem = menuBar->FindItem (id);
00076 CPPUNIT_ASSERT_MESSAGE ("Menu item specified with 'id' not found in menu bar",
00077 menuItem != NULL);
00078
00079 } else {
00080
00081 menuItem = (*menu)->FindItem (id, menu);
00082 CPPUNIT_ASSERT_MESSAGE ("Menu item specified with 'id' not found in 'menu'",
00083 menuItem != NULL);
00084 }
00085 menuItem->Check (!menuItem->IsChecked ());
00086
00087 wxCommandEvent evt (wxEVT_COMMAND_MENU_SELECTED, id);
00088 evt.SetEventObject (window);
00089 evt.SetInt (1);
00090 ::wxPostEvent (window->GetEventHandler (), evt);
00091 }
00092
00093
00094 void EventSimulationHelper::SelectAndCheckMenuItem (int id, wxMenu *menu)
00095 {
00096
00097 wxMenuItem *menuItem = menu->FindItem (id, &menu);
00098 CPPUNIT_ASSERT_MESSAGE ("Menu item specified with 'id' not found in 'menu'",
00099 menuItem != NULL);
00100 menuItem->Check (!menuItem->IsChecked ());
00101
00102 wxCommandEvent evt (wxEVT_COMMAND_MENU_SELECTED, id);
00103 evt.SetEventObject (menu);
00104 evt.SetInt (1);
00105 ::wxPostEvent (menu, evt);
00106 }
00107
00108
00109 void EventSimulationHelper::ClickButton (int id, wxWindow *parent)
00110 {
00111 wxCommandEvent evt (wxEVT_COMMAND_BUTTON_CLICKED, id);
00112 evt.SetEventObject (parent);
00113 ::wxPostEvent (parent->GetEventHandler (), evt);
00114 }
00115
00116
00117 void EventSimulationHelper::SetTextCtrlValue (
00118 wxTextCtrl *textCtrl, const wxString &value)
00119 {
00120 textCtrl->SetValue (value);
00121
00122 #ifdef __WXGTK__
00123 # if wxCHECK_VERSION(2, 7, 0)
00124 # if wxCHECK_VERSION(2, 8, 0)
00125 # else
00126 wxCommandEvent evt (wxEVT_COMMAND_TEXT_UPDATED, textCtrl->GetId ());
00127 evt.SetEventObject (textCtrl);
00128 evt.SetString (value);
00129 ::wxPostEvent (textCtrl->GetEventHandler (), evt);
00130 # endif
00131 # endif
00132 #endif
00133 }
00134
00135
00136 wxTreeItemId EventSimulationHelper::GetNthTreeChild (wxTreeCtrl *treeCtrl, unsigned int idx, const wxTreeItemId &id)
00137 {
00138 wxASSERT (idx > 0);
00139
00140 wxTreeItemId cur;
00141 wxTreeItemIdValue cookie;
00142 cur = treeCtrl->GetFirstChild (id, cookie);
00143 for (unsigned int i = 1; i < idx; i++) {
00144
00145 cur = treeCtrl->GetNextChild (id, cookie);
00146 }
00147
00148 return cur;
00149 }
00150
00151
00152 void EventSimulationHelper::SelectTreeItem (const wxTreeItemId &id,
00153 wxTreeCtrl *treeCtrl)
00154 {
00155 wxTreeEvent evt (wxEVT_COMMAND_TREE_SEL_CHANGED, treeCtrl->GetId ());
00156 evt.SetItem (id);
00157 evt.SetEventObject (treeCtrl);
00158
00159
00160
00161
00162
00163 treeCtrl->GetEventHandler ()->ProcessEvent (evt);
00164
00165
00166 treeCtrl->SelectItem (id);
00167 }
00168
00169
00170 void EventSimulationHelper::RightClickTreeItem (
00171 const wxTreeItemId &id, wxTreeCtrl *treeCtrl)
00172 {
00173 wxRect treeItemRect;
00174 bool isOk = treeCtrl->GetBoundingRect (id, treeItemRect, true);
00175 CPPUNIT_ASSERT_MESSAGE ("Tree item bounding box query failed", isOk);
00176
00177 wxPoint treeItemCenter;
00178 treeItemCenter.x = treeItemRect.x + treeItemRect.width / 2;
00179 treeItemCenter.y = treeItemRect.y + treeItemRect.height / 2;
00180
00181 wxTreeEvent evt (wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, treeCtrl->GetId ());
00182 evt.SetItem (id);
00183 evt.SetPoint (treeItemCenter);
00184 evt.SetEventObject (treeCtrl);
00185
00186
00187
00188 treeCtrl->GetEventHandler ()->ProcessEvent (evt);
00189 }
00190
00191
00192 void EventSimulationHelper::SelectNotebookPage (wxNotebook *notebook,
00193 unsigned int page)
00194 {
00195
00196 notebook->SetSelection (page);
00197 }
00198
00199
00200 void EventSimulationHelper::SelectChoiceItem (wxChoice *choice,
00201 unsigned int item)
00202 {
00203
00204 choice->SetSelection (item);
00205
00206 wxCommandEvent evt (wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId ());
00207 evt.SetInt (item);
00208 evt.SetEventObject (choice);
00209 evt.SetString (choice->GetString (item));
00210 ::wxPostEvent (choice->GetEventHandler (), evt);
00211 }
00212
00213
00214 void EventSimulationHelper::SetCheckboxState (wxCheckBox *checkbox,
00215 bool isChecked)
00216 {
00217
00218 checkbox->SetValue (isChecked);
00219
00220 wxCommandEvent evt (wxEVT_COMMAND_CHECKBOX_CLICKED, checkbox->GetId ());
00221 evt.SetEventObject (checkbox);
00222 evt.SetInt (isChecked);
00223 ::wxPostEvent (checkbox->GetEventHandler (), evt);
00224 }
00225
00226
00227 void EventSimulationHelper::SelectRadioBoxItem (wxRadioBox *radioBox,
00228 unsigned int item)
00229 {
00230
00231 radioBox->SetSelection (item);
00232
00233 wxCommandEvent evt (wxEVT_COMMAND_RADIOBOX_SELECTED, radioBox->GetId ());
00234 evt.SetEventObject (radioBox);
00235 evt.SetInt (item);
00236 evt.SetString (radioBox->GetString (item));
00237 ::wxPostEvent (radioBox->GetEventHandler (), evt);
00238 }
00239
00240
00241 void EventSimulationHelper::SetSliderValue (wxSlider *slider,
00242 unsigned int value)
00243 {
00244
00245 slider->SetValue (value);
00246
00247 wxCommandEvent evt2 (wxEVT_COMMAND_SLIDER_UPDATED, slider->GetId ());
00248 evt2.SetEventObject (slider);
00249 evt2.SetInt (value);
00250 ::wxPostEvent (slider->GetEventHandler (), evt2);
00251
00252 wxCommandEvent evt (wxEVT_SCROLL_THUMBRELEASE, slider->GetId ());
00253 evt.SetEventObject (slider);
00254 evt.SetInt (value);
00255 ::wxPostEvent (slider->GetEventHandler (), evt);
00256 }
00257
00258
00259 void EventSimulationHelper::SetSpinCtrlValue (wxSpinCtrl *spinCtrl,
00260 int value)
00261 {
00262
00263 EventSimulationHelper::s_isSettingSpinCtrlValue = true;
00264 spinCtrl->SetValue (value);
00265 EventSimulationHelper::s_isSettingSpinCtrlValue = false;
00266
00267 wxCommandEvent evt (wxEVT_COMMAND_SPINCTRL_UPDATED, spinCtrl->GetId ());
00268 evt.SetEventObject (spinCtrl);
00269 evt.SetInt (value);
00270 ::wxPostEvent (spinCtrl->GetEventHandler (), evt);
00271 }
00272
00273
00274 bool EventSimulationHelper::IsSettingSpinCtrlValue ()
00275 {
00276 return EventSimulationHelper::s_isSettingSpinCtrlValue;
00277 }
00278
00279
00280 void EventSimulationHelper::ToggleToolOnlyEvent (int id,
00281 bool enabled, wxWindow *parent)
00282 {
00283 wxCommandEvent evt (wxEVT_COMMAND_TOOL_CLICKED, id);
00284 evt.SetEventObject (parent);
00285 evt.SetInt (enabled);
00286 ::wxPostEvent (parent->GetEventHandler (), evt);
00287 }
00288
00289
00290 void EventSimulationHelper::ToggleTool (int id, bool enabled,
00291 wxToolBar *toolbar, wxWindow *parent)
00292 {
00293
00294 toolbar->ToggleTool (id, enabled);
00295
00296 wxCommandEvent evt (wxEVT_COMMAND_TOOL_CLICKED, id);
00297 evt.SetEventObject (parent);
00298 evt.SetInt (enabled);
00299 ::wxPostEvent (parent->GetEventHandler (), evt);
00300 }