Commit b2caae50 authored by Sandro Knauß's avatar Sandro Knauß
Browse files

Add tests for KMComposerWin.

parent 3c810b1f
Pipeline #51802 canceled with stage
set( EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR})
add_definitions( -DTEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data" )
include_directories(
BEFORE
${kmail_SOURCE_DIR}
......@@ -43,6 +44,9 @@ if (KDEPIM_RUN_AKONADI_TEST)
set(KDEPIMLIBS_RUN_ISOLATED_TESTS TRUE)
set(KDEPIMLIBS_RUN_SQLITE_ISOLATED_TESTS TRUE)
add_akonadi_isolated_test_advanced( kmcomposerwintest.cpp ""
"Qt5::Test;KF5::IdentityManagement;KF5::MessageCore;KF5::TemplateParser;KF5::XmlGui;Qt5::Widgets;KF5::ConfigWidgets;KF5::I18n;kmailprivate")
add_akonadi_isolated_test_advanced( tagselectdialogtest.cpp "" "kmailprivate;KF5::MailCommon;KF5::Libkdepim;KF5::ItemViews;KF5::TemplateParser;KF5::XmlGui;KF5::Completion;KF5::I18n")
add_akonadi_isolated_test_advanced(kmcommandstest.cpp ""
......
{"addr": "friends@kde.org","autocrypt_timestamp": "","count_have_ach": 0,"count_no_ach": 0,"gossip_key": "mDMEYBITIhYJKwYBBAHaRw8BAQdA4iY3uF1o5HoUZgLrQO/iAnpKzTxoGH5BydurzRg40WG0KHJlY2lwaWVudDEgPHJlY2lwaWVudEBhdXRvY3J5cHQuZXhhbXBsZT6IkAQTFggAOBYhBHP4W7K5+EaSF8OJ6mITWYVSrMvrBQJgEhMiAhsDBQsJCAcCBhUKCQgLAgQWAgMBAh4BAheAAAoJEGITWYVSrMvrMX4A/2pk18AxL/uJXlic8D4Bj3crNOOz4sBm/tgLk4GpwKLvAQCbwd3O9MQNKNQ0BrMjN0egRLsOO0/P3EfRkH1DP6lLCrg4BGASEyISCisGAQQBl1UBBQEBB0AcR0uenAjI/mCD5Nte45gZ1veh97TvBqFPmVdUuFIDFgMBCAeIeAQYFggAIBYhBHP4W7K5+EaSF8OJ6mITWYVSrMvrBQJgEhMiAhsMAAoJEGITWYVSrMvr3BMBAM2/0ZJw/7SWPQmtVwbwaACqyiJPBGBPBx4nnGU+KhNxAQCbWYOibm+u+corh7Jccn6AVYlRKHmczkKQ7GlzQHvMCQ==","gossip_timestamp":"2011-05-26T01:16:54+01:00","keydata":"","prefer_encrypt":false}
/*
SPDX-FileCopyrightText: 2016 Sandro Knauß <sknauss@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "kmcomposerwintest.h"
#include "composer.h"
#include "kmkernel.h"
#include <KIdentityManagement/Identity>
#include <KIdentityManagement/IdentityCombo>
#include <KIdentityManagement/IdentityManager>
#include <KMime/Message>
#include <QEventLoop>
#include <QLabel>
#include <QStandardPaths>
#include <QTimer>
#include <QTemporaryDir>
#include <QTest>
QTEST_MAIN(KMComposerWinTest)
KMime::Message::Ptr createItem(const KIdentityManagement::Identity &ident)
{
QByteArray data
= "From: Konqui <konqui@kde.org>\n"
"To: Friends <friends@kde.org>\n"
"Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n"
"Subject: Sample message\n"
"MIME-Version: 1.0\n"
"X-KMail-Identity: " + QByteArray::number(ident.uoid()) + "\n"
"Content-type: text/plain; charset=us-ascii\n"
"\n"
"\n"
"This is explicitly typed plain US-ASCII text.\n"
"It DOES end with a linebreak.\n"
"\n";
auto msgPtr = KMime::Message::Ptr(new KMime::Message());
msgPtr->setContent(data);
msgPtr->parse();
return msgPtr;
}
KMComposerWinTest::KMComposerWinTest(QObject *parent)
: QObject(parent)
, mKernel(new KMKernel(parent))
{
}
KMComposerWinTest::~KMComposerWinTest()
{
delete mKernel;
}
void KMComposerWinTest::init()
{
autocryptDir.removeRecursively();
autocryptDir.mkpath(QStringLiteral("."));
}
void KMComposerWinTest::cleanup()
{
autocryptDir.removeRecursively();
QEventLoop loop;
auto w = mKernel->mainWin();
loop.connect(w, &QMainWindow::destroyed, &loop, &QEventLoop::quit);
w->close();
loop.exec();
resetIdentities();
}
void KMComposerWinTest::initTestCase()
{
qputenv("LC_ALL", "C");
qputenv("KDEHOME", QFile::encodeName(QDir::homePath() + QLatin1String("/.qttest")).constData());
const QDir genericDataLocation(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation));
autocryptDir = QDir(genericDataLocation.filePath(QStringLiteral("autocrypt")));
const KIdentityManagement::Identity &def = mKernel->identityManager()->defaultIdentity();
KIdentityManagement::Identity &i1 = mKernel->identityManager()->modifyIdentityForUoid(def.uoid());
i1.setIdentityName(QStringLiteral("default"));
mKernel->identityManager()->newFromScratch(QStringLiteral("test2"));
mKernel->identityManager()->newFromScratch(QStringLiteral("test3"));
mKernel->identityManager()->newFromScratch(QStringLiteral("autocrypt"));
mKernel->identityManager()->commit();
resetIdentities();
}
void KMComposerWinTest::resetIdentities()
{
KIdentityManagement::Identity &i1 = mKernel->identityManager()->modifyIdentityForName(QStringLiteral("default"));
i1.setFullName(QStringLiteral("default"));
i1.setPrimaryEmailAddress(QStringLiteral("firstname.lastname@example.com"));
i1.setPGPSigningKey("0x123456789");
i1.setPGPEncryptionKey("0x123456789");
i1.setPgpAutoSign(true);
i1.setPgpAutoEncrypt(false);
KIdentityManagement::Identity &i2 = mKernel->identityManager()->modifyIdentityForName(QStringLiteral("test2"));
i2.setFullName(QStringLiteral("second"));
i2.setPrimaryEmailAddress(QStringLiteral("secundus@example.com"));
i2.setPGPSigningKey("0x234567890");
i2.setPGPEncryptionKey("0x234567890");
i2.setPgpAutoSign(false);
i2.setPgpAutoEncrypt(false);
KIdentityManagement::Identity &i3 = mKernel->identityManager()->modifyIdentityForName(QStringLiteral("test3"));
i3.setFullName(QStringLiteral("third"));
i3.setPrimaryEmailAddress(QStringLiteral("drei@example.com"));
i3.setPGPSigningKey("0x345678901");
i3.setPGPEncryptionKey("0x345678901");
i3.setPgpAutoSign(true);
i3.setPgpAutoEncrypt(true);
KIdentityManagement::Identity &i4 = mKernel->identityManager()->modifyIdentityForName(QStringLiteral("autocrypt"));
i4.setFullName(QStringLiteral("autocrypt"));
i4.setPrimaryEmailAddress(QStringLiteral("autocrypt@example.com"));
i4.setPGPSigningKey("0x456789012");
i4.setPGPEncryptionKey("0x456789012");
i4.setPgpAutoSign(true);
i4.setPgpAutoEncrypt(true);
i4.setAutocryptEnabled(true);
mKernel->identityManager()->commit();
}
void KMComposerWinTest::testSignature_data()
{
const auto im = mKernel->identityManager();
QTest::addColumn<uint>("uoid");
QTest::addColumn<bool>("sign");
QTest::newRow("default") << im->defaultIdentity().uoid() << true;
QTest::newRow("secondus@example.com") << im->identityForAddress(QStringLiteral("secundus@example.com")).uoid() << false;
QTest::newRow("drei@example.com") << im->identityForAddress(QStringLiteral("drei@example.com")).uoid() << true;
}
void KMComposerWinTest::testSignature()
{
QFETCH(uint, uoid);
QFETCH(bool, sign);
const auto ident = mKernel->identityManager()->identityForUoid(uoid);
const auto msg(createItem(ident));
auto composer = KMail::makeComposer(msg);
composer->show();
QVERIFY(QTest::qWaitForWindowExposed(composer));
QCoreApplication::processEvents(QEventLoop::AllEvents);
auto *signature = composer->findChild<QLabel *>(QStringLiteral("signatureindicator"));
QVERIFY(signature);
QCOMPARE(signature->isVisible(), sign);
composer->close();
}
void KMComposerWinTest::testEncryption_data()
{
const auto im = mKernel->identityManager();
QTest::addColumn<uint>("uoid");
QTest::addColumn<bool>("encrypt");
QTest::newRow("default") << im->defaultIdentity().uoid() << false;
QTest::newRow("secondus@example.com") << im->identityForAddress(QStringLiteral("secundus@example.com")).uoid() << false;
QTest::newRow("drei@example.com") << im->identityForAddress(QStringLiteral("drei@example.com")).uoid() << false;
QTest::newRow("autocrypt@example.com") << im->identityForAddress(QStringLiteral("autocrypt@example.com")).uoid() << true;
}
void KMComposerWinTest::testEncryption()
{
QFETCH(uint, uoid);
QFETCH(bool, encrypt);
QFile file1(QLatin1String(TEST_DATA_DIR) + QStringLiteral("/autocrypt/friends%40kde.org.json"));
QVERIFY(file1.copy(autocryptDir.filePath(QStringLiteral("friends%40kde.org.json"))));
const auto ident = mKernel->identityManager()->identityForUoid(uoid);
const auto msg(createItem(ident));
auto composer = KMail::makeComposer(msg);
composer->show();
QVERIFY(QTest::qWaitForWindowExposed(composer));
QCoreApplication::processEvents(QEventLoop::AllEvents);
auto *encryption = composer->findChild<QLabel *>(QStringLiteral("encryptionindicator"));
QVERIFY(encryption);
QEXPECT_FAIL("autocrypt@example.com", "No autocrypt support for the moment.", Continue);
QCOMPARE(encryption->isVisible(), encrypt);
composer->close();
}
void KMComposerWinTest::testChangeIdentity()
{
QFile file1(QLatin1String(TEST_DATA_DIR) + QStringLiteral("/autocrypt/friends%40kde.org.json"));
QVERIFY(file1.copy(autocryptDir.filePath(QStringLiteral("friends%40kde.org.json"))));
const auto im = mKernel->identityManager();
auto ident = im->defaultIdentity();
const auto msg(createItem(ident));
auto composer = KMail::makeComposer(msg);
composer->show();
QVERIFY(QTest::qWaitForWindowExposed(composer));
QCoreApplication::processEvents(QEventLoop::AllEvents);
auto encryption = composer->findChild<QLabel *>(QStringLiteral("encryptionindicator"));
auto signature = composer->findChild<QLabel *>(QStringLiteral("signatureindicator"));
QVERIFY(encryption);
QVERIFY(signature);
QCOMPARE(encryption->isVisible(), false);
QCOMPARE(signature->isVisible(), true);
{
ident = im->identityForAddress(QStringLiteral("autocrypt@example.com"));
auto identCombo = composer->findChild<KIdentityManagement::IdentityCombo *>(QStringLiteral("identitycombo"));
QVERIFY(identCombo);
identCombo->setCurrentIdentity(ident);
QEventLoop loop;
QTimer::singleShot(50, &loop, SLOT(quit()));
loop.exec();
QCoreApplication::processEvents(QEventLoop::AllEvents);
QEXPECT_FAIL("", "Encryption state is not update correctly.", Continue);
QCOMPARE(encryption->isVisible(), true);
QCOMPARE(signature->isVisible(), true);
}
{
ident = im->identityForAddress(QStringLiteral("secundus@example.com"));
auto identCombo = composer->findChild<KIdentityManagement::IdentityCombo *>(QStringLiteral("identitycombo"));
QVERIFY(identCombo);
identCombo->setCurrentIdentity(ident);
QEventLoop loop;
QTimer::singleShot(50, &loop, SLOT(quit()));
loop.exec();
QCoreApplication::processEvents(QEventLoop::AllEvents);
QCOMPARE(encryption->isVisible(), false);
QCOMPARE(signature->isVisible(), false);
}
}
/*
SPDX-FileCopyrightText: 2021 Sandro Knauß <sknauss@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef KMCOMPOSERWINTEST_H
#define KMCOMPOSERWINTEST_H
#include <QObject>
class KMKernel;
class KMComposerWinTest : public QObject
{
Q_OBJECT
public:
explicit KMComposerWinTest(QObject *parent = nullptr);
~KMComposerWinTest();
private Q_SLOTS:
void init();
void cleanup();
void initTestCase();
void testEncryption_data();
void testEncryption();
void testSignature_data();
void testSignature();
void testChangeIdentity();
private:
void resetIdentities();
KMKernel *mKernel = nullptr;
QDir autocryptDir;
};
#endif // KMCOMPOSERWINTEST_H
......@@ -278,6 +278,7 @@ KMComposerWin::KMComposerWin(const KMime::Message::Ptr &aMsg,
v->addWidget(mHeadersToEditorSplitter);
auto identity = new KIdentityManagement::IdentityCombo(kmkernel->identityManager(), mHeadersArea);
identity->setCurrentIdentity(mId);
identity->setObjectName(QStringLiteral("identitycombo"));
connect(identity, &KIdentityManagement::IdentityCombo::identityDeleted, this, &KMComposerWin::slotIdentityDeleted);
connect(identity, &KIdentityManagement::IdentityCombo::invalidIdentity, this, &KMComposerWin::slotInvalidIdentity);
mComposerBase->setIdentityCombo(identity);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment