From e4b0ad08809647bce682dfad656cc9c47de697be Mon Sep 17 00:00:00 2001 From: Niels Ole Salscheider Date: Wed, 4 Jan 2017 13:09:58 +0100 Subject: Add test for QXmppMamManager --- tests/qxmppmammanager/qxmppmammanager.pro | 3 + tests/qxmppmammanager/tst_qxmppmammanager.cpp | 271 ++++++++++++++++++++++++++ tests/tests.pro | 1 + 3 files changed, 275 insertions(+) create mode 100644 tests/qxmppmammanager/qxmppmammanager.pro create mode 100644 tests/qxmppmammanager/tst_qxmppmammanager.cpp (limited to 'tests') diff --git a/tests/qxmppmammanager/qxmppmammanager.pro b/tests/qxmppmammanager/qxmppmammanager.pro new file mode 100644 index 00000000..685ea4ee --- /dev/null +++ b/tests/qxmppmammanager/qxmppmammanager.pro @@ -0,0 +1,3 @@ +include(../tests.pri) +TARGET = tst_qxmppmammanager +SOURCES += tst_qxmppmammanager.cpp diff --git a/tests/qxmppmammanager/tst_qxmppmammanager.cpp b/tests/qxmppmammanager/tst_qxmppmammanager.cpp new file mode 100644 index 00000000..5fc545f8 --- /dev/null +++ b/tests/qxmppmammanager/tst_qxmppmammanager.cpp @@ -0,0 +1,271 @@ +/* + * Copyright (C) 2017 The QXmpp developers + * + * Authors: + * Niels Ole Salscheider + * + * Source: + * https://github.com/qxmpp-project/qxmpp + * + * This file is a part of QXmpp library. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 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 + * Lesser General Public License for more details. + * + */ + +#include +#include "QXmppMessage.h" +#include "QXmppMamManager.h" +#include "util.h" + +class QXmppMamTestHelper : public QObject +{ + Q_OBJECT + +public slots: + void archivedMessageReceived(const QString &queryId, const QXmppMessage &message); + void resultsRecieved(const QString &queryId, const QXmppResultSetReply &resultSetReply, bool complete); + +public: + QXmppMessage m_expectedMessage; + QXmppResultSetReply m_expectedResultSetReply; + QString m_expectedQueryId; + bool m_expectedComplete; + bool m_signalTriggered; + + void compareMessages(const QXmppMessage &lhs, const QXmppMessage &rhs) const; + void compareResultSetReplys(const QXmppResultSetReply &lhs, const QXmppResultSetReply &rhs) const; +}; + +class tst_QXmppMamManager : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase(); + + void testHandleStanza_data(); + void testHandleStanza(); + + void testHandleResultIq_data(); + void testHandleResultIq(); + +private: + QXmppMamTestHelper m_helper; + QXmppMamManager m_manager; +}; + +void tst_QXmppMamManager::initTestCase() +{ + connect(&m_manager, SIGNAL(archivedMessageReceived(const QString&, const QXmppMessage&)), + &m_helper, SLOT(archivedMessageReceived(const QString&, const QXmppMessage&))); + + connect(&m_manager, SIGNAL(resultsRecieved(const QString&, const QXmppResultSetReply&, bool)), + &m_helper, SLOT(resultsRecieved(const QString&, const QXmppResultSetReply&, bool))); +} + +void tst_QXmppMamManager::testHandleStanza_data() +{ + QTest::addColumn("xml"); + QTest::addColumn("accept"); + QTest::addColumn("expectedMessage"); + QTest::addColumn("expectedQueryId"); + + QTest::newRow("stanza1") + << QByteArray("" + "" + "" + "" + "" + "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo." + "" + "" + "" + "") + << true + << QByteArray("" + "" + "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo." + "") + << QString("f27"); + + QTest::newRow("stanza2") + << QByteArray("" + "" + "" + "" + "" + "What man art thou that thus bescreen'd in night so stumblest on my counsel?" + "" + "" + "" + "") + << false + << QByteArray() + << QString(); + + QTest::newRow("stanza3") + << QByteArray( + "" + "" + "" + "" + "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo." + "" + "" + "") + << false + << QByteArray() + << QString(); +} + +void tst_QXmppMamManager::testHandleStanza() +{ + QFETCH(QByteArray, xml); + QFETCH(bool, accept); + QFETCH(QByteArray, expectedMessage); + QFETCH(QString, expectedQueryId); + + QDomDocument doc; + QCOMPARE(doc.setContent(xml, true), true); + QDomElement element = doc.documentElement(); + + m_helper.m_signalTriggered = false; + m_helper.m_expectedMessage = QXmppMessage(); + if(!expectedMessage.isEmpty()) { + parsePacket(m_helper.m_expectedMessage, expectedMessage); + } + m_helper.m_expectedQueryId = expectedQueryId; + + bool accepted = m_manager.handleStanza(element); + QCOMPARE(accepted, accept); + QCOMPARE(m_helper.m_signalTriggered, accept); +} + +void tst_QXmppMamManager::testHandleResultIq_data() +{ + QTest::addColumn("xml"); + QTest::addColumn("accept"); + QTest::addColumn("expectedResultSetReply"); + QTest::addColumn("expectedComplete"); + + QTest::newRow("stanza1") + << QByteArray("" + "" + "" + "28482-98726-73623" + "09af3-cc343-b409f" + "" + "" + "") + << true + << QByteArray("" + "28482-98726-73623" + "09af3-cc343-b409f" + "") + << false; + + QTest::newRow("stanza2") + << QByteArray("" + "" + "" + "28482-98726-73623" + "09af3-cc343-b409f" + "" + "" + "") + << true + << QByteArray("" + "28482-98726-73623" + "09af3-cc343-b409f" + "") + << true; +} + +void tst_QXmppMamManager::testHandleResultIq() +{ + QFETCH(QByteArray, xml); + QFETCH(bool, accept); + QFETCH(QByteArray, expectedResultSetReply); + QFETCH(bool, expectedComplete); + + QDomDocument doc; + QCOMPARE(doc.setContent(xml, true), true); + QDomElement element = doc.documentElement(); + + m_helper.m_signalTriggered = false; + m_helper.m_expectedResultSetReply = QXmppResultSetReply(); + if(!expectedResultSetReply.isEmpty()) { + parsePacket(m_helper.m_expectedResultSetReply, expectedResultSetReply); + } + m_helper.m_expectedComplete = expectedComplete; + + bool accepted = m_manager.handleStanza(element); + QCOMPARE(accepted, accept); + QCOMPARE(m_helper.m_signalTriggered, accept); +} + +void QXmppMamTestHelper::archivedMessageReceived(const QString &queryId, const QXmppMessage &message) +{ + m_signalTriggered = true; + + compareMessages(message, m_expectedMessage); + QCOMPARE(queryId, m_expectedQueryId); +} + +void QXmppMamTestHelper::resultsRecieved(const QString &queryId, const QXmppResultSetReply &resultSetReply, bool complete) +{ + Q_UNUSED(queryId); + m_signalTriggered = true; + + compareResultSetReplys(resultSetReply, m_expectedResultSetReply); + QCOMPARE(complete, m_expectedComplete); +} + +void QXmppMamTestHelper::compareMessages(const QXmppMessage &lhs, const QXmppMessage &rhs) const +{ + QCOMPARE(lhs.body(), rhs.body()); + QCOMPARE(lhs.from(), rhs.from()); + QCOMPARE(lhs.id(), rhs.id()); + QCOMPARE(lhs.isAttentionRequested(), rhs.isAttentionRequested()); + QCOMPARE(lhs.isMarkable(), rhs.isMarkable()); + QCOMPARE(lhs.isPrivate(), rhs.isPrivate()); + QCOMPARE(lhs.isReceiptRequested(), rhs.isReceiptRequested()); + QCOMPARE(lhs.lang(), rhs.lang()); + QCOMPARE(lhs.to(), rhs.to()); + QCOMPARE(lhs.thread(), rhs.thread()); + QCOMPARE(lhs.stamp(), rhs.stamp()); + QCOMPARE(lhs.type(), rhs.type()); +} + +void QXmppMamTestHelper::compareResultSetReplys(const QXmppResultSetReply &lhs, const QXmppResultSetReply &rhs) const +{ + QCOMPARE(lhs.first(), rhs.first()); + QCOMPARE(lhs.last(), rhs.last()); + QCOMPARE(lhs.count(), rhs.count()); + QCOMPARE(lhs.index(), rhs.index()); + QCOMPARE(lhs.isNull(), rhs.isNull()); +} + +QTEST_MAIN(tst_QXmppMamManager) +#include "tst_qxmppmammanager.moc" diff --git a/tests/tests.pro b/tests/tests.pro index 69418ade..987ac7ee 100644 --- a/tests/tests.pro +++ b/tests/tests.pro @@ -10,6 +10,7 @@ SUBDIRS = \ qxmppiceconnection \ qxmppiq \ qxmppjingleiq \ + qxmppmammanager \ qxmppmessage \ qxmppnonsaslauthiq \ qxmpppresence \ -- cgit v1.2.3