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

0001 /*
0002  * SPDX-FileCopyrightText: 2014-2019 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 #ifndef ZANSHINCONTEXT_H
0007 #define ZANSHINCONTEXT_H
0008 
0009 #include <QObject>
0010 #include <QPersistentModelIndex>
0011 #include <QSharedPointer>
0012 #include <QPointer>
0013 #include <QList>
0014 
0015 #include "domain/task.h"
0016 #include "domain/datasource.h"
0017 #include "presentation/errorhandler.h"
0018 
0019 #include "testlib/akonadifakedata.h"
0020 #include "testlib/fakejob.h"
0021 
0022 class QAbstractItemModel;
0023 class QSortFilterProxyModel;
0024 class MonitorSpy;
0025 
0026 class FakeErrorHandler : public Presentation::ErrorHandler
0027 {
0028 public:
0029     void doDisplayMessage(const QString &) override;
0030 };
0031 
0032 #define Given(a) QVERIFY(a)
0033 #define When(a) QVERIFY(a)
0034 #define Then(a) QVERIFY(a)
0035 #define And(a) QVERIFY(a)
0036 
0037 class ZanshinContext : public QObject
0038 {
0039     Q_OBJECT
0040 public:
0041     struct TableData
0042     {
0043         QList<QByteArray> roles;
0044         QList<QList<QVariant>> rows;
0045     };
0046 
0047     explicit ZanshinContext(QObject *parent = nullptr);
0048 
0049     // GIVEN
0050     [[nodiscard]] bool I_display_the_available_data_sources();
0051     [[nodiscard]] bool I_display_the_available_pages();
0052     [[nodiscard]] bool I_display_the_page(const QString &pageName);
0053     [[nodiscard]] bool
0054     there_is_an_item_in_the_central_list(const QString &taskName);
0055     [[nodiscard]] bool
0056     there_is_an_item_in_the_available_data_sources(const QString &sourceName);
0057     [[nodiscard]] bool
0058     the_central_list_contains_items_named(const QStringList &taskNames);
0059 
0060     // WHEN
0061     [[nodiscard]] bool I_look_at_the_central_list();
0062     [[nodiscard]] bool I_check_the_item();
0063     [[nodiscard]] bool I_uncheck_the_item();
0064     [[nodiscard]] bool I_remove_the_item();
0065     [[nodiscard]] bool I_promote_the_item();
0066     [[nodiscard]] bool I_add_a_project(const QString &projectName,
0067                                        const QString &parentSourceName);
0068     [[nodiscard]] bool I_add_a_context(const QString &contextName,
0069                                        const QString &parentSourceName);
0070     [[nodiscard]] bool I_add_a_task(const QString &taskName);
0071     [[nodiscard]] bool I_rename_a_page(const QString &path,
0072                                        const QString &oldName,
0073                                        const QString &newName);
0074     [[nodiscard]] bool I_remove_a_page(const QString &path,
0075                                        const QString &pageName);
0076     [[nodiscard]] bool I_add_a_task_child(const QString &childName,
0077                                           const QString &parentName);
0078     [[nodiscard]] bool I_list_the_items();
0079     [[nodiscard]] bool I_open_the_item_in_the_editor();
0080     [[nodiscard]] bool I_mark_the_item_done_in_the_editor();
0081     [[nodiscard]] bool I_change_the_editor_field(const QString &field,
0082                                                  const QVariant &value);
0083     [[nodiscard]] bool I_rename_the_item(const QString &taskName);
0084     [[nodiscard]] bool I_open_the_item_in_the_editor_again();
0085     [[nodiscard]] bool
0086     I_drop_the_item_on_the_central_list(const QString &dropSiteName);
0087     [[nodiscard]] bool I_drop_the_item_on_the_blank_area_of_the_central_list();
0088     [[nodiscard]] bool
0089     I_drop_items_on_the_central_list(const QString &dropSiteName);
0090     [[nodiscard]] bool
0091     I_drop_the_item_on_the_page_list(const QString &pageName);
0092     [[nodiscard]] bool I_drop_items_on_the_page_list(const QString &pageName);
0093     [[nodiscard]] bool I_change_the_setting(const QString &key, qint64 id);
0094     [[nodiscard]] bool
0095     I_change_the_default_data_source(const QString &sourceName);
0096 
0097     // THEN
0098     [[nodiscard]] bool the_list_is(const TableData &data);
0099     [[nodiscard]] bool the_list_contains(const QString &itemName);
0100     [[nodiscard]] bool the_list_does_not_contain(const QString &itemName);
0101     [[nodiscard]] bool the_task_corresponding_to_the_item_is_done();
0102     [[nodiscard]] bool the_editor_shows_the_task_as_done();
0103     [[nodiscard]] bool
0104     the_editor_shows_the_field(const QString &field,
0105                                const QVariant &expectedValue);
0106     [[nodiscard]] bool the_default_data_source_is(const QString &sourceName);
0107     [[nodiscard]] bool the_setting_is(const QString &key, qint64 expectedId);
0108 
0109   private:
0110     void setModel(QAbstractItemModel *model);
0111     QAbstractItemModel *sourceModel() const;
0112     QAbstractItemModel *model() const;
0113 
0114     Domain::Task::Ptr currentTask() const;
0115     Domain::DataSource::Ptr dataSourceFromName(const QString &sourceName);
0116 
0117     void waitForEmptyJobQueue();
0118     void waitForStableState();
0119 
0120     void collectIndicesImpl(const QModelIndex &root = QModelIndex());
0121     void collectIndices();
0122 
0123     QSharedPointer<QObject> m_appModel;
0124 
0125     QList<QPersistentModelIndex> m_indices;
0126     QPersistentModelIndex m_index;
0127     QObject *m_presentation;
0128     QObject *m_editor;
0129     QList<QPersistentModelIndex> m_dragIndices;
0130 
0131     Testlib::AkonadiFakeData m_data;
0132 
0133     QSortFilterProxyModel *m_proxyModel;
0134     QAbstractItemModel *m_model;
0135     QPointer<QAbstractItemModel> m_sourceModel;
0136     MonitorSpy *m_monitorSpy;
0137     FakeErrorHandler m_errorHandler;
0138 };
0139 #endif