From 978042b5827db74a088d4230abb6e9d9c7d054b5 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Wed, 10 Jul 2024 14:02:52 +0200 Subject: [PATCH] CppEditor: Use a timeout in waitForRehighlightedSemanticDocument() As in all the other wait() functions. This makes sure we never hang indefinitely in tests. Change-Id: I119cc0d6d6146f193ffde37207a94f61b3c70d87 Reviewed-by: Christian Stenger --- src/plugins/cppeditor/cppdoxygen_test.cpp | 2 +- src/plugins/cppeditor/cpptoolstestcase.cpp | 12 ++++++++++-- src/plugins/cppeditor/cpptoolstestcase.h | 2 +- src/plugins/cppeditor/cppuseselections_test.cpp | 2 +- src/plugins/cppeditor/fileandtokenactions_test.cpp | 2 +- .../followsymbol_switchmethoddecldef_test.cpp | 2 +- .../cppeditor/quickfixes/cppquickfix_test.cpp | 2 +- 7 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/plugins/cppeditor/cppdoxygen_test.cpp b/src/plugins/cppeditor/cppdoxygen_test.cpp index 2d85345303a..8ef6a6fff0f 100644 --- a/src/plugins/cppeditor/cppdoxygen_test.cpp +++ b/src/plugins/cppeditor/cppdoxygen_test.cpp @@ -488,7 +488,7 @@ void DoxygenTest::runTest(const QByteArray &original, // testDocument.m_editorWidget->unfoldAll(); testDocument.m_editor->setCursorPosition(testDocument.m_cursorPosition); - TestCase::waitForRehighlightedSemanticDocument(testDocument.m_editorWidget); + QVERIFY(TestCase::waitForRehighlightedSemanticDocument(testDocument.m_editorWidget)); // Send 'ENTER' key press QKeyEvent event(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier); diff --git a/src/plugins/cppeditor/cpptoolstestcase.cpp b/src/plugins/cppeditor/cpptoolstestcase.cpp index 70435abe9f9..3ac2feeca38 100644 --- a/src/plugins/cppeditor/cpptoolstestcase.cpp +++ b/src/plugins/cppeditor/cpptoolstestcase.cpp @@ -295,10 +295,18 @@ bool TestCase::waitForProcessedEditorDocument(const FilePath &filePath, int time return waitForProcessedEditorDocument_internal(editorDocument, timeOutInMs); } -CPlusPlus::Document::Ptr TestCase::waitForRehighlightedSemanticDocument(CppEditorWidget *editorWidget) +CPlusPlus::Document::Ptr TestCase::waitForRehighlightedSemanticDocument( + CppEditorWidget *editorWidget, int timeoutInMs) { - while (!editorWidget->isSemanticInfoValid()) + QElapsedTimer timer; + timer.start(); + + while (!editorWidget->isSemanticInfoValid()) { + if (timer.elapsed() >= timeoutInMs) + return {}; QCoreApplication::processEvents(); + QThread::msleep(20); + } return editorWidget->semanticInfo().doc; } diff --git a/src/plugins/cppeditor/cpptoolstestcase.h b/src/plugins/cppeditor/cpptoolstestcase.h index 6e9f0133ba2..6439075efbd 100644 --- a/src/plugins/cppeditor/cpptoolstestcase.h +++ b/src/plugins/cppeditor/cpptoolstestcase.h @@ -140,7 +140,7 @@ public: const Utils::FilePath &filePath, int timeOutInMs = 5000); static CPlusPlus::Document::Ptr waitForRehighlightedSemanticDocument( - CppEditorWidget *editorWidget); + CppEditorWidget *editorWidget, int timeoutInMs = defaultTimeOutInMs); enum { defaultTimeOutInMs = 30 * 1000 /*= 30 secs*/ }; static bool waitUntilProjectIsFullyOpened(ProjectExplorer::Project *project, diff --git a/src/plugins/cppeditor/cppuseselections_test.cpp b/src/plugins/cppeditor/cppuseselections_test.cpp index 460e502c8fe..85ed9be442e 100644 --- a/src/plugins/cppeditor/cppuseselections_test.cpp +++ b/src/plugins/cppeditor/cppuseselections_test.cpp @@ -77,7 +77,7 @@ UseSelectionsTestCase::UseSelectionsTestCase(CppTestDocument &testFile, closeEditorAtEndOfTestCase(testFile.m_editor); testFile.m_editor->setCursorPosition(testFile.m_cursorPosition); - waitForRehighlightedSemanticDocument(m_editorWidget); + QVERIFY(waitForRehighlightedSemanticDocument(m_editorWidget)); bool hasTimedOut; const SelectionList selections = waitForUseSelections(&hasTimedOut); diff --git a/src/plugins/cppeditor/fileandtokenactions_test.cpp b/src/plugins/cppeditor/fileandtokenactions_test.cpp index 7c8175fa188..9e9a7a2f29f 100644 --- a/src/plugins/cppeditor/fileandtokenactions_test.cpp +++ b/src/plugins/cppeditor/fileandtokenactions_test.cpp @@ -164,7 +164,7 @@ TestActionsTestCase::TestActionsTestCase(const Actions &tokenActions, const Acti QVERIFY(CppModelManager::workingCopy().get(filePath)); // Rehighlight - waitForRehighlightedSemanticDocument(editorWidget); + QVERIFY(waitForRehighlightedSemanticDocument(editorWidget)); // Run all file actions executeActionsOnEditorWidget(editorWidget, fileActions); diff --git a/src/plugins/cppeditor/followsymbol_switchmethoddecldef_test.cpp b/src/plugins/cppeditor/followsymbol_switchmethoddecldef_test.cpp index 41dd6b538f6..3d19a84b758 100644 --- a/src/plugins/cppeditor/followsymbol_switchmethoddecldef_test.cpp +++ b/src/plugins/cppeditor/followsymbol_switchmethoddecldef_test.cpp @@ -310,7 +310,7 @@ F2TestCase::F2TestCase(CppEditorAction action, // Rehighlight if (!useClangd) - waitForRehighlightedSemanticDocument(testFile->m_editorWidget); + QVERIFY(waitForRehighlightedSemanticDocument(testFile->m_editorWidget)); } // Activate editor of initial test file diff --git a/src/plugins/cppeditor/quickfixes/cppquickfix_test.cpp b/src/plugins/cppeditor/quickfixes/cppquickfix_test.cpp index 2bde0a10212..f487764101b 100644 --- a/src/plugins/cppeditor/quickfixes/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/quickfixes/cppquickfix_test.cpp @@ -104,7 +104,7 @@ BaseQuickFixTestCase::BaseQuickFixTestCase(const QList &testDoc } // Rehighlight - waitForRehighlightedSemanticDocument(document->m_editorWidget); + QVERIFY(waitForRehighlightedSemanticDocument(document->m_editorWidget)); } // Enforce the default cpp code style, so we are independent of config file settings.