Skip to content

Commit

Permalink
Add test for pubsub item
Browse files Browse the repository at this point in the history
  • Loading branch information
jbruechert committed Oct 2, 2022
1 parent 3e99aba commit 556e8ce
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/base/QXmppMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ class QXMPP_EXPORT QXmppMessage : public QXmppStanza
const QVector<QXmppFileShare> &sharedFiles() const;
void setSharedFiles(const QVector<QXmppFileShare> &sharedFiles);

// XEP-0449: Stickers
const std::optional<QString> &stickerPackId() const;
void setStickerPackId(const std::optional<QString> &stickerPackId);
// XEP-0449: Stickers
const std::optional<QString> &stickerPackId() const;
void setStickerPackId(const std::optional<QString> &stickerPackId);

/// \cond
#ifdef BUILD_OMEMO
Expand Down
36 changes: 31 additions & 5 deletions src/base/QXmppStickerPackItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "QXmppConstants_p.h"
#include "QXmppEncryptedFileSource.h"
#include "QXmppFileMetadata.h"
#include "qdebug.h"

#include <QXmlStreamWriter>

Expand All @@ -16,13 +17,16 @@ class QXmppStickerItemPrivate : public QSharedData
QXmppFileMetadata metadata;
QVector<QXmppHttpFileSource> httpSources;
QVector<QXmppEncryptedFileSource> encryptedSources;
std::optional<QString> suggest;
};

QXmppStickerItem::QXmppStickerItem()
: d(new QXmppStickerItemPrivate())
{
}

QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppStickerItem)

///
/// \class QXmppStickerItem
///
Expand Down Expand Up @@ -77,20 +81,34 @@ void QXmppStickerItem::setEncryptedSources(const QVector<QXmppEncryptedFileSourc
d->encryptedSources = encryptedSources;
}

QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppStickerItem)
const std::optional<QString> &QXmppStickerItem::suggest() const
{
return d->suggest;
}

void QXmppStickerItem::setSuggest(const std::optional<QString> &suggest)
{
d->suggest = suggest;
}

/// \cond
void QXmppStickerItem::toXml(QXmlStreamWriter *writer) const
{
writer->writeStartElement("item");
d->metadata.toXml(writer);
writer->writeStartElement("sources");
writer->writeDefaultNamespace(ns_sfs);
for (const auto &httpSource : d->httpSources) {
httpSource.toXml(writer);
}
for (const auto &encryptedSource : d->encryptedSources) {
encryptedSource.toXml(writer);
}
writer->writeEndElement();
if (d->suggest) {
writer->writeTextElement("suggest", *d->suggest);
}
writer->writeEndElement();
}

bool QXmppStickerItem::parse(const QDomElement &element)
Expand All @@ -115,6 +133,10 @@ bool QXmppStickerItem::parse(const QDomElement &element)
}
}

if (auto el = element.firstChildElement("suggest"); !el.isNull()) {
d->suggest = el.text();
}

return true;
}
/// \endcond
Expand All @@ -125,13 +147,16 @@ class QXmppStickerPackItemPrivate : public QSharedData
QString name;
QString summary;
QVector<QXmppStickerItem> items;
QXmppHash hash;
};

QXmppStickerPackItem::QXmppStickerPackItem()
: d(new QXmppStickerPackItemPrivate())
{
}

QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppStickerPackItem)

///
/// \class QXmppStickerPackitem
///
Expand Down Expand Up @@ -186,21 +211,21 @@ void QXmppStickerPackItem::setItems(const QVector<QXmppStickerItem> &items)
d->items = items;
}

QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppStickerPackItem)

void QXmppStickerPackItem::parsePayload(const QDomElement &payloadElement)
{
d->name = payloadElement.firstChildElement("name").text();
d->summary = payloadElement.firstChildElement("summary").text();

for (auto firstChild = payloadElement.firstChildElement("item");
!firstChild.isNull();
firstChild.nextSibling()) {
firstChild = firstChild.nextSiblingElement("item")) {
QXmppStickerItem stickerItem;
stickerItem.parse(payloadElement);
stickerItem.parse(firstChild);

d->items.push_back(std::move(stickerItem));
}

d->hash.parse(payloadElement.firstChildElement("hash"));
}

void QXmppStickerPackItem::serializePayload(QXmlStreamWriter *writer) const
Expand All @@ -215,5 +240,6 @@ void QXmppStickerPackItem::serializePayload(QXmlStreamWriter *writer) const
item.toXml(writer);
}

d->hash.toXml(writer);
writer->writeEndElement();
}
5 changes: 5 additions & 0 deletions src/base/QXmppStickerPackItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <QVector>

#include <optional>

class QXmppFileMetadata;
class QXmppHttpFileSource;
class QXmppEncryptedFileSource;
Expand All @@ -31,6 +33,9 @@ class QXMPP_EXPORT QXmppStickerItem
const QVector<QXmppEncryptedFileSource> &encryptedSources() const;
void setEncryptedSources(const QVector<QXmppEncryptedFileSource> &encryptedSources);

const std::optional<QString> &suggest() const;
void setSuggest(const std::optional<QString> &suggest);

/// \cond
bool parse(const QDomElement &element);
void toXml(QXmlStreamWriter *writer) const;
Expand Down
1 change: 1 addition & 0 deletions tests/qxmppmessage/tst_qxmppmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,7 @@ void tst_QXmppMessage::testStickers()
parsePacket(message1, xml);
QVERIFY(!message1.sharedFiles().empty());
QVERIFY(message1.stickerPackId().has_value());
Q_ASSERT(message1.stickerPackId().value() == QStringLiteral("EpRv28DHHzFrE4zd"));
serializePacket(message1, xml);
}

Expand Down
46 changes: 46 additions & 0 deletions tests/qxmpppubsub/tst_qxmpppubsub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "QXmppPubSubAffiliation.h"
#include "QXmppPubSubSubscription.h"
#include "QXmppStickerPackItem.h"

#include "pubsubutil.h"
#include "util.h"
Expand Down Expand Up @@ -55,6 +56,7 @@ class tst_QXmppPubSub : public QObject
Q_SLOT void testIsItem_data();
Q_SLOT void testIsItem();
Q_SLOT void testTestItem();
Q_SLOT void testStickerPackItem();
};

void tst_QXmppPubSub::testAffiliation_data()
Expand Down Expand Up @@ -280,5 +282,49 @@ void tst_QXmppPubSub::testTestItem()
QVERIFY(!TestItem::isItem(xmlToDom(invalidXml)));
}

void tst_QXmppPubSub::testStickerPackItem()
{
QByteArray xml(
"<item id='EpRv28DHHzFrE4zd+xaNpVb4'>"
"<pack xmlns='urn:xmpp:stickers:0'>"
"<name>Marsey the Cat</name>"
"<summary>Be cute or be cynical, this little kitten works both ways.</summary>"
"<item>"
"<file xmlns='urn:xmpp:file:metadata:0'>"
"<desc>👍</desc>"
"<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>0AdP8lJOWJrugSKOIAqfEKqFatIpG5JBCjjxY253ojQ=</hash>"
"<height>512</height>"
"<media-type>image/png</media-type>"
"<size>71045</size>"
"<width>512</width>"
"</file>"
"<sources xmlns='urn:xmpp:sfs:0'>"
"<url-data xmlns='http://jabber.org/protocol/url-data' target='https://download.montague.lit/51078299-d071-46e1-b6d3-3de4a8ab67d6/sticker_marsey_thumbs_up.png'/>"
"</sources>"
"<suggest>+1</suggest>"
"</item>"
"<item>"
"<file xmlns='urn:xmpp:file:metadata:0'>"
"<desc>😘</desc>"
"<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>gw+6xdCgOcvCYSKuQNrXH33lV9NMzuDf/s0huByCDsY=</hash>"
"<height>512</height>"
"<media-type>image/png</media-type>"
"<size>67016</size>"
"<width>512</width>"
"</file>"
"<sources xmlns='urn:xmpp:sfs:0'>"
"<url-data xmlns='http://jabber.org/protocol/url-data' target='https://download.montague.lit/51078299-d071-46e1-b6d3-3de4a8ab67d6/sticker_marsey_kiss.png'/>"
"</sources>"
"</item>"
"<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>EpRv28DHHzFrE4zd+xaNpVb4jbu4s74XtioExNjQzZ0=</hash>"
"</pack>"
"</item>");

QXmppStickerPackItem item;
parsePacket(item, xml);
QCOMPARE(item.items().size(), 2);
serializePacket(item, xml);
}

QTEST_MAIN(tst_QXmppPubSub)
#include "tst_qxmpppubsub.moc"

0 comments on commit 556e8ce

Please sign in to comment.