File indexing completed on 2024-11-17 04:49:34

0001 /*
0002  * Copyright (c) 2019 Alexander Potashev <aspotashev@gmail.com>
0003  *
0004  * This program is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU General Public License as
0006  * published by the Free Software Foundation; either version 2 of
0007  * the License or (at your option) version 3 or any later version
0008  * accepted by the membership of KDE e.V. (or its successor approved
0009  * by the membership of KDE e.V.), which shall act as a proxy
0010  * defined in Section 14 of version 3 of the license.
0011  *
0012  * This program is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015  * GNU General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU General Public License
0018  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #include <QTest>
0022 
0023 #include "dialogs/historydialog.h"
0024 #include "model/eventsmodel.h"
0025 #include "model/task.h"
0026 #include "model/tasksmodel.h"
0027 #include "taskview.h"
0028 
0029 #include "helpers.h"
0030 
0031 class TaskTest : public QObject
0032 {
0033     Q_OBJECT
0034 
0035 private Q_SLOTS:
0036     void testProperties();
0037     void testRefreshTimes();
0038     void testDurationFromEvent();
0039     void testDeleteRunning();
0040 };
0041 
0042 void TaskTest::testProperties()
0043 {
0044     auto *taskView = createTaskView(this, false);
0045     QVERIFY(taskView);
0046 
0047     auto *task1 = taskView->addTask(QStringLiteral("1"));
0048     QVERIFY(task1);
0049     QVERIFY(task1->isRoot());
0050     QCOMPARE(task1->depth(), 0);
0051 
0052     auto *task2 = taskView->addTask(QStringLiteral("2"), QString(), 0, 0, QVector<int>(0, 0), task1);
0053     QVERIFY(task2);
0054     QVERIFY(!task2->isRoot());
0055     QCOMPARE(task2->depth(), 1);
0056 }
0057 
0058 void TaskTest::testRefreshTimes()
0059 {
0060     auto *taskView = createTaskView(this, false);
0061     QVERIFY(taskView);
0062 
0063     auto *task1 = taskView->addTask(QStringLiteral("1"));
0064     auto *task2 = taskView->addTask(QStringLiteral("2"), QString(), 0, 0, QVector<int>(0, 0), task1);
0065     task2->changeTime(5, taskView->storage()->eventsModel());
0066 
0067     QCOMPARE(5, task2->time());
0068     QCOMPARE(5, task2->sessionTime());
0069 
0070     QCOMPARE(taskView->storage()->save(), QString());
0071 
0072     // Open saved file again
0073     auto *taskView2 = new TaskView();
0074     taskView2->load(taskView->storage()->fileUrl());
0075 
0076     taskView2->reFreshTimes();
0077 
0078     auto *task2Mirror = taskView2->storage()->tasksModel()->taskByUID(task2->uid());
0079     QCOMPARE(5, task2Mirror->time());
0080     QCOMPARE(5, task2Mirror->sessionTime());
0081 }
0082 
0083 void TaskTest::testDurationFromEvent()
0084 {
0085     auto *taskView = createTaskView(this, false);
0086     QVERIFY(taskView);
0087 
0088     auto *task = taskView->addTask(QStringLiteral("1"));
0089     task->changeTime(1, taskView->storage()->eventsModel());
0090     QCOMPARE(task->time(), 1);
0091 
0092     // Simulate increment of the hour number of event in "Edit History" dialog.
0093     // Such editing operation loses sub-second precision.
0094     auto *event = taskView->storage()->eventsModel()->eventsForTask(task)[0];
0095     QString endDateString = event->dtEnd().addSecs(3600).toString(HistoryDialog::dateTimeFormat);
0096     event->setDtEnd(QDateTime::fromString(endDateString, HistoryDialog::dateTimeFormat));
0097 
0098     // Duration must become 61 minutes
0099     taskView->reFreshTimes();
0100     QCOMPARE(task->time(), 61);
0101 }
0102 
0103 void TaskTest::testDeleteRunning()
0104 {
0105     auto *taskView = createTaskView(this, false);
0106     QVERIFY(taskView);
0107 
0108     auto *task = taskView->addTask(QStringLiteral("1"));
0109     taskView->startTimerForNow(task);
0110 
0111     taskView->deleteTaskBatch(task);
0112 }
0113 
0114 QTEST_MAIN(TaskTest)
0115 
0116 #include "tasktest.moc"