1
/****************************************************************************
2
**
3
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the test suite of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** GNU Lesser General Public License Usage
11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
**
18
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
**
22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
#ifndef GUITEST_H
42
#define GUITEST_H
43
44
#include <QAccessibleInterface>
45
#include <QSet>
46
#include <QWidget>
47
#include <QPainter>
48
49
QT_USE_NAMESPACE
50
51
/*
52
    GuiTest provides tools for:
53
     - navigating the Qt Widget hiearchy using the accessibilty APIs.
54
     - Simulating platform mouse and keybord events.
55
*/
56
57
/*
58
    InterfaceChildPair specifies an accessibilty interface item.
59
*/
60
class InterfaceChildPair {
61
public:
62
    InterfaceChildPair() : iface(0), possibleChild(0) {}
63
    InterfaceChildPair(QAccessibleInterface *iface, int possibleChild)
64
    :iface(iface), possibleChild(possibleChild)    
65
    { }
66
    
67
    QAccessibleInterface *iface;
68
    int possibleChild;
69
};
70
71
class TestBase {
72
public:
73
    virtual bool operator()(InterfaceChildPair candidate) = 0;
74
    virtual ~TestBase() {}
75
};
76
77
/*
78
    WidgetNavigator navigates a Qt GUI hierarchy using the QAccessibility APIs.
79
*/
80
class WidgetNavigator {
81
public:
82
    WidgetNavigator() {};
83
    ~WidgetNavigator();
84
85
    void printAll(QWidget *widget);
86
    void printAll(InterfaceChildPair interface);
87
    
88
    InterfaceChildPair find(QAccessible::Text textType, const QString &text, QWidget *start);
89
    InterfaceChildPair find(QAccessible::Text textType, const QString &text, QAccessibleInterface *start);
90
91
    InterfaceChildPair recursiveSearch(TestBase *test, QAccessibleInterface *iface, int possibleChild);
92
    
93
    void deleteInDestructor(QAccessibleInterface * interface);
94
    static QWidget *getWidget(InterfaceChildPair interface);
95
private:
96
    QSet<QAccessibleInterface *> interfaces;
97
};
98
99
/*
100
    NativeEvents contains platform-specific code for simulating mouse and keybord events.
101
    (Implemented so far: mouseClick on Mac)
102
*/
103
namespace NativeEvents {
104
    enum MousePosition { UpdatePosition, DontUpdatePosition };
105
    /*
106
        Simulates a mouse click with button at globalPos.
107
    */
108
    void mouseClick(const QPoint &globalPos, Qt::MouseButtons buttons, MousePosition updateMouse = DontUpdatePosition);
109
};
110
111
class ColorWidget : public QWidget
112
{
113
public:
114
    ColorWidget(QWidget *parent = 0, QColor color = QColor(Qt::red))
115
       : QWidget(parent), color(color) {}
116
    
117
    QColor color;
118
119
protected:
120
    void paintEvent(QPaintEvent  *)
121
    {
122
        QPainter p(this);
123
        p.fillRect(this->rect(), color);
124
    }
125
};
126
127
class DelayedAction : public QObject
128
{
129
Q_OBJECT
130
public:
131
    DelayedAction() : delay(0), next(0) {}
132
    virtual ~DelayedAction(){}
133
public slots:
134
    virtual void run();
135
public:
136
    int delay;
137
    DelayedAction *next;
138
};
139
140
class ClickLaterAction : public DelayedAction
141
{
142
Q_OBJECT
143
public:
144
    ClickLaterAction(InterfaceChildPair interface, Qt::MouseButtons buttons = Qt::LeftButton);
145
    ClickLaterAction(QWidget *widget, Qt::MouseButtons buttons = Qt::LeftButton);
146
protected slots:
147
    void run();
148
private:
149
    bool useInterface;
150
    InterfaceChildPair interface;
151
    QWidget *widget;
152
    Qt::MouseButtons buttons;
153
};
154
155
/*
156
    
157
*/
158
class GuiTester : public QObject
159
{
160
Q_OBJECT
161
public:
162
    GuiTester();
163
    ~GuiTester();
164
    enum Direction {Horizontal = 1, Vertical = 2, HorizontalAndVertical = 3};
165
    Q_DECLARE_FLAGS(Directions, Direction)
166
    bool isFilled(const QImage image, const QRect &rect, const QColor &color);
167
    bool isContent(const QImage image, const QRect &rect, Directions directions = HorizontalAndVertical);
168
protected slots:
169
    void exitLoopSlot();
170
protected:
171
    void clickLater(InterfaceChildPair interface, Qt::MouseButtons buttons = Qt::LeftButton, int delay = 300);
172
    void clickLater(QWidget *widget, Qt::MouseButtons buttons = Qt::LeftButton, int delay = 300);
173
174
    void clearSequence();
175
    void addToSequence(DelayedAction *action, int delay = 0);
176
    void runSequence();
177
    WidgetNavigator wn;
178
private:
179
    QSet<DelayedAction *> actions;
180
    DelayedAction *startAction;
181
    DelayedAction *lastAction;
182
};
183
184
Q_DECLARE_OPERATORS_FOR_FLAGS(GuiTester::Directions)
185
186
#endif