From f4b806ffbc2f11a5a64fab2856dfe68ae7d603c8 Mon Sep 17 00:00:00 2001 From: Joerg Bornemann Date: Wed, 18 May 2016 14:55:13 +0200 Subject: Fix pasting images to web pages WebKit expects pasted images to have the MIME type image/png (see code and comment in DataObjectItem::getAsFile()). Task-number: QTBUG-53409 Change-Id: I2b0c1244d309687ad190db26c5b00718ed0c4258 Reviewed-by: Allan Sandfeld Jensen --- .../widgets/qwebenginepage/tst_qwebenginepage.cpp | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp') diff --git a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp index 5d53c0468..f29b24933 100644 --- a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp +++ b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -120,6 +121,7 @@ private Q_SLOTS: void geolocationRequestJS(); void loadFinished(); void actionStates(); + void pasteImage(); void popupFormSubmission(); void userStyleSheet(); void userStyleSheetFromLocalFileUrl(); @@ -459,6 +461,35 @@ void tst_QWebEnginePage::actionStates() QTRY_VERIFY(!stopAction->isEnabled()); } +static QImage imageWithoutAlpha(const QImage &image) +{ + QImage result = image; + QPainter painter(&result); + painter.fillRect(result.rect(), Qt::green); + painter.drawImage(0, 0, image); + return result; +} + +void tst_QWebEnginePage::pasteImage() +{ + // Pixels with an alpha value of 0 will have different RGB values after the + // test -> clipboard -> webengine -> test roundtrip. + // Clear the alpha channel to make QCOMPARE happy. + const QImage origImage = imageWithoutAlpha(QImage(":/resources/image.png")); + QClipboard *clipboard = QGuiApplication::clipboard(); + clipboard->setImage(origImage); + QWebEnginePage *page = m_view->page(); + page->load(QUrl("qrc:///resources/pasteimage.html")); + QVERIFY(waitForSignal(m_view, SIGNAL(loadFinished(bool)))); + page->triggerAction(QWebEnginePage::Paste); + QTRY_VERIFY(evaluateJavaScriptSync(page, + "window.myImageDataURL ? window.myImageDataURL.length : 0").toInt() > 0); + QByteArray data = evaluateJavaScriptSync(page, "window.myImageDataURL").toByteArray(); + data.remove(0, data.indexOf(";base64,") + 8); + const QImage image = QImage::fromData(QByteArray::fromBase64(data), "PNG"); + QCOMPARE(image, origImage); +} + class ConsolePage : public QWebEnginePage { public: -- cgit v1.2.3