File indexing completed on 2025-02-23 05:00:20
0001 /* 0002 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 #include "webengineaccesskeyanchorfromhtmltest.h" 0007 #include "../webengineaccesskeyutils.h" 0008 #include <QHBoxLayout> 0009 #include <QSignalSpy> 0010 #include <QStandardPaths> 0011 #include <QTest> 0012 #include <QWebEngineView> 0013 #include <WebEngineViewer/WebEngineManageScript> 0014 0015 template<typename Arg, typename R, typename C> 0016 struct InvokeWrapper { 0017 R *receiver; 0018 void (C::*memberFunction)(Arg); 0019 void operator()(Arg result) 0020 { 0021 (receiver->*memberFunction)(result); 0022 } 0023 }; 0024 0025 template<typename Arg, typename R, typename C> 0026 0027 InvokeWrapper<Arg, R, C> invoke(R *receiver, void (C::*memberFunction)(Arg)) 0028 { 0029 InvokeWrapper<Arg, R, C> wrapper = {receiver, memberFunction}; 0030 return wrapper; 0031 } 0032 0033 TestWebEngineAccessKey::TestWebEngineAccessKey(QWidget *parent) 0034 : QWidget(parent) 0035 { 0036 auto hbox = new QHBoxLayout(this); 0037 mEngineView = new QWebEngineView(this); 0038 connect(mEngineView, &QWebEngineView::loadFinished, this, &TestWebEngineAccessKey::loadFinished); 0039 hbox->addWidget(mEngineView); 0040 } 0041 0042 TestWebEngineAccessKey::~TestWebEngineAccessKey() = default; 0043 0044 void TestWebEngineAccessKey::setHtml(const QString &html) 0045 { 0046 mEngineView->setHtml(html); 0047 } 0048 0049 void TestWebEngineAccessKey::handleSearchAccessKey(const QVariant &var) 0050 { 0051 const QVariantList lst = var.toList(); 0052 QList<WebEngineViewer::WebEngineAccessKeyAnchor> anchorList; 0053 anchorList.reserve(lst.count()); 0054 for (const QVariant &anchor : lst) { 0055 anchorList << WebEngineViewer::WebEngineAccessKeyAnchor(anchor); 0056 } 0057 Q_EMIT accessKeySearchFinished(anchorList); 0058 } 0059 0060 void TestWebEngineAccessKey::loadFinished(bool b) 0061 { 0062 Q_UNUSED(b) 0063 mEngineView->page()->runJavaScript(WebEngineViewer::WebEngineAccessKeyUtils::script(), 0064 WebEngineViewer::WebEngineManageScript::scriptWordId(), 0065 invoke(this, &TestWebEngineAccessKey::handleSearchAccessKey)); 0066 } 0067 0068 Q_DECLARE_METATYPE(QList<WebEngineViewer::WebEngineAccessKeyAnchor>) 0069 0070 WebEngineAccessKeyAnchorFromHtmlTest::WebEngineAccessKeyAnchorFromHtmlTest(QObject *parent) 0071 : QObject(parent) 0072 { 0073 qRegisterMetaType<QList<WebEngineViewer::WebEngineAccessKeyAnchor>>(); 0074 QStandardPaths::setTestModeEnabled(true); 0075 } 0076 0077 void WebEngineAccessKeyAnchorFromHtmlTest::shouldNotShowAccessKeyWhenHtmlAsNotAnchor() 0078 { 0079 TestWebEngineAccessKey w; 0080 QSignalSpy accessKeySpy(&w, &TestWebEngineAccessKey::accessKeySearchFinished); 0081 w.setHtml(QStringLiteral("<body>foo</body>")); 0082 QVERIFY(accessKeySpy.wait()); 0083 QCOMPARE(accessKeySpy.count(), 1); 0084 const auto resultLst = accessKeySpy.at(0).at(0).value<QList<WebEngineViewer::WebEngineAccessKeyAnchor>>(); 0085 QCOMPARE(resultLst.count(), 0); 0086 } 0087 0088 void WebEngineAccessKeyAnchorFromHtmlTest::shouldReturnOneAnchor() 0089 { 0090 TestWebEngineAccessKey w; 0091 QSignalSpy accessKeySpy(&w, &TestWebEngineAccessKey::accessKeySearchFinished); 0092 w.setHtml(QStringLiteral("<body>foo<a href=\"http://www.kde.org\">foo</a></body>")); 0093 QVERIFY(accessKeySpy.wait()); 0094 QCOMPARE(accessKeySpy.count(), 1); 0095 const auto resultLst = accessKeySpy.at(0).at(0).value<QList<WebEngineViewer::WebEngineAccessKeyAnchor>>(); 0096 QCOMPARE(resultLst.count(), 1); 0097 } 0098 0099 void WebEngineAccessKeyAnchorFromHtmlTest::shouldReturnTwoAnchor() 0100 { 0101 TestWebEngineAccessKey w; 0102 QSignalSpy accessKeySpy(&w, &TestWebEngineAccessKey::accessKeySearchFinished); 0103 w.setHtml(QStringLiteral("<body>foo<a href=\"http://www.kde.org\">foo</a><a href=\"http://www.kde.vv\">foo</a></body>")); 0104 QVERIFY(accessKeySpy.wait()); 0105 QCOMPARE(accessKeySpy.count(), 1); 0106 const auto resultLst = accessKeySpy.at(0).at(0).value<QList<WebEngineViewer::WebEngineAccessKeyAnchor>>(); 0107 QCOMPARE(resultLst.count(), 2); 0108 } 0109 0110 QTEST_MAIN(WebEngineAccessKeyAnchorFromHtmlTest) 0111 0112 #include "moc_webengineaccesskeyanchorfromhtmltest.cpp"