2 * Copyright (c) 2014 Sandro Knauß <knauss@kolabsys.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * As a special exception, permission is given to link this program
19 * with any edition of Qt, and distribute the resulting executable,
20 * without including the source code for Qt in the source distribution.
23 #include "noteeditdialogtest.h"
24 #include "dialog/noteeditdialog.h"
25 #include <AkonadiCore/Collection>
26 #include <AkonadiWidgets/CollectionComboBox>
27 #include <AkonadiCore/EntityTreeModel>
29 #include <KMime/KMimeMessage>
30 #include <QStandardItemModel>
31 #include <QPushButton>
34 #include <qtestkeyboard.h>
35 #include <qtestmouse.h>
36 #include <kpimtextedit/richtexteditorwidget.h>
37 #include <kpimtextedit/richtexteditor.h>
43 NoteEditDialogTest::NoteEditDialogTest()
45 qRegisterMetaType
<Akonadi::Collection
>();
46 qRegisterMetaType
<Akonadi::Item
>();
47 qRegisterMetaType
<KMime::Message::Ptr
>();
49 QStandardItemModel
*model
= new QStandardItemModel
;
50 for (int id
= 42; id
< 51; ++id
) {
51 Akonadi::Collection
collection(id
);
52 collection
.setRights(Akonadi::Collection::AllRights
);
53 collection
.setName(QString::number(id
));
54 collection
.setContentMimeTypes(QStringList() << Akonadi::NoteUtils::noteMimeType());
56 QStandardItem
*item
= new QStandardItem(collection
.name());
57 item
->setData(QVariant::fromValue(collection
),
58 Akonadi::EntityTreeModel::CollectionRole
);
59 item
->setData(QVariant::fromValue(collection
.id()),
60 Akonadi::EntityTreeModel::CollectionIdRole
);
62 model
->appendRow(item
);
64 NoteEditDialog::_k_noteEditStubModel
= model
;
67 void NoteEditDialogTest::shouldHaveDefaultValuesOnCreation()
70 QVERIFY(!edit
.note());
71 QLineEdit
*notetitle
= edit
.findChild
<QLineEdit
*>(QStringLiteral("notetitle"));
72 KPIMTextEdit::RichTextEditorWidget
*notetext
= edit
.findChild
<KPIMTextEdit::RichTextEditorWidget
*>(QStringLiteral("notetext"));
73 QPushButton
*ok
= edit
.findChild
<QPushButton
*>(QStringLiteral("save-button"));
75 QCOMPARE(notetitle
->text(), QString());
77 QCOMPARE(notetext
->toPlainText(), QString());
79 QCOMPARE(ok
->isEnabled(), false);
82 void NoteEditDialogTest::shouldEmitCollectionChanged()
85 QSignalSpy
spy(&edit
, SIGNAL(collectionChanged(Akonadi::Collection
)));
86 edit
.setCollection(Akonadi::Collection(42));
87 QCOMPARE(spy
.count(), 1);
88 QCOMPARE(spy
.at(0).at(0).value
<Akonadi::Collection
>(), Akonadi::Collection(42));
91 void NoteEditDialogTest::shouldNotEmitWhenCollectionIsNotChanged()
94 edit
.setCollection(Akonadi::Collection(42));
95 QSignalSpy
spy(&edit
, SIGNAL(collectionChanged(Akonadi::Collection
)));
96 edit
.setCollection(Akonadi::Collection(42));
97 QCOMPARE(spy
.count(), 0);
100 void NoteEditDialogTest::shouldHaveSameValueAfterSet()
104 Akonadi::NoteUtils::NoteMessageWrapper note
;
106 item
.setMimeType(Akonadi::NoteUtils::noteMimeType());
107 KMime::Message::Ptr
message(note
.message());
108 item
.setPayload(message
);
110 edit
.setCollection(Akonadi::Collection(42));
112 QCOMPARE(edit
.collection(), Akonadi::Collection(42));
113 QCOMPARE(edit
.note()->encodedContent(), message
->encodedContent());
116 void NoteEditDialogTest::shouldHaveFilledText()
120 Akonadi::NoteUtils::NoteMessageWrapper note
;
121 QString title
= QStringLiteral("title");
122 QString text
= QStringLiteral("text");
123 note
.setTitle(title
);
126 item
.setMimeType(Akonadi::NoteUtils::noteMimeType());
127 item
.setPayload(note
.message());
130 QLineEdit
*notetitle
= edit
.findChild
<QLineEdit
*>(QStringLiteral("notetitle"));
131 KPIMTextEdit::RichTextEditorWidget
*notetext
= edit
.findChild
<KPIMTextEdit::RichTextEditorWidget
*>(QStringLiteral("notetext"));
132 QCOMPARE(notetitle
->text(), title
);
133 QCOMPARE(notetext
->toPlainText(), text
);
136 void NoteEditDialogTest::shouldHaveRichText()
140 Akonadi::NoteUtils::NoteMessageWrapper note
;
141 QString title
= QStringLiteral("title");
142 QString text
= QStringLiteral("text");
143 note
.setTitle(title
);
144 note
.setText(text
, Qt::RichText
);
146 item
.setMimeType(Akonadi::NoteUtils::noteMimeType());
147 item
.setPayload(note
.message());
150 KPIMTextEdit::RichTextEditorWidget
*notetext
= edit
.findChild
<KPIMTextEdit::RichTextEditorWidget
*>(QStringLiteral("notetext"));
151 QCOMPARE(notetext
->toPlainText(), text
);
152 QVERIFY(notetext
->editor()->acceptRichText());
155 void NoteEditDialogTest::shouldDefaultCollectionIsValid()
158 Akonadi::CollectionComboBox
*akonadicombobox
= edit
.findChild
<Akonadi::CollectionComboBox
*>(QStringLiteral("akonadicombobox"));
159 QVERIFY(akonadicombobox
);
160 QVERIFY(akonadicombobox
->currentCollection().isValid());
163 void NoteEditDialogTest::shouldEmitCollectionChangedWhenCurrentCollectionWasChanged()
166 Akonadi::CollectionComboBox
*akonadicombobox
= edit
.findChild
<Akonadi::CollectionComboBox
*>(QStringLiteral("akonadicombobox"));
167 akonadicombobox
->setCurrentIndex(0);
168 QCOMPARE(akonadicombobox
->currentIndex(), 0);
169 QSignalSpy
spy(&edit
, SIGNAL(collectionChanged(Akonadi::Collection
)));
170 akonadicombobox
->setCurrentIndex(3);
171 QCOMPARE(akonadicombobox
->currentIndex(), 3);
172 QCOMPARE(spy
.count(), 1);
175 void NoteEditDialogTest::shouldEmitCorrectCollection()
178 Akonadi::CollectionComboBox
*akonadicombobox
= edit
.findChild
<Akonadi::CollectionComboBox
*>(QStringLiteral("akonadicombobox"));
180 Akonadi::NoteUtils::NoteMessageWrapper note
;
181 QString title
= QStringLiteral("title");
182 QString text
= QStringLiteral("text");
183 note
.setTitle(title
);
186 item
.setMimeType(Akonadi::NoteUtils::noteMimeType());
187 item
.setPayload(note
.message());
190 akonadicombobox
->setCurrentIndex(3);
191 Akonadi::Collection col
= akonadicombobox
->currentCollection();
192 QSignalSpy
spy(&edit
, SIGNAL(createNote(Akonadi::Item
,Akonadi::Collection
)));
193 QPushButton
*ok
= edit
.findChild
<QPushButton
*>(QStringLiteral("save-button"));
194 QTest::mouseClick(ok
, Qt::LeftButton
);
195 QCOMPARE(spy
.count(), 1);
196 QCOMPARE(spy
.at(0).at(1).value
<Akonadi::Collection
>(), col
);
199 void NoteEditDialogTest::shouldNotEmitNoteWhenTitleIsEmpty()
202 Akonadi::NoteUtils::NoteMessageWrapper note
;
204 item
.setMimeType(Akonadi::NoteUtils::noteMimeType());
205 item
.setPayload(note
.message());
208 QSignalSpy
spy(&edit
, SIGNAL(createNote(Akonadi::Item
,Akonadi::Collection
)));
209 QPushButton
*ok
= edit
.findChild
<QPushButton
*>(QStringLiteral("save-button"));
211 QLineEdit
*notetitle
= edit
.findChild
<QLineEdit
*>(QStringLiteral("notetitle"));
212 notetitle
->setText(QString());
213 QTest::mouseClick(ok
, Qt::LeftButton
);
214 QCOMPARE(spy
.count(), 0);
215 notetitle
->setText(QStringLiteral("F"));
216 QTest::mouseClick(ok
, Qt::LeftButton
);
217 QCOMPARE(spy
.count(), 1);
220 void NoteEditDialogTest::shouldNotEmitNoteWhenTextIsEmpty()
223 Akonadi::NoteUtils::NoteMessageWrapper note
;
225 item
.setMimeType(Akonadi::NoteUtils::noteMimeType());
226 item
.setPayload(note
.message());
229 QSignalSpy
spy(&edit
, SIGNAL(createNote(Akonadi::Item
,Akonadi::Collection
)));
230 QPushButton
*ok
= edit
.findChild
<QPushButton
*>(QStringLiteral("save-button"));
232 //Need to set title to empty, 'cause NoteUtils uses default title: "New Note"
233 QLineEdit
*notetitle
= edit
.findChild
<QLineEdit
*>(QStringLiteral("notetitle"));
234 notetitle
->setText(QString());
236 QTest::mouseClick(ok
, Qt::LeftButton
);
237 QCOMPARE(spy
.count(), 0);
238 KPIMTextEdit::RichTextEditorWidget
*notetext
= edit
.findChild
<KPIMTextEdit::RichTextEditorWidget
*>(QStringLiteral("notetext"));
239 notetext
->editor()->setText(QStringLiteral("F"));
240 QTest::mouseClick(ok
, Qt::LeftButton
);
241 QCOMPARE(spy
.count(), 1);
244 void NoteEditDialogTest::shouldNoteHasCorrectText()
247 Akonadi::NoteUtils::NoteMessageWrapper note
;
248 QString
text(QStringLiteral("text"));
251 item
.setMimeType(Akonadi::NoteUtils::noteMimeType());
252 item
.setPayload(note
.message());
255 QSignalSpy
spy(&edit
, SIGNAL(createNote(Akonadi::Item
,Akonadi::Collection
)));
256 QPushButton
*ok
= edit
.findChild
<QPushButton
*>(QStringLiteral("save-button"));
258 QTest::mouseClick(ok
, Qt::LeftButton
);
259 QCOMPARE(spy
.count(), 1);
260 Akonadi::NoteUtils::NoteMessageWrapper
rNote(spy
.at(0).at(0).value
<Akonadi::Item
>().payload
<KMime::Message::Ptr
>());
261 QCOMPARE(rNote
.text(), text
);
262 KPIMTextEdit::RichTextEditorWidget
*notetext
= edit
.findChild
<KPIMTextEdit::RichTextEditorWidget
*>(QStringLiteral("notetext"));
263 QString text2
= QStringLiteral("F");
264 notetext
->editor()->setText(text2
);
265 QTest::mouseClick(ok
, Qt::LeftButton
);
266 QCOMPARE(spy
.count(), 2);
267 Akonadi::NoteUtils::NoteMessageWrapper
r2Note(spy
.at(1).at(0).value
<Akonadi::Item
>().payload
<KMime::Message::Ptr
>());
268 QCOMPARE(r2Note
.text(), text2
);
272 void NoteEditDialogTest::shouldNoteHasCorrectTitle()
275 Akonadi::NoteUtils::NoteMessageWrapper note
;
276 QString
text(QStringLiteral("text"));
279 item
.setMimeType(Akonadi::NoteUtils::noteMimeType());
280 item
.setPayload(note
.message());
283 QSignalSpy
spy(&edit
, SIGNAL(createNote(Akonadi::Item
,Akonadi::Collection
)));
284 QPushButton
*ok
= edit
.findChild
<QPushButton
*>(QStringLiteral("save-button"));
286 QTest::mouseClick(ok
, Qt::LeftButton
);
287 QCOMPARE(spy
.count(), 1);
288 Akonadi::NoteUtils::NoteMessageWrapper
rNote(spy
.at(0).at(0).value
<Akonadi::Item
>().payload
<KMime::Message::Ptr
>());
289 QCOMPARE(rNote
.title(), text
);
290 QLineEdit
*notetitle
= edit
.findChild
<QLineEdit
*>(QStringLiteral("notetitle"));
291 QString text2
= QStringLiteral("F");
292 notetitle
->setText(text2
);
293 QTest::mouseClick(ok
, Qt::LeftButton
);
294 QCOMPARE(spy
.count(), 2);
295 Akonadi::NoteUtils::NoteMessageWrapper
r2Note(spy
.at(1).at(0).value
<Akonadi::Item
>().payload
<KMime::Message::Ptr
>());
296 QCOMPARE(r2Note
.title(), text2
);
300 void NoteEditDialogTest::shouldNoteHasCorrectTextFormat()
303 Akonadi::NoteUtils::NoteMessageWrapper note
;
304 QString
text(QStringLiteral("text"));
307 item
.setMimeType(Akonadi::NoteUtils::noteMimeType());
308 item
.setPayload(note
.message());
311 QSignalSpy
spy(&edit
, SIGNAL(createNote(Akonadi::Item
,Akonadi::Collection
)));
312 QPushButton
*ok
= edit
.findChild
<QPushButton
*>(QStringLiteral("save-button"));
314 QTest::mouseClick(ok
, Qt::LeftButton
);
315 QCOMPARE(spy
.count(), 1);
316 Akonadi::NoteUtils::NoteMessageWrapper
rNote(spy
.at(0).at(0).value
<Akonadi::Item
>().payload
<KMime::Message::Ptr
>());
317 QCOMPARE(rNote
.textFormat(), Qt::PlainText
);
318 KPIMTextEdit::RichTextEditorWidget
*notetext
= edit
.findChild
<KPIMTextEdit::RichTextEditorWidget
*>(QStringLiteral("notetext"));
319 notetext
->editor()->setAcceptRichText(true);
320 QTest::mouseClick(ok
, Qt::LeftButton
);
321 QCOMPARE(spy
.count(), 2);
322 Akonadi::NoteUtils::NoteMessageWrapper
r2Note(spy
.at(1).at(0).value
<Akonadi::Item
>().payload
<KMime::Message::Ptr
>());
323 QCOMPARE(r2Note
.textFormat(), Qt::RichText
);
327 void NoteEditDialogTest::shouldShouldEnabledSaveEditorButton()
330 Akonadi::NoteUtils::NoteMessageWrapper note
;
331 QString
text(QStringLiteral("text"));
334 item
.setMimeType(Akonadi::NoteUtils::noteMimeType());
335 item
.setPayload(note
.message());
339 QPushButton
*ok
= edit
.findChild
<QPushButton
*>(QStringLiteral("save-button"));
340 QLineEdit
*notetitle
= edit
.findChild
<QLineEdit
*>(QStringLiteral("notetitle"));
342 QCOMPARE(ok
->isEnabled(), true);
345 QCOMPARE(ok
->isEnabled(), false);
348 QTEST_MAIN(NoteEditDialogTest
)