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
32#include <QKeySequenceEdit>
33#include <QLineEdit>
34#include <QString>
35
36Q_DECLARE_METATYPE(Qt::Key)
37Q_DECLARE_METATYPE(Qt::KeyboardModifiers)
38
39class tst_QKeySequenceEdit : public QObject
40{
41 Q_OBJECT
42
43private slots:
44 void testSetters();
45 void testKeys_data();
46 void testKeys();
47 void testLineEditContents();
48};
49
50void tst_QKeySequenceEdit::testSetters()
51{
52 QKeySequenceEdit edit;
53 QSignalSpy spy(&edit, SIGNAL(keySequenceChanged(QKeySequence)));
54 QCOMPARE(edit.keySequence(), QKeySequence());
55
56 edit.setKeySequence(QKeySequence::New);
57 QCOMPARE(edit.keySequence(), QKeySequence(QKeySequence::New));
58
59 edit.clear();
60 QCOMPARE(edit.keySequence(), QKeySequence());
61
62 QCOMPARE(spy.count(), 2);
63}
64
65void tst_QKeySequenceEdit::testKeys_data()
66{
67 QTest::addColumn<Qt::Key>(name: "key");
68 QTest::addColumn<Qt::KeyboardModifiers>(name: "modifiers");
69 QTest::addColumn<QKeySequence>(name: "keySequence");
70
71 QTest::newRow(dataTag: "1") << Qt::Key_N << Qt::KeyboardModifiers(Qt::ControlModifier) << QKeySequence("Ctrl+N");
72 QTest::newRow(dataTag: "2") << Qt::Key_N << Qt::KeyboardModifiers(Qt::AltModifier) << QKeySequence("Alt+N");
73 QTest::newRow(dataTag: "3") << Qt::Key_N << Qt::KeyboardModifiers(Qt::ShiftModifier) << QKeySequence("Shift+N");
74 QTest::newRow(dataTag: "4") << Qt::Key_N << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << QKeySequence("Ctrl+Shift+N");
75}
76
77void tst_QKeySequenceEdit::testKeys()
78{
79 QFETCH(Qt::Key, key);
80 QFETCH(Qt::KeyboardModifiers, modifiers);
81 QFETCH(QKeySequence, keySequence);
82 QKeySequenceEdit edit;
83
84 QSignalSpy spy(&edit, SIGNAL(editingFinished()));
85 QTest::keyPress(widget: &edit, key, modifier: modifiers);
86 QTest::keyRelease(widget: &edit, key, modifier: modifiers);
87
88 QCOMPARE(spy.count(), 0);
89 QCOMPARE(edit.keySequence(), keySequence);
90 QTRY_COMPARE(spy.count(), 1);
91}
92
93void tst_QKeySequenceEdit::testLineEditContents()
94{
95 QKeySequenceEdit edit;
96 QLineEdit *le = edit.findChild<QLineEdit*>();
97 QVERIFY(le);
98
99 QCOMPARE(le->text(), QString());
100
101 edit.setKeySequence(QKeySequence::New);
102 QCOMPARE(edit.keySequence(), QKeySequence(QKeySequence::New));
103
104 edit.clear();
105 QCOMPARE(le->text(), QString());
106
107 edit.setKeySequence(QKeySequence::New);
108 QVERIFY(le->text() != QString());
109
110 edit.setKeySequence(QKeySequence());
111 QCOMPARE(le->text(), QString());
112}
113
114QTEST_MAIN(tst_QKeySequenceEdit)
115#include "tst_qkeysequenceedit.moc"
116

source code of qtbase/tests/auto/widgets/widgets/qkeysequenceedit/tst_qkeysequenceedit.cpp