| 1 |
// Copyright (C) 2024 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include <QtTest/QtTest> |
| 5 |
|
| 6 |
#include <QtHelp/QHelpEngineCore> |
| 7 |
#include <QtQmlLS/private/qqmllshelputils_p.h> |
| 8 |
|
| 9 |
// TODO (Qt 7.0) |
| 10 |
// Remove this test as well as the plugin from QtTools when the QtHelp lib |
| 11 |
// is split into QtHelpCore and QtHelp. Then QmlLS can depend only on QtHelpCore. |
| 12 |
class tst_HelpEnginePlugin : public QObject |
| 13 |
{ |
| 14 |
Q_OBJECT |
| 15 |
private slots: |
| 16 |
void initTestCase(); |
| 17 |
void documentationForItem_data(); |
| 18 |
void documentationForItem(); |
| 19 |
|
| 20 |
private: |
| 21 |
HelpManager helpManager; |
| 22 |
}; |
| 23 |
|
| 24 |
void tst_HelpEnginePlugin::initTestCase() |
| 25 |
{ |
| 26 |
helpManager.setDocumentationRootPath(QStringLiteral(DATADIR)); |
| 27 |
} |
| 28 |
|
| 29 |
void tst_HelpEnginePlugin::documentationForItem_data() |
| 30 |
{ |
| 31 |
using namespace QQmlJS::Dom; |
| 32 |
QTest::addColumn<DomItem>("fileItem"); |
| 33 |
QTest::addColumn<QLspSpecification::Position>("hoveredPosition"); |
| 34 |
QTest::addColumn<QByteArray>("expectedDocumentation"); |
| 35 |
const auto fileObject = [](const QString &filePath) { |
| 36 |
QFile f(filePath); |
| 37 |
DomItem file; |
| 38 |
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) |
| 39 |
return file; |
| 40 |
QString code = f.readAll(); |
| 41 |
DomCreationOption option = DomCreationOption::Extended; |
| 42 |
QStringList dirs = { QLibraryInfo::path(QLibraryInfo::Qml2ImportsPath) }; |
| 43 |
auto envPtr = DomEnvironment::create( |
| 44 |
dirs, |
| 45 |
QQmlJS::Dom::DomEnvironment::Option::SingleThreaded |
| 46 |
| QQmlJS::Dom::DomEnvironment::Option::NoDependencies, |
| 47 |
option); |
| 48 |
envPtr->loadBuiltins(); |
| 49 |
envPtr->loadFile(FileToLoad::fromMemory(envPtr, filePath, code), |
| 50 |
[&file](Path, const DomItem &, const DomItem &newIt) { |
| 51 |
file = newIt.fileObject(); |
| 52 |
}); |
| 53 |
envPtr->loadPendingDependencies(); |
| 54 |
return file; |
| 55 |
}; |
| 56 |
const auto filePath = QStringLiteral(DATADIR) + "/elements.qml"_L1; |
| 57 |
const auto fileItem = fileObject(filePath); |
| 58 |
QTest::addRow("qmlObjectHoveredAtBegin") << fileItem << QLspSpecification::Position{ 7, 4 } |
| 59 |
<< QByteArray("Encapsulates a QML component definition."); |
| 60 |
QTest::addRow("qmlObjectHoveredAtEnd") << fileItem << QLspSpecification::Position{ 7, 13 } |
| 61 |
<< QByteArray("Encapsulates a QML component definition."); |
| 62 |
QTest::addRow("propertyBinding") << fileItem << QLspSpecification::Position{ 15, 15 } |
| 63 |
<< QByteArray("Sets the interval between triggers, in milliseconds."); |
| 64 |
QTest::addRow("propertyTypeIsSameAsName") << fileItem << QLspSpecification::Position{10, 35 } |
| 65 |
<< QByteArray("The component URL. This is the URL that was used to construct the component."); |
| 66 |
QTest::addRow("method") << fileItem << QLspSpecification::Position{ 16, 26 } |
| 67 |
<< QByteArray("Restarts the timer"); |
| 68 |
} |
| 69 |
|
| 70 |
void tst_HelpEnginePlugin::documentationForItem() |
| 71 |
{ |
| 72 |
using namespace QQmlJS::Dom; |
| 73 |
QFETCH(DomItem, fileItem); |
| 74 |
QFETCH(QLspSpecification::Position, hoveredPosition); |
| 75 |
QFETCH(QByteArray, expectedDocumentation); |
| 76 |
HelpManager helpManager; |
| 77 |
helpManager.setDocumentationRootPath(QStringLiteral(DATADIR)); |
| 78 |
const auto actual = helpManager.documentationForItem(fileItem, hoveredPosition); |
| 79 |
QVERIFY(actual.has_value()); |
| 80 |
QCOMPARE(actual.value(), expectedDocumentation); |
| 81 |
} |
| 82 |
|
| 83 |
QTEST_MAIN(tst_HelpEnginePlugin) |
| 84 |
#include "tst_helpengineplugin.moc" |