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#include <QtTest/QtTest>
30
31#include <private/qinputcontrol_p.h>
32#include <QtGui/QKeyEvent>
33
34class tst_QInputControl: public QObject
35{
36 Q_OBJECT
37private slots:
38 void isAcceptableInput_data();
39 void isAcceptableInput();
40 void tabOnlyAcceptableInputForTextEdit();
41};
42
43void tst_QInputControl::isAcceptableInput_data()
44{
45 QTest::addColumn<QString>(name: "text");
46 QTest::addColumn<Qt::KeyboardModifiers>(name: "modifiers");
47 QTest::addColumn<bool>(name: "acceptable");
48
49 QTest::newRow(dataTag: "empty-string") << QString() << Qt::KeyboardModifiers() << false;
50 QTest::newRow(dataTag: "zwnj") << QString(QChar(0x200C)) << Qt::KeyboardModifiers() << true;
51 QTest::newRow(dataTag: "zwnj-with-ctrl") << QString(QChar(0x200C)) << Qt::KeyboardModifiers(Qt::ControlModifier) << true;
52 QTest::newRow(dataTag: "zwnj-with-ctrl-shift") << QString(QChar(0x200C)) << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << true;
53 QTest::newRow(dataTag: "zwj") << QString(QChar(0x200D)) << Qt::KeyboardModifiers() << true;
54 QTest::newRow(dataTag: "zwj-with-ctrl") << QString(QChar(0x200D)) << Qt::KeyboardModifiers(Qt::ControlModifier) << true;
55 QTest::newRow(dataTag: "zwj-with-ctrl-shift") << QString(QChar(0x200D)) << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << true;
56 QTest::newRow(dataTag: "printable-latin") << QString(QLatin1Char('a')) << Qt::KeyboardModifiers() << true;
57 QTest::newRow(dataTag: "printable-latin-with-ctrl") << QString(QLatin1Char('a')) << Qt::KeyboardModifiers(Qt::ControlModifier) << false;
58 QTest::newRow(dataTag: "printable-latin-with-ctrl-shift") << QString(QLatin1Char('a')) << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << false;
59 QTest::newRow(dataTag: "printable-hebrew") << QString(QChar(0x2135)) << Qt::KeyboardModifiers() << true;
60 QTest::newRow(dataTag: "private-use-area") << QString(QChar(0xE832)) << Qt::KeyboardModifiers() << true;
61 QTest::newRow(dataTag: "good-surrogate-0") << QString::fromUtf16(str: u"\U0001F44D") << Qt::KeyboardModifiers() << true;
62 {
63 const QChar data[] = { QChar(0xD800), QChar(0xDC00) };
64 const QString str = QString(data, 2);
65 QTest::newRow(dataTag: "good-surrogate-1") << str << Qt::KeyboardModifiers() << true;
66 }
67 {
68 const QChar data[] = { QChar(0xD800), QChar(0xDFFF) };
69 const QString str = QString(data, 2);
70 QTest::newRow(dataTag: "good-surrogate-2") << str << Qt::KeyboardModifiers() << true;
71 }
72 {
73 const QChar data[] = { QChar(0xDBFF), QChar(0xDC00) };
74 const QString str = QString(data, 2);
75 QTest::newRow(dataTag: "good-surrogate-3") << str << Qt::KeyboardModifiers() << true;
76 }
77 {
78 const QChar data[] = { QChar(0xDBFF), QChar(0xDFFF) };
79 const QString str = QString(data, 2);
80 QTest::newRow(dataTag: "good-surrogate-4") << str << Qt::KeyboardModifiers() << true;
81 }
82 {
83 const QChar data[] = { QChar(0xD7FF), QChar(0xDC00) };
84 const QString str = QString(data, 2);
85 QTest::newRow(dataTag: "bad-surrogate-1") << str << Qt::KeyboardModifiers() << false;
86 }
87 {
88 const QChar data[] = { QChar(0xD7FF), QChar(0xDFFF) };
89 const QString str = QString(data, 2);
90 QTest::newRow(dataTag: "bad-surrogate-2") << str << Qt::KeyboardModifiers() << false;
91 }
92 {
93 const QChar data[] = { QChar(0xDC00), QChar(0xDC00) };
94 const QString str = QString(data, 2);
95 QTest::newRow(dataTag: "bad-surrogate-3") << str << Qt::KeyboardModifiers() << false;
96 }
97 {
98 const QChar data[] = { QChar(0xD800), QChar(0xE000) };
99 const QString str = QString(data, 2);
100 QTest::newRow(dataTag: "bad-surrogate-4") << str << Qt::KeyboardModifiers() << false;
101 }
102 {
103 const QChar data[] = { QChar(0xD800) };
104 const QString str = QString(data, 1);
105 QTest::newRow(dataTag: "bad-surrogate-5") << str << Qt::KeyboardModifiers() << false;
106 }
107 QTest::newRow(dataTag: "multiple-printable") << QStringLiteral("foobar") << Qt::KeyboardModifiers() << true;
108 QTest::newRow(dataTag: "rlm") << QString(QChar(0x200F)) << Qt::KeyboardModifiers() << true;
109 QTest::newRow(dataTag: "rlm-with-ctrl") << QString(QChar(0x200F)) << Qt::KeyboardModifiers(Qt::ControlModifier) << true;
110 QTest::newRow(dataTag: "rlm-with-ctrl-shift") << QString(QChar(0x200F)) << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << true;
111 QTest::newRow(dataTag: "lrm") << QString(QChar(0x200E)) << Qt::KeyboardModifiers() << true;
112 QTest::newRow(dataTag: "lrm-with-ctrl") << QString(QChar(0x200E)) << Qt::KeyboardModifiers(Qt::ControlModifier) << true;
113 QTest::newRow(dataTag: "lrm-with-ctrl-shift") << QString(QChar(0x200E)) << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << true;
114 QTest::newRow(dataTag: "rlo") << QString(QChar(0x202E)) << Qt::KeyboardModifiers() << true;
115 QTest::newRow(dataTag: "rlo-with-ctrl") << QString(QChar(0x202E)) << Qt::KeyboardModifiers(Qt::ControlModifier) << true;
116 QTest::newRow(dataTag: "rlo-with-ctrl-shift") << QString(QChar(0x202E)) << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << true;
117 QTest::newRow(dataTag: "lro") << QString(QChar(0x202D)) << Qt::KeyboardModifiers() << true;
118 QTest::newRow(dataTag: "lro-with-ctrl") << QString(QChar(0x202D)) << Qt::KeyboardModifiers(Qt::ControlModifier) << true;
119 QTest::newRow(dataTag: "lro-with-ctrl-shift") << QString(QChar(0x202D)) << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << true;
120 QTest::newRow(dataTag: "lre") << QString(QChar(0x202B)) << Qt::KeyboardModifiers() << true;
121 QTest::newRow(dataTag: "lre-with-ctrl") << QString(QChar(0x202B)) << Qt::KeyboardModifiers(Qt::ControlModifier) << true;
122 QTest::newRow(dataTag: "lre-with-ctrl-shift") << QString(QChar(0x202B)) << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << true;
123 QTest::newRow(dataTag: "rle") << QString(QChar(0x202A)) << Qt::KeyboardModifiers() << true;
124 QTest::newRow(dataTag: "rle-with-ctrl") << QString(QChar(0x202A)) << Qt::KeyboardModifiers(Qt::ControlModifier) << true;
125 QTest::newRow(dataTag: "rle-with-ctrl-shift") << QString(QChar(0x202A)) << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << true;
126 QTest::newRow(dataTag: "pdf") << QString(QChar(0x202C)) << Qt::KeyboardModifiers() << true;
127 QTest::newRow(dataTag: "pdf-with-ctrl") << QString(QChar(0x202C)) << Qt::KeyboardModifiers(Qt::ControlModifier) << true;
128 QTest::newRow(dataTag: "pdf-with-ctrl-shift") << QString(QChar(0x202C)) << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << true;
129
130}
131
132void tst_QInputControl::isAcceptableInput()
133{
134 QFETCH(QString, text);
135 QFETCH(Qt::KeyboardModifiers, modifiers);
136 QFETCH(bool, acceptable);
137
138 QKeyEvent keyEvent(QKeyEvent::KeyPress, Qt::Key_unknown, modifiers, text);
139
140 {
141 QInputControl inputControl(QInputControl::TextEdit);
142 QCOMPARE(inputControl.isAcceptableInput(&keyEvent), acceptable);
143 }
144
145 {
146 QInputControl inputControl(QInputControl::LineEdit);
147 QCOMPARE(inputControl.isAcceptableInput(&keyEvent), acceptable);
148 }
149}
150
151void tst_QInputControl::tabOnlyAcceptableInputForTextEdit()
152{
153 QKeyEvent keyEvent(QKeyEvent::KeyPress, Qt::Key_unknown, Qt::KeyboardModifiers(), QLatin1String("\t"));
154
155 {
156 QInputControl inputControl(QInputControl::TextEdit);
157 QCOMPARE(inputControl.isAcceptableInput(&keyEvent), true);
158 }
159
160 {
161 QInputControl inputControl(QInputControl::LineEdit);
162 QCOMPARE(inputControl.isAcceptableInput(&keyEvent), false);
163 }
164}
165
166QTEST_MAIN(tst_QInputControl)
167#include "tst_qinputcontrol.moc"
168
169

source code of qtbase/tests/auto/gui/text/qinputcontrol/tst_qinputcontrol.cpp