File indexing completed on 2024-12-01 09:55:45

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