LibreOffice Module vcl (master)  1
debugevent.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  */
9 
10 #include <comphelper/random.hxx>
11 #include <rtl/string.hxx>
12 #include <sal/log.hxx>
13 #include <vcl/keycodes.hxx>
14 #include <vcl/svapp.hxx>
15 #include <vcl/wrkwin.hxx>
16 #include <vcl/menu.hxx>
17 #include <debugevent.hxx>
18 #include <window.h>
19 #include <salwtype.hxx>
20 
21 DebugEventInjector::DebugEventInjector( sal_uInt32 nMaxEvents) :
22  Timer("debug event injector")
23  , mnEventsLeft( nMaxEvents )
24 {
25  SetTimeout( 1000 /* ms */ );
26  Start();
27 }
28 
29 static double getRandom()
30 {
32 }
33 
35 {
36  vcl::Window *pParent;
37 
38  if (getRandom() < 0.80)
39  if (vcl::Window * pWindow = Application::GetFocusWindow())
40  return pWindow;
41 
42  if (getRandom() > 0.50 ||
43  !(pParent = Application::GetActiveTopWindow()))
44  {
45  // select a top window at random
46  long nIdx = Application::GetTopWindowCount() * getRandom();
47  pParent = Application::GetTopWindow( nIdx );
48  }
49  assert (pParent != nullptr);
50 
51  std::vector< vcl::Window *> aChildren;
52  pParent->CollectChildren( aChildren );
53 
54  return aChildren[ aChildren.size() * getRandom() ];
55 }
56 
57 
58 static void CollectMenuItemIds( Menu *pMenu, std::vector< SalMenuEvent > &rIds )
59 {
60  sal_uInt16 nItems = pMenu->GetItemCount();
61  for (sal_uInt16 i = 0; i < nItems; i++)
62  {
63  if (pMenu->GetItemType( i ) != MenuItemType::SEPARATOR || getRandom() < 0.01)
64  rIds.emplace_back( pMenu->GetItemId( i ), pMenu );
65  PopupMenu *pPopup = pMenu->GetPopupMenu( i );
66  if (pPopup)
67  CollectMenuItemIds( pPopup, rIds );
68  }
69 }
70 
72 {
74  if (!pFocus)
75  return;
76 
77  SystemWindow *pSysWin = pFocus->GetSystemWindow();
78  if (!pSysWin)
79  return;
80 
81  MenuBar *pMenuBar = pSysWin->GetMenuBar();
82  if (!pMenuBar)
83  return;
84 
85  SalEvent nEvents[] = {
96  };
97 
98  std::vector< SalMenuEvent > aIds;
99  CollectMenuItemIds( pMenuBar, aIds );
100 
101  SalEvent nEvent = nEvents[ static_cast<int>(getRandom() * SAL_N_ELEMENTS( nEvents )) ];
102  SalMenuEvent aEvent = aIds[ getRandom() * aIds.size() ];
103  bool bHandled = ImplWindowFrameProc( pSysWin, nEvent, &aEvent);
104 
105  SAL_INFO( "vcl.debugevent",
106  "Injected menu event " << aEvent.mpMenu
107  << " (" << aEvent.mnId << ") '"
108  << static_cast<Menu *>(aEvent.mpMenu)->GetItemText( aEvent.mnId ) << "' -> "
109  << bHandled );
110 }
111 
112 static void InitKeyEvent( SalKeyEvent &rKeyEvent )
113 {
114  if (getRandom() < 0.01)
115  rKeyEvent.mnRepeat = getRandom() * 20;
116  else
117  rKeyEvent.mnRepeat = 0;
118 }
119 
121 {
122  SalKeyEvent aKeyEvent;
123  vcl::Window *pWindow = ChooseWindow();
124 
125  InitKeyEvent( aKeyEvent );
126 
127  if (getRandom() < 0.10) // Occasionally a truly random event
128  {
129  aKeyEvent.mnCode = getRandom() * KEY_CODE_MASK;
130  aKeyEvent.mnCharCode = getRandom() * 0xffff;
131  }
132  else
133  {
134  static struct {
135  sal_uInt16 nCodeStart, nCodeEnd;
136  char aCharStart;
137  } const nTextCodes[] = {
138  { KEY_0, KEY_9, '0' },
139  { KEY_A, KEY_Z, 'a' }
140  };
141 
142  size_t i = getRandom() * SAL_N_ELEMENTS( nTextCodes );
143  int offset = int( getRandom() * ( nTextCodes[i].nCodeEnd - nTextCodes[i].nCodeStart ) );
144  aKeyEvent.mnCode = nTextCodes[i].nCodeStart + offset;
145  aKeyEvent.mnCharCode = nTextCodes[i].aCharStart + offset;
146 // fprintf( stderr, "Char '%c' offset %d into record %d base '%c'\n",
147 // aKeyEvent.mnCharCode, offset, (int)i, nTextCodes[i].aCharStart );
148  }
149 
150  if( getRandom() < 0.05 ) // modifier
151  aKeyEvent.mnCode |= static_cast<sal_uInt16>( getRandom() * KEY_MODIFIERS_MASK ) & KEY_MODIFIERS_MASK;
152 
153  bool bHandled = ImplWindowFrameProc( pWindow, SalEvent::KeyInput, &aKeyEvent);
154 
155  SAL_INFO( "vcl.debugevent",
156  "Injected key 0x" << std::hex << static_cast<int>(aKeyEvent.mnCode) << std::dec
157  << " -> " << bHandled
158  << " win " << pWindow );
159 
160  ImplWindowFrameProc( pWindow, SalEvent::KeyUp, &aKeyEvent );
161 }
162 
163 /*
164  * The more heuristics we have to inform this the better,
165  * key-bindings, menu entries, allowable entry types etc.
166  */
168 {
169 // fprintf( stderr, "%6d - ", (int)mnEventsLeft );
170 
171  double nRand = getRandom();
172  if (nRand < 0.30)
173  {
174  int nEvents = getRandom() * 10;
175  for (int i = 0; i < nEvents; i++)
176  InjectTextEvent();
177  }
178  else if (nRand < 0.60)
180  else if (nRand < 0.95)
181  InjectMenuEvent();
182 }
183 
185 {
186  vcl::Window *pWindow = ChooseWindow();
187 
188  static struct {
189  double mnProb;
190  sal_uInt16 mnKey;
191  } const nWeights[] = {
192  // edit / escape etc. - 50%
193  { 0.20, KEY_SPACE },
194  { 0.10, KEY_TAB },
195  { 0.07, KEY_RETURN },
196  { 0.05, KEY_DELETE },
197  { 0.05, KEY_BACKSPACE },
198 
199  // navigate - 45%
200  { 0.15, KEY_LEFT },
201  { 0.10, KEY_RIGHT },
202  { 0.05, KEY_UP },
203  { 0.05, KEY_DOWN },
204  { 0.05, KEY_PAGEUP },
205  { 0.05, KEY_PAGEDOWN },
206 
207  // other
208  { 0.01, KEY_INSERT },
209  { 0.02, KEY_HOME },
210  { 0.02, KEY_END },
211  };
212 
213  double d = 0.0, nRand = getRandom();
214  sal_uInt16 nKey = KEY_SPACE;
215  for (auto & rWeight : nWeights)
216  {
217  d += rWeight.mnProb;
218  assert (d < 1.01);
219  if ( nRand < d )
220  {
221  nKey = rWeight.mnKey;
222  break;
223  }
224  }
225 
226  SalKeyEvent aKeyEvent;
227  InitKeyEvent( aKeyEvent );
228  aKeyEvent.mnCode = nKey;
229 
230  if (getRandom() < 0.15) // modifier
231  aKeyEvent.mnCode |= static_cast<sal_uInt16>(getRandom() * KEY_MODIFIERS_MASK) & KEY_MODIFIERS_MASK;
232 
233  aKeyEvent.mnCharCode = 0x0; // hopefully unused.
234 
235  bool bHandled = ImplWindowFrameProc( pWindow, SalEvent::KeyInput, &aKeyEvent );
236 
237  SAL_INFO( "vcl.debugevent",
238  "Injected edit / move key 0x" << std::hex << static_cast<int>(aKeyEvent.mnCode) << std::dec
239  << " -> " << bHandled
240  << " win " << pWindow );
241  ImplWindowFrameProc( pWindow, SalEvent::KeyUp, &aKeyEvent );
242 }
243 
245 {
246  InjectEvent();
247  mnEventsLeft--;
248  if (mnEventsLeft > 0)
249  {
250  SetTimeout( 1 );
251  Start();
252  }
253  else
255 }
256 
258 {
259  sal_uInt32 nEvents;
260  const char *pEvents = getenv("VCL_EVENT_INJECTION");
261  if (!pEvents)
262  return nullptr;
263  nEvents = OString( pEvents ).toUInt32();
264  if (nEvents > 0)
265  return new DebugEventInjector( nEvents );
266  else
267  return nullptr;
268 }
269 
270 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr sal_uInt16 KEY_CODE_MASK
Definition: keycodes.hxx:27
PopupMenu * GetPopupMenu(sal_uInt16 nItemId) const
Definition: menu.cxx:760
SystemWindow * GetSystemWindow() const
Definition: stacking.cxx:817
static vcl::Window * GetTopWindow(long nIndex)
Get the nth top window.
Definition: svapp.cxx:1089
bool ImplWindowFrameProc(vcl::Window *_pWindow, SalEvent nEvent, const void *pEvent)
Definition: winproc.cxx:2358
constexpr sal_uInt16 KEY_A
Definition: keycodes.hxx:56
constexpr sal_uInt16 KEY_0
Definition: keycodes.hxx:45
const wchar_t *typedef int(__stdcall *DllNativeUnregProc)(int
static void CollectMenuItemIds(Menu *pMenu, std::vector< SalMenuEvent > &rIds)
Definition: debugevent.cxx:58
virtual void Invoke() override
Calls the maInvokeHandler with the parameter this.
Definition: debugevent.cxx:244
static void InjectKeyNavEdit()
Definition: debugevent.cxx:184
constexpr sal_uInt16 KEY_SPACE
Definition: keycodes.hxx:123
constexpr sal_uInt16 KEY_UP
Definition: keycodes.hxx:111
sal_uInt16 mnCode
Definition: salwtype.hxx:107
constexpr sal_uInt16 KEY_END
Definition: keycodes.hxx:115
constexpr sal_uInt16 KEY_9
Definition: keycodes.hxx:54
sal_uInt32 mnEventsLeft
Definition: debugevent.hxx:20
MenuItemType GetItemType(sal_uInt16 nPos) const
Definition: menu.cxx:665
constexpr sal_uInt16 KEY_PAGEUP
Definition: keycodes.hxx:116
DebugEventInjector(sal_uInt32 nMaxEvents)
Definition: debugevent.cxx:21
SAL_DLLPRIVATE void CollectChildren(::std::vector< vcl::Window * > &rAllChildren)
Add all children to rAllChildren recursively.
Definition: window.cxx:2175
static void Quit()
Quit the program.
Definition: svapp.cxx:526
static void InitKeyEvent(SalKeyEvent &rKeyEvent)
Definition: debugevent.cxx:112
double d
#define SAL_N_ELEMENTS(arr)
constexpr sal_uInt16 KEY_MODIFIERS_MASK
Definition: keycodes.hxx:34
static void InjectMenuEvent()
Definition: debugevent.cxx:71
constexpr sal_uInt16 KEY_DOWN
Definition: keycodes.hxx:110
int i
static long GetTopWindowCount()
Return the number of top-level windows being used by the application.
Definition: svapp.cxx:1075
constexpr sal_uInt16 KEY_HOME
Definition: keycodes.hxx:114
sal_uInt16 GetItemId(sal_uInt16 nPos) const
Definition: menu.cxx:633
constexpr sal_uInt16 KEY_PAGEDOWN
Definition: keycodes.hxx:117
virtual void Start() override
Activates the timer task.
Definition: timer.cxx:83
sal_uInt16 mnCharCode
Definition: salwtype.hxx:108
void SetTimeout(sal_uInt64 nTimeoutMs)
Definition: timer.cxx:89
double uniform_real_distribution(double a=0.0, double b=1.0)
static vcl::Window * GetActiveTopWindow()
Get the "active" top window.
Definition: svapp.cxx:1108
constexpr sal_uInt16 KEY_RETURN
Definition: keycodes.hxx:119
SalEvent
Definition: salwtype.hxx:40
constexpr sal_uInt16 KEY_RIGHT
Definition: keycodes.hxx:113
static double getRandom()
Definition: debugevent.cxx:29
static vcl::Window * GetFocusWindow()
Get the currently focused window.
Definition: svapp.cxx:1054
Definition: menu.hxx:121
MenuBar * GetMenuBar() const
Definition: syswin.hxx:161
#define SAL_INFO(area, stream)
sal_uInt16 GetItemCount() const
Definition: menu.cxx:587
static void InjectTextEvent()
Definition: debugevent.cxx:120
constexpr sal_uInt16 KEY_BACKSPACE
Definition: keycodes.hxx:122
static void InjectEvent()
Definition: debugevent.cxx:167
sal_uInt16 mnRepeat
Definition: salwtype.hxx:109
static vcl::Window * ChooseWindow()
Definition: debugevent.cxx:34
constexpr sal_uInt16 KEY_Z
Definition: keycodes.hxx:81
Definition: timer.hxx:26
constexpr sal_uInt16 KEY_DELETE
Definition: keycodes.hxx:125
constexpr sal_uInt16 KEY_LEFT
Definition: keycodes.hxx:112
static DebugEventInjector * getCreate()
Definition: debugevent.cxx:257
constexpr sal_uInt16 KEY_INSERT
Definition: keycodes.hxx:124
AnyEventRef aEvent
constexpr sal_uInt16 KEY_TAB
Definition: keycodes.hxx:121