File indexing completed on 2024-05-19 05:01:15
0001 /* 0002 SPDX-FileCopyrightText: 2016 David Faure <faure@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include <webenginepart.h> 0008 #include "webengine_testutils.h" 0009 0010 #include <KIO/Job> 0011 #include "kf5compat.h" //For NavigationExtension 0012 #include <KPluginMetaData> 0013 0014 #include <QTest> 0015 #include <QObject> 0016 #include <QSignalSpy> 0017 #include <QWebEngineView> 0018 #include <QJsonDocument> 0019 0020 namespace { 0021 KPluginMetaData dummyMetaData() 0022 { 0023 QJsonObject jo = QJsonDocument::fromJson( 0024 "{ \"KPlugin\": {\n" 0025 " \"Id\": \"webenginepart\",\n" 0026 " \"Name\": \"WebEngine\",\n" 0027 " \"Version\": \"0.1\"\n" 0028 "}\n}").object(); 0029 return KPluginMetaData(jo, QString()); 0030 } 0031 } 0032 0033 class WebEnginePartApiTest : public QObject 0034 { 0035 Q_OBJECT 0036 private Q_SLOTS: 0037 void initTestCase(); 0038 void shouldHaveNavigationExtension(); 0039 void shouldEmitStartedAndCompleted(); 0040 void shouldEmitStartAndCompleteWithPendingAction(); 0041 void shouldEmitSetWindowCaption(); 0042 void shouldEmitOpenUrlNotifyOnClick(); 0043 0044 }; 0045 0046 void WebEnginePartApiTest::initTestCase() 0047 { 0048 qRegisterMetaType<KIO::Job *>(); // for the KParts started signal 0049 } 0050 0051 void WebEnginePartApiTest::shouldHaveNavigationExtension() 0052 { 0053 // GIVEN 0054 WebEnginePart part(nullptr, nullptr, dummyMetaData()); 0055 0056 // WHEN 0057 KParts::NavigationExtension *ext = KParts::NavigationExtension::childObject(&part); 0058 0059 // THEN 0060 QVERIFY(ext); 0061 } 0062 0063 void WebEnginePartApiTest::shouldEmitStartedAndCompleted() 0064 { 0065 // GIVEN 0066 WebEnginePart part(nullptr, nullptr, dummyMetaData()); 0067 QSignalSpy spyStarted(&part, &KParts::ReadOnlyPart::started); 0068 QSignalSpy spyCompleted(&part, SIGNAL(completed())); 0069 QSignalSpy spySetWindowCaption(&part, &KParts::ReadOnlyPart::setWindowCaption); 0070 KParts::NavigationExtension *ext = KParts::NavigationExtension::childObject(&part); 0071 QSignalSpy spyOpenUrlNotify(ext, &KParts::NavigationExtension::openUrlNotify); 0072 const QUrl url(QStringLiteral("data:text/html, <p>Hello World</p>")); 0073 0074 // WHEN 0075 part.openUrl(url); 0076 0077 // THEN 0078 QVERIFY(spyStarted.wait()); 0079 QVERIFY(spySetWindowCaption.wait()); 0080 QCOMPARE(spySetWindowCaption.at(0).at(0).toUrl().toString(), url.toString()); 0081 QVERIFY(spyCompleted.wait()); 0082 QVERIFY(spyOpenUrlNotify.isEmpty()); 0083 } 0084 0085 void WebEnginePartApiTest::shouldEmitStartAndCompleteWithPendingAction() 0086 { 0087 // GIVEN 0088 WebEnginePart part(nullptr, nullptr, dummyMetaData()); 0089 QSignalSpy spyStarted(&part, &KParts::ReadOnlyPart::started); 0090 QSignalSpy spyCompleted(&part, SIGNAL(completedWithPendingAction())); 0091 QSignalSpy spySetWindowCaption(&part, &KParts::ReadOnlyPart::setWindowCaption); 0092 KParts::NavigationExtension *ext = KParts::NavigationExtension::childObject(&part); 0093 QSignalSpy spyOpenUrlNotify(ext, &KParts::NavigationExtension::openUrlNotify); 0094 const QUrl url(QStringLiteral("data:text/html, <html><head><meta http-equiv=\"refresh\"><body><p>Hello World</p></body></html>")); 0095 0096 // WHEN 0097 part.openUrl(url); 0098 0099 // THEN 0100 QVERIFY(spyStarted.wait()); 0101 QVERIFY(spySetWindowCaption.wait()); 0102 QCOMPARE(spySetWindowCaption.at(0).at(0).toUrl().toString(), url.toString()); 0103 QVERIFY(spyCompleted.wait()); 0104 QVERIFY(spyOpenUrlNotify.isEmpty()); 0105 } 0106 0107 0108 void WebEnginePartApiTest::shouldEmitSetWindowCaption() 0109 { 0110 // GIVEN 0111 WebEnginePart part(nullptr, nullptr, dummyMetaData()); 0112 QSignalSpy spyStarted(&part, &KParts::ReadOnlyPart::started); 0113 QSignalSpy spyCompleted(&part, SIGNAL(completed())); 0114 QSignalSpy spySetWindowCaption(&part, &KParts::ReadOnlyPart::setWindowCaption); 0115 0116 // WHEN opening a URL with a title tag 0117 part.openUrl(QUrl(QStringLiteral("data:text/html, <title>Custom Title</title><p>Hello World</p>"))); 0118 0119 // THEN 0120 QVERIFY(spyStarted.wait()); 0121 QVERIFY(spyCompleted.wait()); 0122 QCOMPARE(spySetWindowCaption.count(), 2); 0123 QCOMPARE(spySetWindowCaption.at(1).at(0).toUrl().toString(), QStringLiteral("Custom Title")); 0124 } 0125 0126 void WebEnginePartApiTest::shouldEmitOpenUrlNotifyOnClick() 0127 { 0128 // GIVEN 0129 WebEnginePart part(nullptr, nullptr, dummyMetaData()); 0130 QSignalSpy spyStarted(&part, &KParts::ReadOnlyPart::started); 0131 QSignalSpy spyCompleted(&part, SIGNAL(completed())); 0132 QSignalSpy spySetWindowCaption(&part, &KParts::ReadOnlyPart::setWindowCaption); 0133 KParts::NavigationExtension *ext = KParts::NavigationExtension::childObject(&part); 0134 QSignalSpy spyOpenUrlNotify(ext, &KParts::NavigationExtension::openUrlNotify); 0135 const QString file = QFINDTESTDATA("data/page-with-link.html"); 0136 QVERIFY(!file.isEmpty()); 0137 const QUrl url = QUrl::fromLocalFile(file); 0138 part.openUrl(url); 0139 QVERIFY(spyCompleted.wait()); 0140 QVERIFY(spyOpenUrlNotify.isEmpty()); 0141 QWebEnginePage *page = part.view()->page(); 0142 const QPoint pos = elementCenter(page, QStringLiteral("linkid")); // doesn't seem fully correct... 0143 part.widget()->show(); 0144 spyCompleted.clear(); 0145 0146 // WHEN clicking on the link 0147 QTest::mouseClick(part.view()->focusProxy(), Qt::LeftButton, Qt::KeyboardModifiers(), pos); 0148 0149 // THEN 0150 QVERIFY(spyCompleted.wait()); 0151 QCOMPARE(spyOpenUrlNotify.count(), 1); 0152 QCOMPARE(part.url().fileName(), QStringLiteral("hello.html")); 0153 } 0154 0155 QTEST_MAIN(WebEnginePartApiTest) 0156 #include "webengine_partapi_test.moc"