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

0001 /*
0002     SPDX-FileCopyrightText: 2010 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <QObject>
0008 
0009 #include "monitor_p.h"
0010 #include "qtest_akonadi.h"
0011 #include <QTest>
0012 
0013 using namespace Akonadi;
0014 
0015 Q_DECLARE_METATYPE(Akonadi::Protocol::ChangeNotification::Operation)
0016 Q_DECLARE_METATYPE(QSet<QByteArray>)
0017 
0018 class MonitorFilterTest : public QObject
0019 {
0020     Q_OBJECT
0021 private Q_SLOTS:
0022     void initTestCase()
0023     {
0024         AkonadiTest::checkTestIsIsolated();
0025         qRegisterMetaType<Akonadi::Item>();
0026         qRegisterMetaType<Akonadi::Collection>();
0027         qRegisterMetaType<QSet<QByteArray>>();
0028         qRegisterMetaType<Akonadi::Item::List>();
0029     }
0030 
0031     void filterConnected_data()
0032     {
0033         QTest::addColumn<Akonadi::Protocol::ChangeNotification::Operation>("op");
0034         QTest::addColumn<QByteArray>("signalName");
0035 
0036         QTest::newRow("itemAdded") << Protocol::ChangeNotification::Add << QByteArray(SIGNAL(itemAdded(Akonadi::Item, Akonadi::Collection)));
0037         QTest::newRow("itemChanged") << Protocol::ChangeNotification::Modify << QByteArray(SIGNAL(itemChanged(Akonadi::Item, QSet<QByteArray>)));
0038         QTest::newRow("itemsFlagsChanged") << Protocol::ChangeNotification::ModifyFlags
0039                                            << QByteArray(SIGNAL(itemsFlagsChanged(Akonadi::Item::List, QSet<QByteArray>, QSet<QByteArray>)));
0040         QTest::newRow("itemRemoved") << Protocol::ChangeNotification::Remove << QByteArray(SIGNAL(itemRemoved(Akonadi::Item)));
0041         QTest::newRow("itemMoved") << Protocol::ChangeNotification::Move
0042                                    << QByteArray(SIGNAL(itemMoved(Akonadi::Item, Akonadi::Collection, Akonadi::Collection)));
0043         QTest::newRow("itemLinked") << Protocol::ChangeNotification::Link << QByteArray(SIGNAL(itemLinked(Akonadi::Item, Akonadi::Collection)));
0044         QTest::newRow("itemUnlinked") << Protocol::ChangeNotification::Unlink << QByteArray(SIGNAL(itemUnlinked(Akonadi::Item, Akonadi::Collection)));
0045     }
0046 
0047     void filterConnected()
0048     {
0049         QFETCH(Protocol::ChangeNotification::Operation, op);
0050         QFETCH(QByteArray, signalName);
0051 
0052         Monitor dummyMonitor;
0053         MonitorPrivate m(0, &dummyMonitor);
0054 
0055         Protocol::ChangeNotification msg;
0056         msg.addEntity(1);
0057         msg.setOperation(op);
0058         msg.setType(Akonadi::Protocol::ChangeNotification::Items);
0059 
0060         QVERIFY(!m.acceptNotification(msg));
0061         m.monitorAll = true;
0062         QVERIFY(m.acceptNotification(msg));
0063         QSignalSpy spy(&dummyMonitor, signalName.constData());
0064         QVERIFY(spy.isValid());
0065         QVERIFY(m.acceptNotification(msg));
0066         m.monitorAll = false;
0067         QVERIFY(!m.acceptNotification(msg));
0068     }
0069 
0070     void filterSession()
0071     {
0072         Monitor dummyMonitor;
0073         MonitorPrivate m(0, &dummyMonitor);
0074         m.monitorAll = true;
0075         QSignalSpy spy(&dummyMonitor, &Monitor::itemAdded);
0076 
0077         Protocol::ChangeNotification msg;
0078         msg.addEntity(1);
0079         msg.setOperation(Protocol::ChangeNotification::Add);
0080         msg.setType(Akonadi::Protocol::ChangeNotification::Items);
0081         msg.setSessionId("foo");
0082 
0083         QVERIFY(m.acceptNotification(msg));
0084         m.sessions.append("bar");
0085         QVERIFY(m.acceptNotification(msg));
0086         m.sessions.append("foo");
0087         QVERIFY(!m.acceptNotification(msg));
0088     }
0089 
0090     void filterResource_data()
0091     {
0092         QTest::addColumn<Akonadi::Protocol::ChangeNotification::Operation>("op");
0093         QTest::addColumn<Akonadi::Protocol::ChangeNotification::Type>("type");
0094         QTest::addColumn<QByteArray>("signalName");
0095 
0096         QTest::newRow("itemAdded") << Protocol::ChangeNotification::Add << Protocol::ChangeNotification::Items
0097                                    << QByteArray(SIGNAL(itemAdded(Akonadi::Item, Akonadi::Collection)));
0098         QTest::newRow("itemChanged") << Protocol::ChangeNotification::Modify << Protocol::ChangeNotification::Items
0099                                      << QByteArray(SIGNAL(itemChanged(Akonadi::Item, QSet<QByteArray>)));
0100         QTest::newRow("itemsFlagsChanged") << Protocol::ChangeNotification::ModifyFlags << Protocol::ChangeNotification::Items
0101                                            << QByteArray(SIGNAL(itemsFlagsChanged(Akonadi::Item::List, QSet<QByteArray>, QSet<QByteArray>)));
0102         QTest::newRow("itemRemoved") << Protocol::ChangeNotification::Remove << Protocol::ChangeNotification::Items
0103                                      << QByteArray(SIGNAL(itemRemoved(Akonadi::Item)));
0104         QTest::newRow("itemMoved") << Protocol::ChangeNotification::Move << Protocol::ChangeNotification::Items
0105                                    << QByteArray(SIGNAL(itemMoved(Akonadi::Item, Akonadi::Collection, Akonadi::Collection)));
0106         QTest::newRow("itemLinked") << Protocol::ChangeNotification::Link << Protocol::ChangeNotification::Items
0107                                     << QByteArray(SIGNAL(itemLinked(Akonadi::Item, Akonadi::Collection)));
0108         QTest::newRow("itemUnlinked") << Protocol::ChangeNotification::Unlink << Protocol::ChangeNotification::Items
0109                                       << QByteArray(SIGNAL(itemUnlinked(Akonadi::Item, Akonadi::Collection)));
0110 
0111         QTest::newRow("colAdded") << Protocol::ChangeNotification::Add << Protocol::ChangeNotification::Collections
0112                                   << QByteArray(SIGNAL(collectionAdded(Akonadi::Collection, Akonadi::Collection)));
0113         QTest::newRow("colChanged") << Protocol::ChangeNotification::Modify << Protocol::ChangeNotification::Collections
0114                                     << QByteArray(SIGNAL(collectionChanged(Akonadi::Collection, QSet<QByteArray>)));
0115         QTest::newRow("colRemoved") << Protocol::ChangeNotification::Remove << Protocol::ChangeNotification::Collections
0116                                     << QByteArray(SIGNAL(collectionRemoved(Akonadi::Collection)));
0117         QTest::newRow("colMoved") << Protocol::ChangeNotification::Move << Protocol::ChangeNotification::Collections
0118                                   << QByteArray(SIGNAL(collectionMoved(Akonadi::Collection, Akonadi::Collection, Akonadi::Collection)));
0119         QTest::newRow("colSubscribed") << Protocol::ChangeNotification::Subscribe << Protocol::ChangeNotification::Collections
0120                                        << QByteArray(SIGNAL(collectionSubscribed(Akonadi::Collection, Akonadi::Collection)));
0121         QTest::newRow("colSubscribed") << Protocol::ChangeNotification::Unsubscribe << Protocol::ChangeNotification::Collections
0122                                        << QByteArray(SIGNAL(collectionUnsubscribed(Akonadi::Collection)));
0123     }
0124 
0125     void filterResource()
0126     {
0127         QFETCH(Protocol::ChangeNotification::Operation, op);
0128         QFETCH(Protocol::ChangeNotification::Type, type);
0129         QFETCH(QByteArray, signalName);
0130 
0131         Monitor dummyMonitor;
0132         MonitorPrivate m(0, &dummyMonitor);
0133         QSignalSpy spy(&dummyMonitor, signalName.constData());
0134         QVERIFY(spy.isValid());
0135 
0136         Protocol::ChangeNotification msg;
0137         msg.addEntity(1);
0138         msg.setOperation(op);
0139         msg.setParentCollection(2);
0140         msg.setType(type);
0141         msg.setResource("foo");
0142         msg.setSessionId("mysession");
0143 
0144         // using the right resource makes it pass
0145         QVERIFY(!m.acceptNotification(msg));
0146         m.resources.insert("bar");
0147         QVERIFY(!m.acceptNotification(msg));
0148         m.resources.insert("foo");
0149         QVERIFY(m.acceptNotification(msg));
0150 
0151         // filtering out the session overwrites the resource
0152         m.sessions.append("mysession");
0153         QVERIFY(!m.acceptNotification(msg));
0154     }
0155 
0156     void filterDestinationResource_data()
0157     {
0158         QTest::addColumn<Akonadi::Protocol::ChangeNotification::Operation>("op");
0159         QTest::addColumn<Akonadi::Protocol::ChangeNotification::Type>("type");
0160         QTest::addColumn<QByteArray>("signalName");
0161 
0162         QTest::newRow("itemMoved") << Protocol::ChangeNotification::Move << Protocol::ChangeNotification::Items
0163                                    << QByteArray(SIGNAL(itemMoved(Akonadi::Item, Akonadi::Collection, Akonadi::Collection)));
0164         QTest::newRow("colMoved") << Protocol::ChangeNotification::Move << Protocol::ChangeNotification::Collections
0165                                   << QByteArray(SIGNAL(collectionMoved(Akonadi::Collection, Akonadi::Collection, Akonadi::Collection)));
0166     }
0167 
0168     void filterDestinationResource()
0169     {
0170         QFETCH(Protocol::ChangeNotification::Operation, op);
0171         QFETCH(Protocol::ChangeNotification::Type, type);
0172         QFETCH(QByteArray, signalName);
0173 
0174         Monitor dummyMonitor;
0175         MonitorPrivate m(0, &dummyMonitor);
0176         QSignalSpy spy(&dummyMonitor, signalName.constData());
0177         QVERIFY(spy.isValid());
0178 
0179         Protocol::ChangeNotification msg;
0180         msg.setOperation(op);
0181         msg.setType(type);
0182         msg.setResource("foo");
0183         msg.setDestinationResource("bar");
0184         msg.setSessionId("mysession");
0185         msg.addEntity(1);
0186 
0187         // using the right resource makes it pass
0188         QVERIFY(!m.acceptNotification(msg));
0189         m.resources.insert("bla");
0190         QVERIFY(!m.acceptNotification(msg));
0191         m.resources.insert("bar");
0192         QVERIFY(m.acceptNotification(msg));
0193 
0194         // filtering out the mimetype does not overwrite resources
0195         msg.addEntity(1, QString(), QString(), QStringLiteral("my/type"));
0196         QVERIFY(m.acceptNotification(msg));
0197 
0198         // filtering out the session overwrites the resource
0199         m.sessions.append("mysession");
0200         QVERIFY(!m.acceptNotification(msg));
0201     }
0202 
0203     void filterMimeType_data()
0204     {
0205         QTest::addColumn<Akonadi::Protocol::ChangeNotification::Operation>("op");
0206         QTest::addColumn<Akonadi::Protocol::ChangeNotification::Type>("type");
0207         QTest::addColumn<QByteArray>("signalName");
0208 
0209         QTest::newRow("itemAdded") << Protocol::ChangeNotification::Add << Protocol::ChangeNotification::Items
0210                                    << QByteArray(SIGNAL(itemAdded(Akonadi::Item, Akonadi::Collection)));
0211         QTest::newRow("itemChanged") << Protocol::ChangeNotification::Modify << Protocol::ChangeNotification::Items
0212                                      << QByteArray(SIGNAL(itemChanged(Akonadi::Item, QSet<QByteArray>)));
0213         QTest::newRow("itemsFlagsChanged") << Protocol::ChangeNotification::ModifyFlags << Protocol::ChangeNotification::Items
0214                                            << QByteArray(SIGNAL(itemsFlagsChanged(Akonadi::Item::List, QSet<QByteArray>, QSet<QByteArray>)));
0215         QTest::newRow("itemRemoved") << Protocol::ChangeNotification::Remove << Protocol::ChangeNotification::Items
0216                                      << QByteArray(SIGNAL(itemRemoved(Akonadi::Item)));
0217         QTest::newRow("itemMoved") << Protocol::ChangeNotification::Move << Protocol::ChangeNotification::Items
0218                                    << QByteArray(SIGNAL(itemMoved(Akonadi::Item, Akonadi::Collection, Akonadi::Collection)));
0219         QTest::newRow("itemLinked") << Protocol::ChangeNotification::Link << Protocol::ChangeNotification::Items
0220                                     << QByteArray(SIGNAL(itemLinked(Akonadi::Item, Akonadi::Collection)));
0221         QTest::newRow("itemUnlinked") << Protocol::ChangeNotification::Unlink << Protocol::ChangeNotification::Items
0222                                       << QByteArray(SIGNAL(itemUnlinked(Akonadi::Item, Akonadi::Collection)));
0223 
0224         QTest::newRow("colAdded") << Protocol::ChangeNotification::Add << Protocol::ChangeNotification::Collections
0225                                   << QByteArray(SIGNAL(collectionAdded(Akonadi::Collection, Akonadi::Collection)));
0226         QTest::newRow("colChanged") << Protocol::ChangeNotification::Modify << Protocol::ChangeNotification::Collections
0227                                     << QByteArray(SIGNAL(collectionChanged(Akonadi::Collection, QSet<QByteArray>)));
0228         QTest::newRow("colRemoved") << Protocol::ChangeNotification::Remove << Protocol::ChangeNotification::Collections
0229                                     << QByteArray(SIGNAL(collectionRemoved(Akonadi::Collection)));
0230         QTest::newRow("colMoved") << Protocol::ChangeNotification::Move << Protocol::ChangeNotification::Collections
0231                                   << QByteArray(SIGNAL(collectionMoved(Akonadi::Collection, Akonadi::Collection, Akonadi::Collection)));
0232         QTest::newRow("colSubscribed") << Protocol::ChangeNotification::Subscribe << Protocol::ChangeNotification::Collections
0233                                        << QByteArray(SIGNAL(collectionSubscribed(Akonadi::Collection, Akonadi::Collection)));
0234         QTest::newRow("colSubscribed") << Protocol::ChangeNotification::Unsubscribe << Protocol::ChangeNotification::Collections
0235                                        << QByteArray(SIGNAL(collectionUnsubscribed(Akonadi::Collection)));
0236     }
0237 
0238     void filterMimeType()
0239     {
0240         QFETCH(Protocol::ChangeNotification::Operation, op);
0241         QFETCH(Protocol::ChangeNotification::Type, type);
0242         QFETCH(QByteArray, signalName);
0243 
0244         Monitor dummyMonitor;
0245         MonitorPrivate m(0, &dummyMonitor);
0246         QSignalSpy spy(&dummyMonitor, signalName.constData());
0247         QVERIFY(spy.isValid());
0248 
0249         Protocol::ChangeNotification msg;
0250         msg.addEntity(1, QString(), QString(), QStringLiteral("my/type"));
0251         msg.setOperation(op);
0252         msg.setParentCollection(2);
0253         msg.setType(type);
0254         msg.setResource("foo");
0255         msg.setSessionId("mysession");
0256 
0257         // using the right resource makes it pass
0258         QVERIFY(!m.acceptNotification(msg));
0259         m.mimetypes.insert(QStringLiteral("your/type"));
0260         QVERIFY(!m.acceptNotification(msg));
0261         m.mimetypes.insert(QStringLiteral("my/type"));
0262         QCOMPARE(m.acceptNotification(msg), type == Protocol::ChangeNotification::Items);
0263 
0264         // filter out the resource does not overwrite mimetype
0265         m.resources.insert("bar");
0266         QCOMPARE(m.acceptNotification(msg), type == Protocol::ChangeNotification::Items);
0267 
0268         // filtering out the session overwrites the mimetype
0269         m.sessions.append("mysession");
0270         QVERIFY(!m.acceptNotification(msg));
0271     }
0272 
0273     void filterCollection_data()
0274     {
0275         QTest::addColumn<Akonadi::Protocol::ChangeNotification::Operation>("op");
0276         QTest::addColumn<Akonadi::Protocol::ChangeNotification::Type>("type");
0277         QTest::addColumn<QByteArray>("signalName");
0278 
0279         QTest::newRow("itemAdded") << Protocol::ChangeNotification::Add << Protocol::ChangeNotification::Items
0280                                    << QByteArray(SIGNAL(itemAdded(Akonadi::Item, Akonadi::Collection)));
0281         QTest::newRow("itemChanged") << Protocol::ChangeNotification::Modify << Protocol::ChangeNotification::Items
0282                                      << QByteArray(SIGNAL(itemChanged(Akonadi::Item, QSet<QByteArray>)));
0283         QTest::newRow("itemsFlagsChanged") << Protocol::ChangeNotification::ModifyFlags << Protocol::ChangeNotification::Items
0284                                            << QByteArray(SIGNAL(itemsFlagsChanged(Akonadi::Item::List, QSet<QByteArray>, QSet<QByteArray>)));
0285         QTest::newRow("itemRemoved") << Protocol::ChangeNotification::Remove << Protocol::ChangeNotification::Items
0286                                      << QByteArray(SIGNAL(itemRemoved(Akonadi::Item)));
0287         QTest::newRow("itemMoved") << Protocol::ChangeNotification::Move << Protocol::ChangeNotification::Items
0288                                    << QByteArray(SIGNAL(itemMoved(Akonadi::Item, Akonadi::Collection, Akonadi::Collection)));
0289         QTest::newRow("itemLinked") << Protocol::ChangeNotification::Link << Protocol::ChangeNotification::Items
0290                                     << QByteArray(SIGNAL(itemLinked(Akonadi::Item, Akonadi::Collection)));
0291         QTest::newRow("itemUnlinked") << Protocol::ChangeNotification::Unlink << Protocol::ChangeNotification::Items
0292                                       << QByteArray(SIGNAL(itemUnlinked(Akonadi::Item, Akonadi::Collection)));
0293 
0294         QTest::newRow("colAdded") << Protocol::ChangeNotification::Add << Protocol::ChangeNotification::Collections
0295                                   << QByteArray(SIGNAL(collectionAdded(Akonadi::Collection, Akonadi::Collection)));
0296         QTest::newRow("colChanged") << Protocol::ChangeNotification::Modify << Protocol::ChangeNotification::Collections
0297                                     << QByteArray(SIGNAL(collectionChanged(Akonadi::Collection, QSet<QByteArray>)));
0298         QTest::newRow("colRemoved") << Protocol::ChangeNotification::Remove << Protocol::ChangeNotification::Collections
0299                                     << QByteArray(SIGNAL(collectionRemoved(Akonadi::Collection)));
0300         QTest::newRow("colMoved") << Protocol::ChangeNotification::Move << Protocol::ChangeNotification::Collections
0301                                   << QByteArray(SIGNAL(collectionMoved(Akonadi::Collection, Akonadi::Collection, Akonadi::Collection)));
0302         QTest::newRow("colSubscribed") << Protocol::ChangeNotification::Subscribe << Protocol::ChangeNotification::Collections
0303                                        << QByteArray(SIGNAL(collectionSubscribed(Akonadi::Collection, Akonadi::Collection)));
0304         QTest::newRow("colSubscribed") << Protocol::ChangeNotification::Unsubscribe << Protocol::ChangeNotification::Collections
0305                                        << QByteArray(SIGNAL(collectionUnsubscribed(Akonadi::Collection)));
0306     }
0307 
0308     void filterCollection()
0309     {
0310         QFETCH(Protocol::ChangeNotification::Operation, op);
0311         QFETCH(Protocol::ChangeNotification::Type, type);
0312         QFETCH(QByteArray, signalName);
0313 
0314         Monitor dummyMonitor;
0315         MonitorPrivate m(0, &dummyMonitor);
0316         QSignalSpy spy(&dummyMonitor, signalName.constData());
0317         QVERIFY(spy.isValid());
0318 
0319         Protocol::ChangeNotification msg;
0320         msg.addEntity(1, QString(), QString(), QStringLiteral("my/type"));
0321         msg.setOperation(op);
0322         msg.setParentCollection(2);
0323         msg.setType(type);
0324         msg.setResource("foo");
0325         msg.setSessionId("mysession");
0326 
0327         // using the right resource makes it pass
0328         QVERIFY(!m.acceptNotification(msg));
0329         m.collections.append(Collection(3));
0330         QVERIFY(!m.acceptNotification(msg));
0331 
0332         for (int colId = 0; colId < 3; ++colId) { // 0 == root, 1 == this, 2 == parent
0333             if (colId == 1 && type == Protocol::ChangeNotification::Items) {
0334                 continue;
0335             }
0336 
0337             m.collections.clear();
0338             m.collections.append(Collection(colId));
0339 
0340             QVERIFY(m.acceptNotification(msg));
0341 
0342             // filter out the resource does overwrite collection
0343             m.resources.insert("bar");
0344             QVERIFY(!m.acceptNotification(msg));
0345             m.resources.clear();
0346 
0347             // filter out the mimetype does overwrite collection, for item operations (mimetype filter has no effect on collections)
0348             m.mimetypes.insert(QStringLiteral("your/type"));
0349             QCOMPARE(!m.acceptNotification(msg), type == Protocol::ChangeNotification::Items);
0350             m.mimetypes.clear();
0351 
0352             // filtering out the session overwrites the mimetype
0353             m.sessions.append("mysession");
0354             QVERIFY(!m.acceptNotification(msg));
0355             m.sessions.clear();
0356 
0357             // filter non-matching resource and matching mimetype make it pass
0358             m.resources.insert("bar");
0359             m.mimetypes.insert(QStringLiteral("my/type"));
0360             QVERIFY(m.acceptNotification(msg));
0361             m.resources.clear();
0362             m.mimetypes.clear();
0363         }
0364     }
0365 };
0366 
0367 QTEST_MAIN(MonitorFilterTest)
0368 
0369 #include "monitorfiltertest.moc"