diff options
author | Andy Shaw <andy.shaw@qt.io> | 2020-03-19 15:59:05 +0100 |
---|---|---|
committer | Andy Shaw <andy.shaw@qt.io> | 2020-03-20 07:28:52 +0100 |
commit | df93624cc31e9da29532eed1d86c9f172575355a (patch) | |
tree | c557d069ae6a4dcffc1da0267af5abe67c0d663d | |
parent | 12420abbfa66e03938571528647be6f6d9034aa7 (diff) |
Set the httpUserAgent after the component has been completed
Since httpUserAgent can be set as a property then it should account for
the fact that init() has not yet been called. So ensure that the
httpUserAgent is set on the profile created when init() is called.
Fixes: QTBUG-82778
Change-Id: I072676e719eedcb0fbc8024f3e902fd1b7073a3e
Reviewed-by: Christian Strømme <christian.stromme@qt.io>
-rw-r--r-- | src/plugins/webengine/qwebenginewebview.cpp | 14 | ||||
-rw-r--r-- | tests/auto/qml/qquickwebview/tst_qquickwebview.cpp | 18 |
2 files changed, 28 insertions, 4 deletions
diff --git a/src/plugins/webengine/qwebenginewebview.cpp b/src/plugins/webengine/qwebenginewebview.cpp index f16371d..7870b3a 100644 --- a/src/plugins/webengine/qwebenginewebview.cpp +++ b/src/plugins/webengine/qwebenginewebview.cpp @@ -81,8 +81,11 @@ QString QWebEngineWebViewPrivate::httpUserAgent() const void QWebEngineWebViewPrivate::setHttpUserAgent(const QString &userAgent) { - m_profile->setHttpUserAgent(userAgent); - Q_EMIT httpUserAgentChanged(userAgent); + m_httpUserAgent = userAgent; + if (m_profile) { + m_profile->setHttpUserAgent(userAgent); + Q_EMIT httpUserAgentChanged(userAgent); + } } QUrl QWebEngineWebViewPrivate::url() const @@ -255,7 +258,12 @@ void QWebEngineWebViewPrivate::QQuickWebEngineViewPtr::init() const Q_ASSERT(webEngineView); QQuickWebEngineProfile *profile = webEngineView->profile(); m_parent->m_profile = profile; - m_parent->m_httpUserAgent = profile->httpUserAgent(); + // When the httpUserAgent is set as a property then it will be set before + // init() is called + if (m_parent->m_httpUserAgent.isEmpty()) + m_parent->m_httpUserAgent = profile->httpUserAgent(); + else + profile->setHttpUserAgent(m_parent->m_httpUserAgent); QObject::connect(webEngineView, &QQuickWebEngineView::urlChanged, m_parent, &QWebEngineWebViewPrivate::q_urlChanged); QObject::connect(webEngineView, &QQuickWebEngineView::loadProgressChanged, m_parent, &QWebEngineWebViewPrivate::q_loadProgressChanged); QObject::connect(webEngineView, &QQuickWebEngineView::loadingChanged, m_parent, &QWebEngineWebViewPrivate::q_loadingChanged); diff --git a/tests/auto/qml/qquickwebview/tst_qquickwebview.cpp b/tests/auto/qml/qquickwebview/tst_qquickwebview.cpp index e3162bc..b1a1364 100644 --- a/tests/auto/qml/qquickwebview/tst_qquickwebview.cpp +++ b/tests/auto/qml/qquickwebview/tst_qquickwebview.cpp @@ -68,6 +68,7 @@ private Q_SLOTS: void multipleWebViewWindows(); void multipleWebViews(); void titleUpdate(); + void changeUserAgent(); private: inline QQuickWebView *newWebView(); @@ -75,11 +76,12 @@ private: void runJavaScript(const QString &script); QScopedPointer<TestWindow> m_window; QScopedPointer<QQmlComponent> m_component; + QQmlEngine *engine = nullptr; }; tst_QQuickWebView::tst_QQuickWebView() { - static QQmlEngine *engine = new QQmlEngine(this); + engine = new QQmlEngine(this); m_component.reset(new QQmlComponent(engine, this)); m_component->setData(QByteArrayLiteral("import QtQuick 2.0\n" "import QtWebView 1.1\n" @@ -332,5 +334,19 @@ void tst_QQuickWebView::titleUpdate() } +void tst_QQuickWebView::changeUserAgent() +{ + QQmlComponent userAgentWebView(engine, this); + userAgentWebView.setData(QByteArrayLiteral("import QtQuick 2.0\n" + "import QtWebView 1.14\n" + "WebView {\n" + "httpUserAgent: \"dummy\"\n" + "}"), + QUrl()); + QObject *viewInstance = userAgentWebView.create(); + QQuickWebView *webView = qobject_cast<QQuickWebView *>(viewInstance); + QCOMPARE(webView->httpUserAgent(), "dummy"); +} + QTEST_MAIN(tst_QQuickWebView) #include "tst_qquickwebview.moc" |