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
|
#include <QtTest>
#include <QTemporaryDir>
#include <QSettings>
#include "config.h"
class TestConfig : public QObject
{
Q_OBJECT
private slots:
void parsesAccounts();
void parsesSavedQueries();
void missingSyncCommandIsEmpty();
void accountWithoutMaildirIsRejected();
void scopedQueryWrapsCorrectly();
void absentSyncCommandIsNoticeNotProblem();
void brokenSyncCommandIsAProblem();
void malformedAccountIsAProblem();
void validConfigHasNoProblems();
};
static QString writeIni(const QTemporaryDir &dir, const QString &body)
{
const QString path = dir.filePath(QStringLiteral("qtmaildir.conf"));
QFile f(path);
f.open(QIODevice::WriteOnly | QIODevice::Text);
f.write(body.toUtf8());
f.close();
return path;
}
void TestConfig::parsesAccounts()
{
QTemporaryDir dir;
const QString path = writeIni(dir, QStringLiteral(
"[account.work]\n"
"name=Test User\n"
"address=user@example.org\n"
"maildir=work-mail\n"
"drafts=Drafts\n"
"\n"
"[account.personal]\n"
"name=Test User\n"
"address=me@example.net\n"
"maildir=personal\n"
));
Config config;
config.load(path);
QCOMPARE(config.accounts().size(), 2);
const Account work = config.account(QStringLiteral("work"));
QCOMPARE(work.key, QStringLiteral("work"));
QCOMPARE(work.name, QStringLiteral("Test User"));
QCOMPARE(work.address, QStringLiteral("user@example.org"));
QCOMPARE(work.maildir, QStringLiteral("work-mail"));
QCOMPARE(work.drafts, QStringLiteral("Drafts"));
// drafts is optional in v1 (send is v2).
const Account personal = config.account(QStringLiteral("personal"));
QVERIFY(personal.drafts.isEmpty());
QVERIFY(personal.isValid());
}
void TestConfig::parsesSavedQueries()
{
QTemporaryDir dir;
const QString path = writeIni(dir, QStringLiteral(
"[queries]\n"
"Inbox=tag:inbox\n"
"Unread=tag:unread\n"
));
Config config;
config.load(path);
const QList<SavedQuery> queries = config.savedQueries();
QCOMPARE(queries.size(), 2);
// QSettings::childKeys() returns keys alphabetically, not in file order,
// so the UI button order is alphabetical. This assertion happens to hold
// either way since "Inbox" < "Unread", but the ordering guarantee is
// alphabetical, not "follows the file".
QCOMPARE(queries.at(0).name, QStringLiteral("Inbox"));
QCOMPARE(queries.at(0).query, QStringLiteral("tag:inbox"));
}
void TestConfig::missingSyncCommandIsEmpty()
{
QTemporaryDir dir;
const QString path = writeIni(dir, QStringLiteral("[general]\n"));
Config config;
config.load(path);
QVERIFY(config.syncCommand().isEmpty());
// The UI uses this to disable the Sync button with a tooltip.
QVERIFY(!config.warnings().isEmpty());
}
void TestConfig::accountWithoutMaildirIsRejected()
{
QTemporaryDir dir;
const QString path = writeIni(dir, QStringLiteral(
"[sync]\n"
"command=/bin/true\n"
"\n"
"[account.broken]\n"
"name=No Maildir\n"
"address=x@example.org\n"
));
Config config;
config.load(path);
// Rejected, reported, and not offered to the user as a scope.
QCOMPARE(config.accounts().size(), 0);
QCOMPARE(config.warnings().size(), 1);
QVERIFY(config.warnings().first().contains(QStringLiteral("broken")));
}
void TestConfig::scopedQueryWrapsCorrectly()
{
Account account;
account.key = QStringLiteral("work");
account.maildir = QStringLiteral("work-mail");
QCOMPARE(account.scopedQuery(QStringLiteral("tag:inbox")),
QStringLiteral("path:\"work-mail/**\" and (tag:inbox)"));
// An empty query still scopes to the account rather than matching nothing.
QCOMPARE(account.scopedQuery(QString()),
QStringLiteral("path:\"work-mail/**\""));
}
void TestConfig::absentSyncCommandIsNoticeNotProblem()
{
// An optional feature simply not being configured must not interrupt
// startup: the modal would fire on every launch and train the user to
// dismiss dialogs without reading them.
QTemporaryDir dir;
const QString path = writeIni(dir, QStringLiteral(
"[account.work]\n"
"maildir=work-mail\n"));
Config config;
config.load(path);
QCOMPARE(config.warnings().size(), 1);
QVERIFY(config.warnings().first().contains(QStringLiteral("No sync command")));
QVERIFY(config.problems().isEmpty());
}
void TestConfig::brokenSyncCommandIsAProblem()
{
// Configured but missing is different: the user asked for sync and is not
// getting it, so they need telling.
QTemporaryDir dir;
const QString path = writeIni(dir, QStringLiteral(
"[sync]\n"
"command=/nonexistent/qtmaildir-test/mailsync.sh\n"
"\n"
"[account.work]\n"
"maildir=work-mail\n"));
Config config;
config.load(path);
QCOMPARE(config.problems().size(), 1);
QVERIFY(config.problems().first().contains(QStringLiteral("does not exist")));
// Problems are a subset of warnings, so a caller wanting everything needs
// only warnings().
QVERIFY(config.warnings().contains(config.problems().first()));
}
void TestConfig::malformedAccountIsAProblem()
{
QTemporaryDir dir;
const QString path = writeIni(dir, QStringLiteral(
"[account.broken]\n"
"name=No Maildir Here\n"));
Config config;
config.load(path);
QVERIFY(!config.problems().isEmpty());
QVERIFY(config.problems().first().contains(QStringLiteral("broken")));
}
void TestConfig::validConfigHasNoProblems()
{
QTemporaryDir dir;
const QString path = writeIni(dir, QStringLiteral(
"[sync]\n"
"command=/bin/true\n"
"\n"
"[account.work]\n"
"maildir=work-mail\n"
"address=user@example.org\n"));
Config config;
config.load(path);
QVERIFY(config.problems().isEmpty());
QVERIFY(config.warnings().isEmpty());
}
QTEST_MAIN(TestConfig)
#include "test_config.moc"
|