kdeconnect-kde/tests/notificationstest.cpp
Kristen McWilliam caaac80ca5
fix: copy auth code to desktop clipboard
SMS auth codes generate a notification that has a "Copy $code" button
(eg. "Copy 123456"), but the button doesn't actually copy the code as
expected. This patch fixes that.

BUG: 451539

BUG: 435614
2024-10-17 13:51:23 -04:00

93 lines
3 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* SPDX-FileCopyrightText: 2024 Kristen McWilliam <kmcwilliampublic@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "kdeconnectplugin.h"
#include "testdaemon.h"
#include <KSystemClipboard>
#include <QtTest>
/**
* Test the NotificationsPlugin class
*/
class NotificationsPluginTest : public QObject
{
Q_OBJECT
public:
NotificationsPluginTest()
{
QStandardPaths::setTestModeEnabled(true);
m_daemon = new TestDaemon;
}
private:
TestDaemon *m_daemon;
private Q_SLOTS:
/**
* If the user selects the action to copy an auth code, the auth code should
* be copied to the desktop clipboard.
*/
void testAuthCodeIsCopied()
{
Device *device = nullptr;
const QList<Device *> devicesList = m_daemon->devicesList();
for (Device *id : devicesList) {
if (id->isReachable()) {
if (!id->isPaired())
id->requestPairing();
device = id;
}
}
if (device == nullptr) {
QFAIL("Unable to determine device");
}
QCOMPARE(device->isReachable(), true);
QCOMPARE(device->isPaired(), true);
KdeConnectPlugin *plugin = device->plugin(QStringLiteral("kdeconnect_notifications"));
QVERIFY(plugin);
const QString key = QStringLiteral("0|com.google.android.apps.messaging|2|com.google.android.apps.messaging:incoming_message:23|10129");
// Note that the auth code we receive to work with has invisible
// characters in it for some reason. (U+2063 INVISIBLE SEPARATOR between
// each digit)
//
// `action` here is equal to `Copy \"671733\"`, but with the invisible
// characters written out explicitly to make it easier to read, and to
// prevent confusion when reading the test code.
const QString action = QStringLiteral("Copy \"6\u20637\u20631\u20637\u20633\u20633\"");
const QString expectedClipboardContents = QStringLiteral("671733");
// Verify that the clipboard does not contain the auth code already
const QString originalClipboardContents = KSystemClipboard::instance()->text(QClipboard::Clipboard);
QVERIFY(originalClipboardContents != expectedClipboardContents);
// Send the action
plugin->metaObject()->invokeMethod(plugin, "sendAction", Q_ARG(QString, key), Q_ARG(QString, action));
// Verify that the clipboard now contains the auth code
const QString updatedClipboardContents = KSystemClipboard::instance()->text(QClipboard::Clipboard);
QCOMPARE(updatedClipboardContents, expectedClipboardContents);
// Set the clipboard back to its original state
auto mimeData = new QMimeData;
mimeData->setText(originalClipboardContents);
KSystemClipboard::instance()->setMimeData(mimeData, QClipboard::Clipboard);
}
};
QTEST_MAIN(NotificationsPluginTest);
#include "notificationstest.moc"