subscriptiondialogtest.cpp 8.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/*
 * Copyright 2020  Daniel Vrátil <dvratil@kde.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License or (at your option) version 3 or any later version
 * accepted by the membership of KDE e.V. (or its successor approved
 * by the membership of KDE e.V.), which shall act as a proxy
 * defined in Section 14 of version 3 of the license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

#include "qtest_akonadi.h"
#include <qnamespace.h>
#include <qtestmouse.h>
#include <shared/aktest.h>
#include <shared/akscopeguard.h>
#include "../libs/test_utils.h"

#include "subscriptiondialog.h"
#include "subscriptionmodel_p.h"
#include "subscriptionjob_p.h"

#include <QSignalSpy>
#include <QTest>
#include <QLineEdit>
#include <QPushButton>
#include <QTreeView>
#include <QDialogButtonBox>
#include <QDialog>
#include <QCheckBox>

#include <memory>
#include <deque>

using namespace Akonadi;

class SubscriptionDialogTest: public QObject
{
    Q_OBJECT

    struct TestSetup {
Volker Krause's avatar
Volker Krause committed
52
        enum { defaultCollectionCount = 7 };
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269


        TestSetup()
        {
            widget = std::make_unique<SubscriptionDialog>(QStringList{
                    Collection::mimeType(),
                    QStringLiteral("application/octet-stream")
            });
            widget->setAttribute(Qt::WA_DeleteOnClose, false);
            widget->show();

            model = widget->findChild<SubscriptionModel*>();
            QVERIFY(model);
            QSignalSpy modelLoadedSpy(model, &SubscriptionModel::loaded);

            buttonBox = widget->findChild<QDialogButtonBox*>();
            QVERIFY(buttonBox);
            QVERIFY(!buttonBox->button(QDialogButtonBox::Ok)->isEnabled());
            searchLineEdit = widget->findChild<QLineEdit*>(QStringLiteral("searchLineEdit"));
            QVERIFY(searchLineEdit);
            subscribedOnlyChkBox = widget->findChild<QCheckBox*>(QStringLiteral("subscribedOnlyCheckBox"));
            QVERIFY(subscribedOnlyChkBox);
            collectionView = widget->findChild<QTreeView*>(QStringLiteral("collectionView"));
            QVERIFY(collectionView);
            subscribeButton = widget->findChild<QPushButton*>(QStringLiteral("subscribeButton"));
            QVERIFY(subscribeButton);
            unsubscribeButton = widget->findChild<QPushButton*>(QStringLiteral("unsubscribeButton"));
            QVERIFY(unsubscribeButton);

            QVERIFY(QTest::qWaitForWindowActive(widget.get()));
            QVERIFY(modelLoadedSpy.wait());
            QTest::qWait(100);

            // Helps with testing :)
            collectionView->expandAll();

            // Post-setup conditions
            QCOMPARE(countTotalRows(), defaultCollectionCount);
            QVERIFY(buttonBox->button(QDialogButtonBox::Ok)->isEnabled());

            valid = true;
        }

        ~TestSetup()
        {
        }

        int countTotalRows(const QModelIndex &parent = {}) const
        {
            const auto count = collectionView->model()->rowCount(parent);
            int total = count;
            for (int i = 0; i < count; ++i) {
                total += countTotalRows(collectionView->model()->index(i, 0, parent));
            }
            return total;
        }

        static bool unsubscribeCollection(const Collection &col)
        {
            return modifySubscription({}, {col});
        }

        static bool subscribeCollection(const Collection &col)
        {
            return modifySubscription({col}, {});
        }

        static bool modifySubscription(const Collection::List &subscribe,
                                       const Collection::List &unsubscribe)
        {
            auto job = new SubscriptionJob();
            job->subscribe(subscribe);
            job->unsubscribe(unsubscribe);
            bool ok = false;
            [job, &ok]() { AKVERIFYEXEC(job); ok = true; }();
            AKVERIFY(ok);

            return true;
        }

        bool selectCollection(const Collection &col)
        {
            AKVERIFY(col.isValid());
            const QModelIndex colIdx = indexForCollection(col);
            AKVERIFY(colIdx.isValid());

            collectionView->scrollTo(colIdx);
            QTest::mouseClick(collectionView->viewport(), Qt::LeftButton, {}, collectionView->visualRect(colIdx).center());

            AKCOMPARE(collectionView->currentIndex(), colIdx);
            return true;
        }

        bool isCollectionChecked(const Collection &col) const
        {
            AKVERIFY(col.isValid());
            const auto colIdx = indexForCollection(col);
            AKVERIFY(colIdx.isValid());

            return collectionView->model()->data(colIdx, Qt::CheckStateRole).value<Qt::CheckState>() == Qt::Checked;
        }

        void acceptDialog()
        {
            auto button = buttonBox->button(QDialogButtonBox::Ok);
            QTest::mouseClick(button, Qt::LeftButton);
        }

        QModelIndex indexForCollection(const Collection &col) const
        {
            auto model = collectionView->model();
            std::deque<QModelIndex> idxQueue;
            idxQueue.push_back(QModelIndex{});
            while (!idxQueue.empty()) {
                const auto idx = idxQueue.front();
                idxQueue.pop_front();
                if (model->data(idx, CollectionModel::CollectionIdRole).value<qint64>() == col.id()) {
                    return idx;
                }
                for (int i = 0; i < model->rowCount(idx); ++i) {
                    idxQueue.push_back(model->index(i, 0, idx));
                }
            }
            return {};
        }

        std::unique_ptr<SubscriptionDialog> widget;
        QDialogButtonBox *buttonBox = nullptr;
        QLineEdit *searchLineEdit = nullptr;
        QCheckBox *subscribedOnlyChkBox = nullptr;
        QTreeView *collectionView = nullptr;
        QPushButton *subscribeButton = nullptr;
        QPushButton *unsubscribeButton = nullptr;
        SubscriptionModel *model = nullptr;

        bool valid = false;
    };

private Q_SLOTS:
    void initTestCase()
    {
        AkonadiTest::checkTestIsIsolated();
    }

    void testSearchFilter()
    {
        TestSetup test;
        QVERIFY(test.valid);

        QTest::keyClicks(test.searchLineEdit, QStringLiteral("foo"));
        QCOMPARE(test.countTotalRows(), 2);
    }

    void testSubscribedOnlyCheckbox()
    {
        const auto col = Collection{collectionIdFromPath(QStringLiteral("res1/foo/bla"))};
        const AkScopeGuard guard([col]() {
            TestSetup::subscribeCollection(col);
        });

        QVERIFY(TestSetup::unsubscribeCollection(col));

        TestSetup test;
        QVERIFY(test.valid);

        test.subscribedOnlyChkBox->setChecked(true);

        QTRY_COMPARE(test.countTotalRows(), test.defaultCollectionCount - 1);

        test.subscribedOnlyChkBox->setChecked(false);

        QTRY_COMPARE(test.countTotalRows(), test.defaultCollectionCount);
    }

    void testSubscribeButton()
    {
        const auto col = Collection{collectionIdFromPath(QStringLiteral("res1/foo/bla"))};
        const AkScopeGuard guard([col]() {
            TestSetup::subscribeCollection(col);
        });

        QVERIFY(TestSetup::unsubscribeCollection(col));

        TestSetup test;
        QVERIFY(test.valid);

        QVERIFY(test.selectCollection(col));
        QTest::mouseClick(test.subscribeButton, Qt::LeftButton);
        QVERIFY(test.isCollectionChecked(col));

        auto monitor = getTestMonitor();
        QSignalSpy monitorSpy(monitor.get(), &Monitor::collectionSubscribed);
        test.acceptDialog();
        QVERIFY(monitorSpy.wait());
    }

    void testUnsubscribeButton()
    {
        const auto col = Collection{collectionIdFromPath(QStringLiteral("res1/foo/bla"))};

        TestSetup test;
        QVERIFY(test.valid);

        QVERIFY(test.selectCollection(col));
        QTest::mouseClick(test.unsubscribeButton, Qt::LeftButton);
        QVERIFY(!test.isCollectionChecked(col));

        auto monitor = getTestMonitor();
        QSignalSpy monitorSpy(monitor.get(), &Monitor::collectionUnsubscribed);
        test.acceptDialog();
        QVERIFY(monitorSpy.wait());
    }
};

QTEST_AKONADIMAIN(SubscriptionDialogTest)

#include "subscriptiondialogtest.moc"