File indexing completed on 2024-12-22 04:12:55

0001 /*
0002  *  SPDX-FileCopyrightText: 2019 Anna Medonosova <anna.medonosova@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "KisMultiFeedRssModelTest.h"
0008 
0009 #include <simpletest.h>
0010 #include <QSignalSpy>
0011 
0012 #include <MockNetworkAccessManager.h>
0013 #include <KisMultiFeedRSSModel.h>
0014 #include <testutil.h>
0015 
0016 
0017 KisMultiFeedRssModelTest::KisMultiFeedRssModelTest(QObject *parent) : QObject(parent)
0018 {
0019 
0020 }
0021 
0022 void KisMultiFeedRssModelTest::testAddFeed()
0023 {
0024     QFETCH(QList<FakeReplyData>, replyDataList);
0025     QFETCH(int, resultArticleCount);
0026 
0027 
0028     MockNetworkAccessManager* nam(new MockNetworkAccessManager());
0029 
0030     QStringList feedList;
0031     for(FakeReplyData& replyData: replyDataList) {
0032         feedList << replyData.url.toString();
0033         nam->setReplyData(replyData);
0034     }
0035 
0036     MultiFeedRssModel* rssModel(new MultiFeedRssModel(nam));
0037     QSignalSpy spy(rssModel, SIGNAL(feedDataChanged()));
0038     QVERIFY(spy.isValid());
0039 
0040     for(QString& feedUrl: feedList) {
0041         rssModel->addFeed(feedUrl);
0042     }
0043 
0044     // wait for signal
0045     QVERIFY(spy.wait());
0046 
0047     QCOMPARE(rssModel->articleCount(), resultArticleCount);
0048 }
0049 
0050 void KisMultiFeedRssModelTest::testAddFeed_data()
0051 {
0052     QTest::addColumn<QList<FakeReplyData>>("replyDataList");
0053     QTest::addColumn<int>("resultArticleCount");
0054 
0055     // setup feed1
0056     QFile rssFile1(TestUtil::fetchDataFileLazy("rss_feeds/feed.xml"));
0057     bool fileOpened = rssFile1.open(QIODevice::ReadOnly | QIODevice::Text);
0058     QVERIFY(fileOpened);
0059 
0060     QString urlFeed1("https://krita.org/en/feed/");
0061 
0062     // create reply data
0063     FakeReplyData replyFeed1;
0064     replyFeed1.url = QUrl(urlFeed1);
0065     replyFeed1.statusCode = 200;
0066     replyFeed1.requestMethod = QNetworkAccessManager::GetOperation;
0067     replyFeed1.contentType = "application/atom+xml";
0068     replyFeed1.responseBody = rssFile1.readAll();
0069 
0070 
0071     // setup feed2
0072     QFile rssFile2(TestUtil::fetchDataFileLazy("rss_feeds/feed.xml"));
0073     fileOpened = rssFile2.open(QIODevice::ReadOnly | QIODevice::Text);
0074     QVERIFY(fileOpened);
0075 
0076     QString urlFeed2("https://krita.org/en/another_feed/");
0077 
0078     FakeReplyData replyFeed2;
0079     replyFeed2.url = QUrl(urlFeed2);
0080     replyFeed2.statusCode = 200;
0081     replyFeed2.requestMethod = QNetworkAccessManager::GetOperation;
0082     replyFeed2.contentType = "application/atom+xml";
0083     replyFeed2.responseBody = rssFile2.readAll();
0084 
0085 
0086     // test with 1 feed
0087     QList<FakeReplyData> listTest1 = { replyFeed1 };
0088     QTest::addRow("1 feed")
0089             << listTest1
0090             << 10;
0091 
0092     // test with 2 feeds
0093     QList<FakeReplyData> listTest2 = { replyFeed1, replyFeed2 };
0094     QTest::addRow("2 feeds")
0095             << listTest2
0096             << 20;
0097 }
0098 
0099 void KisMultiFeedRssModelTest::testRemoveFeed()
0100 {
0101     QFETCH(QList<FakeReplyData>, replyDataList);
0102     QFETCH(QString, removeFeedUrl);
0103     QFETCH(int, resultArticleCount);
0104 
0105 
0106     MockNetworkAccessManager* nam(new MockNetworkAccessManager());
0107 
0108     QStringList feedList;
0109     for(FakeReplyData& replyData: replyDataList) {
0110         feedList << replyData.url.toString();
0111         nam->setReplyData(replyData);
0112     }
0113 
0114     MultiFeedRssModel* rssModel(new MultiFeedRssModel(nam));
0115     QSignalSpy spyAdd(rssModel, SIGNAL(feedDataChanged()));
0116     QVERIFY(spyAdd.isValid());
0117 
0118     for(QString& feedUrl: feedList) {
0119         rssModel->addFeed(feedUrl);
0120     }
0121 
0122     // wait for signal
0123     QVERIFY(spyAdd.wait());
0124 
0125     // the remove test itself
0126     rssModel->removeFeed(removeFeedUrl);
0127 
0128     QCOMPARE(rssModel->articleCount(), resultArticleCount);
0129 }
0130 
0131 void KisMultiFeedRssModelTest::testRemoveFeed_data()
0132 {
0133     QTest::addColumn<QList<FakeReplyData>>("replyDataList");
0134     QTest::addColumn<QString>("removeFeedUrl");
0135     QTest::addColumn<int>("resultArticleCount");
0136 
0137     // setup feed1
0138     QFile rssFile1(TestUtil::fetchDataFileLazy("rss_feeds/feed.xml"));
0139     bool fileOpened = rssFile1.open(QIODevice::ReadOnly | QIODevice::Text);
0140     QVERIFY(fileOpened);
0141 
0142     QString urlFeed1("https://krita.org/en/feed/");
0143 
0144     // create reply data
0145     FakeReplyData replyFeed1;
0146     replyFeed1.url = QUrl(urlFeed1);
0147     replyFeed1.statusCode = 200;
0148     replyFeed1.requestMethod = QNetworkAccessManager::GetOperation;
0149     replyFeed1.contentType = "application/atom+xml";
0150     replyFeed1.responseBody = rssFile1.readAll();
0151 
0152     // setup feed2
0153     QFile rssFile2(TestUtil::fetchDataFileLazy("rss_feeds/feed.xml"));
0154     fileOpened = rssFile2.open(QIODevice::ReadOnly | QIODevice::Text);
0155     QVERIFY(fileOpened);
0156 
0157     QString urlFeed2("https://krita.org/en/another_feed/");
0158 
0159     FakeReplyData replyFeed2;
0160     replyFeed2.url = QUrl(urlFeed2);
0161     replyFeed2.statusCode = 200;
0162     replyFeed2.requestMethod = QNetworkAccessManager::GetOperation;
0163     replyFeed2.contentType = "application/atom+xml";
0164     replyFeed2.responseBody = rssFile2.readAll();
0165 
0166 
0167     // test with 1 feed
0168     QList<FakeReplyData> listTest1 = { replyFeed1 };
0169     QTest::addRow("1 feed")
0170             << listTest1
0171             << urlFeed1
0172             << 0;
0173 
0174     // test with 2 feeds
0175     QList<FakeReplyData> listTest2 = { replyFeed1, replyFeed2 };
0176     QTest::addRow("2 feeds")
0177             << listTest2
0178             << urlFeed1
0179             << 10;
0180 }
0181 
0182 SIMPLE_TEST_MAIN(KisMultiFeedRssModelTest);
0183