qt6windows7/tests/auto/testlib/selftests/sleep/tst_sleep.cpp

64 lines
1.1 KiB
C++
Raw Normal View History

2023-10-30 06:33:08 +08:00
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QtCore/QCoreApplication>
#include <QtCore/QElapsedTimer>
#include <QTest>
2023-11-02 05:23:55 +08:00
#ifdef Q_OS_UNIX
#include <QtCore/private/qcore_unix_p.h>
#include <QtCore/qsystemdetection.h>
#include <time.h>
#endif
using namespace std::chrono_literals;
2023-10-30 06:33:08 +08:00
class tst_Sleep: public QObject
{
Q_OBJECT
private slots:
void sleep();
void wait();
};
void tst_Sleep::sleep()
{
QElapsedTimer t;
t.start();
QTest::qSleep(100);
2023-11-02 05:23:55 +08:00
QCOMPARE_GE(t.durationElapsed(), 90ms);
2023-10-30 06:33:08 +08:00
QTest::qSleep(1000);
2023-11-02 05:23:55 +08:00
QCOMPARE_GE(t.durationElapsed(), 1s);
2023-10-30 06:33:08 +08:00
QTest::qSleep(1000 * 10); // 10 seconds
2023-11-02 05:23:55 +08:00
QCOMPARE_GE(t.durationElapsed(), 10s);
2023-10-30 06:33:08 +08:00
}
void tst_Sleep::wait()
{
QElapsedTimer t;
t.start();
2023-11-02 05:23:55 +08:00
t.start();
2023-10-30 06:33:08 +08:00
QTest::qWait(1);
2023-11-02 05:23:55 +08:00
QCOMPARE_GE(t.durationElapsed(), 1ms);
2023-10-30 06:33:08 +08:00
QTest::qWait(10);
2023-11-02 05:23:55 +08:00
QCOMPARE_GE(t.durationElapsed(), 11ms);
2023-10-30 06:33:08 +08:00
QTest::qWait(100);
2023-11-02 05:23:55 +08:00
QCOMPARE_GE(t.durationElapsed(), 111ms);
2023-10-30 06:33:08 +08:00
QTest::qWait(1000);
2023-11-02 05:23:55 +08:00
QCOMPARE_GE(t.durationElapsed(), 1111ms);
2023-10-30 06:33:08 +08:00
}
QTEST_MAIN(tst_Sleep)
#include "tst_sleep.moc"