File indexing completed on 2024-11-10 04:40:15

0001 /*
0002     SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <QObject>
0008 
0009 #include "attributefactory.h"
0010 #include "control.h"
0011 #include "item.h"
0012 #include "itemcreatejob.h"
0013 #include "itemfetchjob.h"
0014 #include "itemfetchscope.h"
0015 #include "itemmodifyjob.h"
0016 #include "monitor.h"
0017 #include "qtest_akonadi.h"
0018 #include "relationcreatejob.h"
0019 #include "relationdeletejob.h"
0020 #include "relationfetchjob.h"
0021 #include "resourceselectjob_p.h"
0022 #include "tagmodifyjob.h"
0023 
0024 using namespace Akonadi;
0025 
0026 class RelationTest : public QObject
0027 {
0028     Q_OBJECT
0029 
0030 private Q_SLOTS:
0031     void initTestCase();
0032 
0033     void testCreateFetch();
0034     void testMonitor();
0035     void testEqualRelation();
0036 };
0037 
0038 void RelationTest::initTestCase()
0039 {
0040     AkonadiTest::checkTestIsIsolated();
0041     AkonadiTest::setAllResourcesOffline();
0042     qRegisterMetaType<Akonadi::Relation>();
0043     qRegisterMetaType<QSet<Akonadi::Relation>>();
0044     qRegisterMetaType<Akonadi::Item::List>();
0045 }
0046 
0047 void RelationTest::testCreateFetch()
0048 {
0049     const Collection res3{AkonadiTest::collectionIdFromPath(QStringLiteral("res3"))};
0050     Item item1;
0051     {
0052         item1.setMimeType(QStringLiteral("application/octet-stream"));
0053         auto append = new ItemCreateJob(item1, res3, this);
0054         AKVERIFYEXEC(append);
0055         item1 = append->item();
0056     }
0057     Item item2;
0058     {
0059         item2.setMimeType(QStringLiteral("application/octet-stream"));
0060         auto append = new ItemCreateJob(item2, res3, this);
0061         AKVERIFYEXEC(append);
0062         item2 = append->item();
0063     }
0064 
0065     Relation rel(Relation::GENERIC, item1, item2);
0066     auto createjob = new RelationCreateJob(rel, this);
0067     AKVERIFYEXEC(createjob);
0068 
0069     // Test fetch & create
0070     {
0071         auto fetchJob = new RelationFetchJob(QList<QByteArray>(), this);
0072         AKVERIFYEXEC(fetchJob);
0073         QCOMPARE(fetchJob->relations().size(), 1);
0074         QCOMPARE(fetchJob->relations().first().type(), QByteArray(Relation::GENERIC));
0075     }
0076 
0077     // Test item fetch
0078     {
0079         auto fetchJob = new ItemFetchJob(item1);
0080         fetchJob->fetchScope().setFetchRelations(true);
0081         AKVERIFYEXEC(fetchJob);
0082         QCOMPARE(fetchJob->items().first().relations().size(), 1);
0083     }
0084 
0085     {
0086         auto fetchJob = new ItemFetchJob(item2);
0087         fetchJob->fetchScope().setFetchRelations(true);
0088         AKVERIFYEXEC(fetchJob);
0089         QCOMPARE(fetchJob->items().first().relations().size(), 1);
0090     }
0091 
0092     // Test delete
0093     {
0094         auto deleteJob = new RelationDeleteJob(rel, this);
0095         AKVERIFYEXEC(deleteJob);
0096 
0097         auto fetchJob = new RelationFetchJob(QList<QByteArray>(), this);
0098         AKVERIFYEXEC(fetchJob);
0099         QCOMPARE(fetchJob->relations().size(), 0);
0100     }
0101 }
0102 
0103 void RelationTest::testMonitor()
0104 {
0105     Akonadi::Monitor monitor;
0106     monitor.setTypeMonitored(Akonadi::Monitor::Relations);
0107     QVERIFY(AkonadiTest::akWaitForSignal(&monitor, &Monitor::monitorReady));
0108 
0109     const Collection res3{AkonadiTest::collectionIdFromPath(QStringLiteral("res3"))};
0110     Item item1;
0111     {
0112         item1.setMimeType(QStringLiteral("application/octet-stream"));
0113         auto append = new ItemCreateJob(item1, res3, this);
0114         AKVERIFYEXEC(append);
0115         item1 = append->item();
0116     }
0117     Item item2;
0118     {
0119         item2.setMimeType(QStringLiteral("application/octet-stream"));
0120         auto append = new ItemCreateJob(item2, res3, this);
0121         AKVERIFYEXEC(append);
0122         item2 = append->item();
0123     }
0124 
0125     Relation rel(Relation::GENERIC, item1, item2);
0126 
0127     {
0128         QSignalSpy addedSpy(&monitor, &Monitor::relationAdded);
0129 
0130         auto createjob = new RelationCreateJob(rel, this);
0131         AKVERIFYEXEC(createjob);
0132 
0133         // We usually pick up signals from the previous tests as well (due to server-side notification caching)
0134         QTRY_VERIFY(addedSpy.count() >= 1);
0135         QTRY_COMPARE(addedSpy.last().first().value<Akonadi::Relation>(), rel);
0136     }
0137 
0138     {
0139         QSignalSpy removedSpy(&monitor, &Monitor::relationRemoved);
0140         QVERIFY(removedSpy.isValid());
0141         auto deleteJob = new RelationDeleteJob(rel, this);
0142         AKVERIFYEXEC(deleteJob);
0143         QTRY_VERIFY(removedSpy.count() >= 1);
0144         QTRY_COMPARE(removedSpy.last().first().value<Akonadi::Relation>(), rel);
0145     }
0146 }
0147 
0148 void RelationTest::testEqualRelation()
0149 {
0150     Relation r1;
0151     Item it1(45);
0152     Item it2(46);
0153     r1.setLeft(it1);
0154     r1.setRight(it2);
0155     r1.setRemoteId(QByteArrayLiteral("foo"));
0156     r1.setType(QByteArrayLiteral("foo1"));
0157 
0158     Relation r2 = r1;
0159     QCOMPARE(r1, r2);
0160 }
0161 
0162 QTEST_AKONADIMAIN(RelationTest)
0163 
0164 #include "relationtest.moc"