1 /*
2 * Copyright (c) 2019 Alexander Potashev <aspotashev@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License or (at your option) version 3 or any later version
8 * accepted by the membership of KDE e.V. (or its successor approved
9 * by the membership of KDE e.V.), which shall act as a proxy
10 * defined in Section 14 of version 3 of the license.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "helpers.h"
22
23 #include <QTemporaryFile>
24 #include <QTextStream>
25
26 #include "taskview.h"
27 #include "model/task.h"
28
29 class TemporaryFileDeleter : public QObject
30 {
31 public:
TemporaryFileDeleter(QObject * parent,QString fileName)32 explicit TemporaryFileDeleter(QObject *parent, QString fileName)
33 : QObject(parent)
34 , m_fileName(std::move(fileName))
35 {
36 }
37
~TemporaryFileDeleter()38 ~TemporaryFileDeleter() override
39 {
40 QFile::remove(m_fileName);
41 }
42
43 private:
44 QString m_fileName;
45 };
46
createTempFile(QObject * parent)47 QUrl createTempFile(QObject *parent)
48 {
49 auto *file = new QTemporaryFile();
50 if (!file->open()) {
51 delete file;
52 return {};
53 }
54 const QString& fileName = file->fileName();
55 // We have to destroy the QTemporaryFile here because
56 // otherwise on Windows it will block the file, so that
57 // the code being tested (e.g. TimeTrackerStorage::save())
58 // will not be able to access it.
59 delete file;
60
61 // Schedule removal of the file after the current unit test ends.
62 new TemporaryFileDeleter(parent, fileName);
63
64 return QUrl::fromLocalFile(fileName);
65 }
66
createTaskView(QObject * parent,bool simpleTree)67 TaskView *createTaskView(QObject *parent, bool simpleTree)
68 {
69 auto *taskView = new TaskView();
70
71 QUrl icsFile = createTempFile(parent);
72 if (icsFile.isEmpty()) {
73 delete taskView;
74 return nullptr;
75 }
76
77 taskView->load(icsFile);
78
79 if (simpleTree) {
80 Task* task1 = taskView->addTask("1");
81 Task* task2 = taskView->addTask("2", QString(), 0, 0, QVector<int>(0, 0), task1);
82 Task* task3 = taskView->addTask("3");
83
84 task1->changeTime(5, taskView->storage()->eventsModel()); // add 5 minutes
85 task2->changeTime(3, taskView->storage()->eventsModel()); // add 3 minutes
86 task3->changeTime(7, taskView->storage()->eventsModel()); // add 7 minutes
87 }
88
89 return taskView;
90 }
91
readTextFile(const QString & path)92 QString readTextFile(const QString &path)
93 {
94 QFile file(path);
95 if (!file.open(QFile::ReadOnly | QFile::Text)) {
96 qFatal("failed to open file: %s", path.toUtf8().constData());
97 }
98
99 QTextStream in(&file);
100 return in.readAll();
101 }
102