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 --- .../qwebengineprofile/tst_qwebengineprofile.cpp | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'tests/auto/widgets/qwebengineprofile') 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