101 lines
3.7 KiB
C++
101 lines
3.7 KiB
C++
#include <QtTest>
|
|
#include <QSpinBox>
|
|
#include <QPushButton>
|
|
#include <QLabel>
|
|
#include <QCheckBox>
|
|
#include "countdown.h"
|
|
#include "mainwindow.h"
|
|
#include <QApplication>
|
|
#include <QDir>
|
|
#include "appearance.h"
|
|
|
|
static void savePreview(QWidget &window, const QString &fileName)
|
|
{
|
|
const QString folder = QString::fromLocal8Bit(qgetenv("QT_TEACHING_SCREENSHOTS"));
|
|
if (folder.isEmpty()) return;
|
|
QDir().mkpath(folder);
|
|
window.show();
|
|
QApplication::processEvents();
|
|
QVERIFY(window.grab().save(QDir(folder).filePath(fileName)));
|
|
}
|
|
|
|
class TimerTest : public QObject {
|
|
Q_OBJECT
|
|
private slots:
|
|
void initTestCase() { applyAppearance(*qApp); }
|
|
void pauseResumeAndDrift()
|
|
{
|
|
Countdown model(10000);
|
|
model.start(100);
|
|
model.start(500); // 重复开始不能重新计时。
|
|
QCOMPARE(model.remaining(2600), qint64(7500));
|
|
model.pause(2600);
|
|
QCOMPARE(model.remaining(90000), qint64(7500));
|
|
QVERIFY(!model.setDuration(2000));
|
|
model.start(100000);
|
|
QCOMPARE(model.remaining(102500), qint64(5000));
|
|
model.update(107500);
|
|
QVERIFY(model.state() == Countdown::State::Finished);
|
|
QCOMPARE(model.remaining(107500), qint64(0));
|
|
model.start(200000);
|
|
QCOMPARE(model.remaining(200000), qint64(10000));
|
|
}
|
|
void delayedRefreshAndReset()
|
|
{
|
|
Countdown model(5000);
|
|
model.start(0);
|
|
model.update(100);
|
|
model.update(4300);
|
|
QCOMPARE(model.remaining(4300), qint64(700));
|
|
model.update(9999);
|
|
QCOMPARE(model.remaining(9999), qint64(0));
|
|
QVERIFY(model.state() == Countdown::State::Finished);
|
|
model.reset();
|
|
QVERIFY(model.state() == Countdown::State::Ready);
|
|
QCOMPARE(model.remaining(9999), qint64(5000));
|
|
}
|
|
void zeroAndDeadlinePause()
|
|
{
|
|
Countdown model(0);
|
|
model.start(0);
|
|
QVERIFY(model.state() == Countdown::State::Ready);
|
|
QVERIFY(!model.setDuration(-1));
|
|
QVERIFY(model.setDuration(1000));
|
|
model.start(10);
|
|
model.pause(1010);
|
|
QVERIFY(model.state() == Countdown::State::Finished);
|
|
}
|
|
void uiWorkflow()
|
|
{
|
|
MainWindow w; w.show();
|
|
auto *start = w.findChild<QPushButton *>(QStringLiteral("startButton"));
|
|
auto *pause = w.findChild<QPushButton *>(QStringLiteral("pauseButton"));
|
|
auto *reset = w.findChild<QPushButton *>(QStringLiteral("resetButton"));
|
|
auto *minutes = w.findChild<QSpinBox *>(QStringLiteral("minutesSpin"));
|
|
auto *seconds = w.findChild<QSpinBox *>(QStringLiteral("secondsSpin"));
|
|
auto *time = w.findChild<QLabel *>(QStringLiteral("timeLabel"));
|
|
w.findChild<QCheckBox *>(QStringLiteral("soundCheck"))->setChecked(false);
|
|
savePreview(w, QStringLiteral("02_timer.png"));
|
|
minutes->setValue(0); seconds->setValue(1);
|
|
QTest::mouseClick(start, Qt::LeftButton);
|
|
QVERIFY(!minutes->isEnabled());
|
|
QTest::mouseClick(pause, Qt::LeftButton);
|
|
QCOMPARE(start->text(), QStringLiteral("继续"));
|
|
const QString pausedText = time->text();
|
|
QTest::qWait(150);
|
|
QCOMPARE(time->text(), pausedText);
|
|
QTest::mouseClick(start, Qt::LeftButton);
|
|
QTRY_COMPARE(time->text(), QStringLiteral("00:00:00"));
|
|
QVERIFY(w.findChild<QLabel *>(QStringLiteral("statusLabel"))->text().contains(QStringLiteral("时间到")));
|
|
QVERIFY(!pause->isEnabled());
|
|
QTest::mouseClick(reset, Qt::LeftButton);
|
|
QCOMPARE(time->text(), QStringLiteral("00:00:01"));
|
|
QVERIFY(minutes->isEnabled());
|
|
seconds->setValue(0);
|
|
QVERIFY(!start->isEnabled());
|
|
}
|
|
};
|
|
QTEST_MAIN(TimerTest)
|
|
#include "test_ClassroomTimer.moc"
|
|
|