From 097f0e78e8a26ea1cda8b7d2e14fcf5551a72abd Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Tue, 19 Apr 2016 13:55:46 +0200 Subject: Fix user-agent override works when setting content Adds test for user-agent override and fixes the override so that it also works when loading with content instead of URLs. Change-Id: I3f61b1d91b7b0908e35216722054168d1c514a87 Reviewed-by: Kai Koehne --- src/core/web_contents_adapter.cpp | 1 + .../qwebengineprofile/tst_qwebengineprofile.cpp | 29 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) 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("Hello world!")); + 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("Hello again!")); + 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" -- cgit v1.2.3