File: signaturetest.cpp

package info (click to toggle)
kidentitymanagement 16.04.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 372 kB
  • ctags: 369
  • sloc: cpp: 2,879; sh: 26; xml: 8; makefile: 5
file content (273 lines) | stat: -rw-r--r-- 11,489 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
    Copyright (c) 2008 Thomas McGuire <mcguire@kde.org>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#undef QT_USE_FAST_CONCATENATION
#undef QT_USE_FAST_OPERATOR_PLUS

#include "qtest.h"
#include "signaturetest.h"

#include "signature.h"

#include <KIconLoader>
#include <KConfigGroup>
#include <QStandardPaths>
#include <KConfig>
#include <QDir>
#include <KActionCollection>

#include <kpimtextedit/richtextcomposer.h>
#include <kpimtextedit/richtextcomposercontroler.h>
#include <kpimtextedit/richtextcomposerimages.h>
using namespace KIdentityManagement;
using namespace KPIMTextEdit;

QTEST_MAIN(SignatureTester)

void SignatureTester::testSignatures()
{
    Signature sig1;
    sig1.setText(QStringLiteral("Hello World"));
    QCOMPARE(sig1.text(), QStringLiteral("Hello World"));
    QCOMPARE(sig1.type(), Signature::Inlined);
    QCOMPARE(sig1.rawText(), QStringLiteral("Hello World"));
    QVERIFY(!sig1.isInlinedHtml());
    QCOMPARE(sig1.withSeparator(), QStringLiteral("-- \nHello World"));

    Signature sig2;
    sig2.setText(QStringLiteral("<b>Hello</b> World"));
    sig2.setInlinedHtml(true);
    QVERIFY(sig2.isInlinedHtml());
    QCOMPARE(sig2.type(), Signature::Inlined);
    QCOMPARE(sig2.rawText(), QStringLiteral("<b>Hello</b> World"));
    QCOMPARE(sig2.withSeparator(), QStringLiteral("-- <br><b>Hello</b> World"));

    // Read this very file in, we use it for the tests
    QFile thisFile(QStringLiteral(__FILE__));
    thisFile.open(QIODevice::ReadOnly);
    QString fileContent = QString::fromUtf8(thisFile.readAll());

    Signature sig3;
    sig3.setPath(QStringLiteral("cat ") + QStringLiteral(__FILE__), true);
    QCOMPARE(sig3.rawText(), fileContent);
    QVERIFY(!sig3.isInlinedHtml());
    QVERIFY(sig3.text().isEmpty());
    QCOMPARE(sig3.type(), Signature::FromCommand);
    QCOMPARE(sig3.withSeparator(), QString(QStringLiteral("-- \n") + fileContent));

    Signature sig4;
    sig4.setPath(QStringLiteral(__FILE__), false);
    QCOMPARE(sig4.rawText(), fileContent);
    QVERIFY(!sig4.isInlinedHtml());
    QVERIFY(sig4.text().isEmpty());
    QCOMPARE(sig4.type(), Signature::FromFile);
    QCOMPARE(sig4.withSeparator(), QString(QStringLiteral("-- \n") + fileContent));
}

static void setCursorPos(QTextEdit &edit, int pos)
{
    QTextCursor cursor(edit.document());
    cursor.setPosition(pos);
    edit.setTextCursor(cursor);
}

void SignatureTester::testTextEditInsertion()
{
    KPIMTextEdit::RichTextComposer edit;
    edit.createActions(new KActionCollection(this));
    Signature sig;
    sig.setEnabledSignature(true);
    sig.setText(QStringLiteral("Hello World"));

    // Test inserting signature at start, with seperators. Make sure two new
    // lines are inserted before the signature

    edit.setPlainText(QStringLiteral("Bla Bla"));
    sig.insertIntoTextEdit(Signature::Start, Signature::AddSeparator | Signature::AddNewLines,
                           &edit);
    QVERIFY(edit.textMode() == KPIMTextEdit::RichTextComposer::Plain);
    QCOMPARE(edit.toPlainText(), QStringLiteral("\n\n-- \nHello World\nBla Bla"));

    // Test inserting signature at end. make sure cursor position is preserved
    edit.clear();
    edit.setPlainText(QStringLiteral("Bla Bla"));
    setCursorPos(edit, 4);
    sig.insertIntoTextEdit(Signature::End, Signature::AddSeparator | Signature::AddNewLines,
                           &edit);
    QCOMPARE(edit.toPlainText(), QStringLiteral("Bla Bla\n-- \nHello World"));
    QCOMPARE(edit.textCursor().position(), 4);

    // test inserting a signature at cursor position. make sure the signature is inserted
    // to the beginning of the line and make sure modified state is preserved
    edit.clear();
    edit.setPlainText(QStringLiteral("Bla Bla"));
    setCursorPos(edit, 4);
    edit.document()->setModified(false);
    sig.insertIntoTextEdit(Signature::AtCursor, Signature::AddSeparator | Signature::AddNewLines,
                           &edit);
    QCOMPARE(edit.toPlainText(), QStringLiteral("-- \nHello World\nBla Bla"));
    QCOMPARE(edit.textCursor().position(), 20);
    QVERIFY(!edit.document()->isModified());

    // make sure undo undoes everything in one go
    edit.undo();
    QCOMPARE(edit.toPlainText(), QStringLiteral("Bla Bla"));

    // test inserting signature without seperator.
    // make sure cursor position and modified state is preserved.
    edit.clear();
    edit.setPlainText(QStringLiteral("Bla Bla"));
    setCursorPos(edit, 4);
    edit.document()->setModified(true);
    sig.insertIntoTextEdit(Signature::End, Signature::AddNewLines, &edit);
    QCOMPARE(edit.toPlainText(), QStringLiteral("Bla Bla\nHello World"));
    QCOMPARE(edit.textCursor().position(), 4);
    QVERIFY(edit.document()->isModified());

    sig.setText(QStringLiteral("Hello<br>World"));
    sig.setInlinedHtml(true);

    // test that html signatures turn html on and have correct line endings (<br> vs \n)
    edit.clear();
    edit.setPlainText(QStringLiteral("Bla Bla"));
    sig.insertIntoTextEdit(Signature::End, Signature::AddSeparator | Signature::AddNewLines,
                           &edit);
    QVERIFY(edit.textMode() == KPIMTextEdit::RichTextComposer::Rich);
    QCOMPARE(edit.toPlainText(), QStringLiteral("Bla Bla\n-- \nHello\nWorld"));
}

void SignatureTester::testBug167961()
{
    KPIMTextEdit::RichTextComposer edit;
    edit.createActions(new KActionCollection(this));
    Signature sig;
    sig.setEnabledSignature(true);
    sig.setText(QStringLiteral("BLA"));

    // Test that the cursor is still at the start when appending a sig into
    // an empty text edit
    sig.insertIntoTextEdit(Signature::End, Signature::AddSeparator | Signature::AddNewLines, &edit);
    QCOMPARE(edit.textCursor().position(), 0);

    // When prepending a sig, the cursor should also be at the start, see bug 211634
    edit.clear();
    sig.insertIntoTextEdit(Signature::Start, Signature::AddSeparator | Signature::AddNewLines, &edit);
    QCOMPARE(edit.textCursor().position(), 0);
}

// Make writeConfig() public, we need it
class MySignature : public Signature
{
public:
    using Signature::writeConfig;
    using Signature::readConfig;
};

void SignatureTester::testImages()
{
    KPIMTextEdit::RichTextComposer edit;
    QIcon::setThemeName(QStringLiteral("breeze"));
    edit.createActions(new KActionCollection(this));
    QString image1Path = KIconLoader::global()->iconPath(QStringLiteral("folder-new"), KIconLoader::Small, false);
    QImage image1, image2;
    QVERIFY(image1.load(image1Path));
    QVERIFY(image2.load(image1Path));
    QString path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + QStringLiteral("emailidentities/unittest/");
    QDir().mkpath(path);
    QString configPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + QStringLiteral("signaturetest");
    QDir().mkpath(configPath);
    KConfig config(configPath);
    KConfigGroup group1 = config.group("Signature1");

    MySignature sig;
    sig.setEnabledSignature(true);
    sig.setImageLocation(path);
    sig.setInlinedHtml(true);
    sig.setText(QStringLiteral("Bla<img src=\"folder-new.png\">Bla"));
    sig.addImage(image1, QStringLiteral("folder-new.png"));
    sig.writeConfig(group1);

    // OK, signature saved, the image should be saved as well
    QDir dir(path);
    QStringList entryList = dir.entryList(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
    QCOMPARE(entryList.count(), 1);
    QCOMPARE(entryList.first(), QStringLiteral("folder-new.png"));

    // Now, set the text of the signature to something without images, then save it.
    // The signature class should have removed the images.
    sig.setText(QStringLiteral("ascii ribbon campaign - against html mail"));
    sig.writeConfig(group1);
    entryList = dir.entryList(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
    QCOMPARE(entryList.count(), 0);

    // Enable images again, this time with two of the buggers
    sig.setText(QStringLiteral("Bla<img src=\"folder-new.png\">Bla<img src=\"arrow-up.png\">Bla"));
    sig.addImage(image1, QStringLiteral("folder-new.png"));
    sig.addImage(image2, QStringLiteral("arrow-up.png"));
    sig.writeConfig(group1);
    entryList = dir.entryList(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
    QCOMPARE(entryList.count(), 2);
    QCOMPARE(entryList.first(), QStringLiteral("arrow-up.png"));
    QCOMPARE(entryList.last(), QStringLiteral("folder-new.png"));

    // Now, create a second signature instance from the same config, and make sure it can still
    // read the images, and it does not mess up
    MySignature sig2;
    sig2.readConfig(group1);
    sig2.insertIntoTextEdit(KIdentityManagement::Signature::End, Signature::AddSeparator | Signature::AddNewLines,
                            &edit);
    QCOMPARE(edit.composerControler()->composerImages()->embeddedImages().count(), 2);
    QCOMPARE(sig2.text(), QStringLiteral("Bla<img src=\"folder-new.png\">Bla<img src=\"arrow-up.png\">Bla"));
    sig2.writeConfig(group1);
    entryList = dir.entryList(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
    QCOMPARE(entryList.count(), 2);
    QCOMPARE(entryList.first(), QStringLiteral("arrow-up.png"));
    QCOMPARE(entryList.last(), QStringLiteral("folder-new.png"));

    // Remove one image from the signature, and make sure only 1 file is left one file system.
    sig2.setText(QStringLiteral("<img src=\"folder-new.png\">"));
    sig2.writeConfig(group1);
    edit.clear();
    sig2.insertIntoTextEdit(Signature::End, Signature::AddSeparator | Signature::AddNewLines,
                            &edit);
    QCOMPARE(edit.composerControler()->composerImages()->embeddedImages().size(), 1);
    entryList = dir.entryList(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
    QCOMPARE(entryList.count(), 1);
}

void SignatureTester::testLinebreaks()
{
    Signature sig;
    sig.setEnabledSignature(true);
    sig.setType(Signature::Inlined);
    sig.setInlinedHtml(true);
    sig.setText(QStringLiteral("Hans Mustermann<br>Musterstr. 42"));

    KPIMTextEdit::RichTextComposer edit;
    edit.createActions(new KActionCollection(this));
    sig.insertIntoTextEdit(Signature::Start, Signature::AddNothing, &edit);
    QCOMPARE(edit.toPlainText(), QStringLiteral("Hans Mustermann\nMusterstr. 42"));

    edit.clear();
    sig.setText(QStringLiteral("<p>Hans Mustermann</p><br>Musterstr. 42"));
    sig.insertIntoTextEdit(Signature::Start, Signature::AddSeparator, &edit);
    QEXPECT_FAIL("", "This test is probably bogus, since Qt doesn't seem to produce HTML like this anymore.", Continue);
    QCOMPARE(edit.toPlainText(), QStringLiteral("-- \nHans Mustermann\nMusterstr. 42"));
}