diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2017-07-24 15:47:48 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2017-10-25 11:02:41 +0000 |
commit | 684cfe05641ba9f3abc7e128d9dab5b331ef0689 (patch) | |
tree | ebaa3721e883360c5313a2ba5f1a9232d880ec6e /tests/auto/widgets/qwebengineprofile | |
parent | d96f8495b59878f50223a81a734ce0983539d8cf (diff) | |
download | qtwebengine-684cfe05641ba9f3abc7e128d9dab5b331ef0689.tar.gz |
Support streaming QIODevices in custom URL scheme handlers
We didn't handle the case where the QIODevice does not have all the data
available all the time.
Change-Id: I6aea8ed48ba9ed297efb907b8f6e5c5fc2a18abd
Reviewed-by: Michal Klocek <michal.klocek@qt.io>
Diffstat (limited to 'tests/auto/widgets/qwebengineprofile')
-rw-r--r-- | tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp b/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp index 093bc2e43..400105152 100644 --- a/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp +++ b/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp @@ -49,6 +49,7 @@ private Q_SLOTS: void urlSchemeHandlers(); void urlSchemeHandlerFailRequest(); void urlSchemeHandlerFailOnRead(); + void urlSchemeHandlerStreaming(); void customUserAgent(); void httpAcceptLanguage(); void downloadItem(); @@ -178,6 +179,74 @@ public: } }; +class StreamingIODevice : public QIODevice { + Q_OBJECT +public: + StreamingIODevice(QObject *parent) : QIODevice(parent), m_bytesRead(0), m_bytesAvailable(0) + { + setOpenMode(QIODevice::ReadOnly); + m_timer.start(100, this); + } + bool isSequential() const override { return true; } + qint64 bytesAvailable() const override + { return m_bytesAvailable; } + bool atEnd() const override + { + return (m_data.size() >= 1000 && m_bytesRead >= 1000); + } +protected: + void timerEvent(QTimerEvent *) override + { + QMutexLocker lock(&m_mutex); + m_bytesAvailable += 200; + m_data.append(200, 'c'); + emit readyRead(); + if (m_data.size() >= 1000) { + m_timer.stop(); + emit readChannelFinished(); + } + } + + qint64 readData(char *data, qint64 maxlen) override + { + QMutexLocker lock(&m_mutex); + qint64 len = qMin(qint64(m_bytesAvailable), maxlen); + if (len) { + memcpy(data, m_data.constData() + m_bytesRead, len); + m_bytesAvailable -= len; + m_bytesRead += len; + } else if (m_data.size() > 0) + return -1; + + return len; + } + qint64 writeData(const char *, qint64) override + { + return 0; + } + +private: + QMutex m_mutex; + QByteArray m_data; + QBasicTimer m_timer; + int m_bytesRead; + int m_bytesAvailable; +}; + +class StreamingUrlSchemeHandler : public QWebEngineUrlSchemeHandler +{ +public: + StreamingUrlSchemeHandler(QObject *parent = nullptr) + : QWebEngineUrlSchemeHandler(parent) + { + } + + void requestStarted(QWebEngineUrlRequestJob *job) + { + job->reply("text/plain;charset=utf-8", new StreamingIODevice(job)); + } +}; + static bool loadSync(QWebEngineView *view, const QUrl &url, int timeout = 5000) { // Ripped off QTRY_VERIFY. @@ -310,6 +379,22 @@ void tst_QWebEngineProfile::urlSchemeHandlerFailOnRead() QCOMPARE(toPlainTextSync(view.page()), QString()); } +void tst_QWebEngineProfile::urlSchemeHandlerStreaming() +{ + StreamingUrlSchemeHandler handler; + QWebEngineProfile profile; + profile.installUrlSchemeHandler("stream", &handler); + QWebEngineView view; + QSignalSpy loadFinishedSpy(&view, SIGNAL(loadFinished(bool))); + view.setPage(new QWebEnginePage(&profile, &view)); + view.settings()->setAttribute(QWebEngineSettings::ErrorPageEnabled, false); + view.load(QUrl(QStringLiteral("stream://whatever"))); + QVERIFY(loadFinishedSpy.wait()); + QByteArray result; + result.append(1000, 'c'); + QCOMPARE(toPlainTextSync(view.page()), QString::fromLatin1(result)); +} + void tst_QWebEngineProfile::customUserAgent() { QString defaultUserAgent = QWebEngineProfile::defaultProfile()->httpUserAgent(); |