summaryrefslogtreecommitdiff
path: root/vcl/source/window/debugevent.cxx
blob: 11d17daa8f2aafb339a3155af28c5fa4ed730bea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <comphelper/random.hxx>
#include <rtl/math.hxx>
#include <rtl/string.hxx>
#include <tools/time.hxx>
#include <vcl/keycodes.hxx>
#include <vcl/svapp.hxx>
#include <vcl/debugevent.hxx>
#include <vcl/wrkwin.hxx>
#include <vcl/menu.hxx>
#include "window.h"
#include "salwtype.hxx"

#if OSL_DEBUG_LEVEL > 0

DebugEventInjector::DebugEventInjector( sal_uInt32 nMaxEvents) :
    mnEventsLeft( nMaxEvents )
{
    SetTimeout( 1000 /* ms */ );
    Start();
}

static double getRandom()
{
    return comphelper::rng::uniform_real_distribution();
}

vcl::Window *DebugEventInjector::ChooseWindow()
{
    vcl::Window *pWindow, *pParent;

    if (getRandom() < 0.80 &&
        (pWindow = Application::GetFocusWindow()))
        return pWindow;

    if (getRandom() > 0.50 ||
        !(pParent = Application::GetActiveTopWindow()))
    {
        // select a top window at random
        long nIdx = Application::GetTopWindowCount() * getRandom();
        if (!(pParent = Application::GetTopWindow( nIdx )))
            pParent = static_cast<vcl::Window *>(Application::GetAppWindow());
    }
    assert (pParent != NULL);

    std::vector< vcl::Window *> aChildren;
    pParent->CollectChildren( aChildren );

    return aChildren[ aChildren.size() * getRandom() ];
}

typedef std::vector< SalMenuEvent > MenuItemIds;

static void CollectMenuItemIds( Menu *pMenu, MenuItemIds &rIds )
{
    sal_uInt16 nItems = pMenu->GetItemCount();
    for (sal_uInt16 i = 0; i < nItems; i++)
    {
        if (pMenu->GetItemType( i ) != MenuItemType::SEPARATOR || getRandom() < 0.01)
            rIds.push_back( SalMenuEvent( pMenu->GetItemId( i ), pMenu ) );
        PopupMenu *pPopup = pMenu->GetPopupMenu( i );
        if (pPopup)
            CollectMenuItemIds( pPopup, rIds );
    }
}

void DebugEventInjector::InjectMenuEvent()
{
    vcl::Window *pFocus = Application::GetFocusWindow();
    if (!pFocus)
        return;

    SystemWindow *pSysWin = pFocus->GetSystemWindow();
    if (!pSysWin)
        return;

    MenuBar *pMenuBar = pSysWin->GetMenuBar();
    if (!pMenuBar)
        return;

    sal_uInt16 nEvents[] = {
        SALEVENT_MENUCOMMAND,
        SALEVENT_MENUCOMMAND,
        SALEVENT_MENUACTIVATE,
        SALEVENT_MENUDEACTIVATE,
        SALEVENT_MENUHIGHLIGHT,
        SALEVENT_MENUCOMMAND,
        SALEVENT_MENUCOMMAND,
        SALEVENT_MENUCOMMAND,
        SALEVENT_MENUBUTTONCOMMAND,
        SALEVENT_MENUBUTTONCOMMAND,
    };

    MenuItemIds aIds;
    CollectMenuItemIds( pMenuBar, aIds );

    sal_uInt16 nEvent = nEvents[ (int)(getRandom() * SAL_N_ELEMENTS( nEvents )) ];
    SalMenuEvent aEvent = aIds[ getRandom() * aIds.size() ];
    bool bHandled = ImplWindowFrameProc( pSysWin, NULL, nEvent, &aEvent);

    SAL_INFO( "vcl.debugevent",
              "Injected menu event " << aEvent.mpMenu
              << " (" << aEvent.mnId << ") '"
              << static_cast<Menu *>(aEvent.mpMenu)->GetItemText( aEvent.mnId ) << "' -> "
              << bHandled );
}

static void InitKeyEvent( SalKeyEvent &rKeyEvent )
{
    double nRand = getRandom();
    if (nRand < 0.001)
        rKeyEvent.mnTime = getRandom() * SAL_MAX_UINT64;
    else
        rKeyEvent.mnTime = tools::Time::GetSystemTicks();

    if (getRandom() < 0.01)
        rKeyEvent.mnRepeat = getRandom() * 20;
    else
        rKeyEvent.mnRepeat = 0;
}

void DebugEventInjector::InjectTextEvent()
{
    SalKeyEvent aKeyEvent;
    vcl::Window *pWindow = ChooseWindow();

    InitKeyEvent( aKeyEvent );

    if (getRandom() < 0.10) // Occasionally a truly random event
    {
        aKeyEvent.mnCode = getRandom() * KEY_CODE_MASK;
        aKeyEvent.mnCharCode = getRandom() * 0xffff;
    }
    else
    {
        struct {
            sal_uInt16 nCodeStart, nCodeEnd;
            char       aCharStart;
        } nTextCodes[] = {
            { KEY_0, KEY_9, '0' },
            { KEY_A, KEY_Z, 'a' }
        };

        size_t i = getRandom() * SAL_N_ELEMENTS( nTextCodes );
        int offset = int( getRandom() * ( nTextCodes[i].nCodeEnd - nTextCodes[i].nCodeStart ) );
        aKeyEvent.mnCode = nTextCodes[i].nCodeStart + offset;
        aKeyEvent.mnCharCode = nTextCodes[i].aCharStart + offset;
//        fprintf( stderr, "Char '%c' offset %d into record %d base '%c'\n",
//                 aKeyEvent.mnCharCode, offset, (int)i, nTextCodes[i].aCharStart );
    }

    if( getRandom() < 0.05 ) // modifier
        aKeyEvent.mnCode |= (sal_uInt16)( getRandom() * KEY_MODIFIERS_MASK ) & KEY_MODIFIERS_MASK;

    bool bHandled = ImplWindowFrameProc( pWindow, NULL, SALEVENT_KEYINPUT, &aKeyEvent);

    SAL_INFO( "vcl.debugevent",
              "Injected key 0x" << std::hex << (int) aKeyEvent.mnCode << std::dec
              << " -> " << bHandled
              << " win " << pWindow );

    ImplWindowFrameProc( pWindow, NULL, SALEVENT_KEYUP, &aKeyEvent );
}

/*
 *   The more heuristics we have to inform this the better,
 * key-bindings, menu entries, allowable entry types etc.
 */
void DebugEventInjector::InjectEvent()
{
//    fprintf( stderr, "%6d - ", (int)mnEventsLeft );

    double nRand = getRandom();
    if (nRand < 0.30)
    {
        int nEvents = getRandom() * 10;
        for (int i = 0; i < nEvents; i++)
            InjectTextEvent();
    }
    else if (nRand < 0.60)
        InjectKeyNavEdit();
    else if (nRand < 0.95)
        InjectMenuEvent();
}

void DebugEventInjector::InjectKeyNavEdit()
{
    vcl::Window *pWindow = ChooseWindow();

    struct {
        double     mnProb;
        sal_uInt16 mnKey;
    } nWeights[] = {
        // edit / escape etc. - 50%
        { 0.20, KEY_SPACE },
        { 0.10, KEY_TAB },
        { 0.07, KEY_RETURN },
        { 0.05, KEY_DELETE },
        { 0.05, KEY_BACKSPACE },

        // navigate - 45%
        { 0.15, KEY_LEFT },
        { 0.10, KEY_RIGHT },
        { 0.05, KEY_UP },
        { 0.05, KEY_DOWN },
        { 0.05, KEY_PAGEUP },
        { 0.05, KEY_PAGEDOWN },

        // other
        { 0.01, KEY_INSERT },
        { 0.02, KEY_HOME },
        { 0.02, KEY_END },
    };

    double d = 0.0, nRand = getRandom();
    sal_uInt16 nKey = KEY_SPACE;
    for ( size_t i = 0; i < SAL_N_ELEMENTS( nWeights ); ++i )
    {
        d += nWeights[i].mnProb;
        assert (d < 1.01);
        if ( nRand < d )
        {
            nKey = nWeights[i].mnKey;
            break;
        }
    }

    SalKeyEvent aKeyEvent;
    InitKeyEvent( aKeyEvent );
    aKeyEvent.mnCode = nKey;

    if (getRandom() < 0.15) // modifier
        aKeyEvent.mnCode |= (sal_uInt16)(getRandom() * KEY_MODIFIERS_MASK) & KEY_MODIFIERS_MASK;

    aKeyEvent.mnCharCode = 0x0; // hopefully unused.

    bool bHandled = ImplWindowFrameProc( pWindow, NULL, SALEVENT_KEYINPUT, &aKeyEvent );

    SAL_INFO( "vcl.debugevent",
              "Injected edit / move key 0x" << std::hex << (int) aKeyEvent.mnCode << std::dec
              << " -> " << bHandled
              << " win " <<  pWindow );
    ImplWindowFrameProc( pWindow, NULL, SALEVENT_KEYUP, &aKeyEvent );
}

void DebugEventInjector::Invoke()
{
    InjectEvent();
    mnEventsLeft--;
    if (mnEventsLeft > 0)
    {
        SetTimeout( 1 );
        Start();
    }
    else
        Application::Quit();
}

DebugEventInjector *DebugEventInjector::getCreate()
{
    sal_uInt32 nEvents;
    const char *pEvents = getenv("VCL_EVENT_INJECTION");
    if (!pEvents)
        return NULL;
    nEvents = OString( pEvents ).toUInt32();
    if (nEvents > 0)
        return new DebugEventInjector( nEvents );
    else
        return NULL;
}

#endif // OSL_DEBUG_LEVEL > 0

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */