aboutsummaryrefslogtreecommitdiff
path: root/source/tests.cpp
diff options
context:
space:
mode:
authorJeremy Lainé <jeremy.laine@m4x.org>2010-06-29 10:17:45 +0000
committerJeremy Lainé <jeremy.laine@m4x.org>2010-06-29 10:17:45 +0000
commit7bc846598f17eea663b265e682a016acdac6beca (patch)
tree44132884c5365553e8cbc265cd5fb0858052485c /source/tests.cpp
parent91ab7f10a2b8139926c09c25921511f93e2af51d (diff)
downloadqxmpp-7bc846598f17eea663b265e682a016acdac6beca.tar.gz
add unit tests for QXmpp parsing (not compiled for now)
Diffstat (limited to 'source/tests.cpp')
-rw-r--r--source/tests.cpp163
1 files changed, 163 insertions, 0 deletions
diff --git a/source/tests.cpp b/source/tests.cpp
new file mode 100644
index 00000000..4a54a691
--- /dev/null
+++ b/source/tests.cpp
@@ -0,0 +1,163 @@
+/*
+ * Copyright (C) 2010 Bolloré telecom
+ *
+ * Author:
+ * Jeremy Lainé
+ *
+ * Source:
+ * http://code.google.com/p/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 <cstdlib>
+
+#include <QCoreApplication>
+#include <QDomDocument>
+#include <QVariant>
+#include <QtTest/QtTest>
+
+#include "QXmppMessage.h"
+#include "QXmppPresence.h"
+#include "tests.h"
+
+static void parsePacket(QXmppPacket &packet, const QByteArray &xml)
+{
+ //qDebug() << "parsing" << xml;
+ QDomDocument doc;
+ QCOMPARE(doc.setContent(xml, true), true);
+ QDomElement element = doc.documentElement();
+ packet.parse(element);
+}
+
+static void serializePacket(QXmppPacket &packet, const QByteArray &xml)
+{
+ QBuffer buffer;
+ buffer.open(QIODevice::ReadWrite);
+ QXmlStreamWriter writer(&buffer);
+ packet.toXml(&writer);
+ //qDebug() << "writing" << buffer.data();
+ QCOMPARE(buffer.data(), xml);
+}
+
+void TestPackets::testMessage()
+{
+ const QByteArray xml(
+ "<message to=\"foo@example.com/QXmpp\" from=\"bar@example.com/QXmpp\" type=\"normal\"/>");
+
+ QXmppMessage message;
+ parsePacket(message, xml);
+ QCOMPARE(message.to(), QString("foo@example.com/QXmpp"));
+ QCOMPARE(message.from(), QString("bar@example.com/QXmpp"));
+ serializePacket(message, xml);
+}
+
+void TestPackets::testMessageFull()
+{
+ const QByteArray xml(
+ "<message to=\"foo@example.com/QXmpp\" from=\"bar@example.com/QXmpp\" type=\"normal\">"
+ "<subject>test subject</subject>"
+ "<body>test body</body>"
+ "<thread>test thread</thread>"
+ "<composing xmlns=\"http://jabber.org/protocol/chatstates\"/>"
+ "</message>");
+
+ QXmppMessage message;
+ parsePacket(message, xml);
+ QCOMPARE(message.to(), QString("foo@example.com/QXmpp"));
+ QCOMPARE(message.from(), QString("bar@example.com/QXmpp"));
+ QCOMPARE(message.type(), QXmppMessage::Normal);
+ QCOMPARE(message.body(), QString("test body"));
+ QCOMPARE(message.subject(), QString("test subject"));
+ QCOMPARE(message.thread(), QString("test thread"));
+ QCOMPARE(message.state(), QXmppMessage::Composing);
+ serializePacket(message, xml);
+}
+
+void TestPackets::testMessageDelay()
+{
+ const QByteArray xml(
+ "<message to=\"foo@example.com/QXmpp\" from=\"bar@example.com/QXmpp\" type=\"normal\">"
+ "<delay xmlns=\"urn:xmpp:delay\" stamp=\"2010-06-29T08:23:06Z\"/>"
+ "</message>");
+
+ QXmppMessage message;
+ parsePacket(message, xml);
+ QCOMPARE(message.stamp(), QDateTime(QDate(2010, 06, 29), QTime(8, 23, 6), Qt::UTC));
+ serializePacket(message, xml);
+}
+
+void TestPackets::testMessageLegacyDelay()
+{
+ const QByteArray xml(
+ "<message to=\"foo@example.com/QXmpp\" from=\"bar@example.com/QXmpp\" type=\"normal\">"
+ "<x xmlns=\"jabber:x:delay\" stamp=\"20100629T08:23:06\"/>"
+ "</message>");
+
+ QXmppMessage message;
+ parsePacket(message, xml);
+ QCOMPARE(message.stamp(), QDateTime(QDate(2010, 06, 29), QTime(8, 23, 6), Qt::UTC));
+ serializePacket(message, xml);
+}
+
+void TestPackets::testPresence()
+{
+ const QByteArray xml(
+ "<presence to=\"foo@example.com/QXmpp\" from=\"bar@example.com/QXmpp\"/>");
+
+ QXmppPresence presence;
+ parsePacket(presence, xml);
+ QCOMPARE(presence.to(), QString("foo@example.com/QXmpp"));
+ QCOMPARE(presence.from(), QString("bar@example.com/QXmpp"));
+ serializePacket(presence, xml);
+}
+
+void TestPackets::testPresenceFull()
+{
+ const QByteArray xml(
+ "<presence to=\"foo@example.com/QXmpp\" from=\"bar@example.com/QXmpp\">"
+ "<show>away</show>"
+ "<status>In a meeting</status>"
+ "<priority>5</priority>"
+ "</presence>"
+ );
+
+ QXmppPresence presence;
+ parsePacket(presence, xml);
+ QCOMPARE(presence.to(), QString("foo@example.com/QXmpp"));
+ QCOMPARE(presence.from(), QString("bar@example.com/QXmpp"));
+ QCOMPARE(presence.status().type(), QXmppPresence::Status::Away);
+ QCOMPARE(presence.status().statusText(), QString("In a meeting"));
+ QCOMPARE(presence.status().priority(), 5);
+ serializePacket(presence, xml);
+}
+
+int main(int argc, char *argv[])
+{
+ QCoreApplication app(argc, argv);
+
+ // run tests
+ int errors = 0;
+
+ TestPackets testPackets;
+ errors += QTest::qExec(&testPackets);
+
+ if (errors)
+ {
+ qWarning() << "Total failed tests:" << errors;
+ return EXIT_FAILURE;
+ }
+ return EXIT_SUCCESS;
+};
+