From b00a436a78e624e2d22877c30e889428f98eaac0 Mon Sep 17 00:00:00 2001
From: Allan Sandfeld Jensen <allan.jensen@digia.com>
Date: Wed, 29 Oct 2014 14:49:14 +0100
Subject: [PATCH] Add runJavaScript test

Adds a test to check the return types of runJavaScript.

Change-Id: Ifd0cb4adf57c972e8a8ad240d51f11f0cc17aa3d
Reviewed-by: Andras Becsi <andras.becsi@digia.com>
---
 .../qwebenginepage/tst_qwebenginepage.cpp     | 68 +++++++++++++++++++
 1 file changed, 68 insertions(+)

diff --git a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp
index b6d3f14c5..5de0f7991 100644
--- a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp
+++ b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp
@@ -193,6 +193,8 @@ private Q_SLOTS:
     void macCopyUnicodeToClipboard();
 #endif
 
+    void runJavaScript();
+
 private:
     QWebEngineView* m_view;
     QWebEnginePage* m_page;
@@ -3736,5 +3738,71 @@ void tst_QWebEnginePage::cssMediaTypePageSetting()
 #endif
 }
 
+class JavaScriptCallback
+{
+public:
+    JavaScriptCallback() { }
+    JavaScriptCallback(const QVariant& _expected) : expected(_expected) { }
+    virtual void operator() (const QVariant& result) {
+        QVERIFY(result.isValid());
+        QCOMPARE(result, expected);
+    }
+private:
+    QVariant expected;
+};
+
+class JavaScriptCallbackNull
+{
+public:
+    virtual void operator() (const QVariant& result) {
+        QVERIFY(result.isNull());
+// FIXME: Returned null values are currently invalid QVariants.
+//        QVERIFY(result.isValid());
+    }
+};
+
+class JavaScriptCallbackUndefined
+{
+public:
+    virtual void operator() (const QVariant& result) {
+        QVERIFY(result.isNull());
+        QVERIFY(!result.isValid());
+    }
+};
+
+void tst_QWebEnginePage::runJavaScript()
+{
+    TestPage page;
+
+    JavaScriptCallback callbackBool(QVariant(false));
+    page.runJavaScript("false", QWebEngineCallback<const QVariant&>(callbackBool));
+
+    JavaScriptCallback callbackInt(QVariant(2));
+    page.runJavaScript("2", QWebEngineCallback<const QVariant&>(callbackInt));
+
+    JavaScriptCallback callbackDouble(QVariant(2.5));
+    page.runJavaScript("2.5", QWebEngineCallback<const QVariant&>(callbackDouble));
+
+    JavaScriptCallback callbackString(QVariant(QStringLiteral("Test")));
+    page.runJavaScript("\"Test\"", QWebEngineCallback<const QVariant&>(callbackString));
+
+    QVariantList list;
+    JavaScriptCallback callbackList(list);
+    page.runJavaScript("[]", QWebEngineCallback<const QVariant&>(callbackList));
+
+    QVariantMap map;
+    map.insert(QStringLiteral("test"), QVariant(2));
+    JavaScriptCallback callbackMap(map);
+    page.runJavaScript("var el = {\"test\": 2}; el", QWebEngineCallback<const QVariant&>(callbackMap));
+
+    JavaScriptCallbackNull callbackNull;
+    page.runJavaScript("null", QWebEngineCallback<const QVariant&>(callbackNull));
+
+    JavaScriptCallbackNull callbackUndefined;
+    page.runJavaScript("undefined", QWebEngineCallback<const QVariant&>(callbackUndefined));
+
+    QTest::qWait(100);
+}
+
 QTEST_MAIN(tst_QWebEnginePage)
 #include "tst_qwebenginepage.moc"
-- 
GitLab