File indexing completed on 2024-02-25 04:02:33

0001 /*
0002     This file is part of the Kate project.
0003     SPDX-FileCopyrightText: 2013 Dominik Haumann <dhaumann@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "messagetest.h"
0009 
0010 #include <katedocument.h>
0011 #include <kateglobal.h>
0012 #include <katemessagewidget.h>
0013 #include <kateview.h>
0014 #include <ktexteditor/message.h>
0015 
0016 #include <QtTestWidgets>
0017 
0018 using namespace KTextEditor;
0019 
0020 QTEST_MAIN(MessageTest)
0021 
0022 void MessageTest::initTestCase()
0023 {
0024     KTextEditor::EditorPrivate::enableUnitTestMode();
0025 }
0026 
0027 void MessageTest::cleanupTestCase()
0028 {
0029 }
0030 
0031 void MessageTest::testPostMessage()
0032 {
0033     KTextEditor::DocumentPrivate doc;
0034 
0035     KTextEditor::ViewPrivate *view = static_cast<KTextEditor::ViewPrivate *>(doc.createView(nullptr));
0036     view->show();
0037     view->resize(400, 300);
0038 
0039     QPointer<Message> message = new Message(QStringLiteral("Message text"), Message::Information);
0040     message->setPosition(Message::TopInView);
0041 
0042     // posing message should succeed
0043     QVERIFY(doc.postMessage(message));
0044 
0045     //
0046     // show message for one second, then delete again
0047     //
0048     QTest::qWait(500);
0049     QVERIFY(view->messageWidget());
0050     QVERIFY(view->messageWidget()->isVisible());
0051 
0052     QVERIFY(message != nullptr);
0053     delete message;
0054     QTest::qWait(1000); // fadeout animation takes 500 ms
0055     QVERIFY(!view->messageWidget()->isVisible());
0056 }
0057 
0058 void MessageTest::testAutoHide()
0059 {
0060     KTextEditor::DocumentPrivate doc;
0061 
0062     KTextEditor::ViewPrivate *view = static_cast<KTextEditor::ViewPrivate *>(doc.createView(nullptr));
0063     view->show();
0064     view->resize(400, 300);
0065 
0066     //
0067     // show a message with autoHide. Check, if it's deleted correctly
0068     // auto hide mode: Message::Immediate
0069     //
0070     QPointer<Message> message = new Message(QStringLiteral("Message text"), Message::Information);
0071     message->setPosition(Message::TopInView);
0072     message->setAutoHide(1000);
0073     message->setAutoHideMode(Message::Immediate);
0074 
0075     doc.postMessage(message);
0076 
0077     QTest::qWait(500);
0078     QVERIFY(view->messageWidget()->isVisible());
0079 
0080     // should be deleted after 1.5 seconds
0081     QTest::qWait(1000);
0082     QVERIFY(message.data() == nullptr);
0083 
0084     // message widget should be hidden after 2 seconds
0085     QTest::qWait(500);
0086     QVERIFY(!view->messageWidget()->isVisible());
0087 }
0088 
0089 void MessageTest::testAutoHideAfterUserInteraction()
0090 {
0091     KTextEditor::DocumentPrivate doc;
0092 
0093     KTextEditor::ViewPrivate *view = static_cast<KTextEditor::ViewPrivate *>(doc.createView(nullptr));
0094     view->show();
0095     view->resize(400, 300);
0096 
0097     //
0098     // show a message with autoHide. Check, if it's deleted correctly
0099     // auto hide mode: Message::AfterUserInteraction
0100     //
0101     QPointer<Message> message = new Message(QStringLiteral("Message text"), Message::Information);
0102     message->setPosition(Message::TopInView);
0103     message->setAutoHide(2000);
0104     QVERIFY(message->autoHideMode() == Message::AfterUserInteraction);
0105 
0106     doc.postMessage(message);
0107 
0108     QTest::qWait(1000);
0109     QVERIFY(view->messageWidget()->isVisible());
0110 
0111     // now trigger user interaction after 1 second
0112     view->insertText(QStringLiteral("Hello world"));
0113     view->setCursorPosition(Cursor(0, 5));
0114 
0115     // should still be there after deleted after another 1.9 seconds
0116     QTest::qWait(1900);
0117     QVERIFY(message.data() != nullptr);
0118     QVERIFY(view->messageWidget()->isVisible());
0119 
0120     // another 300ms later: 3.2 seconds are gone, message should be deleted
0121     // and fade animation should be active
0122     QTest::qWait(300);
0123     QVERIFY(message.data() == nullptr);
0124     QVERIFY(view->messageWidget()->isVisible());
0125 
0126     // after a total of 3.7 seconds, widget should be hidden
0127     QTest::qWait(500);
0128     QVERIFY(!view->messageWidget()->isVisible());
0129 }
0130 
0131 void MessageTest::testMessageQueue()
0132 {
0133     KTextEditor::DocumentPrivate doc;
0134 
0135     KTextEditor::ViewPrivate *view = static_cast<KTextEditor::ViewPrivate *>(doc.createView(nullptr));
0136     view->show();
0137     view->resize(400, 300);
0138 
0139     //
0140     // add two messages, both with autoHide to 1 second, and check that the queue is processed correctly
0141     // auto hide mode: Message::Immediate
0142     //
0143     QPointer<Message> m1 = new Message(QStringLiteral("Info text"), Message::Information);
0144     m1->setPosition(Message::TopInView);
0145     m1->setAutoHide(1000);
0146     m1->setAutoHideMode(Message::Immediate);
0147 
0148     QPointer<Message> m2 = new Message(QStringLiteral("Error text"), Message::Error);
0149     m2->setPosition(Message::TopInView);
0150     m2->setAutoHide(1000);
0151     m2->setAutoHideMode(Message::Immediate);
0152 
0153     // post both messages
0154     QVERIFY(doc.postMessage(m1));
0155     QVERIFY(doc.postMessage(m2));
0156 
0157     // after 0.5s, first message should be visible, (timer of m1 triggered)
0158     QTest::qWait(500);
0159     QVERIFY(view->messageWidget()->isVisible());
0160     QVERIFY(m1.data() != nullptr);
0161     QVERIFY(m2.data() != nullptr);
0162 
0163     // after 1.2s, first message is deleted, and hide animation is active
0164     QTest::qWait(700);
0165     QVERIFY(view->messageWidget()->isVisible());
0166     QVERIFY(m1.data() == nullptr);
0167     QVERIFY(m2.data() != nullptr);
0168 
0169     // timer of m2 triggered after 1.5s, i.e. after hide animation if finished
0170     QTest::qWait(500);
0171 
0172     // after 2.1s, second message should be visible
0173     QTest::qWait(500);
0174     QVERIFY(view->messageWidget()->isVisible());
0175     QVERIFY(m2.data() != nullptr);
0176 
0177     // after 2.6s, second message is deleted, and hide animation is active
0178     QTest::qWait(500);
0179     QVERIFY(view->messageWidget()->isVisible());
0180     QVERIFY(m2.data() == nullptr);
0181 
0182     // after a total of 3.1s, animation is finished and widget is hidden
0183     QTest::qWait(500);
0184     QVERIFY(!view->messageWidget()->isVisible());
0185 }
0186 
0187 void MessageTest::testPriority()
0188 {
0189     KTextEditor::DocumentPrivate doc;
0190 
0191     KTextEditor::ViewPrivate *view = static_cast<KTextEditor::ViewPrivate *>(doc.createView(nullptr));
0192     view->show();
0193     view->resize(400, 300);
0194 
0195     //
0196     // add two messages
0197     // - m1: no auto hide timer, priority 0
0198     // - m2: auto hide timer of 1 second, priority 1
0199     // test:
0200     // - m1 should be hidden in favour of m2
0201     // - changing text of m1 while m2 is displayed should not change the displayed text
0202     //
0203     QPointer<Message> m1 = new Message(QStringLiteral("m1"), Message::Positive);
0204     m1->setPosition(Message::TopInView);
0205     QVERIFY(m1->priority() == 0);
0206 
0207     QPointer<Message> m2 = new Message(QStringLiteral("m2"), Message::Error);
0208     m2->setPosition(Message::TopInView);
0209     m2->setAutoHide(1000);
0210     m2->setAutoHideMode(Message::Immediate);
0211     m2->setPriority(1);
0212     QVERIFY(m2->priority() == 1);
0213 
0214     // post m1
0215     QVERIFY(doc.postMessage(m1));
0216 
0217     // after 1s, message should be displayed
0218     QTest::qWait(1000);
0219     QVERIFY(view->messageWidget()->isVisible());
0220     QCOMPARE(view->messageWidget()->text(), QStringLiteral("m1"));
0221     QVERIFY(m1.data() != nullptr);
0222 
0223     // post m2, m1 should be hidden, and m2 visible
0224     QVERIFY(doc.postMessage(m2));
0225     QVERIFY(m2.data() != nullptr);
0226 
0227     // alter text of m1 when m2 is visible, shouldn't influence m2
0228     QTest::qWait(600);
0229     m1->setText(QStringLiteral("m1 changed"));
0230 
0231     // after 0.7 seconds, m2 is visible
0232     QTest::qWait(100);
0233     QCOMPARE(view->messageWidget()->text(), QStringLiteral("m2"));
0234     QVERIFY(m2.data() != nullptr);
0235 
0236     // after 1.6 seconds, m2 is hidden again and m1 is visible again
0237     QTest::qWait(900);
0238     QVERIFY(view->messageWidget()->isVisible());
0239     QVERIFY(m1.data() != nullptr);
0240     QVERIFY(m2.data() == nullptr);
0241 
0242     // finally check m1 again
0243     QTest::qWait(1000);
0244     QCOMPARE(view->messageWidget()->text(), QStringLiteral("m1 changed"));
0245 }
0246 
0247 void MessageTest::testCreateView()
0248 {
0249     KTextEditor::DocumentPrivate doc;
0250 
0251     //
0252     // - first post a message
0253     // - then create two views
0254     //
0255     // test:
0256     // - verify that both views get the message
0257     // - verify that, once the message is deleted, both views hide the message
0258     //
0259     QPointer<Message> m1 = new Message(QStringLiteral("message"), Message::Positive);
0260     m1->setPosition(Message::TopInView);
0261     QVERIFY(m1->priority() == 0);
0262 
0263     // first post message to doc without views
0264     QVERIFY(doc.postMessage(m1));
0265 
0266     // now create views
0267     KTextEditor::ViewPrivate *v1 = static_cast<KTextEditor::ViewPrivate *>(doc.createView(nullptr));
0268     KTextEditor::ViewPrivate *v2 = static_cast<KTextEditor::ViewPrivate *>(doc.createView(nullptr));
0269     v1->show();
0270     v2->show();
0271     v1->resize(400, 300);
0272     v2->resize(400, 300);
0273 
0274     // make sure both views show the message
0275     QVERIFY(v1->messageWidget()->isVisible());
0276     QVERIFY(v2->messageWidget()->isVisible());
0277     QCOMPARE(v1->messageWidget()->text(), QStringLiteral("message"));
0278     QCOMPARE(v2->messageWidget()->text(), QStringLiteral("message"));
0279     QVERIFY(m1.data() != nullptr);
0280 
0281     // delete message, then check after fadeout time 0f 0.5s whether message is gone
0282     delete m1;
0283     QTest::qWait(600);
0284     QVERIFY(!v1->messageWidget()->isVisible());
0285     QVERIFY(!v2->messageWidget()->isVisible());
0286 }
0287 
0288 void MessageTest::testHideView()
0289 {
0290     KTextEditor::DocumentPrivate doc;
0291 
0292     KTextEditor::ViewPrivate *view = static_cast<KTextEditor::ViewPrivate *>(doc.createView(nullptr));
0293     view->show();
0294     view->resize(400, 300);
0295 
0296     // create message that hides after 2s immediately
0297     QPointer<Message> message = new Message(QStringLiteral("Message text"), Message::Information);
0298     message->setAutoHide(2000);
0299     message->setAutoHideMode(Message::Immediate);
0300     message->setPosition(Message::TopInView);
0301 
0302     // posting message should succeed
0303     QVERIFY(doc.postMessage(message));
0304 
0305     //
0306     // test:
0307     // - show the message for 1.5s, then hide the view
0308     // - the auto hide timer will continue, no matter what
0309     // - showing the view again after the auto hide timer is finished + animation time really hide the widget
0310     //
0311     QTest::qWait(1100);
0312     QVERIFY(view->messageWidget()->isVisible());
0313     QCOMPARE(view->messageWidget()->text(), QStringLiteral("Message text"));
0314 
0315     // hide view
0316     view->hide();
0317 
0318     // wait 1s, message should be null (after total of 2200 ms)
0319     QTest::qWait(1100);
0320     QVERIFY(message.data() == nullptr);
0321 
0322     // show view again, message contents should be fading for the lasting 300 ms
0323     view->show();
0324     QVERIFY(view->messageWidget()->isVisible());
0325     QCOMPARE(view->messageWidget()->text(), QStringLiteral("Message text"));
0326 
0327     // wait another 0.5s, then message widget should be hidden
0328     QTest::qWait(500);
0329     QVERIFY(message.data() == nullptr);
0330     QVERIFY(!view->messageWidget()->isVisible());
0331 }
0332 
0333 void MessageTest::testHideViewAfterUserInteraction()
0334 {
0335     KTextEditor::DocumentPrivate doc;
0336 
0337     KTextEditor::ViewPrivate *view = static_cast<KTextEditor::ViewPrivate *>(doc.createView(nullptr));
0338     view->show();
0339     view->resize(400, 300);
0340 
0341     // create message that hides after 2s immediately
0342     QPointer<Message> message = new Message(QStringLiteral("Message text"), Message::Information);
0343     message->setAutoHide(2000);
0344     QVERIFY(message->autoHideMode() == Message::AfterUserInteraction);
0345     message->setPosition(Message::TopInView);
0346 
0347     // posting message should succeed
0348     QVERIFY(doc.postMessage(message));
0349 
0350     //
0351     // test:
0352     // - show the message for 1.5s, then hide the view
0353     // - this should stop the autoHide timer
0354     // - showing the view again should restart the autoHide timer (again 2s)
0355     //
0356     QTest::qWait(1500);
0357     QVERIFY(view->messageWidget()->isVisible());
0358     QCOMPARE(view->messageWidget()->text(), QStringLiteral("Message text"));
0359 
0360     // hide view
0361     view->hide();
0362 
0363     // wait 1s, check that message is still valid
0364     QTest::qWait(1000);
0365     QVERIFY(message.data() != nullptr);
0366 
0367     //
0368     // show view again, and trigger user interaction through resize:
0369     // should retrigger the autoHide timer
0370     //
0371     view->show();
0372     QTest::qWait(2000);
0373     view->insertText(QStringLiteral("Hello world"));
0374     view->setCursorPosition(Cursor(0, 5));
0375 
0376     // wait 1.5s and check that message is still displayed
0377     QTest::qWait(1500);
0378     QVERIFY(message.data() != nullptr);
0379     QVERIFY(view->messageWidget()->isVisible());
0380     QCOMPARE(view->messageWidget()->text(), QStringLiteral("Message text"));
0381 
0382     // wait another 0.8s, then the message is deleted
0383     QTest::qWait(800);
0384     QVERIFY(message.data() == nullptr);
0385     QVERIFY(view->messageWidget()->isVisible());
0386 
0387     // another 0.5s, and the message widget should be hidden
0388     QTest::qWait(600);
0389     QVERIFY(!view->messageWidget()->isVisible());
0390 }
0391 
0392 #include "moc_messagetest.cpp"