diff options
author | Allan Sandfeld Jensen <allan.jensen@theqtcompany.com> | 2016-02-04 13:50:57 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@theqtcompany.com> | 2016-02-04 14:13:39 +0100 |
commit | 6722bbbf4ea2ddcdd9194f26a883029ec3afc39a (patch) | |
tree | c3290b82fac21c4990be8472ae6cb4ca043205cf /tests/auto/widgets/qwebengineprofile | |
parent | 41d69eb0fa2375f0da6ba9b35136f5598be4b3a4 (diff) | |
parent | 82f4d13a13b40d9cb7710f6dd4190175a272a394 (diff) | |
download | qtwebengine-6722bbbf4ea2ddcdd9194f26a883029ec3afc39a.tar.gz |
Merge branch '5.6' into dev
Change-Id: I0bb971f01ee1e02da768f336680c8ec0254ab2b0
Diffstat (limited to 'tests/auto/widgets/qwebengineprofile')
-rw-r--r-- | tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp b/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp index dfc1d39df..8c23af7ee 100644 --- a/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp +++ b/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp @@ -27,7 +27,11 @@ ****************************************************************************/ #include "../util.h" +#include <QtCore/qbuffer.h> #include <QtTest/QtTest> +#include <QtWebEngineCore/qwebengineurlrequestjob.h> +#include <QtWebEngineCore/qwebengineurlschemehandler.h> +#include <QtWebEngineWidgets/qwebengineview.h> #include <qwebengineprofile.h> #include <qwebenginepage.h> @@ -40,6 +44,8 @@ private Q_SLOTS: void profileConstructors(); void clearDataFromCache(); void disableCache(); + void urlSchemeHandlers(); + void urlSchemeHandlerFailRequest(); }; void tst_QWebEngineProfile::defaultProfile() @@ -137,5 +143,104 @@ void tst_QWebEngineProfile::disableCache() cacheDir.removeRecursively(); } +class RedirectingUrlSchemeHandler : public QWebEngineUrlSchemeHandler +{ +public: + void requestStarted(QWebEngineUrlRequestJob *job) + { + job->redirect(QUrl(QStringLiteral("data:text/plain;charset=utf-8,") + + job->requestUrl().fileName())); + } +}; + +class ReplyingUrlSchemeHandler : public QWebEngineUrlSchemeHandler +{ + QBuffer m_buffer; + QByteArray m_bufferData; +public: + ReplyingUrlSchemeHandler(QObject *parent = nullptr) + : QWebEngineUrlSchemeHandler(parent) + { + m_buffer.setBuffer(&m_bufferData); + } + + void requestStarted(QWebEngineUrlRequestJob *job) + { + m_bufferData = job->requestUrl().toString().toUtf8(); + job->reply("text/plain;charset=utf-8", &m_buffer); + } +}; + +void tst_QWebEngineProfile::urlSchemeHandlers() +{ + RedirectingUrlSchemeHandler mailtoHandler; + QWebEngineProfile profile(QStringLiteral("urlSchemeHandlers")); + profile.installUrlSchemeHandler("mailto", &mailtoHandler); + QWebEngineView view; + QSignalSpy loadFinishedSpy(&view, SIGNAL(loadFinished(bool))); + view.setPage(new QWebEnginePage(&profile, &view)); + QString emailAddress = QStringLiteral("egon@olsen-banden.dk"); + view.load(QUrl(QStringLiteral("mailto:") + emailAddress)); + QVERIFY(loadFinishedSpy.wait()); + QCOMPARE(toPlainTextSync(view.page()), emailAddress); + + // Install a gopher handler after the view has been fully initialized. + ReplyingUrlSchemeHandler gopherHandler; + profile.installUrlSchemeHandler("gopher", &gopherHandler); + QUrl url = QUrl(QStringLiteral("gopher://olsen-banden.dk/benny")); + view.load(url); + QVERIFY(loadFinishedSpy.wait()); + QCOMPARE(toPlainTextSync(view.page()), url.toString()); + + // Remove the mailto scheme, and check whether it is not handled anymore. + profile.removeUrlScheme("mailto"); + emailAddress = QStringLiteral("kjeld@olsen-banden.dk"); + view.load(QUrl(QStringLiteral("mailto:") + emailAddress)); + QVERIFY(loadFinishedSpy.wait()); + QVERIFY(toPlainTextSync(view.page()) != emailAddress); + + // Check if gopher is still working after removing mailto. + url = QUrl(QStringLiteral("gopher://olsen-banden.dk/yvonne")); + view.load(url); + QVERIFY(loadFinishedSpy.wait()); + QCOMPARE(toPlainTextSync(view.page()), url.toString()); + + // Does removeAll work? + profile.removeAllUrlSchemeHandlers(); + url = QUrl(QStringLiteral("gopher://olsen-banden.dk/harry")); + view.load(url); + QVERIFY(loadFinishedSpy.wait()); + QVERIFY(toPlainTextSync(view.page()) != url.toString()); + + // Install a handler that is owned by the view. Make sure this doesn't crash on shutdown. + profile.installUrlSchemeHandler("aviancarrier", new ReplyingUrlSchemeHandler(&view)); + url = QUrl(QStringLiteral("aviancarrier:inspector.mortensen@politistyrke.dk")); + view.load(url); + QVERIFY(loadFinishedSpy.wait()); + QCOMPARE(toPlainTextSync(view.page()), url.toString()); +} + +class FailingUrlSchemeHandler : public QWebEngineUrlSchemeHandler +{ +public: + void requestStarted(QWebEngineUrlRequestJob *job) override + { + job->fail(QWebEngineUrlRequestJob::RequestFailed); + } +}; + +void tst_QWebEngineProfile::urlSchemeHandlerFailRequest() +{ + FailingUrlSchemeHandler handler; + QWebEngineProfile profile; + profile.installUrlSchemeHandler("foo", &handler); + QWebEngineView view; + QSignalSpy loadFinishedSpy(&view, SIGNAL(loadFinished(bool))); + view.setPage(new QWebEnginePage(&profile, &view)); + view.load(QUrl(QStringLiteral("foo://bar"))); + QVERIFY(loadFinishedSpy.wait()); + QVERIFY(toPlainTextSync(view.page()).isEmpty()); +} + QTEST_MAIN(tst_QWebEngineProfile) #include "tst_qwebengineprofile.moc" |