File indexing completed on 2025-03-09 04:53:55
0001 /* 0002 SPDX-FileCopyrightText: 2016 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include <QObject> 0008 #include <QTest> 0009 0010 #include "recipient/recipientline.h" 0011 #include "recipient/recipientseditor.h" 0012 0013 #include <KMime/Types> 0014 #include <QClipboard> 0015 0016 class RecipientsLineTestFactory : public MessageComposer::RecipientLineFactory 0017 { 0018 Q_OBJECT 0019 0020 public: 0021 explicit RecipientsLineTestFactory(QObject *parent = nullptr) 0022 : MessageComposer::RecipientLineFactory(parent) 0023 { 0024 } 0025 0026 KPIM::MultiplyingLine *newLine(QWidget *parent) override 0027 { 0028 auto line = qobject_cast<MessageComposer::RecipientLineNG *>(MessageComposer::RecipientLineFactory::newLine(parent)); 0029 line->setEnableAkonadiSearch(false); 0030 line->setEnableIndexSearch(false); 0031 return line; 0032 } 0033 }; 0034 0035 class RecipientsEditorTest : public QObject 0036 { 0037 Q_OBJECT 0038 0039 private Q_SLOTS: 0040 void test_addLineOnCommaPress(); 0041 void test_splitStringInputToLines(); 0042 void test_splitPastedListToLines(); 0043 }; 0044 0045 void RecipientsEditorTest::test_addLineOnCommaPress() 0046 { 0047 MessageComposer::RecipientsEditor editor(new RecipientsLineTestFactory()); 0048 editor.show(); 0049 editor.activateWindow(); 0050 QVERIFY(QTest::qWaitForWindowActive(&editor)); 0051 0052 QVERIFY(editor.recipients().isEmpty()); 0053 0054 auto lineEdit = editor.lines().constFirst()->findChild<MessageComposer::RecipientLineEdit *>(); 0055 lineEdit->setFocus(); 0056 0057 // Simulate typing email address 0058 QTest::keyClicks(lineEdit, QStringLiteral("\"Vratil, Daniel\" <dvratil@kde.org>"), Qt::NoModifier, 10); 0059 0060 QCOMPARE(editor.recipients().size(), 1); 0061 QCOMPARE(editor.recipients().constFirst()->email(), QStringLiteral("\"Vratil, Daniel\" <dvratil@kde.org>")); 0062 0063 QTest::keyClick(lineEdit, Qt::Key_Comma, Qt::NoModifier, 0); 0064 0065 lineEdit = editor.lines().at(1)->findChild<MessageComposer::RecipientLineEdit *>(); 0066 QVERIFY(lineEdit->hasFocus()); 0067 0068 QTest::keyClicks(lineEdit, QStringLiteral("test@example.test"), Qt::NoModifier, 10); 0069 QCOMPARE(editor.recipients().size(), 2); 0070 QCOMPARE(editor.recipients().at(1)->email(), QStringLiteral("test@example.test")); 0071 QCOMPARE(editor.recipients().at(1)->type(), editor.recipients().at(0)->type()); 0072 } 0073 0074 void RecipientsEditorTest::test_splitStringInputToLines() 0075 { 0076 MessageComposer::RecipientsEditor editor(new RecipientsLineTestFactory()); 0077 0078 QCOMPARE(editor.recipients().size(), 0); 0079 0080 const auto mboxes = KMime::Types::Mailbox::listFromUnicodeString(QStringLiteral("test@example.com, \"Vrátil, Daniel\" <dvratil@kde.org>")); 0081 editor.setRecipientString(mboxes, MessageComposer::Recipient::To); 0082 0083 QCOMPARE(editor.recipients().size(), 2); 0084 QCOMPARE(editor.recipients().at(0)->email(), QStringLiteral("test@example.com")); 0085 QCOMPARE(editor.recipients().at(1)->email(), QStringLiteral("\"Vrátil, Daniel\" <dvratil@kde.org>")); 0086 } 0087 0088 void RecipientsEditorTest::test_splitPastedListToLines() 0089 { 0090 MessageComposer::RecipientsEditor editor(new RecipientsLineTestFactory()); 0091 0092 QCOMPARE(editor.recipients().size(), 0); 0093 0094 const auto clipboard = QApplication::clipboard(); 0095 const QString oldText = clipboard->text(); 0096 0097 clipboard->setText(QStringLiteral("test@example.com, \"Vrátil, Daniel\" <dvratil@kde.org>")); 0098 0099 auto lineEdit = editor.lines().at(0)->findChild<MessageComposer::RecipientLineEdit *>(); 0100 // paste() is protected in KPIM::AddresseeLineEdit 0101 QMetaObject::invokeMethod(lineEdit, "paste"); 0102 0103 // This will still fail the test, but will allow us to reset the clipboard 0104 [&editor]() { 0105 QCOMPARE(editor.recipients().size(), 2); 0106 QCOMPARE(editor.recipients().at(0)->email(), QStringLiteral("test@example.com")); 0107 QCOMPARE(editor.recipients().at(1)->email(), QStringLiteral("\"Vrátil, Daniel\" <dvratil@kde.org>")); 0108 }(); 0109 0110 clipboard->setText(oldText); 0111 } 0112 0113 QTEST_MAIN(RecipientsEditorTest) 0114 0115 #include "recipientseditortest.moc"