File indexing completed on 2024-04-28 15:31:52

0001 /*
0002     SPDX-FileCopyrightText: 2014 Dominik Haumann <dhaumann@kde.org>
0003     SPDX-FileCopyrightText: 2017 Friedrich W. H. Kossebau <kossebau@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "kmessagewidgetautotest.h"
0009 
0010 #include <QSignalSpy>
0011 #include <QTest>
0012 #include <kmessagewidget.h>
0013 
0014 QTEST_MAIN(KMessageWidgetTest)
0015 
0016 // KMessageWidget is currently hardcoded to a 500 ms timeline and default QTimeLine 40 ms update interval
0017 // let's have 7 updates for now, should be save
0018 const int overlappingWaitingTime = 280;
0019 
0020 // clang-format off
0021 #define CHECK_FULLY_VISIBLE \
0022     QVERIFY(w.isVisible()); \
0023     QCOMPARE(w.height(), w.sizeHint().height()); \
0024 
0025 #define CHECK_FULLY_NOT_VISIBLE \
0026     QCOMPARE(w.height(), 0); \
0027     QVERIFY(!w.isVisible());
0028 // clang-format on
0029 
0030 void KMessageWidgetTest::testAnimationSignals()
0031 {
0032     KMessageWidget w(QStringLiteral("Hello world"));
0033 
0034     QSignalSpy showSignalsSpy(&w, &KMessageWidget::showAnimationFinished);
0035     QSignalSpy hideSignalsSpy(&w, &KMessageWidget::hideAnimationFinished);
0036 
0037     QCOMPARE(showSignalsSpy.count(), 0);
0038 
0039     //
0040     // test: showing the message widget should emit showAnimationFinished()
0041     // exactly once after the show animation is finished
0042     //
0043     w.animatedShow();
0044 
0045     QTRY_VERIFY(!w.isShowAnimationRunning());
0046     QCOMPARE(showSignalsSpy.count(), 1);
0047     CHECK_FULLY_VISIBLE
0048 
0049     //
0050     // test: hiding the message widget should emit hideAnimationFinished()
0051     // exactly once after the show animation is finished
0052     //
0053     w.animatedHide();
0054 
0055     QTRY_VERIFY(!w.isHideAnimationRunning());
0056     QCOMPARE(hideSignalsSpy.count(), 1);
0057     CHECK_FULLY_NOT_VISIBLE
0058 }
0059 
0060 void KMessageWidgetTest::testShowOnVisible()
0061 {
0062     KMessageWidget w(QStringLiteral("Hello world"));
0063     QSignalSpy showSignalsSpy(&w, &KMessageWidget::showAnimationFinished);
0064     w.show();
0065     CHECK_FULLY_VISIBLE
0066 
0067     // test: call show on visible
0068     w.animatedShow();
0069 
0070     QTRY_VERIFY(!w.isShowAnimationRunning());
0071     QCOMPARE(showSignalsSpy.count(), 1);
0072     CHECK_FULLY_VISIBLE
0073 }
0074 
0075 void KMessageWidgetTest::testHideOnInvisible()
0076 {
0077     KMessageWidget w(QStringLiteral("Hello world"));
0078     QSignalSpy hideSignalsSpy(&w, &KMessageWidget::hideAnimationFinished);
0079 
0080     // test: call hide on invisible
0081     w.animatedHide();
0082 
0083     QTRY_VERIFY(!w.isHideAnimationRunning());
0084     QCOMPARE(hideSignalsSpy.count(), 1);
0085     QVERIFY(!w.isVisible()); // Not CHECK_FULLY_NOT_VISIBLE because since it was never shown height is not what it would be otherwise
0086 }
0087 
0088 void KMessageWidgetTest::testOverlappingShowAndHide_data()
0089 {
0090     QTest::addColumn<bool>("delay");
0091     QTest::newRow("instant") << false;
0092     QTest::newRow("delay") << true;
0093 }
0094 
0095 void KMessageWidgetTest::testOverlappingShowAndHide()
0096 {
0097     QFETCH(bool, delay);
0098 
0099     KMessageWidget w(QStringLiteral("Hello world"));
0100 
0101     QSignalSpy showSignalsSpy(&w, &KMessageWidget::showAnimationFinished);
0102     QSignalSpy hideSignalsSpy(&w, &KMessageWidget::hideAnimationFinished);
0103 
0104     // test: call hide while show animation still running
0105     w.animatedShow();
0106     QVERIFY(w.isVisible()); // Not CHECK_FULLY_VISIBLE because we're not waiting for it to finish
0107     if (delay) {
0108         QTest::qWait(overlappingWaitingTime);
0109     }
0110     w.animatedHide();
0111 
0112     QVERIFY(!w.isShowAnimationRunning());
0113     QCOMPARE(showSignalsSpy.count(), 1);
0114 
0115     QTRY_VERIFY(!w.isHideAnimationRunning());
0116     QCOMPARE(hideSignalsSpy.count(), 1);
0117     CHECK_FULLY_NOT_VISIBLE
0118 }
0119 
0120 void KMessageWidgetTest::testOverlappingHideAndShow_data()
0121 {
0122     QTest::addColumn<bool>("delay");
0123     QTest::newRow("instant") << false;
0124     QTest::newRow("delay") << true;
0125 }
0126 
0127 void KMessageWidgetTest::testOverlappingHideAndShow()
0128 {
0129     QFETCH(bool, delay);
0130 
0131     KMessageWidget w(QStringLiteral("Hello world"));
0132     QSignalSpy showSignalsSpy(&w, &KMessageWidget::showAnimationFinished);
0133     QSignalSpy hideSignalsSpy(&w, &KMessageWidget::hideAnimationFinished);
0134     w.show();
0135     CHECK_FULLY_VISIBLE
0136 
0137     // test: call show while hide animation still running
0138     w.animatedHide();
0139     if (delay) {
0140         QTest::qWait(overlappingWaitingTime);
0141     }
0142     w.animatedShow();
0143 
0144     QVERIFY(!w.isHideAnimationRunning());
0145     QCOMPARE(hideSignalsSpy.count(), 1);
0146 
0147     QTRY_VERIFY(!w.isShowAnimationRunning());
0148     QCOMPARE(showSignalsSpy.count(), 1);
0149     CHECK_FULLY_VISIBLE
0150 }
0151 
0152 void KMessageWidgetTest::testOverlappingDoubleShow_data()
0153 {
0154     QTest::addColumn<bool>("delay");
0155     QTest::newRow("instant") << false;
0156     QTest::newRow("delay") << true;
0157 }
0158 
0159 void KMessageWidgetTest::testOverlappingDoubleShow()
0160 {
0161     QFETCH(bool, delay);
0162 
0163     KMessageWidget w(QStringLiteral("Hello world"));
0164     QSignalSpy showSignalsSpy(&w, &KMessageWidget::showAnimationFinished);
0165 
0166     // test: call show while show animation still running
0167     w.animatedShow();
0168     if (delay) {
0169         QTest::qWait(overlappingWaitingTime);
0170     }
0171     w.animatedShow();
0172 
0173     QTRY_VERIFY(!w.isShowAnimationRunning());
0174     QCOMPARE(showSignalsSpy.count(), 1);
0175     CHECK_FULLY_VISIBLE
0176 }
0177 
0178 void KMessageWidgetTest::testOverlappingDoubleHide_data()
0179 {
0180     QTest::addColumn<bool>("delay");
0181     QTest::newRow("instant") << false;
0182     QTest::newRow("delay") << true;
0183 }
0184 
0185 void KMessageWidgetTest::testOverlappingDoubleHide()
0186 {
0187     QFETCH(bool, delay);
0188 
0189     KMessageWidget w(QStringLiteral("Hello world"));
0190     QSignalSpy hideSignalsSpy(&w, &KMessageWidget::hideAnimationFinished);
0191     w.show();
0192 
0193     // test: call hide while hide animation still running
0194     w.animatedHide();
0195     if (delay) {
0196         QTest::qWait(overlappingWaitingTime);
0197     }
0198     w.animatedHide();
0199 
0200     QTRY_VERIFY(!w.isHideAnimationRunning());
0201     QCOMPARE(hideSignalsSpy.count(), 1);
0202     CHECK_FULLY_NOT_VISIBLE
0203 }
0204 
0205 void KMessageWidgetTest::testHideWithNotYetShownParent()
0206 {
0207     QWidget parent;
0208     KMessageWidget w(QStringLiteral("Hello world"), &parent);
0209     QSignalSpy hideSignalsSpy(&w, &KMessageWidget::hideAnimationFinished);
0210 
0211     // test: call hide while not yet visible
0212     w.animatedHide();
0213 
0214     QTRY_VERIFY(!w.isHideAnimationRunning());
0215     parent.show();
0216 
0217     QCOMPARE(hideSignalsSpy.count(), 1);
0218     QVERIFY(!w.isVisible()); // Not CHECK_FULLY_NOT_VISIBLE because since it was never shown height is not what it would be otherwise
0219 }
0220 
0221 void KMessageWidgetTest::testNonAnimatedShowAfterAnimatedHide()
0222 {
0223     KMessageWidget w(QStringLiteral("Hello world"));
0224     QSignalSpy hideSignalsSpy(&w, &KMessageWidget::hideAnimationFinished);
0225 
0226     w.show();
0227     CHECK_FULLY_VISIBLE
0228 
0229     w.animatedHide();
0230 
0231     QTRY_VERIFY(!w.isHideAnimationRunning());
0232 
0233     QCOMPARE(hideSignalsSpy.count(), 1);
0234     CHECK_FULLY_NOT_VISIBLE
0235 
0236     w.show();
0237     CHECK_FULLY_VISIBLE
0238 }
0239 
0240 void KMessageWidgetTest::testResizeFlickerOnAnimatedShow()
0241 {
0242     KMessageWidget w(QStringLiteral("Hello world"));
0243 
0244     w.animatedShow();
0245 
0246     QCOMPARE(w.height(), 0);
0247 }
0248 
0249 #include "moc_kmessagewidgetautotest.cpp"