File indexing completed on 2024-12-01 05:16:50

0001 /* This file is part of Kairo Timer
0002 
0003    SPDX-FileCopyrightText: 2016 (c) Kevin Ottens <ervin@kde.org>
0004 
0005    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 
0007 */
0008 
0009 #include <QtTest>
0010 #include <QSignalSpy>
0011 
0012 #include <memory>
0013 
0014 #include "timernotificationcontrol.h"
0015 
0016 #include "timercontrol.h"
0017 #include "soundcontrolstub.h"
0018 
0019 class TimerNotificationControlTest : public QObject
0020 {
0021     Q_OBJECT
0022 private slots:
0023     void initTestCase()
0024     {
0025         qRegisterMetaType<TimerControl*>();
0026         qRegisterMetaType<SoundControlInterface*>();
0027     }
0028 
0029     void shouldHaveDefaultState()
0030     {
0031         // GIVEN
0032         auto notification = std::make_unique<TimerNotificationControl>();
0033 
0034         // THEN
0035         QCOMPARE(notification->sound(), static_cast<SoundControlInterface*>(nullptr));
0036         QCOMPARE(notification->timer(), static_cast<TimerControl*>(nullptr));
0037     }
0038 
0039     void shouldHaveSoundControl()
0040     {
0041         // GIVEN
0042         auto notification = std::make_unique<TimerNotificationControl>();
0043         auto sound = new SoundControlStub(notification.get());
0044         auto soundSpy = std::make_unique<QSignalSpy>(notification.get(), &TimerNotificationControl::soundChanged);
0045 
0046         // WHEN
0047         notification->setSound(sound);
0048 
0049         // THEN
0050         QCOMPARE(notification->sound(), sound);
0051         QCOMPARE(soundSpy->size(), 1);
0052         QCOMPARE(soundSpy->takeFirst().first().value<SoundControlInterface*>(), sound);
0053 
0054         // WHEN
0055         notification->setSound(sound);
0056 
0057         // THEN
0058         QCOMPARE(soundSpy->size(), 0);
0059     }
0060 
0061     void shouldHaveTimerControl()
0062     {
0063         // GIVEN
0064         auto notification = std::make_unique<TimerNotificationControl>();
0065         auto timer = new TimerControl(notification.get());
0066         auto timerSpy = std::make_unique<QSignalSpy>(notification.get(), &TimerNotificationControl::timerChanged);
0067 
0068         // WHEN
0069         notification->setTimer(timer);
0070 
0071         // THEN
0072         QCOMPARE(notification->timer(), timer);
0073         QCOMPARE(timerSpy->size(), 1);
0074         QCOMPARE(timerSpy->takeFirst().first().value<TimerControl*>(), timer);
0075 
0076         // WHEN
0077         notification->setTimer(timer);
0078 
0079         // THEN
0080         QCOMPARE(timerSpy->size(), 0);
0081     }
0082 
0083     void shouldPlayLongBeepOnTimerFinished()
0084     {
0085         // GIVEN
0086         auto notification = std::make_unique<TimerNotificationControl>();
0087 
0088         auto sound = new SoundControlStub(notification.get());
0089         notification->setSound(sound);
0090 
0091         auto timer = new TimerControl(notification.get());
0092         notification->setTimer(timer);
0093 
0094         // WHEN
0095         emit timer->timerFinished();
0096 
0097         // THEN
0098         QCOMPARE(sound->calls().size(), 1);
0099         QCOMPARE(sound->calls().first(), SoundControlStub::LongBeep);
0100         sound->clear();
0101 
0102         // WHEN
0103         notification->setTimer(nullptr);
0104         emit timer->timerFinished();
0105 
0106         // THEN
0107         QVERIFY(sound->calls().isEmpty());
0108     }
0109 
0110     void shouldPlayShortBeepOnTimerSkipped()
0111     {
0112         // GIVEN
0113         auto notification = std::make_unique<TimerNotificationControl>();
0114 
0115         auto sound = new SoundControlStub(notification.get());
0116         notification->setSound(sound);
0117 
0118         auto timer = new TimerControl(notification.get());
0119         notification->setTimer(timer);
0120 
0121         // WHEN
0122         emit timer->timerSkipped();
0123 
0124         // THEN
0125         QCOMPARE(sound->calls().size(), 1);
0126         QCOMPARE(sound->calls().first(), SoundControlStub::ShortBeep);
0127         sound->clear();
0128 
0129         // WHEN
0130         notification->setTimer(nullptr);
0131         emit timer->timerSkipped();
0132 
0133         // THEN
0134         QVERIFY(sound->calls().isEmpty());
0135     }
0136 
0137     void shouldPlayShortBeepOnLastTenCountdownPlainSecondsOnly()
0138     {
0139         // GIVEN
0140         auto notification = std::make_unique<TimerNotificationControl>();
0141 
0142         auto sound = new SoundControlStub(notification.get());
0143         notification->setSound(sound);
0144 
0145         auto timer = new TimerControl(notification.get());
0146         timer->setModel({"countdown", 50000});
0147         notification->setTimer(timer);
0148 
0149         // WHEN
0150         emit timer->valueChanged(50000);
0151         emit timer->valueChanged(47550);
0152         emit timer->valueChanged(10100);
0153         emit timer->valueChanged(10010);
0154         emit timer->valueChanged(10000);
0155         emit timer->valueChanged(9990);
0156         emit timer->valueChanged(9010);
0157         emit timer->valueChanged(9000);
0158         emit timer->valueChanged(8990);
0159         emit timer->valueChanged(8000);
0160         emit timer->valueChanged(7000);
0161         emit timer->valueChanged(6000);
0162         emit timer->valueChanged(5000);
0163         emit timer->valueChanged(4000);
0164         emit timer->valueChanged(3000);
0165         emit timer->valueChanged(2000);
0166         emit timer->valueChanged(1000);
0167         emit timer->valueChanged(0);
0168 
0169         // THEN
0170         QCOMPARE(sound->calls().size(), 10);
0171         QCOMPARE(sound->calls().first(), SoundControlStub::ShortBeep);
0172         sound->clear();
0173 
0174         // WHEN
0175         timer->setModel({"timer"});
0176         emit timer->valueChanged(9000);
0177 
0178         // THEN
0179         QVERIFY(sound->calls().isEmpty());
0180 
0181         // WHEN
0182         notification->setTimer(nullptr);
0183         emit timer->valueChanged(9000);
0184 
0185         // THEN
0186         QVERIFY(sound->calls().isEmpty());
0187     }
0188 };
0189 
0190 QTEST_MAIN(TimerNotificationControlTest)
0191 
0192 #include "timernotificationcontroltest.moc"