File indexing completed on 2025-01-05 04:59:56

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Kevin Ottens <ervin@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_zanshin.h>
0008 
0009 #include "domain/task.h"
0010 
0011 #include "utils/datetime.h"
0012 
0013 using namespace Domain;
0014 
0015 class TaskTest : public QObject
0016 {
0017     Q_OBJECT
0018 public:
0019     explicit TaskTest(QObject *parent = nullptr)
0020         : QObject(parent)
0021     {
0022         qRegisterMetaType<Task::Recurrence>();
0023         qRegisterMetaType<Task::Attachments>();
0024     }
0025 
0026 private slots:
0027     void shouldHaveEmptyPropertiesByDefault()
0028     {
0029         Task t;
0030         QCOMPARE(t.text(), QString());
0031         QCOMPARE(t.title(), QString());
0032         QCOMPARE(t.isDone(), false);
0033         QCOMPARE(t.startDate(), QDate());
0034         QCOMPARE(t.dueDate(), QDate());
0035         QCOMPARE(t.doneDate(), QDate());
0036         QCOMPARE(t.recurrence(), Domain::Task::NoRecurrence);
0037         QVERIFY(t.attachments().isEmpty());
0038     }
0039 
0040     void shouldNotifyTextChanges()
0041     {
0042         Task t;
0043         QSignalSpy spy(&t, &Task::textChanged);
0044         t.setText(QStringLiteral("foo"));
0045         QCOMPARE(spy.count(), 1);
0046         QCOMPARE(spy.first().first().toString(), QStringLiteral("foo"));
0047     }
0048 
0049     void shouldNotNotifyIdenticalTextChanges()
0050     {
0051         Task t;
0052         t.setText(QStringLiteral("foo"));
0053         QSignalSpy spy(&t, &Task::textChanged);
0054         t.setText(QStringLiteral("foo"));
0055         QCOMPARE(spy.count(), 0);
0056     }
0057 
0058     void shouldNotifyTitleChanges()
0059     {
0060         Task t;
0061         QSignalSpy spy(&t, &Task::titleChanged);
0062         t.setTitle(QStringLiteral("foo"));
0063         QCOMPARE(spy.count(), 1);
0064         QCOMPARE(spy.first().first().toString(), QStringLiteral("foo"));
0065     }
0066 
0067     void shouldNotNotifyIdenticalTitleChanges()
0068     {
0069         Task t;
0070         t.setTitle(QStringLiteral("foo"));
0071         QSignalSpy spy(&t, &Task::titleChanged);
0072         t.setTitle(QStringLiteral("foo"));
0073         QCOMPARE(spy.count(), 0);
0074     }
0075 
0076     void shouldHaveValueBasedAttachment()
0077     {
0078         Task::Attachment a;
0079         QVERIFY(!a.isValid());
0080         QVERIFY(!a.isUri());
0081         QCOMPARE(a.uri(), QUrl());
0082         QCOMPARE(a.data(), QByteArray());
0083         QCOMPARE(a.label(), QString());
0084         QCOMPARE(a.mimeType(), QString());
0085         QCOMPARE(a.iconName(), QString());
0086 
0087         a.setUri(QUrl("https://www.kde.org"));
0088         QVERIFY(a.isValid());
0089         QVERIFY(a.isUri());
0090         QCOMPARE(a.uri(), QUrl("https://www.kde.org"));
0091         QCOMPARE(a.data(), QByteArray());
0092         QCOMPARE(a.label(), QString());
0093         QCOMPARE(a.mimeType(), QString());
0094         QCOMPARE(a.iconName(), QString());
0095 
0096         a.setData(QByteArrayLiteral("foobar"));
0097         QVERIFY(a.isValid());
0098         QVERIFY(!a.isUri());
0099         QCOMPARE(a.uri(), QUrl());
0100         QCOMPARE(a.data(), QByteArrayLiteral("foobar"));
0101         QCOMPARE(a.label(), QString());
0102         QCOMPARE(a.mimeType(), QString());
0103         QCOMPARE(a.iconName(), QString());
0104 
0105         a.setLabel(QStringLiteral("baz"));
0106         QVERIFY(a.isValid());
0107         QVERIFY(!a.isUri());
0108         QCOMPARE(a.uri(), QUrl());
0109         QCOMPARE(a.data(), QByteArrayLiteral("foobar"));
0110         QCOMPARE(a.label(), QStringLiteral("baz"));
0111         QCOMPARE(a.mimeType(), QString());
0112         QCOMPARE(a.iconName(), QString());
0113 
0114         a.setMimeType(QStringLiteral("text/plain"));
0115         QVERIFY(a.isValid());
0116         QVERIFY(!a.isUri());
0117         QCOMPARE(a.uri(), QUrl());
0118         QCOMPARE(a.data(), QByteArrayLiteral("foobar"));
0119         QCOMPARE(a.label(), QStringLiteral("baz"));
0120         QCOMPARE(a.mimeType(), QStringLiteral("text/plain"));
0121         QCOMPARE(a.iconName(), QString());
0122 
0123         a.setIconName(QStringLiteral("text"));
0124         QVERIFY(a.isValid());
0125         QVERIFY(!a.isUri());
0126         QCOMPARE(a.uri(), QUrl());
0127         QCOMPARE(a.data(), QByteArrayLiteral("foobar"));
0128         QCOMPARE(a.label(), QStringLiteral("baz"));
0129         QCOMPARE(a.mimeType(), QStringLiteral("text/plain"));
0130         QCOMPARE(a.iconName(), QStringLiteral("text"));
0131 
0132         a.setUri(QUrl("https://www.kde.org"));
0133         QVERIFY(a.isValid());
0134         QVERIFY(a.isUri());
0135         QCOMPARE(a.uri(), QUrl("https://www.kde.org"));
0136         QCOMPARE(a.data(), QByteArray());
0137         QCOMPARE(a.label(), QStringLiteral("baz"));
0138         QCOMPARE(a.mimeType(), QStringLiteral("text/plain"));
0139         QCOMPARE(a.iconName(), QStringLiteral("text"));
0140 
0141         a.setUri(QUrl());
0142         QVERIFY(!a.isValid());
0143         QVERIFY(!a.isUri());
0144         QCOMPARE(a.uri(), QUrl());
0145         QCOMPARE(a.data(), QByteArray());
0146         QCOMPARE(a.label(), QStringLiteral("baz"));
0147         QCOMPARE(a.mimeType(), QStringLiteral("text/plain"));
0148         QCOMPARE(a.iconName(), QStringLiteral("text"));
0149     }
0150 
0151     void shouldNotifyStatusChanges()
0152     {
0153         Task t;
0154         QSignalSpy spy(&t, &Task::doneChanged);
0155         t.setDone(true);
0156         QCOMPARE(spy.count(), 1);
0157         QCOMPARE(spy.first().first().toBool(), true);
0158     }
0159 
0160     void shouldNotNotifyIdenticalStatusChanges()
0161     {
0162         Task t;
0163         t.setDone(true);
0164         QSignalSpy spy(&t, &Task::doneChanged);
0165         t.setDone(true);
0166         QCOMPARE(spy.count(), 0);
0167     }
0168 
0169     void shouldNotifyStartDateChanges()
0170     {
0171         Task t;
0172         QSignalSpy spy(&t, &Task::startDateChanged);
0173         t.setStartDate(QDate(2014, 1, 13));
0174         QCOMPARE(spy.count(), 1);
0175         QCOMPARE(spy.first().first().toDate(), QDate(2014, 1, 13));
0176     }
0177 
0178     void shouldNotNotifyIdenticalStartDateChanges()
0179     {
0180         Task t;
0181         t.setStartDate(QDate(2014, 1, 13));
0182         QSignalSpy spy(&t, &Task::startDateChanged);
0183         t.setStartDate(QDate(2014, 1, 13));
0184         QCOMPARE(spy.count(), 0);
0185     }
0186 
0187     void shouldNotifyDueDateChanges()
0188     {
0189         Task t;
0190         QSignalSpy spy(&t, &Task::dueDateChanged);
0191         t.setDueDate(QDate(2014, 1, 13));
0192         QCOMPARE(spy.count(), 1);
0193         QCOMPARE(spy.first().first().toDate(), QDate(2014, 1, 13));
0194     }
0195 
0196     void shouldNotNotifyIdenticalDueDateChanges()
0197     {
0198         Task t;
0199         t.setDueDate(QDate(2014, 1, 13));
0200         QSignalSpy spy(&t, &Task::dueDateChanged);
0201         t.setDueDate(QDate(2014, 1, 13));
0202         QCOMPARE(spy.count(), 0);
0203     }
0204 
0205     void shouldNotifyRecurrenceChanges()
0206     {
0207         Task t;
0208         QSignalSpy spy(&t, &Task::recurrenceChanged);
0209         t.setRecurrence(Task::RecursWeekly);
0210         QCOMPARE(spy.count(), 1);
0211         QCOMPARE(spy.first().first().value<Task::Recurrence>(), Task::RecursWeekly);
0212     }
0213 
0214     void shouldNotNotifyIdenticalRecurrenceChanges()
0215     {
0216         Task t;
0217         t.setRecurrence(Task::RecursWeekly);
0218         QSignalSpy spy(&t, &Task::recurrenceChanged);
0219         t.setRecurrence(Task::RecursWeekly);
0220         QCOMPARE(spy.count(), 0);
0221     }
0222 
0223     void shouldNotifyAttachmentsChanges()
0224     {
0225         Task::Attachments attachments;
0226         attachments.append(Task::Attachment(QByteArrayLiteral("foobar")));
0227         attachments.append(Task::Attachment(QUrl("https://www.kde.org")));
0228 
0229         Task t;
0230         QSignalSpy spy(&t, &Task::attachmentsChanged);
0231         t.setAttachments(attachments);
0232         QCOMPARE(spy.count(), 1);
0233         QCOMPARE(spy.first().first().value<Task::Attachments>(), attachments);
0234     }
0235 
0236     void shouldNotNotifyIdenticalAttachmentsChanges()
0237     {
0238         Task::Attachments attachments;
0239         attachments.append(Task::Attachment(QByteArrayLiteral("foobar")));
0240         attachments.append(Task::Attachment(QUrl("https://www.kde.org")));
0241 
0242         Task t;
0243         t.setAttachments(attachments);
0244         QSignalSpy spy(&t, &Task::attachmentsChanged);
0245         t.setAttachments(attachments);
0246         QCOMPARE(spy.count(), 0);
0247     }
0248 
0249     void shouldNotifyDoneDateChanges()
0250     {
0251         Task t;
0252         QSignalSpy spy(&t, &Task::doneDateChanged);
0253         t.setDoneDate(QDate(2014, 1, 13));
0254         QCOMPARE(spy.count(), 1);
0255         QCOMPARE(spy.first().first().toDate(), QDate(2014, 1, 13));
0256     }
0257 
0258     void shouldNotNotifyIdenticalDoneDateChanges()
0259     {
0260         Task t;
0261         t.setDoneDate(QDate(2014, 1, 13));
0262         QSignalSpy spy(&t, &Task::doneDateChanged);
0263         t.setDoneDate(QDate(2014, 1, 13));
0264         QCOMPARE(spy.count(), 0);
0265     }
0266 
0267     void shouldNotifyDoneDateSet()
0268     {
0269         Task t;
0270         QSignalSpy spy(&t, &Task::doneDateChanged);
0271         t.setDone(true);
0272         QCOMPARE(spy.count(), 1);
0273         QCOMPARE(spy.takeFirst().at(0).toDate(), Utils::DateTime::currentDate());
0274     }
0275 
0276     void shouldNotifyDoneDateUnset()
0277     {
0278         Task t;
0279 
0280         t.setDone(true);
0281         QSignalSpy spy(&t, &Task::doneDateChanged);
0282         t.setDone(false);
0283         QCOMPARE(spy.count(), 1);
0284         QCOMPARE(spy.takeFirst().at(0).toDate(), QDate());
0285     }
0286 };
0287 
0288 ZANSHIN_TEST_MAIN(TaskTest)
0289 
0290 #include "tasktest.moc"