1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtTest/QtTest>
31#include <QtTest/private/qtesthelpers_p.h>
32
33#include <qcoreapplication.h>
34#include <qdebug.h>
35#include <qabstractspinbox.h>
36#include <qlineedit.h>
37#include <qspinbox.h>
38
39#include "../../../shared/platforminputcontext.h"
40#include <private/qinputmethod_p.h>
41
42
43class tst_QAbstractSpinBox : public QObject
44{
45Q_OBJECT
46
47public:
48 tst_QAbstractSpinBox();
49 virtual ~tst_QAbstractSpinBox();
50
51private slots:
52 void initTestCase();
53 void cleanupTestCase();
54
55 void getSetCheck();
56
57 // task-specific tests below me:
58 void task183108_clear();
59 void task228728_cssselector();
60
61 void inputMethodUpdate();
62
63private:
64 PlatformInputContext m_platformInputContext;
65};
66
67tst_QAbstractSpinBox::tst_QAbstractSpinBox()
68{
69}
70
71tst_QAbstractSpinBox::~tst_QAbstractSpinBox()
72{
73}
74
75class MyAbstractSpinBox : public QAbstractSpinBox
76{
77public:
78 MyAbstractSpinBox() : QAbstractSpinBox() {}
79 QLineEdit *lineEdit() { return QAbstractSpinBox::lineEdit(); }
80 void setLineEdit(QLineEdit *le) { QAbstractSpinBox::setLineEdit(le); }
81};
82
83void tst_QAbstractSpinBox::initTestCase()
84{
85 QInputMethodPrivate *inputMethodPrivate = QInputMethodPrivate::get(qApp->inputMethod());
86 inputMethodPrivate->testContext = &m_platformInputContext;
87}
88
89void tst_QAbstractSpinBox::cleanupTestCase()
90{
91 QInputMethodPrivate *inputMethodPrivate = QInputMethodPrivate::get(qApp->inputMethod());
92 inputMethodPrivate->testContext = 0;
93}
94
95// Testing get/set functions
96void tst_QAbstractSpinBox::getSetCheck()
97{
98 MyAbstractSpinBox obj1;
99 // ButtonSymbols QAbstractSpinBox::buttonSymbols()
100 // void QAbstractSpinBox::setButtonSymbols(ButtonSymbols)
101 obj1.setButtonSymbols(QAbstractSpinBox::ButtonSymbols(QAbstractSpinBox::UpDownArrows));
102 QCOMPARE(QAbstractSpinBox::ButtonSymbols(QAbstractSpinBox::UpDownArrows), obj1.buttonSymbols());
103 obj1.setButtonSymbols(QAbstractSpinBox::ButtonSymbols(QAbstractSpinBox::PlusMinus));
104 QCOMPARE(QAbstractSpinBox::ButtonSymbols(QAbstractSpinBox::PlusMinus), obj1.buttonSymbols());
105
106 // bool QAbstractSpinBox::wrapping()
107 // void QAbstractSpinBox::setWrapping(bool)
108 obj1.setWrapping(false);
109 QCOMPARE(false, obj1.wrapping());
110 obj1.setWrapping(true);
111 QCOMPARE(true, obj1.wrapping());
112
113 // QLineEdit * QAbstractSpinBox::lineEdit()
114 // void QAbstractSpinBox::setLineEdit(QLineEdit *)
115 QLineEdit *var3 = new QLineEdit(0);
116 obj1.setLineEdit(var3);
117 QCOMPARE(var3, obj1.lineEdit());
118#ifndef QT_DEBUG
119 obj1.setLineEdit((QLineEdit *)0); // Will assert in debug, so only test in release
120 QCOMPARE(var3, obj1.lineEdit()); // Setting 0 should keep the current editor
121#endif
122 // delete var3; // No delete, since QAbstractSpinBox takes ownership
123}
124
125void tst_QAbstractSpinBox::task183108_clear()
126{
127 QAbstractSpinBox *sbox;
128
129 sbox = new QSpinBox;
130 sbox->clear();
131 sbox->show();
132 qApp->processEvents();
133 QVERIFY(sbox->text().isEmpty());
134
135 delete sbox;
136 sbox = new QSpinBox;
137 sbox->clear();
138 sbox->show();
139 sbox->hide();
140 qApp->processEvents();
141 QCOMPARE(sbox->text(), QString());
142
143 delete sbox;
144 sbox = new QSpinBox;
145 sbox->show();
146 sbox->clear();
147 qApp->processEvents();
148 QCOMPARE(sbox->text(), QString());
149
150 delete sbox;
151 sbox = new QSpinBox;
152 sbox->show();
153 sbox->clear();
154 sbox->hide();
155 qApp->processEvents();
156 QCOMPARE(sbox->text(), QString());
157
158 delete sbox;
159}
160
161void tst_QAbstractSpinBox::task228728_cssselector()
162{
163 //QAbstractSpinBox does some call to stylehint into his constructor.
164 //so while the stylesheet want to access property, it should not crash
165 qApp->setStyleSheet("[alignment=\"1\"], [text=\"foo\"] { color:black; }" );
166 QSpinBox box;
167}
168
169void tst_QAbstractSpinBox::inputMethodUpdate()
170{
171 if (QGuiApplication::platformName().startsWith(s: QLatin1String("wayland"), cs: Qt::CaseInsensitive))
172 QSKIP("Wayland: This fails. Figure out why.");
173
174 QSpinBox box;
175
176 QSpinBox *testWidget = &box;
177 testWidget->setRange(min: 0, max: 1);
178
179 QTestPrivate::centerOnScreen(w: testWidget);
180 testWidget->clear();
181 testWidget->show();
182 QVERIFY(QTest::qWaitForWindowExposed(testWidget));
183
184 testWidget->activateWindow();
185 testWidget->setFocus();
186 QTRY_VERIFY(testWidget->hasFocus());
187 QTRY_COMPARE(qApp->focusObject(), testWidget);
188
189 m_platformInputContext.m_updateCallCount = 0;
190 {
191 QList<QInputMethodEvent::Attribute> attributes;
192 QInputMethodEvent event("1", attributes);
193 QApplication::sendEvent(receiver: testWidget, event: &event);
194 }
195 QVERIFY(m_platformInputContext.m_updateCallCount >= 1);
196
197 m_platformInputContext.m_updateCallCount = 0;
198 {
199 QList<QInputMethodEvent::Attribute> attributes;
200 attributes << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 0, 1, QVariant());
201 QInputMethodEvent event("1", attributes);
202 QApplication::sendEvent(receiver: testWidget, event: &event);
203 }
204 QVERIFY(m_platformInputContext.m_updateCallCount >= 1);
205
206 m_platformInputContext.m_updateCallCount = 0;
207 {
208 QList<QInputMethodEvent::Attribute> attributes;
209 QInputMethodEvent event("", attributes);
210 event.setCommitString(commitString: "1");
211 QApplication::sendEvent(receiver: testWidget, event: &event);
212 }
213 QVERIFY(m_platformInputContext.m_updateCallCount >= 1);
214 QCOMPARE(testWidget->value(), 1);
215
216 m_platformInputContext.m_updateCallCount = 0;
217 {
218 QList<QInputMethodEvent::Attribute> attributes;
219 attributes << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, 0, 0, QVariant());
220 QInputMethodEvent event("", attributes);
221 QApplication::sendEvent(receiver: testWidget, event: &event);
222 }
223 QVERIFY(m_platformInputContext.m_updateCallCount >= 1);
224}
225
226
227QTEST_MAIN(tst_QAbstractSpinBox)
228#include "tst_qabstractspinbox.moc"
229

source code of qtbase/tests/auto/widgets/widgets/qabstractspinbox/tst_qabstractspinbox.cpp