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

0001 /*
0002     SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "akonaditestfake_export.h"
0010 #include <QDebug>
0011 #include <QModelIndex>
0012 #include <QObject>
0013 #include <QVariantList>
0014 
0015 enum SignalType {
0016     NoSignal,
0017     RowsAboutToBeInserted,
0018     RowsInserted,
0019     RowsAboutToBeRemoved,
0020     RowsRemoved,
0021     RowsAboutToBeMoved,
0022     RowsMoved,
0023     DataChanged,
0024 };
0025 
0026 struct ExpectedSignal {
0027     ExpectedSignal(SignalType type, int start, int end, const QVariantList &newData)
0028         : ExpectedSignal{type, start, end, {}, newData}
0029     {
0030     }
0031 
0032     ExpectedSignal(SignalType type, int start, int end, const QVariant &parentData = {}, const QVariantList &newData = {})
0033         : signalType(type)
0034         , startRow(start)
0035         , endRow(end)
0036         , parentData(parentData)
0037         , newData(newData)
0038     {
0039     }
0040 
0041     ExpectedSignal(SignalType type,
0042                    int start,
0043                    int end,
0044                    const QVariant &sourceParentData,
0045                    int destRow,
0046                    const QVariant &destParentData,
0047                    const QVariantList &newData)
0048         : signalType(type)
0049         , startRow(start)
0050         , endRow(end)
0051         , parentData(destParentData)
0052         , sourceParentData(sourceParentData)
0053         , destRow(destRow)
0054         , newData(newData)
0055     {
0056     }
0057 
0058     SignalType signalType;
0059     int startRow;
0060     int endRow;
0061     QVariant parentData;
0062     QVariant sourceParentData;
0063     int destRow = 0;
0064     QVariantList newData;
0065 };
0066 
0067 Q_DECLARE_METATYPE(QModelIndex)
0068 
0069 class AKONADITESTFAKE_EXPORT ModelSpy : public QObject, public QList<QVariantList>
0070 {
0071     Q_OBJECT
0072 public:
0073     explicit ModelSpy(QAbstractItemModel *model, QObject *parent = nullptr);
0074 
0075     bool isEmpty() const;
0076 
0077     void setExpectedSignals(const QList<ExpectedSignal> &expectedSignals);
0078     QList<ExpectedSignal> expectedSignals() const;
0079 
0080     void verifySignal(SignalType type, const QModelIndex &parent, int start, int end);
0081     void verifySignal(SignalType type, const QModelIndex &parent, int start, int end, const QModelIndex &destParent, int destStart);
0082     void verifySignal(SignalType type, const QModelIndex &topLeft, const QModelIndex &bottomRight);
0083 
0084     void startSpying();
0085     void stopSpying();
0086     bool isSpying()
0087     {
0088         return m_isSpying;
0089     }
0090 
0091 protected Q_SLOTS:
0092     void rowsAboutToBeInserted(const QModelIndex &parent, int start, int end);
0093     void rowsInserted(const QModelIndex &parent, int start, int end);
0094     void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
0095     void rowsRemoved(const QModelIndex &parent, int start, int end);
0096     void rowsAboutToBeMoved(const QModelIndex &srcParent, int start, int end, const QModelIndex &destParent, int destStart);
0097     void rowsMoved(const QModelIndex &srcParent, int start, int end, const QModelIndex &destParent, int destStart);
0098 
0099     void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
0100 
0101 private:
0102     QAbstractItemModel *const m_model = nullptr;
0103     bool m_isSpying;
0104     QList<ExpectedSignal> m_expectedSignals;
0105 };
0106 
0107 AKONADITESTFAKE_EXPORT QDebug operator<<(QDebug dbg, SignalType type);