File indexing completed on 2025-01-19 04:57:00

0001 /*
0002  * SPDX-FileCopyrightText: 2016-2017 David Faure <faure@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_gui_zanshin.h>
0008 
0009 #include "widgets/runningtaskwidget.h"
0010 #include "presentation/runningtaskmodelinterface.h"
0011 #include <QAbstractButton>
0012 
0013 class RunningTaskModelStub : public Presentation::RunningTaskModelInterface
0014 {
0015     Q_OBJECT
0016 public:
0017     Domain::Task::Ptr runningTask() const override { return m_runningTask; }
0018     void setRunningTask(const Domain::Task::Ptr &runningTask) override
0019     {
0020         m_runningTask = runningTask;
0021         emit runningTaskChanged(m_runningTask);
0022     }
0023 
0024     void stopTask() override
0025     {
0026         Q_ASSERT(m_runningTask);
0027         setRunningTask(Domain::Task::Ptr());
0028     }
0029 
0030     void doneTask() override
0031     {
0032         Q_ASSERT(m_runningTask);
0033         m_runningTask->setDone(true);
0034         stopTask();
0035     }
0036 
0037     void taskDeleted(const Domain::Task::Ptr &task) override
0038     {
0039         Q_ASSERT(task);
0040         if (m_runningTask == task)
0041             setRunningTask(Domain::Task::Ptr());
0042     }
0043 
0044     void currentTaskRenamed()
0045     {
0046         emit runningTaskChanged(m_runningTask);
0047     }
0048 
0049 private:
0050     Domain::Task::Ptr m_runningTask;
0051 };
0052 
0053 class RunningTaskWidgetTest : public QObject
0054 {
0055     Q_OBJECT
0056 private slots:
0057     void shouldHaveDefaultState()
0058     {
0059         Widgets::RunningTaskWidget widget;
0060         QVERIFY(widget.isHidden());
0061     }
0062 
0063     void shouldShowWhenRunningATask()
0064     {
0065         // GIVEN
0066         Widgets::RunningTaskWidget widget;
0067         auto task = Domain::Task::Ptr::create();
0068         const QString title = QStringLiteral("title");
0069         task->setTitle(title);
0070         RunningTaskModelStub model;
0071         widget.setModel(&model);
0072 
0073         // WHEN
0074         model.setRunningTask(task);
0075 
0076         // THEN
0077         QCOMPARE(widget.currentText(), title);
0078         QVERIFY(!widget.isHidden());
0079     }
0080 
0081     void shouldShowWhenRunningADifferentTask()
0082     {
0083         // GIVEN
0084         Widgets::RunningTaskWidget widget;
0085         auto task1 = Domain::Task::Ptr::create();
0086         task1->setTitle(QStringLiteral("task1"));
0087         auto task2 = Domain::Task::Ptr::create();
0088         task2->setTitle(QStringLiteral("task2"));
0089         RunningTaskModelStub model;
0090         widget.setModel(&model);
0091         model.setRunningTask(task1);
0092 
0093         // WHEN
0094         model.setRunningTask(task2);
0095 
0096         // THEN
0097         QCOMPARE(widget.currentText(), QStringLiteral("task2"));
0098         QVERIFY(!widget.isHidden());
0099     }
0100 
0101     void shouldUpdateWhenRenamingATask()
0102     {
0103         // GIVEN
0104         Widgets::RunningTaskWidget widget;
0105         auto task = Domain::Task::Ptr::create();
0106         task->setTitle(QStringLiteral("task1"));
0107         RunningTaskModelStub model;
0108         widget.setModel(&model);
0109         model.setRunningTask(task);
0110         QCOMPARE(widget.currentText(), QStringLiteral("task1"));
0111 
0112         // WHEN
0113         task->setTitle(QStringLiteral("renamed task1"));
0114         model.currentTaskRenamed();
0115 
0116         // THEN
0117         QCOMPARE(widget.currentText(), QStringLiteral("renamed task1"));
0118         QVERIFY(!widget.isHidden());
0119     }
0120 
0121     void shouldStopAndHideOnClickingStop()
0122     {
0123         // GIVEN
0124         Widgets::RunningTaskWidget widget;
0125         auto task = Domain::Task::Ptr::create();
0126         RunningTaskModelStub model;
0127         widget.setModel(&model);
0128         model.setRunningTask(task);
0129         auto button = widget.findChild<QAbstractButton *>("stopButton");
0130         QVERIFY(button);
0131 
0132         // WHEN
0133         button->click();
0134 
0135         // THEN stopTask should have been called
0136         QCOMPARE(model.runningTask(), Domain::Task::Ptr());
0137         QVERIFY(widget.isHidden());
0138     }
0139 
0140     void shouldMarkAsDoneAndHideOnClickingDone()
0141     {
0142         // GIVEN
0143         Widgets::RunningTaskWidget widget;
0144         auto task = Domain::Task::Ptr::create();
0145         RunningTaskModelStub model;
0146         widget.setModel(&model);
0147         model.setRunningTask(task);
0148         QVERIFY(!task->isDone());
0149         auto button = widget.findChild<QAbstractButton *>("doneButton");
0150         QVERIFY(button);
0151 
0152         // WHEN
0153         button->click();
0154 
0155         // THEN doneTask should have been called
0156         QVERIFY(task->isDone());
0157         QVERIFY(widget.isHidden());
0158     }
0159 
0160     void shouldHideOnExternalStop()
0161     {
0162         // GIVEN
0163         Widgets::RunningTaskWidget widget;
0164         auto task = Domain::Task::Ptr::create();
0165         RunningTaskModelStub model;
0166         widget.setModel(&model);
0167         model.setRunningTask(task);
0168 
0169         // WHEN
0170         model.stopTask();
0171 
0172         // THEN
0173         QVERIFY(widget.isHidden());
0174     }
0175 
0176     void shouldHideOnExternalDone()
0177     {
0178         // GIVEN
0179         Widgets::RunningTaskWidget widget;
0180         auto task = Domain::Task::Ptr::create();
0181         RunningTaskModelStub model;
0182         widget.setModel(&model);
0183         model.setRunningTask(task);
0184 
0185         // WHEN
0186         model.doneTask();
0187 
0188         // THEN
0189         QVERIFY(widget.isHidden());
0190     }
0191 
0192     void shouldHideOnDeletion()
0193     {
0194         // GIVEN
0195         Widgets::RunningTaskWidget widget;
0196         auto task = Domain::Task::Ptr::create();
0197         RunningTaskModelStub model;
0198         widget.setModel(&model);
0199         model.setRunningTask(task);
0200 
0201         // WHEN
0202         model.taskDeleted(task);
0203 
0204         // THEN
0205         QVERIFY(widget.isHidden());
0206     }
0207 };
0208 
0209 ZANSHIN_TEST_MAIN(RunningTaskWidgetTest)
0210 
0211 #include "runningtaskwidgettest.moc"