File indexing completed on 2025-01-05 04:55:02

0001 #include <QTest>
0002 #include <QDebug>
0003 #include <QStandardItemModel>
0004 #include <sink/test.h>
0005 #include <sink/store.h>
0006 #include <sink/resourcecontrol.h>
0007 #include "krecursivefilterproxymodel.h"
0008 #include "folderlistmodel.h"
0009 
0010 class TestModel : public KRecursiveFilterProxyModel {
0011 public:
0012 
0013     TestModel()
0014         :KRecursiveFilterProxyModel()
0015     {
0016         auto model = QSharedPointer<QStandardItemModel>::create();
0017         setSourceModel(model.data());
0018         mModel = model;
0019     }
0020 
0021     bool lessThan(const QModelIndex &left, const QModelIndex &right) const Q_DECL_OVERRIDE
0022     {
0023         return left.data(Qt::DisplayRole).toString() < right.data(Qt::DisplayRole).toString();
0024     }
0025 
0026     bool acceptRow(int sourceRow, const QModelIndex &sourceParent) const Q_DECL_OVERRIDE
0027     {
0028         auto index = sourceModel()->index(sourceRow, 0, sourceParent);
0029         return index.data(Qt::DisplayRole).toString().contains("accept");
0030     }
0031 
0032     QSharedPointer<QStandardItemModel> mModel;
0033 };
0034 
0035 bool contains(QAbstractItemModel &model, const QModelIndex &parent, QString s)
0036 {
0037     for (int row = 0; row < model.rowCount(parent); row++) {
0038         auto idx = model.index(row, 0, parent);
0039         if (idx.data(Qt::DisplayRole).toString() == s) {
0040             return true;
0041         }
0042         if (contains(model, idx, s)) {
0043             return true;
0044         }
0045     }
0046     return false;
0047 }
0048 
0049 class FolderlistModelTest : public QObject
0050 {
0051     Q_OBJECT
0052 private slots:
0053 
0054     void initTestCase()
0055     {
0056         Sink::Test::initTest();
0057     }
0058 
0059     void testRecursiveFilterModel()
0060     {
0061         TestModel model;
0062         auto root = new QStandardItem{"acceptroot"};
0063         model.mModel->appendRow(root);
0064         auto item = new QStandardItem{"accept1"};
0065         root->appendRow(item);
0066         item->appendRow(new QStandardItem{"accept11"});
0067         QVERIFY(contains(model, {}, "acceptroot"));
0068         QVERIFY(contains(model, {}, "accept1"));
0069         QVERIFY(contains(model, {}, "accept11"));
0070     }
0071 
0072     void testFolderListModel()
0073     {
0074         Sink::ApplicationDomain::DummyResource::create("account1");
0075 
0076         using namespace Sink::ApplicationDomain;
0077         auto account = ApplicationDomainType::createEntity<SinkAccount>();
0078         Sink::Store::create(account).exec().waitForFinished();
0079 
0080         auto resource = Sink::ApplicationDomain::DummyResource::create(account.identifier());
0081         Sink::Store::create(resource).exec().waitForFinished();
0082 
0083         auto folder = ApplicationDomainType::createEntity<Folder>(resource.identifier());
0084         Sink::Store::create(folder).exec().waitForFinished();
0085 
0086         Sink::ResourceControl::flushMessageQueue(resource.identifier()).exec().waitForFinished();
0087 
0088         FolderListModel model;
0089         model.setAccountId(account.identifier());
0090         QTRY_COMPARE(model.rowCount({}), 1);
0091         {
0092             auto subfolder = ApplicationDomainType::createEntity<Folder>(resource.identifier());
0093             subfolder.setParent(folder);
0094             Sink::Store::create(subfolder).exec().waitForFinished();
0095             Sink::ResourceControl::flushMessageQueue(resource.identifier()).exec().waitForFinished();
0096         }
0097         QTRY_COMPARE(model.rowCount(model.index(0, 0, {})), 1);
0098     }
0099 };
0100 
0101 QTEST_MAIN(FolderlistModelTest)
0102 #include "folderlistmodeltest.moc"