From 6aef365a55bb4fed0c156ebc852e90ca1086c091 Mon Sep 17 00:00:00 2001
From: Peter Varga <pvarga@inf.u-szeged.hu>
Date: Thu, 20 Jul 2017 14:45:11 +0200
Subject: [PATCH] Add HideScrollbars setting

[ChangeLog][Settings] Added setting to hide scrollbars.

Change-Id: I17695bb54a460621358345d303302b93eb342696
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
---
 src/core/web_engine_settings.cpp              |  2 +
 src/core/web_engine_settings.h                |  3 +-
 src/webengine/api/qquickwebenginesettings.cpp | 18 ++++++++
 src/webengine/api/qquickwebenginesettings_p.h |  4 ++
 .../api/qwebenginesettings.cpp                |  2 +
 src/webenginewidgets/api/qwebenginesettings.h |  3 +-
 .../doc/src/qwebenginesettings_lgpl.qdoc      |  3 ++
 .../qwebenginepage/tst_qwebenginepage.cpp     | 42 +++++--------------
 8 files changed, 44 insertions(+), 33 deletions(-)

diff --git a/src/core/web_engine_settings.cpp b/src/core/web_engine_settings.cpp
index 4be985e0c..fe40298bb 100644
--- a/src/core/web_engine_settings.cpp
+++ b/src/core/web_engine_settings.cpp
@@ -223,6 +223,7 @@ void WebEngineSettings::initDefaults()
         s_defaultAttributes.insert(PluginsEnabled, false);
         s_defaultAttributes.insert(FullScreenSupportEnabled, false);
         s_defaultAttributes.insert(ScreenCaptureEnabled, false);
+        s_defaultAttributes.insert(HideScrollbars, false);
         // The following defaults matches logic in render_view_host_impl.cc
         // But first we must ensure the WebContext has been initialized
         QtWebEngineCore::WebEngineContext::current();
@@ -326,6 +327,7 @@ void WebEngineSettings::applySettingsToWebPreferences(content::WebPreferences *p
     prefs->should_print_backgrounds = testAttribute(PrintElementBackgrounds);
     prefs->allow_running_insecure_content = testAttribute(AllowRunningInsecureContent);
     prefs->allow_geolocation_on_insecure_origins = testAttribute(AllowGeolocationOnInsecureOrigins);
+    prefs->hide_scrollbars = testAttribute(HideScrollbars);
 
     // Fonts settings.
     prefs->standard_font_family_map[content::kCommonScript] = toString16(fontFamily(StandardFont));
diff --git a/src/core/web_engine_settings.h b/src/core/web_engine_settings.h
index 7defb0013..1304d2ae9 100644
--- a/src/core/web_engine_settings.h
+++ b/src/core/web_engine_settings.h
@@ -84,7 +84,8 @@ public:
         PrintElementBackgrounds,
         AllowRunningInsecureContent,
         AllowGeolocationOnInsecureOrigins,
-        AllowWindowActivationFromJavaScript
+        AllowWindowActivationFromJavaScript,
+        HideScrollbars
     };
 
     // Must match the values from the public API in qwebenginesettings.h.
diff --git a/src/webengine/api/qquickwebenginesettings.cpp b/src/webengine/api/qquickwebenginesettings.cpp
index cdbb25e4c..98a3eef39 100644
--- a/src/webengine/api/qquickwebenginesettings.cpp
+++ b/src/webengine/api/qquickwebenginesettings.cpp
@@ -372,6 +372,16 @@ bool QQuickWebEngineSettings::allowWindowActivationFromJavaScript() const
     return d_ptr->testAttribute(WebEngineSettings::AllowWindowActivationFromJavaScript);
 }
 
+/*!
+  \qmlproperty bool WebEngineSettings::hideScrollbars
+  \since QtWebEngine 1.6
+  Hides scrollbars. Disabled by default.
+*/
+bool QQuickWebEngineSettings::hideScrollbars() const
+{
+    return d_ptr->testAttribute(WebEngineSettings::HideScrollbars);
+}
+
 /*!
     \qmlproperty string WebEngineSettings::defaultTextEncoding
     \since QtWebEngine 1.2
@@ -582,6 +592,14 @@ void QQuickWebEngineSettings::setAllowWindowActivationFromJavaScript(bool on)
         Q_EMIT allowWindowActivationFromJavaScriptChanged();
 }
 
+void QQuickWebEngineSettings::setHideScrollbars(bool on)
+{
+    bool wasOn = d_ptr->testAttribute(WebEngineSettings::HideScrollbars);
+    d_ptr->setAttribute(WebEngineSettings::HideScrollbars, on);
+    if (wasOn != on)
+        Q_EMIT hideScrollbarsChanged();
+}
+
 void QQuickWebEngineSettings::setParentSettings(QQuickWebEngineSettings *parentSettings)
 {
     d_ptr->setParentSettings(parentSettings->d_ptr.data());
diff --git a/src/webengine/api/qquickwebenginesettings_p.h b/src/webengine/api/qquickwebenginesettings_p.h
index 1b9988bce..da838f52f 100644
--- a/src/webengine/api/qquickwebenginesettings_p.h
+++ b/src/webengine/api/qquickwebenginesettings_p.h
@@ -87,6 +87,7 @@ class Q_WEBENGINE_PRIVATE_EXPORT QQuickWebEngineSettings : public QObject {
     Q_PROPERTY(bool allowRunningInsecureContent READ allowRunningInsecureContent WRITE setAllowRunningInsecureContent NOTIFY allowRunningInsecureContentChanged REVISION 3 FINAL)
     Q_PROPERTY(bool allowGeolocationOnInsecureOrigins READ allowGeolocationOnInsecureOrigins WRITE setAllowGeolocationOnInsecureOrigins NOTIFY allowGeolocationOnInsecureOriginsChanged REVISION 4 FINAL)
     Q_PROPERTY(bool allowWindowActivationFromJavaScript READ allowWindowActivationFromJavaScript WRITE setAllowWindowActivationFromJavaScript NOTIFY allowWindowActivationFromJavaScriptChanged REVISION 5 FINAL)
+    Q_PROPERTY(bool hideScrollbars READ hideScrollbars WRITE setHideScrollbars NOTIFY hideScrollbarsChanged REVISION 5 FINAL)
 
 public:
     ~QQuickWebEngineSettings();
@@ -115,6 +116,7 @@ public:
     bool allowRunningInsecureContent() const;
     bool allowGeolocationOnInsecureOrigins() const;
     bool allowWindowActivationFromJavaScript() const;
+    bool hideScrollbars() const;
 
     void setAutoLoadImages(bool on);
     void setJavascriptEnabled(bool on);
@@ -140,6 +142,7 @@ public:
     void setAllowRunningInsecureContent(bool on);
     void setAllowGeolocationOnInsecureOrigins(bool on);
     void setAllowWindowActivationFromJavaScript(bool on);
+    void setHideScrollbars(bool on);
 
 signals:
     void autoLoadImagesChanged();
@@ -166,6 +169,7 @@ signals:
     Q_REVISION(3) void allowRunningInsecureContentChanged();
     Q_REVISION(4) void allowGeolocationOnInsecureOriginsChanged();
     Q_REVISION(5) void allowWindowActivationFromJavaScriptChanged();
+    Q_REVISION(5) void hideScrollbarsChanged();
 
 private:
     explicit QQuickWebEngineSettings(QQuickWebEngineSettings *parentSettings = 0);
diff --git a/src/webenginewidgets/api/qwebenginesettings.cpp b/src/webenginewidgets/api/qwebenginesettings.cpp
index 6e24b4b51..439e00590 100644
--- a/src/webenginewidgets/api/qwebenginesettings.cpp
+++ b/src/webenginewidgets/api/qwebenginesettings.cpp
@@ -99,6 +99,8 @@ static WebEngineSettings::Attribute toWebEngineAttribute(QWebEngineSettings::Web
         return WebEngineSettings::AllowGeolocationOnInsecureOrigins;
     case QWebEngineSettings::AllowWindowActivationFromJavaScript:
         return WebEngineSettings::AllowWindowActivationFromJavaScript;
+    case QWebEngineSettings::HideScrollbars:
+        return WebEngineSettings::HideScrollbars;
 
     default:
         return WebEngineSettings::UnsupportedInCoreSettings;
diff --git a/src/webenginewidgets/api/qwebenginesettings.h b/src/webenginewidgets/api/qwebenginesettings.h
index 4b997f0ac..1857e5228 100644
--- a/src/webenginewidgets/api/qwebenginesettings.h
+++ b/src/webenginewidgets/api/qwebenginesettings.h
@@ -90,7 +90,8 @@ public:
         PrintElementBackgrounds,
         AllowRunningInsecureContent,
         AllowGeolocationOnInsecureOrigins,
-        AllowWindowActivationFromJavaScript
+        AllowWindowActivationFromJavaScript,
+        HideScrollbars
     };
 
     enum FontSize {
diff --git a/src/webenginewidgets/doc/src/qwebenginesettings_lgpl.qdoc b/src/webenginewidgets/doc/src/qwebenginesettings_lgpl.qdoc
index 26f3964da..907f6f643 100644
--- a/src/webenginewidgets/doc/src/qwebenginesettings_lgpl.qdoc
+++ b/src/webenginewidgets/doc/src/qwebenginesettings_lgpl.qdoc
@@ -165,6 +165,9 @@
     \value  AllowWindowActivationFromJavaScript
             Allows the window.focus() method in JavaScript. Disallowed by default.
             (Added in Qt 5.10)
+    \value  HideScrollbars
+            Hides scrollbars.
+            Disabled by default. (Added in Qt 5.10)
 
 */
 
diff --git a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp
index 393902a9e..4cb8e23e5 100644
--- a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp
+++ b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp
@@ -3515,39 +3515,19 @@ void tst_QWebEnginePage::scrollPosition()
 
 void tst_QWebEnginePage::scrollbarsOff()
 {
-#if !defined(QWEBENGINEPAGE_EVALUATEJAVASCRIPT)
-    QSKIP("QWEBENGINEPAGE_EVALUATEJAVASCRIPT");
-#else
     QWebEngineView view;
-    QWebEngineFrame* mainFrame = view.page();
-
-    mainFrame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
-    mainFrame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
-
-    QString html("<script>" \
-                 "   function checkScrollbar() {" \
-                 "       if (innerWidth === document.documentElement.offsetWidth)" \
-                 "           document.getElementById('span1').innerText = 'SUCCESS';" \
-                 "       else" \
-                 "           document.getElementById('span1').innerText = 'FAIL';" \
-                 "   }" \
-                 "</script>" \
-                 "<body>" \
-                 "   <div style='margin-top:1000px ; margin-left:1000px'>" \
-                 "       <a id='offscreen' href='a'>End</a>" \
-                 "   </div>" \
-                 "<span id='span1'></span>" \
-                 "</body>");
-
-
-    QSignalSpy loadSpy(&view, &QWebEngineView::loadFinished);
-    view.setHtml(html);
-    QVERIFY(loadSpy.wait(200);
-    QCOMPARE(loadSpy.count(), 1);
+    view.page()->settings()->setAttribute(QWebEngineSettings::HideScrollbars, true);
 
-    mainFrame->evaluateJavaScript("checkScrollbar();");
-    QCOMPARE(mainFrame->documentElement().findAll("span").at(0).toPlainText(), QString("SUCCESS"));
-#endif
+    QString html("<html><body>"
+                 "   <div style='margin-top:1000px ; margin-left:1000px'>"
+                 "       <a id='offscreen' href='a'>End</a>"
+                 "   </div>"
+                 "</body></html>");
+
+    QSignalSpy loadSpy(&view, SIGNAL(loadFinished(bool)));
+    view.setHtml(html);
+    QTRY_COMPARE(loadSpy.count(), 1);
+    QVERIFY(evaluateJavaScriptSync(view.page(), "innerWidth == document.documentElement.offsetWidth").toBool());
 }
 
 void tst_QWebEnginePage::horizontalScrollAfterBack()
-- 
GitLab