Commit ffb77b12 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Add autotest for QWebEngineUrlRequestInterceptor

parent 2cd774d2
Pipeline #21564 failed with stage
in 5 minutes and 4 seconds
......@@ -52,3 +52,13 @@ target_link_libraries(testselectionchangedwebengine
Qt5::Widgets KF5::WebEngineViewer Qt5::WebEngine Qt5::WebEngineWidgets KF5::XmlGui KF5::IconThemes KF5::MessageViewer
)
####
set(testwebengineviewinterceptor_test_SRCS
testwebengineviewinterceptor.cpp
)
add_executable(testwebengineviewinterceptor ${testwebengineviewinterceptor_test_SRCS})
target_link_libraries(testwebengineviewinterceptor
Qt5::Widgets Qt5::WebEngineWidgets
)
/*
Copyright (C) 2020 Laurent Montel <montel@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "testwebengineviewinterceptor.h"
#include <QApplication>
#include <QVBoxLayout>
#include <QWebEngineView>
#include <QWebEngineProfile>
#include <QWebEngineUrlRequestInterceptor>
class CidUrlRequestInterceptor : public QWebEngineUrlRequestInterceptor
{
public:
explicit CidUrlRequestInterceptor(QObject *p = nullptr);
void interceptRequest(QWebEngineUrlRequestInfo &info) override;
~CidUrlRequestInterceptor();
};
CidUrlRequestInterceptor::CidUrlRequestInterceptor(QObject *p)
: QWebEngineUrlRequestInterceptor(p)
{
}
void CidUrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
{
const QUrl urlRequestUrl(info.requestUrl());
if (urlRequestUrl.scheme() == QLatin1String("cid")) {
QUrl r = QUrl::fromLocalFile(QStringLiteral("/source5/kde/kde5.14/share/icons/oxygen/base/16x16/status/audio-volume-medium.png"));
qDebug() << " r " << r;
info.redirect(/*QUrl::fromUserInput(QStringLiteral("file://home/laurent/repeat.svg"))*/r);
}
return;
}
CidUrlRequestInterceptor::~CidUrlRequestInterceptor()
{
}
TestWebEngineViewInterceptor::TestWebEngineViewInterceptor(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *vbox = new QVBoxLayout(this);
mWebEngineView = new QWebEngineView(this);
mWebEngineView->page()->profile()->setUrlRequestInterceptor(new CidUrlRequestInterceptor(this));
const QString htmlStr = QStringLiteral("<html>"
"<head>"
"<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">"
"</head>"
"<body><p>Xcvwxcvwxvccwv xwvc w<img src=\"cid:1506382412@KDE\" />cvbxcvbxcvbcvxbvcxbx</p>"
"<p>Xcv</p>"
"<p>bxcvbxcvbxcvb</p>"
"<br /></body>"
"</html>");
mWebEngineView->setHtml(htmlStr);
vbox->addWidget(mWebEngineView);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setAttribute(Qt::AA_UseHighDpiPixmaps, true);
TestWebEngineViewInterceptor *testWebEngine = new TestWebEngineViewInterceptor;
testWebEngine->show();
testWebEngine->resize(600, 400);
const int ret = app.exec();
return ret;
}
/*
Copyright (C) 2020 Laurent Montel <montel@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef TESTWEBENGINEVIEWINTERCEPTOR_H
#define TESTWEBENGINEVIEWINTERCEPTOR_H
#include <QWidget>
class QWebEngineView;
class TestWebEngineViewInterceptor : public QWidget
{
Q_OBJECT
public:
explicit TestWebEngineViewInterceptor(QWidget *parent = nullptr);
private:
QWebEngineView *mWebEngineView = nullptr;
};
#endif // TESTWEBENGINEVIEWINTERCEPTOR_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment