File indexing completed on 2025-01-05 04:59:59

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Kevin Ottens <ervin@kde.org>
0003  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 
0007 #include <testlib/qtest_zanshin.h>
0008 
0009 #include <QStringListModel>
0010 
0011 #include "presentation/pagemodel.h"
0012 
0013 class FakePageModel : public Presentation::PageModel
0014 {
0015     Q_OBJECT
0016 public:
0017     explicit FakePageModel(QObject *parent = nullptr)
0018         : Presentation::PageModel(parent),
0019           createCount(0),
0020           itemModel(nullptr)
0021     {
0022     }
0023 
0024     Domain::Task::Ptr addItem(const QString &, const QModelIndex &) override { return Domain::Task::Ptr::create(); }
0025     void removeItem(const QModelIndex &) override {}
0026     void promoteItem(const QModelIndex &) override {}
0027 
0028 private:
0029     QAbstractItemModel *createCentralListModel() override
0030     {
0031         createCount++;
0032         itemModel = new QStringListModel(this);
0033         return itemModel;
0034     }
0035 
0036 public:
0037     int createCount;
0038     QAbstractItemModel *itemModel;
0039 };
0040 
0041 class PageModelTest : public QObject
0042 {
0043     Q_OBJECT
0044 private slots:
0045     void shouldLazilyCreateModelOnlyOnce()
0046     {
0047         // GIVEN
0048         FakePageModel model;
0049         QCOMPARE(model.createCount, 0);
0050         QVERIFY(!model.itemModel);
0051 
0052         // WHEN
0053         QAbstractItemModel *itemModel = model.centralListModel();
0054 
0055         // THEN
0056         QCOMPARE(model.createCount, 1);
0057         QCOMPARE(itemModel, model.itemModel);
0058 
0059         // WHEN
0060         itemModel = model.centralListModel();
0061 
0062         // THEN
0063         QCOMPARE(model.createCount, 1);
0064         QCOMPARE(itemModel, model.itemModel);
0065     }
0066 };
0067 
0068 ZANSHIN_TEST_MAIN(PageModelTest)
0069 
0070 #include "pagemodeltest.moc"