| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2023 Volker Krause <[email protected]> |
| 3 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 4 | */ |
| 5 | |
| 6 | #include "klineediteventhandler.h" |
| 7 | |
| 8 | #include <QDialog> |
| 9 | #include <QDialogButtonBox> |
| 10 | #include <QLineEdit> |
| 11 | #include <QObject> |
| 12 | #include <QSignalSpy> |
| 13 | #include <QTest> |
| 14 | |
| 15 | class KLineEditEventHandlerTest : public QObject |
| 16 | { |
| 17 | Q_OBJECT |
| 18 | private Q_SLOTS: |
| 19 | void testReturnCatching() |
| 20 | { |
| 21 | QDialog dialog; |
| 22 | auto lineEdit = new QLineEdit(&dialog); |
| 23 | auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok, &dialog); |
| 24 | connect(buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept); |
| 25 | |
| 26 | QSignalSpy returnSpy(lineEdit, &QLineEdit::returnPressed); |
| 27 | QSignalSpy dialogSpy(&dialog, &QDialog::accepted); |
| 28 | |
| 29 | dialog.show(); |
| 30 | QTest::keyPress(lineEdit, Qt::Key_Enter); |
| 31 | QCOMPARE(returnSpy.size(), 1); |
| 32 | QCOMPARE(dialogSpy.size(), 1); |
| 33 | |
| 34 | KLineEditEventHandler::catchReturnKey(lineEdit); |
| 35 | dialog.show(); |
| 36 | QTest::keyPress(lineEdit, Qt::Key_Enter); |
| 37 | QCOMPARE(returnSpy.size(), 2); |
| 38 | QCOMPARE(dialogSpy.size(), 1); |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | QTEST_MAIN(KLineEditEventHandlerTest) |
| 43 | |
| 44 | #include "klineediteventhandlertest.moc" |