diff options
-rw-r--r-- | src/core/web_contents_adapter.cpp | 1 | ||||
-rw-r--r-- | tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp | 29 |
2 files changed, 30 insertions, 0 deletions
diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp index cd6505df5..b77a56b9b 100644 --- a/src/core/web_contents_adapter.cpp +++ b/src/core/web_contents_adapter.cpp @@ -485,6 +485,7 @@ void WebContentsAdapter::setContent(const QByteArray &data, const QString &mimeT params.virtual_url_for_data_url = baseUrl.isEmpty() ? GURL(url::kAboutBlankURL) : toGurl(baseUrl); params.can_load_local_resources = true; params.transition_type = ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_API); + params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE; d->webContents->GetController().LoadURLWithParams(params); d->webContents->Focus(); } diff --git a/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp b/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp index 7cd423356..e89940d71 100644 --- a/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp +++ b/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp @@ -52,6 +52,7 @@ private Q_SLOTS: void profileConstructors(); void urlSchemeHandlers(); void urlSchemeHandlerFailRequest(); + void customUserAgent(); }; void tst_QWebEngineProfile::defaultProfile() @@ -188,5 +189,33 @@ void tst_QWebEngineProfile::urlSchemeHandlerFailRequest() QCOMPARE(toPlainTextSync(view.page()), QString()); } +void tst_QWebEngineProfile::customUserAgent() +{ + QString defaultUserAgent = QWebEngineProfile::defaultProfile()->httpUserAgent(); + QWebEnginePage page; + QSignalSpy loadFinishedSpy(&page, SIGNAL(loadFinished(bool))); + page.setHtml(QStringLiteral("<html><body>Hello world!</body></html>")); + QTRY_COMPARE(loadFinishedSpy.count(), 1); + + // First test the user-agent is default + QCOMPARE(evaluateJavaScriptSync(&page, QStringLiteral("navigator.userAgent")).toString(), defaultUserAgent); + + const QString testUserAgent = QStringLiteral("tst_QWebEngineProfile 1.0"); + QWebEngineProfile testProfile; + testProfile.setHttpUserAgent(testUserAgent); + + // Test a new profile with custom user-agent works + QWebEnginePage page2(&testProfile); + QSignalSpy loadFinishedSpy2(&page2, SIGNAL(loadFinished(bool))); + page2.setHtml(QStringLiteral("<html><body>Hello again!</body></html>")); + QTRY_COMPARE(loadFinishedSpy2.count(), 1); + QCOMPARE(evaluateJavaScriptSync(&page2, QStringLiteral("navigator.userAgent")).toString(), testUserAgent); + QCOMPARE(evaluateJavaScriptSync(&page, QStringLiteral("navigator.userAgent")).toString(), defaultUserAgent); + + // Test an existing page and profile with custom user-agent works + QWebEngineProfile::defaultProfile()->setHttpUserAgent(testUserAgent); + QCOMPARE(evaluateJavaScriptSync(&page, QStringLiteral("navigator.userAgent")).toString(), testUserAgent); +} + QTEST_MAIN(tst_QWebEngineProfile) #include "tst_qwebengineprofile.moc" |