Changeset View
Changeset View
Standalone View
Standalone View
messagecomposer/autotests/recipientseditortest.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2016 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or modify it | ||||
5 | under the terms of the GNU Library General Public License as published by | ||||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||||
7 | option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||||
12 | License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||||
17 | 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include <QObject> | ||||
21 | #include <QTest> | ||||
22 | | ||||
23 | #include "recipient/recipientseditor.h" | ||||
24 | #include "recipient/recipientline.h" | ||||
25 | | ||||
26 | #include <KMime/Types> | ||||
27 | #include <QClipboard> | ||||
28 | #include <QSignalSpy> | ||||
29 | | ||||
30 | class RecipientsLineTestFactory : public MessageComposer::RecipientLineFactory | ||||
31 | { | ||||
32 | Q_OBJECT | ||||
33 | | ||||
34 | public: | ||||
35 | explicit RecipientsLineTestFactory(QObject *parent = Q_NULLPTR) | ||||
36 | : MessageComposer::RecipientLineFactory(parent) | ||||
37 | { | ||||
38 | } | ||||
39 | | ||||
40 | KPIM::MultiplyingLine *newLine(QWidget *parent) Q_DECL_OVERRIDE | ||||
41 | { | ||||
42 | auto line = qobject_cast<MessageComposer::RecipientLineNG*>( | ||||
43 | MessageComposer::RecipientLineFactory::newLine(parent)); | ||||
44 | line->setEnableAkonadiSearch(false); | ||||
45 | line->setEnableIndexSearch(false); | ||||
46 | return line; | ||||
47 | } | ||||
48 | }; | ||||
49 | | ||||
50 | | ||||
51 | | ||||
52 | class RecipientsEditorTest : public QObject | ||||
53 | { | ||||
54 | Q_OBJECT | ||||
55 | | ||||
56 | private Q_SLOTS: | ||||
57 | void test_addLineOnCommaPress(); | ||||
58 | void test_splitStringInputToLines(); | ||||
59 | void test_splitPastedListToLines(); | ||||
60 | }; | ||||
61 | | ||||
62 | | ||||
63 | void RecipientsEditorTest::test_addLineOnCommaPress() | ||||
64 | { | ||||
65 | MessageComposer::RecipientsEditor editor(new RecipientsLineTestFactory()); | ||||
66 | editor.show(); | ||||
67 | QTest::qWaitForWindowActive(&editor); | ||||
68 | | ||||
69 | QCOMPARE(editor.recipients().size(), 0); | ||||
70 | | ||||
71 | auto lineEdit = editor.lines().first()->findChild<MessageComposer::RecipientLineEdit*>(); | ||||
72 | lineEdit->setFocus(); | ||||
73 | | ||||
74 | // Simulate typing email address | ||||
75 | QTest::keyClicks(lineEdit, QStringLiteral("\"Vratil, Daniel\" <dvratil@kde.org>"), Qt::NoModifier, 10); | ||||
76 | | ||||
77 | QCOMPARE(editor.recipients().size(), 1); | ||||
78 | QCOMPARE(editor.recipients().first()->email(), QStringLiteral("dvratil@kde.org")); | ||||
79 | | ||||
80 | QTest::keyClick(lineEdit, Qt::Key_Comma, Qt::NoModifier, 0); | ||||
81 | | ||||
82 | lineEdit = editor.lines().at(1)->findChild<MessageComposer::RecipientLineEdit*>(); | ||||
83 | QVERIFY(lineEdit->hasFocus()); | ||||
84 | | ||||
85 | QTest::keyClicks(lineEdit, QStringLiteral("test@example.test"), Qt::NoModifier, 10); | ||||
86 | QCOMPARE(editor.recipients().size(), 2); | ||||
87 | QCOMPARE(editor.recipients().at(1)->email(), QStringLiteral("test@example.test")); | ||||
88 | QCOMPARE(editor.recipients().at(1)->type(), editor.recipients().at(0)->type()); | ||||
89 | } | ||||
90 | | ||||
91 | void RecipientsEditorTest::test_splitStringInputToLines() | ||||
92 | { | ||||
93 | MessageComposer::RecipientsEditor editor(new RecipientsLineTestFactory()); | ||||
94 | | ||||
95 | QCOMPARE(editor.recipients().size(), 0); | ||||
96 | | ||||
97 | const auto mboxes = KMime::Types::Mailbox::listFromUnicodeString(QStringLiteral("test@example.com, \"Vrátil, Daniel\" <dvratil@kde.org>")); | ||||
98 | editor.setRecipientString(mboxes, MessageComposer::Recipient::To); | ||||
99 | | ||||
100 | QCOMPARE(editor.recipients().size(), 2); | ||||
101 | QCOMPARE(editor.recipients().at(0)->email(), QStringLiteral("test@example.com")); | ||||
102 | QCOMPARE(editor.recipients().at(1)->email(), QStringLiteral("dvratil@kde.org")); | ||||
103 | } | ||||
104 | | ||||
105 | void RecipientsEditorTest::test_splitPastedListToLines() | ||||
106 | { | ||||
107 | MessageComposer::RecipientsEditor editor(new RecipientsLineTestFactory()); | ||||
108 | | ||||
109 | QCOMPARE(editor.recipients().size(), 0); | ||||
110 | | ||||
111 | const auto clipboard = QApplication::clipboard(); | ||||
112 | const QString oldText = clipboard->text(); | ||||
113 | | ||||
114 | clipboard->setText(QStringLiteral("test@example.com, \"Vrátil, Daniel\" <dvratil@kde.org>")); | ||||
115 | | ||||
116 | | ||||
117 | auto lineEdit = editor.lines().at(0)->findChild<MessageComposer::RecipientLineEdit*>(); | ||||
118 | // paste() is protected in KPIM::AddresseeLineEdit | ||||
119 | QMetaObject::invokeMethod(lineEdit, "paste"); | ||||
120 | | ||||
121 | // This will still fail the test, but will allow us to reset the clipboard | ||||
122 | [&editor]() { | ||||
123 | QCOMPARE(editor.recipients().size(), 2); | ||||
124 | QCOMPARE(editor.recipients().at(0)->email(), QStringLiteral("test@example.com")); | ||||
125 | QCOMPARE(editor.recipients().at(1)->email(), QStringLiteral("dvratil@kde.org")); | ||||
126 | }(); | ||||
127 | | ||||
128 | clipboard->setText(oldText); | ||||
129 | } | ||||
130 | | ||||
131 | QTEST_MAIN(RecipientsEditorTest) | ||||
132 | | ||||
133 | #include "recipientseditortest.moc" |