summaryrefslogtreecommitdiffstats
path: root/old/plugins/qtuitest_widgets/qtwidgets/testabstractbutton.cpp
blob: 379f92d607db2089f25c2aad7aea7f5e8e05c1b7 (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
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of QtUiTest.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "testabstractbutton.h"
#include "testwidgetslog.h"

#include <QAbstractButton>
#include <QTimer>
#include <QTime>
#include <QPointer>
#include <QStyleOptionButton>

namespace QtUiTest {

TestAbstractButton::TestAbstractButton(QObject *_q)
    : TestGenericTextWidget(_q), q(qobject_cast<QAbstractButton*>(_q)),
      m_pressed(false)
{
    if (!QtUiTest::connectFirst(q, SIGNAL(pressed()), this, SIGNAL(activated())))
        Q_ASSERT(false);
    if (!connect(q, SIGNAL(toggled(bool)), this, SLOT(on_toggled(bool))))
        Q_ASSERT(false);
}

void TestAbstractButton::on_toggled(bool state)
{ emit stateChanged(state); }

Qt::CheckState TestAbstractButton::checkState() const
{ return q->isChecked() ? Qt::Checked : Qt::Unchecked; }

bool TestAbstractButton::setCheckState(Qt::CheckState state)
{
    if (state == checkState()) return true;
    if (!q->isCheckable()) {
        QtUiTest::setErrorString("This abstract button is not checkable.");
        return false;
    }
    bool ret = activate(QtUiTest::NoOptions);
    TestWidgetsLog() << "activated:" << ret;
    if (ret && (state != checkState()) && !QtUiTest::waitForSignal(q, SIGNAL(toggled(bool)))) {
        QtUiTest::setErrorString("Successfully activated button, but check state did not change "
                "to expected value.");
        return false;
    }
    TestWidgetsLog() << "state:" << ret;
    return ret;
}

bool TestAbstractButton::activate()
{
    return activate((QtUiTest::InputOption)0);
}

bool TestAbstractButton::activate(QtUiTest::InputOption opt)
{
    if (QtUiTest::mousePreferred()) {
        int t;
        m_pressed = false;

        QPoint pos;
        if (q->isCheckable()) {
            QStyle const* style = q->style();
            QStyleOptionButton opt;
            opt.initFrom(q);
            QRect rect = style->subElementRect( QStyle::SE_CheckBoxIndicator, &opt, q);
            pos = rect.center();
        } else {
            pos = q->rect().center();
        }

        QRegion region = QRegion(pos.x()-1, pos.y()-1, 3, 3);
        for (t = 0; t < 2; ++t) {
            ensureVisibleRegion(region);
            QtUiTest::mousePress(mapToGlobal(pos), Qt::LeftButton, opt);
            QtUiTest::waitForSignal(q, SIGNAL(pressed()));
            if (q->isDown())
                break;
        }
        if (!q->isDown()) {
            qWarning("Button did not react to mouse press.");
            QtUiTest::mouseRelease(mapToGlobal(pos), Qt::LeftButton, opt);
            return false;
        }
        if (t > 0)
            qWarning("Button did not react to mouse press on first try.");

        QtUiTest::mouseRelease(mapToGlobal(pos), Qt::LeftButton, opt);
        return true;
    } else {
        Qt::Key key = Qt::Key(QtUiTest::Key_ActivateButton);
        Qt::KeyboardModifiers mod = 0;

        if (q->focusPolicy() == Qt::NoFocus) {
            /* Handle buttons which need to be activated by a shortcut */
            QKeySequence ks = q->shortcut();
            TestWidgetsLog() << "Can't give focus to button; need to use shortcut"
                    << ks.toString();
            if (ks.isEmpty()) {
                QtUiTest::setErrorString("Button has NoFocus policy set and does not appear "
                        "to have any shortcut set.  Therefore, it is impossible to activate "
                        "using only the keyboard.");
                return false;
            }

            int key_and_mod = ks[0];
            mod = QFlag(key_and_mod & (0xfe000000));
            key = Qt::Key(key_and_mod & (0x01ffffff));
        }
        else {
            if (!hasFocus()) setFocus();
            if (!hasFocus()) return false;
        }

        QPointer<QObject> safeQ(q);

        if (!QtUiTest::keyClick(q, SIGNAL(pressed()), key, mod, opt))
            return false;

        if (safeQ && q->isDown() && !QtUiTest::waitForSignal(q, SIGNAL(released()))) {
            QtUiTest::setErrorString("Button did not become released after key click");
            return false;
        }
        return true;
    }
    return false;
}

QString TestAbstractButton::labelText() const
{
    QString ret = text();
    if (ret.isEmpty())
        return ret;

    QObject *w = parent();
    while (w) {
        QtUiTest::Widget *qw = qtuitest_cast<QtUiTest::Widget*>(w);
        QtUiTest::LabelWidget *lw = qtuitest_cast<QtUiTest::LabelWidget*>(w);
        if (lw) {
            if (!lw->labelText().isEmpty()) {
                ret.prepend(lw->labelText() + "/");
                break;
            }
        }
        w = qw->parent();
    }
    return ret;
}

QObject* TestAbstractButton::buddy() const
{
    return q;
}

bool TestAbstractButton::canWrap(QObject *o)
{ return qobject_cast<QAbstractButton*>(o); }

}