diff options
| author | Linus Jahn <lnj@kaidan.im> | 2019-10-20 16:43:50 +0200 |
|---|---|---|
| committer | LNJ <lnj@kaidan.im> | 2019-10-23 13:19:32 +0200 |
| commit | 979918a40759064c01f31bc099217086b81936d9 (patch) | |
| tree | 29b5803c7d816af2d3cf42a4fd7651a8d9c1cd5c /tests/qxmppdataform/tst_qxmppdataform.cpp | |
| parent | 86ca79a0d5480a307e6117798e3c216ad23fca51 (diff) | |
| download | qxmpp-979918a40759064c01f31bc099217086b81936d9.tar.gz | |
Refactor data form media element
* Add QXmppDataForm::MediaSource instead of using a QPair<QString,
QString> to save the URIs and content types.
* Deprecate QXmppDataForm::Media:
The extra class was useless: Each Field has exactly one media element
and the media element has only two attributes (size and media sources)
* Add mediaSources and mediaSize attributes to the QXmppDataForm::Field
* Deprecate getters/setters for the Media element of QXmppDataForm::Field
(they are still working and tested)
Diffstat (limited to 'tests/qxmppdataform/tst_qxmppdataform.cpp')
| -rw-r--r-- | tests/qxmppdataform/tst_qxmppdataform.cpp | 149 |
1 files changed, 130 insertions, 19 deletions
diff --git a/tests/qxmppdataform/tst_qxmppdataform.cpp b/tests/qxmppdataform/tst_qxmppdataform.cpp index 0774bac1..c794fcc4 100644 --- a/tests/qxmppdataform/tst_qxmppdataform.cpp +++ b/tests/qxmppdataform/tst_qxmppdataform.cpp @@ -4,6 +4,7 @@ * Authors: * Andrey Batyiev * Jeremy Lainé + * Linus Jahn * * Source: * https://github.com/qxmpp-project/qxmpp @@ -22,7 +23,13 @@ * */ +#include <QtGlobal> #include <QObject> + +// deprecated methods are also tested: this is used to avoid unnecessary warnings +#undef QT_DEPRECATED_X +#define QT_DEPRECATED_X(text) + #include "QXmppDataForm.h" #include "util.h" @@ -34,6 +41,7 @@ private slots: void testSimple(); void testSubmit(); void testMedia(); + void testMediaSource(); }; void tst_QXmppDataForm::testSimple() @@ -78,20 +86,24 @@ void tst_QXmppDataForm::testSubmit() void tst_QXmppDataForm::testMedia() { - const QByteArray xml( + const QByteArray xml = QByteArrayLiteral( "<x xmlns=\"jabber:x:data\" type=\"form\">" - "<field type=\"text-single\" label=\"Enter the text you see\" var=\"ocr\">" - "<value/>" - "<media xmlns=\"urn:xmpp:media-element\" height=\"80\" width=\"290\">" - "<uri type=\"image/jpeg\">" - "http://www.victim.com/challenges/ocr.jpeg?F3A6292C" - "</uri>" - "<uri type=\"image/png\">" - "cid:sha1+f24030b8d91d233bac14777be5ab531ca3b9f102@bob.xmpp.org" - "</uri>" - "</media>" - "</field>" - "</x>"); + "<field type=\"text-single\">" + "<media xmlns=\"urn:xmpp:media-element\" width=\"290\" height=\"80\">" + "<uri type=\"image/jpeg\">" + "http://www.victim.com/challenges/ocr.jpeg?F3A6292C" + "</uri>" + "<uri type=\"image/png\">" + "cid:sha1+f24030b8d91d233bac14777be5ab531ca3b9f102@bob.xmpp.org" + "</uri>" + "</media>" + "</field>" + "</x>" + ); + + // + // test parsing + // QXmppDataForm form; parsePacket(form, xml); @@ -100,16 +112,115 @@ void tst_QXmppDataForm::testMedia() QCOMPARE(form.fields().size(), 1); QCOMPARE(form.fields().at(0).type(), QXmppDataForm::Field::TextSingleField); QCOMPARE(form.fields().at(0).isRequired(), false); - QCOMPARE(form.fields().at(0).media().uris().size(), 2); + QCOMPARE(form.fields().at(0).mediaSize(), QSize(290, 80)); + QCOMPARE(form.fields().at(0).mediaSources().size(), 2); + QCOMPARE( + form.fields().at(0).mediaSources().at(0).uri().toString(), + QStringLiteral("http://www.victim.com/challenges/ocr.jpeg?F3A6292C") + ); + QCOMPARE( + form.fields().at(0).mediaSources().at(0).contentType(), + QMimeDatabase().mimeTypeForName(QStringLiteral("image/jpeg")) + ); + QCOMPARE( + form.fields().at(0).mediaSources().at(1).uri().toString(), + QStringLiteral("cid:sha1+f24030b8d91d233bac14777be5ab531ca3b9f102@bob.xmpp.org") + ); + QCOMPARE( + form.fields().at(0).mediaSources().at(1).contentType(), + QMimeDatabase().mimeTypeForName(QStringLiteral("image/png")) + ); + + // deprecated QCOMPARE(form.fields().at(0).media().isNull(), false); - QCOMPARE(form.fields().at(0).media().height(), 80); QCOMPARE(form.fields().at(0).media().width(), 290); - QCOMPARE(form.fields().at(0).media().uris().at(0).first, QString("image/jpeg")); - QCOMPARE(form.fields().at(0).media().uris().at(0).second, QString("http://www.victim.com/challenges/ocr.jpeg?F3A6292C")); - QCOMPARE(form.fields().at(0).media().uris().at(1).first, QString("image/png")); - QCOMPARE(form.fields().at(0).media().uris().at(1).second, QString("cid:sha1+f24030b8d91d233bac14777be5ab531ca3b9f102@bob.xmpp.org")); + QCOMPARE(form.fields().at(0).media().height(), 80); + QCOMPARE(form.fields().at(0).media().uris().size(), 2); + QCOMPARE( + form.fields().at(0).media().uris().at(0).first, + QStringLiteral("image/jpeg") + ); + QCOMPARE( + form.fields().at(0).media().uris().at(0).second, + QStringLiteral("http://www.victim.com/challenges/ocr.jpeg?F3A6292C") + ); + QCOMPARE( + form.fields().at(0).media().uris().at(1).first, + QStringLiteral("image/png") + ); + QCOMPARE( + form.fields().at(0).media().uris().at(1).second, + QStringLiteral("cid:sha1+f24030b8d91d233bac14777be5ab531ca3b9f102@bob.xmpp.org") + ); serializePacket(form, xml); + + // + // test non-const getters + // + + QXmppDataForm::Field mediaField1; + mediaField1.mediaSize().setWidth(290); + mediaField1.mediaSize().setHeight(80); + mediaField1.mediaSources() << QXmppDataForm::MediaSource( + QUrl(QStringLiteral("http://www.victim.com/challenges/ocr.jpeg?F3A6292C")), + QMimeDatabase().mimeTypeForName(QStringLiteral("image/jpeg")) + ); + mediaField1.mediaSources() << QXmppDataForm::MediaSource( + QUrl(QStringLiteral("cid:sha1+f24030b8d91d233bac14777be5ab531ca3b9f102@bob.xmpp.org")), + QMimeDatabase().mimeTypeForName(QStringLiteral("image/png")) + ); + + QXmppDataForm form2; + form2.setType(QXmppDataForm::Form); + form2.setFields(QList<QXmppDataForm::Field>() << mediaField1); + serializePacket(form2, xml); + + // + // test setters + // + + QXmppDataForm::Field mediaField2; + mediaField2.setMediaSize(QSize(290, 80)); + QVector<QXmppDataForm::MediaSource> sources; + sources << QXmppDataForm::MediaSource( + QUrl(QStringLiteral("http://www.victim.com/challenges/ocr.jpeg?F3A6292C")), + QMimeDatabase().mimeTypeForName(QStringLiteral("image/jpeg")) + ); + sources << QXmppDataForm::MediaSource( + QUrl(QStringLiteral("cid:sha1+f24030b8d91d233bac14777be5ab531ca3b9f102@bob.xmpp.org")), + QMimeDatabase().mimeTypeForName(QStringLiteral("image/png")) + ); + mediaField2.setMediaSources(sources); + + QXmppDataForm form3; + form3.setType(QXmppDataForm::Form); + form3.fields().append(mediaField2); + serializePacket(form3, xml); + + // + // test compatibility of deprecated methods + // + + QXmppDataForm::Field mediaFieldBefore = mediaField1; + mediaField1.setMedia(mediaField1.media()); + QCOMPARE(mediaField1, mediaFieldBefore); + + QXmppDataForm::Field mediaField2Before = mediaField2; + mediaField2.setMedia(mediaField2.media()); + QCOMPARE(mediaField2, mediaField2Before); +} + +void tst_QXmppDataForm::testMediaSource() +{ + QXmppDataForm::MediaSource source; + QCOMPARE(source.uri().toString(), QString()); + QCOMPARE(source.contentType(), QMimeType()); + + source.setUri(QUrl("https://xmpp.org/index.html")); + QCOMPARE(source.uri(), QUrl("https://xmpp.org/index.html")); + source.setContentType(QMimeDatabase().mimeTypeForName("application/xml")); + QCOMPARE(source.contentType(), QMimeDatabase().mimeTypeForName("application/xml")); } QTEST_MAIN(tst_QXmppDataForm) |
