File indexing completed on 2025-01-05 05:00: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 "utils/mockobject.h"
0010 #include "utils/jobhandler.h"
0011 
0012 #include "presentation/runningtaskmodel.h"
0013 
0014 #include "testhelpers.h"
0015 
0016 using namespace mockitopp;
0017 using namespace mockitopp::matcher;
0018 
0019 class TestDependencies
0020 {
0021 public:
0022     TestDependencies()
0023     {
0024         m_taskProvider = Domain::QueryResultProvider<Domain::Task::Ptr>::Ptr::create();
0025         auto taskResult = Domain::QueryResult<Domain::Task::Ptr>::create(m_taskProvider);
0026 
0027         m_taskQueriesMock(&Domain::TaskQueries::findAll).when().thenReturn(taskResult);
0028         m_taskQueriesMockInstance = m_taskQueriesMock.getInstance();
0029 
0030         Utils::MockObject<Domain::TaskRepository> taskRepositoryMock;
0031         taskRepositoryMock(&Domain::TaskRepository::update).when(any<Domain::Task::Ptr>())
0032                                                                 .thenReturn(0);
0033         m_taskRepositoryMockInstance = taskRepositoryMock.getInstance();
0034     }
0035     Utils::MockObject<Domain::TaskQueries> m_taskQueriesMock;
0036 
0037     Domain::TaskQueries::Ptr m_taskQueriesMockInstance;
0038     Domain::TaskRepository::Ptr m_taskRepositoryMockInstance;
0039     Domain::QueryResultProvider<Domain::Task::Ptr>::Ptr m_taskProvider;
0040 };
0041 
0042 class RunningTaskModelTest : public QObject
0043 {
0044     Q_OBJECT
0045 private slots:
0046     void shouldDoInitialListing()
0047     {
0048         // GIVEN
0049         TestDependencies deps;
0050         // Three tasks, one being marked as running
0051         auto firstTask = Domain::Task::Ptr::create();
0052         firstTask->setTitle(QStringLiteral("rootTask"));
0053         auto initialTask = Domain::Task::Ptr::create();
0054         initialTask->setTitle(QStringLiteral("initialTask"));
0055         initialTask->setRunning(true);
0056         auto otherTask = Domain::Task::Ptr::create();
0057         otherTask->setTitle(QStringLiteral("otherTask"));
0058 
0059         // WHEN
0060         Presentation::RunningTaskModel model(deps.m_taskQueriesMockInstance, deps.m_taskRepositoryMockInstance);
0061         QVERIFY(!model.runningTask());
0062         deps.m_taskProvider->append(firstTask);
0063         deps.m_taskProvider->append(initialTask);
0064         deps.m_taskProvider->append(otherTask);
0065 
0066         // THEN
0067         QCOMPARE(model.runningTask(), initialTask);
0068     }
0069 
0070     void shouldStartTask()
0071     {
0072         // GIVEN
0073         TestDependencies deps;
0074         Presentation::RunningTaskModel model(deps.m_taskQueriesMockInstance, deps.m_taskRepositoryMockInstance);
0075         Domain::Task::Ptr task = Domain::Task::Ptr::create();
0076         QSignalSpy spy(&model, &Presentation::RunningTaskModel::runningTaskChanged);
0077 
0078         // WHEN
0079         model.setRunningTask(task);
0080 
0081         // THEN
0082         QCOMPARE(model.runningTask(), task);
0083         QVERIFY(task->isRunning());
0084         QCOMPARE(spy.count(), 1);
0085         QCOMPARE(spy.at(0).at(0).value<Domain::Task::Ptr>(), task);
0086     }
0087 
0088     void shouldHandleStopTask()
0089     {
0090         // GIVEN
0091         TestDependencies deps;
0092         Presentation::RunningTaskModel model(deps.m_taskQueriesMockInstance, deps.m_taskRepositoryMockInstance);
0093         Domain::Task::Ptr task = Domain::Task::Ptr::create();
0094         model.setRunningTask(task);
0095         QSignalSpy spy(&model, &Presentation::RunningTaskModel::runningTaskChanged);
0096 
0097         // WHEN
0098         model.stopTask();
0099 
0100         // THEN
0101         QCOMPARE(model.runningTask(), Domain::Task::Ptr());
0102         QVERIFY(!task->isRunning());
0103         QCOMPARE(spy.count(), 1);
0104         QCOMPARE(spy.at(0).at(0).value<Domain::Task::Ptr>(), Domain::Task::Ptr());
0105     }
0106 
0107     void shouldHandleDoneTask()
0108     {
0109         // GIVEN
0110         TestDependencies deps;
0111         Presentation::RunningTaskModel model(deps.m_taskQueriesMockInstance, deps.m_taskRepositoryMockInstance);
0112         Domain::Task::Ptr task = Domain::Task::Ptr::create();
0113         model.setRunningTask(task);
0114         QSignalSpy spy(&model, &Presentation::RunningTaskModel::runningTaskChanged);
0115 
0116         // WHEN
0117         model.doneTask();
0118 
0119         // THEN
0120         QCOMPARE(model.runningTask(), Domain::Task::Ptr());
0121         QVERIFY(!task->isRunning());
0122         QVERIFY(task->isDone());
0123         QCOMPARE(spy.count(), 1);
0124         QCOMPARE(spy.at(0).at(0).value<Domain::Task::Ptr>(), Domain::Task::Ptr());
0125     }
0126 
0127     void shouldHandleSwitchingToAnotherTask()
0128     {
0129         // GIVEN
0130         TestDependencies deps;
0131         Presentation::RunningTaskModel model(deps.m_taskQueriesMockInstance, deps.m_taskRepositoryMockInstance);
0132         Domain::Task::Ptr task = Domain::Task::Ptr::create();
0133         model.setRunningTask(task);
0134         Domain::Task::Ptr task2 = Domain::Task::Ptr::create();
0135         QSignalSpy spy(&model, &Presentation::RunningTaskModel::runningTaskChanged);
0136 
0137         // WHEN
0138         model.setRunningTask(task2);
0139 
0140         // THEN
0141         QCOMPARE(model.runningTask(), task2);
0142         QVERIFY(!task->isRunning());
0143         QVERIFY(task2->isRunning());
0144         QCOMPARE(spy.count(), 1);
0145         QCOMPARE(spy.at(0).at(0).value<Domain::Task::Ptr>(), task2);
0146     }
0147 
0148     void shouldHandleTaskDeletion()
0149     {
0150         // GIVEN
0151         TestDependencies deps;
0152         Presentation::RunningTaskModel model(deps.m_taskQueriesMockInstance, deps.m_taskRepositoryMockInstance);
0153         Domain::Task::Ptr task = Domain::Task::Ptr::create();
0154         model.setRunningTask(task);
0155         QSignalSpy spy(&model, &Presentation::RunningTaskModel::runningTaskChanged);
0156 
0157         // WHEN
0158         model.taskDeleted(task);
0159 
0160         // THEN
0161         QCOMPARE(model.runningTask(), Domain::Task::Ptr());
0162         QVERIFY(!task->isRunning());
0163         QVERIFY(!task->isDone());
0164         QCOMPARE(spy.count(), 1);
0165         QCOMPARE(spy.at(0).at(0).value<Domain::Task::Ptr>(), Domain::Task::Ptr());
0166     }
0167 
0168     void shouldIgnoreDeletionOfAnotherTask()
0169     {
0170         // GIVEN
0171         TestDependencies deps;
0172         Presentation::RunningTaskModel model(deps.m_taskQueriesMockInstance, deps.m_taskRepositoryMockInstance);
0173         Domain::Task::Ptr task = Domain::Task::Ptr::create();
0174         model.setRunningTask(task);
0175         QSignalSpy spy(&model, &Presentation::RunningTaskModel::runningTaskChanged);
0176         Domain::Task::Ptr task2 = Domain::Task::Ptr::create();
0177 
0178         // WHEN
0179         model.taskDeleted(task2);
0180 
0181         // THEN
0182         QCOMPARE(model.runningTask(), task);
0183         QVERIFY(task->isRunning());
0184         QCOMPARE(spy.count(), 0);
0185     }
0186 
0187     void shouldUpdateOnTaskRenaming()
0188     {
0189         // GIVEN
0190         TestDependencies deps;
0191         Presentation::RunningTaskModel model(deps.m_taskQueriesMockInstance, deps.m_taskRepositoryMockInstance);
0192         Domain::Task::Ptr task = Domain::Task::Ptr::create();
0193         task->setTitle(QStringLiteral("Old title"));
0194         model.setRunningTask(task);
0195         //Domain::Task::Ptr task2 = Domain::Task::Ptr::create();
0196         //model.setRunningTask(task2);
0197         QSignalSpy spy(&model, &Presentation::RunningTaskModel::runningTaskChanged);
0198 
0199         // WHEN
0200         task->setTitle(QStringLiteral("New title"));
0201 
0202         // THEN
0203         QCOMPARE(model.runningTask(), task);
0204         QVERIFY(task->isRunning());
0205         QCOMPARE(spy.count(), 1);
0206         QCOMPARE(spy.at(0).at(0).value<Domain::Task::Ptr>(), task);
0207     }
0208 
0209     void shouldIgnoreRenamingAnotherTask()
0210     {
0211         // GIVEN
0212         TestDependencies deps;
0213         Presentation::RunningTaskModel model(deps.m_taskQueriesMockInstance, deps.m_taskRepositoryMockInstance);
0214         Domain::Task::Ptr task = Domain::Task::Ptr::create();
0215         task->setTitle(QStringLiteral("Task 1 title"));
0216         model.setRunningTask(task);
0217         Domain::Task::Ptr task2 = Domain::Task::Ptr::create();
0218         task2->setTitle(QStringLiteral("Task 2 title"));
0219         QSignalSpy spy(&model, &Presentation::RunningTaskModel::runningTaskChanged);
0220 
0221         // WHEN
0222         task2->setTitle(QStringLiteral("New task 2 title"));
0223 
0224         // THEN
0225         QCOMPARE(model.runningTask(), task);
0226         QVERIFY(task->isRunning());
0227         QCOMPARE(spy.count(), 0);
0228     }
0229 
0230 
0231 
0232 private:
0233 };
0234 
0235 ZANSHIN_TEST_MAIN(RunningTaskModelTest)
0236 
0237 #include "runningtaskmodeltest.moc"