File indexing completed on 2024-10-06 12:56:09

0001 // SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: GPL-3.0-or-later
0003 
0004 #include <QtTest/QtTest>
0005 
0006 #include "mockaccount.h"
0007 #include "timeline/poll.h"
0008 #include "timeline/post.h"
0009 #include <QJsonObject>
0010 
0011 class PostTest : public QObject
0012 {
0013     Q_OBJECT
0014 
0015 private Q_SLOTS:
0016     void initTestCase()
0017     {
0018     }
0019 
0020     void testFromJson()
0021     {
0022         MockAccount account;
0023 
0024         QFile statusExampleApi;
0025         statusExampleApi.setFileName(QLatin1String(DATA_DIR) + QLatin1Char('/') + "status.json");
0026         statusExampleApi.open(QIODevice::ReadOnly);
0027 
0028         const auto doc = QJsonDocument::fromJson(statusExampleApi.readAll());
0029         Post post(&account, doc.object());
0030 
0031         QCOMPARE(post.spoilerText(), "SPOILER");
0032         QCOMPARE(post.content(), "<p>LOREM</p>");
0033         QVERIFY(post.card());
0034         QCOMPARE(post.contentType(), QString());
0035         QCOMPARE(post.sensitive(), false);
0036         QCOMPARE(post.visibility(), Post::Visibility::Public);
0037 
0038         QCOMPARE(post.authorIdentity()->displayName(), "Eugen :kde:");
0039         QCOMPARE(post.authorIdentity()->displayNameHtml(), "Eugen <img height=\"16\" align=\"middle\" width=\"16\" src=\"https://kde.org\">");
0040     }
0041 
0042     void testFromJsonWithPoll()
0043     {
0044         MockAccount account;
0045 
0046         QFile statusExampleApi;
0047         statusExampleApi.setFileName(QLatin1String(DATA_DIR) + QLatin1Char('/') + "status-poll.json");
0048         statusExampleApi.open(QIODevice::ReadOnly);
0049 
0050         const auto doc = QJsonDocument::fromJson(statusExampleApi.readAll());
0051         Post post(&account, doc.object());
0052 
0053         QVERIFY(post.poll());
0054         const auto poll = post.poll();
0055         QCOMPARE(poll->id(), "34830");
0056         QCOMPARE(poll->expiresAt().date().year(), 2019);
0057         QCOMPARE(poll->expired(), true);
0058         QCOMPARE(poll->multiple(), false);
0059         QCOMPARE(poll->votesCount(), 10);
0060         QCOMPARE(poll->votersCount(), -1);
0061         QCOMPARE(poll->voted(), true);
0062         const auto ownVotes = poll->ownVotes();
0063         QCOMPARE(ownVotes.count(), 1);
0064         QCOMPARE(ownVotes[0], 1);
0065         const auto options = poll->options();
0066         QCOMPARE(options.count(), 2);
0067         QCOMPARE(options[0]["title"], QStringLiteral("accept"));
0068         QCOMPARE(options[0]["votesCount"], 6);
0069         QCOMPARE(options[1]["title"], QStringLiteral("deny <img height=\"16\" align=\"middle\" width=\"16\" src=\"https://kde.org\">"));
0070         QCOMPARE(options[1]["votesCount"], 4);
0071     }
0072 };
0073 
0074 QTEST_MAIN(PostTest)
0075 #include "posttest.moc"